Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Execution Hooks around UseCase Interactor #106

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ linters-settings:
threshold: 100
misspell:
locale: US
nlreturn:
block-size: 2
unused:
check-exported: false
unparam:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ It can provide information about itself that will be exposed in generated docume

```go
// Create use case interactor with references to input/output types and interaction function.
u := usecase.NewIOI(new(helloInput), new(helloOutput), func(ctx context.Context, input, output interface{}) error {
u := usecase.NewIOI(new(helloInput), new(helloOutput), func(ctx context.Context, input, output any) error {
var (
in = input.(*helloInput)
out = output.(*helloOutput)
Expand Down Expand Up @@ -220,7 +220,7 @@ u.SetTitle("Greeter")
u.SetDescription("Greeter greets you.")
u.Input = new(helloInput)
u.Output = new(helloOutput)
u.Interactor = usecase.Interact(func(ctx context.Context, input, output interface{}) error {
u.Interactor = usecase.Interact(func(ctx context.Context, input, output any) error {
// Do something about input to prepare output.
return nil
})
Expand Down
4 changes: 2 additions & 2 deletions _examples/advanced-generic/error_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

type customErr struct {
Message string `json:"msg"`
Details map[string]interface{} `json:"details,omitempty"`
Message string `json:"msg"`
Details map[string]any `json:"details,omitempty"`
}

func errorResponse() usecase.Interactor {
Expand Down
3 changes: 2 additions & 1 deletion _examples/advanced-generic/output_headers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -41,7 +42,7 @@ func Test_outputHeaders(t *testing.T) {

assert.Equal(t, resp.StatusCode, http.StatusOK)

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.NoError(t, resp.Body.Close())

Expand Down
2 changes: 1 addition & 1 deletion _examples/advanced-generic/output_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func outputCSVWriter() usecase.Interactor {
usecase.OutputWithEmbeddedWriter
}

u := usecase.NewInteractor(func(ctx context.Context, _ interface{}, out *writerOutput) (err error) {
u := usecase.NewInteractor(func(ctx context.Context, _ any, out *writerOutput) (err error) {
out.Header = "abc"

c := csv.NewWriter(out)
Expand Down
3 changes: 2 additions & 1 deletion _examples/advanced-generic/request_response_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package main

import (
"bytes"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -32,7 +33,7 @@ func Test_requestResponseMapping(t *testing.T) {

assert.Equal(t, http.StatusNoContent, resp.StatusCode)

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.NoError(t, resp.Body.Close())
assert.Equal(t, "", string(body))
Expand Down
2 changes: 1 addition & 1 deletion _examples/advanced-generic/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func NewRouter() http.Handler {
func(handler http.Handler) http.Handler {
var h *nethttp.Handler
if nethttp.HandlerAs(handler, &h) {
h.MakeErrResp = func(ctx context.Context, err error) (int, interface{}) {
h.MakeErrResp = func(ctx context.Context, err error) (int, any) {
code, er := rest.Err(err)

var ae anotherErr
Expand Down
2 changes: 1 addition & 1 deletion _examples/advanced/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func dummy() usecase.Interactor {
return usecase.NewIOI(nil, nil, func(ctx context.Context, input, output interface{}) error {
return usecase.NewIOI(nil, nil, func(ctx context.Context, input, output any) error {
return nil
})
}
6 changes: 3 additions & 3 deletions _examples/advanced/error_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
)

type customErr struct {
Message string `json:"msg"`
Details map[string]interface{} `json:"details,omitempty"`
Message string `json:"msg"`
Details map[string]any `json:"details,omitempty"`
}

func errorResponse() usecase.Interactor {
Expand All @@ -23,7 +23,7 @@ func errorResponse() usecase.Interactor {
Status string `json:"status"`
}

u := usecase.NewIOI(new(errType), new(okResp), func(ctx context.Context, input, output interface{}) (err error) {
u := usecase.NewIOI(new(errType), new(okResp), func(ctx context.Context, input, output any) (err error) {
var (
in = input.(*errType)
out = output.(*okResp)
Expand Down
2 changes: 1 addition & 1 deletion _examples/advanced/file_multi_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func fileMultiUploader() usecase.Interactor {
Query int `json:"inQuery"`
}

u := usecase.NewIOI(new(upload), new(info), func(ctx context.Context, input, output interface{}) (err error) {
u := usecase.NewIOI(new(upload), new(info), func(ctx context.Context, input, output any) (err error) {
var (
in = input.(*upload)
out = output.(*info)
Expand Down
2 changes: 1 addition & 1 deletion _examples/advanced/file_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func fileUploader() usecase.Interactor {
Query int `json:"inQuery"`
}

u := usecase.NewIOI(new(upload), new(info), func(ctx context.Context, input, output interface{}) (err error) {
u := usecase.NewIOI(new(upload), new(info), func(ctx context.Context, input, output any) (err error) {
var (
in = input.(*upload)
out = output.(*info)
Expand Down
2 changes: 1 addition & 1 deletion _examples/advanced/gzip_pass_through.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func directGzip() usecase.Interactor {
}

u := usecase.NewIOI(new(gzipPassThroughInput), new(gzipPassThroughOutput),
func(ctx context.Context, input, output interface{}) error {
func(ctx context.Context, input, output any) error {
var (
in = input.(*gzipPassThroughInput)
out = output.(*gzipPassThroughOutput)
Expand Down
2 changes: 1 addition & 1 deletion _examples/advanced/json_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func jsonBody() usecase.Interactor {
}

u := usecase.NewIOI(new(inputWithJSON), new(outputWithJSON),
func(ctx context.Context, input, output interface{}) (err error) {
func(ctx context.Context, input, output any) (err error) {
var (
in = input.(*inputWithJSON)
out = output.(*outputWithJSON)
Expand Down
2 changes: 1 addition & 1 deletion _examples/advanced/json_body_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func jsonBodyValidation() usecase.Interactor {
u.Input = new(inputWithJSON)
u.Output = new(outputWithJSON)

u.Interactor = usecase.Interact(func(ctx context.Context, input, output interface{}) (err error) {
u.Interactor = usecase.Interact(func(ctx context.Context, input, output any) (err error) {
var (
in = input.(*inputWithJSON)
out = output.(*outputWithJSON)
Expand Down
2 changes: 1 addition & 1 deletion _examples/advanced/json_map_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func jsonMapBody() usecase.Interactor {
Data JSONMapPayload `json:"data"`
}

u := usecase.NewIOI(new(jsonMapReq), new(jsonOutput), func(ctx context.Context, input, output interface{}) (err error) {
u := usecase.NewIOI(new(jsonMapReq), new(jsonOutput), func(ctx context.Context, input, output any) (err error) {
var (
in = input.(*jsonMapReq)
out = output.(*jsonOutput)
Expand Down
2 changes: 1 addition & 1 deletion _examples/advanced/json_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func jsonParam() usecase.Interactor {
JSONPayload
}

u := usecase.NewIOI(new(inputWithJSON), new(outputWithJSON), func(ctx context.Context, input, output interface{}) (err error) {
u := usecase.NewIOI(new(inputWithJSON), new(outputWithJSON), func(ctx context.Context, input, output any) (err error) {
var (
in = input.(*inputWithJSON)
out = output.(*outputWithJSON)
Expand Down
2 changes: 1 addition & 1 deletion _examples/advanced/json_slice_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func jsonSliceBody() usecase.Interactor {
Data JSONSlicePayload `json:"data"`
}

u := usecase.NewIOI(new(jsonSliceReq), new(jsonOutput), func(ctx context.Context, input, output interface{}) (err error) {
u := usecase.NewIOI(new(jsonSliceReq), new(jsonOutput), func(ctx context.Context, input, output any) (err error) {
var (
in = input.(*jsonSliceReq)
out = output.(*jsonOutput)
Expand Down
2 changes: 1 addition & 1 deletion _examples/advanced/no_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func noValidation() usecase.Interactor {
} `json:"data"`
}

u := usecase.NewIOI(new(inputPort), new(outputPort), func(ctx context.Context, input, output interface{}) (err error) {
u := usecase.NewIOI(new(inputPort), new(outputPort), func(ctx context.Context, input, output any) (err error) {
in := input.(*inputPort)
out := output.(*outputPort)

Expand Down
2 changes: 1 addition & 1 deletion _examples/advanced/output_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func outputHeaders() usecase.Interactor {

u.Output = new(headerOutput)

u.Interactor = usecase.Interact(func(ctx context.Context, input, output interface{}) (err error) {
u.Interactor = usecase.Interact(func(ctx context.Context, input, output any) (err error) {
out := output.(*headerOutput)

out.Header = "abc"
Expand Down
2 changes: 1 addition & 1 deletion _examples/advanced/output_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func outputCSVWriter() usecase.Interactor {

u.Output = new(writerOutput)

u.Interactor = usecase.Interact(func(ctx context.Context, input, output interface{}) (err error) {
u.Interactor = usecase.Interact(func(ctx context.Context, input, output any) (err error) {
out := output.(*writerOutput)

out.Header = "abc"
Expand Down
2 changes: 1 addition & 1 deletion _examples/advanced/query_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func queryObject() usecase.Interactor {
}

u := usecase.NewIOI(new(inputQueryObject), new(outputQueryObject),
func(ctx context.Context, input, output interface{}) (err error) {
func(ctx context.Context, input, output any) (err error) {
var (
in = input.(*inputQueryObject)
out = output.(*outputQueryObject)
Expand Down
2 changes: 1 addition & 1 deletion _examples/advanced/request_response_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func reqRespMapping() usecase.Interactor {
Val2 int `json:"-" description:"Simple scalar value with sample validation." required:"true" minimum:"3"`
}

u := usecase.NewIOI(new(inputPort), new(outputPort), func(ctx context.Context, input, output interface{}) (err error) {
u := usecase.NewIOI(new(inputPort), new(outputPort), func(ctx context.Context, input, output any) (err error) {
var (
in = input.(*inputPort)
out = output.(*outputPort)
Expand Down
3 changes: 2 additions & 1 deletion _examples/advanced/request_response_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -30,7 +31,7 @@ func Test_requestResponseMapping(t *testing.T) {

assert.Equal(t, http.StatusNoContent, resp.StatusCode)

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.NoError(t, resp.Body.Close())
assert.Equal(t, "", string(body))
Expand Down
2 changes: 1 addition & 1 deletion _examples/advanced/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func NewRouter() http.Handler {
func(handler http.Handler) http.Handler {
var h *nethttp.Handler
if nethttp.HandlerAs(handler, &h) {
h.MakeErrResp = func(ctx context.Context, err error) (int, interface{}) {
h.MakeErrResp = func(ctx context.Context, err error) (int, any) {
code, er := rest.Err(err)

var ae anotherErr
Expand Down
2 changes: 1 addition & 1 deletion _examples/advanced/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func validation() usecase.Interactor {
} `json:"data" required:"true"`
}

u := usecase.NewIOI(new(inputPort), new(outputPort), func(ctx context.Context, input, output interface{}) (err error) {
u := usecase.NewIOI(new(inputPort), new(outputPort), func(ctx context.Context, input, output any) (err error) {
in := input.(*inputPort)
out := output.(*outputPort)

Expand Down
2 changes: 1 addition & 1 deletion _examples/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func main() {
}

// Create use case interactor with references to input/output types and interaction function.
u := usecase.NewIOI(new(helloInput), new(helloOutput), func(ctx context.Context, input, output interface{}) error {
u := usecase.NewIOI(new(helloInput), new(helloOutput), func(ctx context.Context, input, output any) error {
var (
in = input.(*helloInput)
out = output.(*helloOutput)
Expand Down
2 changes: 1 addition & 1 deletion _examples/task-api/internal/infra/log/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func UseCaseMiddleware() usecase.Middleware {
name = hasName.Name()
}

return usecase.Interact(func(ctx context.Context, input, output interface{}) error {
return usecase.Interact(func(ctx context.Context, input, output any) error {
err := next.Interact(ctx, input, output)
if err != nil {
log.Printf("usecase %s request (%v) failed: %v", name, input, err)
Expand Down
2 changes: 1 addition & 1 deletion _examples/task-api/internal/infra/repository/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (tr *Task) Create(ctx context.Context, value task.Value) (task.Entity, erro
if t.Value.Goal == value.Goal {
return task.Entity{}, usecase.Error{
StatusCode: status.AlreadyExists,
Context: map[string]interface{}{
Context: map[string]any{
"task": t,
},
Value: errors.New("task with same goal already exists"),
Expand Down
2 changes: 1 addition & 1 deletion _examples/task-api/internal/usecase/create_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func CreateTask(
TaskCreator() task.Creator
},
) usecase.IOInteractor {
u := usecase.NewIOI(new(task.Value), new(task.Entity), func(ctx context.Context, input, output interface{}) error {
u := usecase.NewIOI(new(task.Value), new(task.Entity), func(ctx context.Context, input, output any) error {
var (
in = input.(*task.Value)
out = output.(*task.Entity)
Expand Down
2 changes: 1 addition & 1 deletion _examples/task-api/internal/usecase/find_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func FindTask(
},
) usecase.IOInteractor {
u := usecase.NewIOI(new(task.Identity), new(task.Entity),
func(ctx context.Context, input, output interface{}) error {
func(ctx context.Context, input, output any) error {
var (
in = input.(*task.Identity)
out = output.(*task.Entity)
Expand Down
2 changes: 1 addition & 1 deletion _examples/task-api/internal/usecase/find_tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func FindTasks(
TaskFinder() task.Finder
},
) usecase.IOInteractor {
u := usecase.NewIOI(nil, new([]task.Entity), func(ctx context.Context, input, output interface{}) error {
u := usecase.NewIOI(nil, new([]task.Entity), func(ctx context.Context, input, output any) error {
out, ok := output.(*[]task.Entity)
if !ok {
return fmt.Errorf("%w: unexpected output type %T", status.Unimplemented, output)
Expand Down
2 changes: 1 addition & 1 deletion _examples/task-api/internal/usecase/finish_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type finishTaskDeps interface {

// FinishTask creates usecase interactor.
func FinishTask(deps finishTaskDeps) usecase.IOInteractor {
u := usecase.NewIOI(new(task.Identity), nil, func(ctx context.Context, input, _ interface{}) error {
u := usecase.NewIOI(new(task.Identity), nil, func(ctx context.Context, input, _ any) error {
var (
in = input.(*task.Identity)
err error
Expand Down
2 changes: 1 addition & 1 deletion _examples/task-api/internal/usecase/update_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func UpdateTask(
TaskUpdater() task.Updater
},
) usecase.Interactor {
u := usecase.NewIOI(new(updateTask), nil, func(ctx context.Context, input, _ interface{}) error {
u := usecase.NewIOI(new(updateTask), nil, func(ctx context.Context, input, _ any) error {
var (
in = input.(*updateTask)
err error
Expand Down
2 changes: 1 addition & 1 deletion chirouter/wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func TestWrapper_Mount(t *testing.T) {
nethttp.HTTPBasicSecurityMiddleware(service.OpenAPICollector, "Admin", "Admin access"),
)

apiV1.Post("/sum", usecase.NewIOI(new([]int), new(int), func(ctx context.Context, input, output interface{}) error {
apiV1.Post("/sum", usecase.NewIOI(new([]int), new(int), func(ctx context.Context, input, output any) error {
return errors.New("oops")
}))

Expand Down
10 changes: 5 additions & 5 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type ErrWithHTTPStatus interface {
// ErrWithFields exposes structured context of error.
type ErrWithFields interface {
error
Fields() map[string]interface{}
Fields() map[string]any
}

// ErrWithAppCode exposes application error code.
Expand Down Expand Up @@ -81,10 +81,10 @@ func Err(err error) (int, ErrResponse) {

// ErrResponse is HTTP error response body.
type ErrResponse struct {
StatusText string `json:"status,omitempty" description:"Status text."`
AppCode int `json:"code,omitempty" description:"Application-specific error code."`
ErrorText string `json:"error,omitempty" description:"Error message."`
Context map[string]interface{} `json:"context,omitempty" description:"Application context."`
StatusText string `json:"status,omitempty" description:"Status text."`
AppCode int `json:"code,omitempty" description:"Application-specific error code."`
ErrorText string `json:"error,omitempty" description:"Error message."`
Context map[string]any `json:"context,omitempty" description:"Application context."`

err error // Original error.
httpStatusCode int // HTTP response status code.
Expand Down
4 changes: 2 additions & 2 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ func TestErr(t *testing.T) {
err := usecase.Error{
StatusCode: status.InvalidArgument,
Value: errors.New("failed"),
Context: map[string]interface{}{"hello": "world"},
Context: map[string]any{"hello": "world"},
}
code, er := rest.Err(err)

assert.Equal(t, http.StatusBadRequest, code)
assert.Equal(t, map[string]interface{}{"hello": "world"}, er.Context)
assert.Equal(t, map[string]any{"hello": "world"}, er.Context)
assert.Equal(t, "invalid argument: failed", er.Error())
assert.Equal(t, "INVALID_ARGUMENT", er.StatusText)
assert.Equal(t, 0, er.AppCode)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/swaggest/rest

go 1.17
go 1.18

require (
github.com/bool64/dev v0.2.22
Expand Down
Loading