Skip to content

Commit

Permalink
add custom WatchCtxDone option for control whether generate watch con…
Browse files Browse the repository at this point in the history
…text done code in register servants
  • Loading branch information
alimy committed Dec 15, 2022
1 parent 3fd74b3 commit a1d8117
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 81 deletions.
24 changes: 18 additions & 6 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,17 @@ type InitOpts struct {
DefaultTag string
EnginePkgName string
EngineImportAlias string
WatchCtxDone bool
NoneQuery bool
Cleanup bool
}

// ParserOpts used for initial parser
type ParserOpts struct {
EngineInfo *EngineInfo
DefaultTag string
NoneQuery bool
EngineInfo *EngineInfo
DefaultTag string
WatchCtxDone bool
NoneQuery bool
}

// GeneratorOpts used for initial generator
Expand Down Expand Up @@ -92,8 +94,9 @@ func (opts Options) InitOpts() *InitOpts {
// ParserOpts return a ParserOpts instance
func (opts *InitOpts) ParserOpts() *ParserOpts {
return &ParserOpts{
DefaultTag: opts.DefaultTag,
NoneQuery: opts.NoneQuery,
DefaultTag: opts.DefaultTag,
WatchCtxDone: opts.WatchCtxDone,
NoneQuery: opts.NoneQuery,
EngineInfo: &EngineInfo{
PkgName: opts.EnginePkgName,
ImportAlias: opts.EngineImportAlias,
Expand Down Expand Up @@ -189,6 +192,14 @@ func SinkPath(path string) Option {
})
}

// WatchCtxDone set generator whether watch context done when Register Servants in
// generated code. default watch context done.
func WatchCtxDone(enable bool) Option {
return optFunc(func(opts *InitOpts) {
opts.WatchCtxDone = enable
})
}

// Cleanup set generator cleanup out first when re-generate code
func Cleanup(enable bool) Option {
return optFunc(func(opts *InitOpts) {
Expand Down Expand Up @@ -293,8 +304,9 @@ func defaultInitOpts() *InitOpts {
RunMode: InSerialMode,
GeneratorName: GeneratorGin,
ParserName: ParserStructTag,
SinkPath: ".gen",
SinkPath: ".auto",
DefaultTag: "mir",
WatchCtxDone: true,
Cleanup: true,
}
}
17 changes: 9 additions & 8 deletions core/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ type FieldDescriptor struct {

// IfaceDescriptor interface Descriptor info
type IfaceDescriptor struct {
Group string
Chain string
PkgName string
TypeName string
Comment string // not support now so always empty
InOuts []reflect.Type
Fields []*FieldDescriptor
EngineInfo *EngineInfo
Group string
Chain string
PkgName string
TypeName string
Comment string // not support now so always empty
InOuts []reflect.Type
Fields []*FieldDescriptor
EngineInfo *EngineInfo
WatchCtxDone bool
}

// IfaceDescriptors interface Descriptor map {TypeName:*IfaceDescriptor}
Expand Down
6 changes: 3 additions & 3 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Expand Down
9 changes: 6 additions & 3 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f h1:OfiFi4JbukWwe3lzw+xunroH1mnC1e2Gy5cxNJApiSY=
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -67,8 +69,9 @@ golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 h1:OH54vjqzRWmbJ62fjuhxy7AxF
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
18 changes: 9 additions & 9 deletions internal/generator/templates/chi_iface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ func Register{{.TypeName}}Servant(e chi.Router, s {{.TypeName}}{{if .IsUseBindin
router.Use(middlewares...)
{{end}}
// register routes info to router
{{range .Fields}}{{if .NotHttpAny }} router.MethodFunc("{{.HttpMethod}}", "{{.Path}}", func(rw http.ResponseWriter, hr *http.Request) {
{{range .Fields}}{{if .NotHttpAny }} router.MethodFunc("{{.HttpMethod}}", "{{.Path}}", func(rw http.ResponseWriter, hr *http.Request) {{if $.WatchCtxDone }}{
select {
case <-hr.Context().Done():
case <- hr.Context().Done():
return
default:
}

{{else}}{ {{end}}
{{if notEmptyStr .InName }}req, err := b.Bind{{.MethodName}}(hr)
if err != nil {
{{if notEmptyStr .OutName }}r.Render{{.MethodName}}(rw, nil, err){{else}}r.Render{{.MethodName}}(rw, err){{end}}
Expand All @@ -63,13 +63,13 @@ func Register{{.TypeName}}Servant(e chi.Router, s {{.TypeName}}{{if .IsUseBindin
r.Render{{.MethodName}}(rw, resp, err){{else}}r.Render{{.MethodName}}(rw, s.{{.MethodName}}(req)){{end}}{{else}}{{if notEmptyStr .OutName}}resp, err := s.{{.MethodName}}()
r.Render{{.MethodName}}(rw, resp, err){{else}}r.Render{{.MethodName}}(rw, s.{{.MethodName}}()){{end}}{{end}}
})
{{else if .JustHttpAny}} router.HandleFunc("{{.Path}}", func(rw http.ResponseWriter, hr *http.Request) {
{{else if .JustHttpAny}} router.HandleFunc("{{.Path}}", func(rw http.ResponseWriter, hr *http.Request) {{if $.WatchCtxDone }}{
select {
case <-hr.Context().Done():
case <- hr.Context().Done():
return
default:
}

{{else}}{ {{end}}
{{if notEmptyStr .InName }}req, err := b.Bind{{.MethodName}}(hr)
if err != nil {
{{if notEmptyStr .OutName }}r.Render{{.MethodName}}(rw, nil, err){{else}}r.Render{{.MethodName}}(rw, err){{end}}
Expand All @@ -80,13 +80,13 @@ func Register{{.TypeName}}Servant(e chi.Router, s {{.TypeName}}{{if .IsUseBindin
r.Render{{.MethodName}}(rw, resp, err){{else}}r.Render{{.MethodName}}(rw, s.{{.MethodName}}()){{end}}{{end}}
})
{{else}}{{$field := .}} {
h := func(rw http.ResponseWriter, hr *http.Request) {
h := func(rw http.ResponseWriter, hr *http.Request) {{if $.WatchCtxDone }}{
select {
case <-hr.Context().Done():
case <- hr.Context().Done():
return
default:
}

{{else}}{ {{end}}
{{if notEmptyStr .InName }}req, err := b.Bind{{.MethodName}}(hr)
if err != nil {
{{if notEmptyStr .OutName }}r.Render{{.MethodName}}(rw, nil, err){{else}}r.Render{{.MethodName}}(rw, err){{end}}
Expand Down
14 changes: 7 additions & 7 deletions internal/generator/templates/echo_iface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ func Register{{.TypeName}}Servant(e *echo.Echo, s {{.TypeName}}{{if .IsUseBindin
router.Use(middlewares...)
{{end}}
// register routes info to router
{{range .Fields}}{{if .NotHttpAny }} router.Add("{{.HttpMethod}}", "{{.Path}}", func(c echo.Context) error {
{{range .Fields}}{{if .NotHttpAny }} router.Add("{{.HttpMethod}}", "{{.Path}}", func(c echo.Context) error {{if $.WatchCtxDone }}{
ctx := c.Request().Context()
select {
case <-ctx.Done():
return ctx.Err()
default:
}

{{else}}{ {{end}}
{{if notEmptyStr .InName }}req, err := b.Bind{{.MethodName}}(c)
if err != nil {
{{if notEmptyStr .OutName }}r.Render{{.MethodName}}(c, nil, err){{else}}r.Render{{.MethodName}}(c, err){{end}}
Expand All @@ -63,14 +63,14 @@ func Register{{.TypeName}}Servant(e *echo.Echo, s {{.TypeName}}{{if .IsUseBindin
r.Render{{.MethodName}}(c, resp, err){{else}}r.Render{{.MethodName}}(c, s.{{.MethodName}}()){{end}}{{end}}
return nil
})
{{else if .JustHttpAny}} router.Any("{{.Path}}", func(c echo.Context) error {
{{else if .JustHttpAny}} router.Any("{{.Path}}", func(c echo.Context) error {{if $.WatchCtxDone }}{
ctx := c.Request().Context()
select {
case <-ctx.Done():
return ctx.Err()
default:
}

{{else}}{ {{end}}
{{if notEmptyStr .InName }}req, err := b.Bind{{.MethodName}}(c)
if err != nil {
{{if notEmptyStr .OutName }}r.Render{{.MethodName}}(c, nil, err){{else}}r.Render{{.MethodName}}(c, err){{end}}
Expand All @@ -82,14 +82,14 @@ func Register{{.TypeName}}Servant(e *echo.Echo, s {{.TypeName}}{{if .IsUseBindin
return nil
})
{{else}}{{$field := .}} {
h := func(c echo.Context) error {
h := func(c echo.Context) error {{if $.WatchCtxDone }}{
ctx := c.Request().Context()
select {
case <-ctx.Done():
return ctx.Err()
return ctx.Err()
default:
}

{{else}}{ {{end}}
{{if notEmptyStr .InName }}req, err := b.Bind{{.MethodName}}(c)
if err != nil {
{{if notEmptyStr .OutName }}r.Render{{.MethodName}}(c, nil, err){{else}}r.Render{{.MethodName}}(c, err){{end}}
Expand Down
12 changes: 6 additions & 6 deletions internal/generator/templates/gin_iface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ func Register{{.TypeName}}Servant(e *gin.Engine, s {{.TypeName}}{{if .IsUseBindi
router.Use(middlewares...)
{{end}}
// register routes info to router
{{range .Fields}}{{if .NotHttpAny }} router.Handle("{{.HttpMethod}}", "{{.Path}}", func(c *gin.Context) {
{{range .Fields}}{{if .NotHttpAny }} router.Handle("{{.HttpMethod}}", "{{.Path}}", func(c *gin.Context) {{if $.WatchCtxDone }}{
select {
case <- c.Request.Context().Done():
return
default:
}

{{else}}{ {{end}}
{{if notEmptyStr .InName }}req, err := b.Bind{{.MethodName}}(c)
if err != nil {
{{if notEmptyStr .OutName }}r.Render{{.MethodName}}(c, nil, err){{else}}r.Render{{.MethodName}}(c, err){{end}}
Expand All @@ -61,13 +61,13 @@ func Register{{.TypeName}}Servant(e *gin.Engine, s {{.TypeName}}{{if .IsUseBindi
r.Render{{.MethodName}}(c, resp, err){{else}}r.Render{{.MethodName}}(c, s.{{.MethodName}}(req)){{end}}{{else}}{{if notEmptyStr .OutName}}resp, err := s.{{.MethodName}}()
r.Render{{.MethodName}}(c, resp, err){{else}}r.Render{{.MethodName}}(c, s.{{.MethodName}}()){{end}}{{end}}
})
{{else if .JustHttpAny}} router.Any("{{.Path}}", func(c *gin.Context) {
{{else if .JustHttpAny}} router.Any("{{.Path}}", func(c *gin.Context) {{if $.WatchCtxDone }}{
select {
case <- c.Request.Context().Done():
return
default:
}

{{else}}{ {{end}}
{{if notEmptyStr .InName }}req, err := b.Bind{{.MethodName}}(c)
if err != nil {
{{if notEmptyStr .OutName }}r.Render{{.MethodName}}(c, nil, err){{else}}r.Render{{.MethodName}}(c, err){{end}}
Expand All @@ -78,13 +78,13 @@ func Register{{.TypeName}}Servant(e *gin.Engine, s {{.TypeName}}{{if .IsUseBindi
r.Render{{.MethodName}}(c, resp, err){{else}}r.Render{{.MethodName}}(c, s.{{.MethodName}}()){{end}}{{end}}
})
{{else}}{{$field := .}} {
h := func(c *gin.Context) {
h := func(c *gin.Context) {{if $.WatchCtxDone }}{
select {
case <- c.Request.Context().Done():
return
default:
}

{{else}}{ {{end}}
{{if notEmptyStr .InName }}req, err := b.Bind{{.MethodName}}(c)
if err != nil {
{{if notEmptyStr .OutName }}r.Render{{.MethodName}}(c, nil, err){{else}}r.Render{{.MethodName}}(c, err){{end}}
Expand Down
12 changes: 6 additions & 6 deletions internal/generator/templates/hertz_iface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ func Register{{.TypeName}}Servant(e *route.Engine, s {{.TypeName}}{{if .IsUseBin
router.Use(middlewares...)
{{end}}
// register routes info to router
{{range .Fields}}{{if .NotHttpAny }} router.Handle("{{.HttpMethod}}", "{{.Path}}", func(c context.Context, ctx *app.RequestContext) {
{{range .Fields}}{{if .NotHttpAny }} router.Handle("{{.HttpMethod}}", "{{.Path}}", func(c context.Context, ctx *app.RequestContext) {{if $.WatchCtxDone }}{
select {
case <- c.Done():
return
default:
}

{{else}}{ {{end}}
{{if notEmptyStr .InName }}req, err := b.Bind{{.MethodName}}(ctx)
if err != nil {
{{if notEmptyStr .OutName }}r.Render{{.MethodName}}(ctx, nil, err){{else}}r.Render{{.MethodName}}(ctx, err){{end}}
Expand All @@ -63,13 +63,13 @@ func Register{{.TypeName}}Servant(e *route.Engine, s {{.TypeName}}{{if .IsUseBin
r.Render{{.MethodName}}(ctx, resp, err){{else}}r.Render{{.MethodName}}(ctx, s.{{.MethodName}}(req)){{end}}{{else}}{{if notEmptyStr .OutName}}resp, err := s.{{.MethodName}}()
r.Render{{.MethodName}}(ctx, resp, err){{else}}r.Render{{.MethodName}}(ctx, s.{{.MethodName}}()){{end}}{{end}}
})
{{else if .JustHttpAny}} router.Any("{{.Path}}", func(c context.Context, ctx *app.RequestContext) {
{{else if .JustHttpAny}} router.Any("{{.Path}}", func(c context.Context, ctx *app.RequestContext) {{if $.WatchCtxDone }}{
select {
case <- c.Done():
return
default:
}

{{else}}{ {{end}}
{{if notEmptyStr .InName }}req, err := b.Bind{{.MethodName}}(ctx)
if err != nil {
{{if notEmptyStr .OutName }}r.Render{{.MethodName}}(ctx, nil, err){{else}}r.Render{{.MethodName}}(ctx, err){{end}}
Expand All @@ -80,13 +80,13 @@ func Register{{.TypeName}}Servant(e *route.Engine, s {{.TypeName}}{{if .IsUseBin
r.Render{{.MethodName}}(ctx, resp, err){{else}}r.Render{{.MethodName}}(ctx, s.{{.MethodName}}()){{end}}{{end}}
})
{{else}}{{$field := .}} {
h := func(c context.Context, ctx *app.RequestContext) {
h := func(c context.Context, ctx *app.RequestContext) {{if $.WatchCtxDone }}{
select {
case <- c.Done():
return
default:
}
{{else}}{ {{end}}
{{if notEmptyStr .InName }}req, err := b.Bind{{.MethodName}}(ctx)
if err != nil {
{{if notEmptyStr .OutName }}r.Render{{.MethodName}}(ctx, nil, err){{else}}r.Render{{.MethodName}}(ctx, err){{end}}
Expand Down
8 changes: 4 additions & 4 deletions internal/generator/templates/httprouter_iface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ type {{.TypeName}}Render interface {

// Register{{.TypeName}}Servant register {{.TypeName}} servant to httprouter
func Register{{.TypeName}}Servant(router *httprouter.Router, s {{.TypeName}}{{if .IsUseBinding }}, b {{.TypeName}}Binding{{end}}, r {{.TypeName}}Render ) {
{{range .Fields}}{{if .NotHttpAny }} router.Handle("{{.HttpMethod}}", "{{joinPath $.Group .Path}}", func(rw http.ResponseWriter, hr *http.Request, p httprouter.Params) {
{{range .Fields}}{{if .NotHttpAny }} router.Handle("{{.HttpMethod}}", "{{joinPath $.Group .Path}}", func(rw http.ResponseWriter, hr *http.Request, p httprouter.Params) {{if $.WatchCtxDone }}{
select {
case <-hr.Context().Done():
return
default:
}

{{else}}{ {{end}}
{{if notEmptyStr .InName }}req, err := b.Bind{{.MethodName}}(hr, p)
if err != nil {
{{if notEmptyStr .OutName }}r.Render{{.MethodName}}(rw, nil, err){{else}}r.Render{{.MethodName}}(rw, err){{end}}
Expand All @@ -53,13 +53,13 @@ func Register{{.TypeName}}Servant(router *httprouter.Router, s {{.TypeName}}{{if
r.Render{{.MethodName}}(rw, resp, err){{else}}r.Render{{.MethodName}}(rw, s.{{.MethodName}}()){{end}}{{end}}
})
{{else}}{{$field := .}} {
h := func(rw http.ResponseWriter, hr *http.Request, p httprouter.Params) {
h := func(rw http.ResponseWriter, hr *http.Request, p httprouter.Params) {{if $.WatchCtxDone }}{
select {
case <-hr.Context().Done():
return
default:
}

{{else}}{ {{end}}
{{if notEmptyStr .InName }}req, err := b.Bind{{.MethodName}}(hr, p)
if err != nil {
{{if notEmptyStr .OutName }}r.Render{{.MethodName}}(rw, nil, err){{else}}r.Render{{.MethodName}}(rw, err){{end}}
Expand Down
Loading

0 comments on commit a1d8117

Please sign in to comment.