swaggest / usecase Goto Github PK
View Code? Open in Web Editor NEWClean Architecture Use Case for Go
Home Page: https://pkg.go.dev/github.com/swaggest/usecase
License: MIT License
Clean Architecture Use Case for Go
Home Page: https://pkg.go.dev/github.com/swaggest/usecase
License: MIT License
Describe the bug
(1) Interactor response struct with ETag
response header...
ETag string `header:"ETag" json:"-" required:"true" description:"Etag."`
...throws response validation error {"header:Etag":["missing value"]}
even if ETag
is present in output.
No such problem when header name is Etag
ETag string `header:"Etag" json:"-" required:"true" description:"Etag."`
(2) Header names should be processed in case insensitive manner. ETag should be allowed (independent of ETagged
interface).
(3) Canonical name is ETag
so consider changing in response/encoder.go
:
-w.Header().Set("Etag", etag)
+w.Header().Set("ETag", etag)
Good afternoon.
Tell me how to properly issue a usecase.NewInteractor so that output can be of different types?
For example, my OpenAPI 3.0 schema describes three different types of responses.
...
"/phonenumber/get/" : {
"post" : {
"operationId" : "getInfo",
"requestBody" : {
"required" : true,
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/RequestInfo"
}
}
}
},
"responses" : {
"200" : {
"content" : {
"application/json" : {
"schema" : {
"oneOf" : [ {
"$ref" : "#/components/schemas/ResponsePhoneExist"
}, {
"$ref" : "#/components/schemas/ResponsePhoneNoExist"
}, {
"$ref" : "#/components/schemas/ResponsePhoneInArchive"
} ]
}
}
}
},
...
ResponsePhoneExist, ResponsePhoneNoExist and ResponsePhoneInArchive are different types in my go app:
type ResponsePhoneExist struct {
Result string json:"result,omitempty" required:"true" enum:"ok" example:"ok"
Info string json:"info,omitempty" required:"true" enum:"exist" example:"exist"
Phonenumber string json:"phonenumber,omitempty" required:"true" minLength:"11" maxLength:"11" example:"79146764408"
Сount int json:"count,omitempty" required:"true" minimum:"0" maximum:"3" example:"1"
Activated string json:"activated,omitempty" required:"true" example:"27.11.2022"
}
type ResponsePhoneNoExist struct {
Result string json:"result,omitempty" required:"true" enum:"ok" example:"ok"
Info string json:"info,omitempty" required:"true" enum:"no exist" example:"no exist"
Phonenumber string json:"phonenumber,omitempty" required:"true" minLength:"11" maxLength:"11" example:"79146764408"
}
type ResponsePhoneInArchive struct {
Result string json:"result,omitempty" required:"true" enum:"ok" example:"ok"
Info string json:"info,omitempty" required:"true" enum:"archive" example:"archive"
Phonenumber string json:"phonenumber,omitempty" required:"true" minLength:"11" maxLength:"11" example:"79146764408"
}
Then I define web service in function main():
func main() {
service := web.DefaultService()
...
service.Docs("/docs", v4emb.New)
service.Post("/phonenumber/get", getPhoneInfo())
err := http.ListenAndServe(":3400", service)
...
}
func getPhoneInfo() usecase.Interactor {
u := usecase.NewInteractor(func(ctx context.Context, request RequestInfo, response ???) error {
*response, err = getInfoIn1C(request.PhoneNumber)
if err != nil {
return status.Wrap(errors.New(err.Error()), status.Internal)
}
return nil
})
u.SetName("getInfo")
u.SetExpectedErrors(status.Unauthenticated, status.InvalidArgument, status.Internal)
return u
}
func getInfoIn1C(phone string) (???, error) {
var out ???
var err error
...
return out, err
}
What type instead "???" should I specify that the automatically generated Open API 3.0 documentation satisfies the fragment given above?
Lines 32 to 33 in 7dca61e
After using this new generic method I got stuck with an issue with pointers.
TR;DR;
I think changing the following would make sense;
var in T
u.Input = in
u.Output = new(o)
Long story
I created a generic helper that used an interface as the type. And I wasn't able to use a pointer. Here's the code:
type Command interface {
AggregateId() uuid.UUID
}
type NewOrg struct {
aggregateId uuid.UUID
}
func (o *NewOrg) AggregateId() uuid.UUID {
o.aggregateId
}
func NewCase[T Command]() usecase.Interactor {
u := usecase.NewInteractor(func(ctx context.Context, in T, out *T) error {
*out = in
return nil
})
n := new(T)
u.SetTitle(fmt.Sprintf("Command %T", n))
u.SetExpectedErrors(es.ErrHandlerNotFound, es.ErrNotCommandHandler)
u.SetExpectedErrors(status.InvalidArgument)
return u
}
....
func main() {
serviceInfo := openapi3.Info{}
serviceInfo.
WithTitle(cfg.ServiceName).
WithVersion(cfg.Version)
s := web.DefaultService()
s.OpenAPI.Info = serviceInfo
s.Use(es.CreateUnit(cli))
s.Use(middleware.Logger)
s.Docs("/docs", v4emb.New)
s.Mount("/debug", middleware.Profiler())
// Doesn't work..
s.Post("/commands/neworganisation", NewCase[*NewOrg](), nethttp.SuccessStatus(http.StatusCreated))
// Didn't work with my underlying framework
s.Post("/commands/neworganisation", NewCase[NewOrg](), nethttp.SuccessStatus(http.StatusCreated))
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.