Skip to content

Commit

Permalink
Update examples with usecase.NewIOI (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Feb 18, 2021
1 parent 1a55618 commit 1beaa1a
Show file tree
Hide file tree
Showing 32 changed files with 222 additions and 279 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/bench.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
bench:
strategy:
matrix:
go-version: [ 1.15.x ]
go-version: [ 1.16.x ]
runs-on: ubuntu-latest
steps:
- name: Install Go
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2.3.0
uses: golangci/golangci-lint-action@v2.4.0
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.34.1
version: v1.37.0

# Optional: golangci-lint command line arguments.
# args: ./the-only-dir-to-analyze/...
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
test:
strategy:
matrix:
go-version: [ 1.13.x, 1.14.x, 1.15.x ]
go-version: [ 1.13.x, 1.14.x, 1.15.x, 1.16.x ]
runs-on: ubuntu-latest
steps:
- name: Install Go
Expand Down Expand Up @@ -52,7 +52,7 @@ jobs:
if: ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' }}
run: cp unit.txt unit-base.txt
- name: Comment Test Coverage
if: matrix.go-version == '1.15.x'
if: matrix.go-version == '1.16.x'
uses: marocchino/sticky-pull-request-comment@v2
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -68,7 +68,7 @@ jobs:
</details>
- name: Upload code coverage
if: matrix.go-version == '1.15.x'
if: matrix.go-version == '1.16.x'
uses: codecov/codecov-action@v1
with:
file: ./unit.coverprofile
Expand Down
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ linters:
- paralleltest
- forbidigo
- exhaustivestruct
- ifshort
- cyclop # TODO review issues.

issues:
exclude-use-default: false
Expand Down
28 changes: 13 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,6 @@ import (

"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/swaggest/swgui/v3cdn"
"github.com/swaggest/usecase"
"github.com/swaggest/usecase/status"
"github.com/swaggest/rest"
"github.com/swaggest/rest/chirouter"
"github.com/swaggest/rest/jsonschema"
Expand All @@ -279,6 +276,9 @@ import (
"github.com/swaggest/rest/request"
"github.com/swaggest/rest/response"
"github.com/swaggest/rest/response/gzip"
"github.com/swaggest/swgui/v3cdn"
"github.com/swaggest/usecase"
"github.com/swaggest/usecase/status"
)

func main() {
Expand Down Expand Up @@ -307,33 +307,25 @@ func main() {
gzip.Middleware, // Response compression with support for direct gzip pass through.
)

// Create use case interactor.
u := usecase.IOInteractor{}

// Describe use case interactor.
u.SetTitle("Greeter")
u.SetDescription("Greeter greets you.")

// Declare input port type.
type helloInput struct {
Locale string `query:"locale" default:"en-US" pattern:"^[a-z]{2}-[A-Z]{2}$"`
Locale string `query:"locale" default:"en-US" pattern:"^[a-z]{2}-[A-Z]{2}$" enum:"ru-RU,en-US"`
Name string `path:"name" minLength:"3"` // Field tags define parameter location and JSON schema constraints.
}
u.Input = new(helloInput)

// Declare output port type.
type helloOutput struct {
Now time.Time `header:"X-Now" json:"-"`
Message string `json:"message"`
}
u.Output = new(helloOutput)

u.SetExpectedErrors(status.InvalidArgument)
messages := map[string]string{
"en-US": "Hello, %s!",
"ru-RU": "Привет, %s!",
}
u.Interactor = usecase.Interact(func(ctx context.Context, input, output interface{}) error {

// 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 {
var (
in = input.(*helloInput)
out = output.(*helloOutput)
Expand All @@ -350,6 +342,12 @@ func main() {
return nil
})

// Describe use case interactor.
u.SetTitle("Greeter")
u.SetDescription("Greeter greets you.")

u.SetExpectedErrors(status.InvalidArgument)

// Add use case handler to router.
r.Method(http.MethodGet, "/hello/{name}", nethttp.NewHandler(u))

Expand Down
29 changes: 17 additions & 12 deletions _examples/advanced/_testdata/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"paths":{
"/file-multi-upload":{
"post":{
"summary":"Files Uploads With 'multipart/form-data'","description":"","operationId":"",
"summary":"Files Uploads With 'multipart/form-data'","description":"",
"operationId":"_examples/advanced.fileMultiUploader",
"parameters":[
{
"name":"in_query","in":"query","description":"Simple scalar value in query.",
Expand All @@ -22,7 +23,8 @@
},
"/file-upload":{
"post":{
"summary":"File Upload With 'multipart/form-data'","description":"","operationId":"",
"summary":"File Upload With 'multipart/form-data'","description":"",
"operationId":"_examples/advanced.fileUploader",
"parameters":[
{
"name":"in_query","in":"query","description":"Simple scalar value in query.",
Expand All @@ -37,7 +39,7 @@
},
"/gzip-pass-through":{
"get":{
"summary":"","description":"","operationId":"",
"summary":"directGzip","description":"","operationId":"_examples/advanced.directGzip",
"parameters":[
{
"name":"plainStruct","in":"query","description":"Output plain structure instead of gzip container.",
Expand All @@ -56,7 +58,7 @@
}
},
"head":{
"summary":"","description":"","operationId":"",
"summary":"directGzip","description":"","operationId":"_examples/advanced.directGzip",
"parameters":[
{
"name":"plainStruct","in":"query","description":"Output plain structure instead of gzip container.",
Expand Down Expand Up @@ -107,7 +109,7 @@
"post":{
"summary":"Request With JSON Body",
"description":"Request with JSON body and query/header/path params, response with JSON body and data from request.",
"operationId":"",
"operationId":"_examples/advanced.jsonBody",
"parameters":[
{
"name":"in_query","in":"query","description":"Simple scalar value in query.",
Expand All @@ -124,16 +126,17 @@
],
"requestBody":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/AdvancedInputWithJSONType2"}}}},
"responses":{
"200":{
"description":"OK",
"201":{
"description":"Created",
"content":{"application/json":{"schema":{"$ref":"#/components/schemas/AdvancedOutputWithJSONType2"}}}
}
}
}
},
"/json-map-body":{
"post":{
"summary":"Request With JSON Map In Body","description":"Request with JSON object (map) body.","operationId":"",
"summary":"Request With JSON Map In Body","description":"Request with JSON object (map) body.",
"operationId":"_examples/advanced.jsonMapBody",
"parameters":[
{
"name":"in_query","in":"query","description":"Simple scalar value in query.",
Expand All @@ -157,7 +160,7 @@
"get":{
"summary":"Request With JSON Query Parameter",
"description":"Request with JSON body and query/header/path params, response with JSON body and data from request.",
"operationId":"",
"operationId":"_examples/advanced.jsonParam",
"parameters":[
{
"name":"in_query","in":"query","description":"Simple scalar value in query.",
Expand Down Expand Up @@ -186,7 +189,7 @@
},
"/json-slice-body":{
"post":{
"summary":"Request With JSON Array In Body","description":"","operationId":"",
"summary":"Request With JSON Array In Body","description":"","operationId":"_examples/advanced.jsonSliceBody",
"parameters":[
{
"name":"in_query","in":"query","description":"Simple scalar value in query.",
Expand Down Expand Up @@ -261,7 +264,8 @@
},
"/query-object":{
"get":{
"summary":"Request With Object As Query Parameter","description":"","operationId":"",
"summary":"Request With Object As Query Parameter","description":"",
"operationId":"_examples/advanced.queryObject",
"parameters":[
{
"name":"in_query","in":"query","description":"Object value in query.","style":"deepObject","explode":true,
Expand Down Expand Up @@ -303,7 +307,8 @@
},
"/validation":{
"post":{
"summary":"Validation","description":"Input/Output with validation. Custom annotation.","operationId":"",
"summary":"Validation","description":"Input/Output with validation. Custom annotation.",
"operationId":"_examples/advanced.validation",
"parameters":[
{
"name":"q","in":"query",
Expand Down
11 changes: 3 additions & 8 deletions _examples/advanced/file_multi_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import (
)

func fileMultiUploader() usecase.Interactor {
u := usecase.IOInteractor{}

u.SetTitle("Files Uploads With 'multipart/form-data'")

type upload struct {
Simple string `formData:"simple" description:"Simple scalar value in body."`
Query int `query:"in_query" description:"Simple scalar value in query."`
Expand All @@ -30,10 +26,7 @@ func fileMultiUploader() usecase.Interactor {
Query int `json:"inQuery"`
}

u.Input = new(upload)
u.Output = new(info)

u.Interactor = usecase.Interact(func(ctx context.Context, input, output interface{}) (err error) {
u := usecase.NewIOI(new(upload), new(info), func(ctx context.Context, input, output interface{}) (err error) {
var (
in = input.(*upload)
out = output.(*info)
Expand Down Expand Up @@ -81,5 +74,7 @@ func fileMultiUploader() usecase.Interactor {
return nil
})

u.SetTitle("Files Uploads With 'multipart/form-data'")

return u
}
11 changes: 3 additions & 8 deletions _examples/advanced/file_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import (
)

func fileUploader() usecase.Interactor {
u := usecase.IOInteractor{}

u.SetTitle("File Upload With 'multipart/form-data'")

type upload struct {
Simple string `formData:"simple" description:"Simple scalar value in body."`
Query int `query:"in_query" description:"Simple scalar value in query."`
Expand All @@ -30,10 +26,7 @@ func fileUploader() usecase.Interactor {
Query int `json:"inQuery"`
}

u.Input = new(upload)
u.Output = new(info)

u.Interactor = usecase.Interact(func(ctx context.Context, input, output interface{}) (err error) {
u := usecase.NewIOI(new(upload), new(info), func(ctx context.Context, input, output interface{}) (err error) {
var (
in = input.(*upload)
out = output.(*info)
Expand Down Expand Up @@ -81,5 +74,7 @@ func fileUploader() usecase.Interactor {
return nil
})

u.SetTitle("File Upload With 'multipart/form-data'")

return u
}
51 changes: 24 additions & 27 deletions _examples/advanced/gzip_pass_through.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ func (dc gzipPassThroughContainer) gzipPassThroughStruct() gzipPassThroughStruct
}

func directGzip() usecase.Interactor {
u := usecase.IOInteractor{}

// Prepare moderately big JSON, resulting JSON payload is ~67KB.
rawData := gzipPassThroughStruct{
ID: 123,
Expand All @@ -67,31 +65,30 @@ func directGzip() usecase.Interactor {
panic(err)
}

u.Input = new(gzipPassThroughInput)
u.Output = new(gzipPassThroughOutput)
u.Interactor = usecase.Interact(func(ctx context.Context, input, output interface{}) error {
var (
in = input.(*gzipPassThroughInput)
out = output.(*gzipPassThroughOutput)
)

if in.PlainStruct {
o := rawData
o.Header = "cba"
*out = o
} else {
o := dataFromCache
o.Header = "abc"
*out = o
}

// Imitating an internal read operation on data in container.
if in.CountItems {
_ = len((*out).gzipPassThroughStruct().Text)
}

return nil
})
u := usecase.NewIOI(new(gzipPassThroughInput), new(gzipPassThroughOutput),
func(ctx context.Context, input, output interface{}) error {
var (
in = input.(*gzipPassThroughInput)
out = output.(*gzipPassThroughOutput)
)

if in.PlainStruct {
o := rawData
o.Header = "cba"
*out = o
} else {
o := dataFromCache
o.Header = "abc"
*out = o
}

// Imitating an internal read operation on data in container.
if in.CountItems {
_ = len((*out).gzipPassThroughStruct().Text)
}

return nil
})

return u
}
Loading

0 comments on commit 1beaa1a

Please sign in to comment.