Skip to content

Commit

Permalink
optimize gin engine style generator
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Dec 13, 2022
1 parent 86a7770 commit e2e2af1
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 78 deletions.
58 changes: 29 additions & 29 deletions examples/mirc/auto/api/site.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions examples/mirc/auto/api/v1/site.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 27 additions & 27 deletions examples/mirc/auto/api/v2/site.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions internal/generator/templates/gin_iface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ type {{.TypeName}} interface {
{{if notEmptyStr .Chain }}// Chain provide handlers chain for gin
{{.Chain}}() gin.HandlersChain
{{end}}
{{range .Fields}} {{.MethodName}}(c *gin.Context{{if notEmptyStr .InName }}, req *{{ .InName }}{{end}}) {{if notEmptyStr .OutName }}(*{{ .OutName}}, mir.Error){{else}}mir.Error{{end}}
{{range .Fields}} {{.MethodName}}({{if notEmptyStr .InName }}*{{ .InName }}{{end}}) {{if notEmptyStr .OutName }}(*{{ .OutName}}, mir.Error){{else}}mir.Error{{end}}
{{end}}

mustEmbedUnimplemented{{.TypeName}}Servant()
}
{{if .IsUseBinding }}
type {{.TypeName}}Binding interface {
{{range .Fields}} {{if notEmptyStr .InName }}Bind{{.MethodName}}(c *gin.Context) (*{{ .InName }}, mir.Error){{end}}
{{range .Fields}} {{if notEmptyStr .InName }}Bind{{.MethodName}}(*gin.Context) (*{{ .InName }}, mir.Error){{end}}
{{end}}

mustEmbedUnimplemented{{.TypeName}}Binding()
}
{{end}}

type {{.TypeName}}Render interface {
{{range .Fields}} {{if notEmptyStr .OutName }}Render{{.MethodName}}(c *gin.Context, data *{{ .OutName }}, err mir.Error){{else}}Render{{.MethodName}}(c *gin.Context, err mir.Error){{end}}
{{range .Fields}} {{if notEmptyStr .OutName }}Render{{.MethodName}}(*gin.Context, *{{ .OutName }}, mir.Error){{else}}Render{{.MethodName}}(*gin.Context, mir.Error){{end}}
{{end}}

mustEmbedUnimplemented{{.TypeName}}Render()
Expand All @@ -50,28 +50,28 @@ func Register{{.TypeName}}Servant(e *gin.Engine, s {{.TypeName}}{{if .IsUseBindi
if err != nil {
{{if notEmptyStr .OutName }}r.Render{{.MethodName}}(c, nil, err){{else}}r.Render{{.MethodName}}(c, err){{end}}
}
{{if notEmptyStr .OutName}}resp, err := s.{{.MethodName}}(c, req)
r.Render{{.MethodName}}(c, resp, err){{else}}r.Render{{.MethodName}}(c, s.{{.MethodName}}(c, req)){{end}}{{else}}{{if notEmptyStr .OutName}}resp, err := s.{{.MethodName}}(c)
r.Render{{.MethodName}}(c, resp, err){{else}}r.Render{{.MethodName}}(c, s.{{.MethodName}}(c)){{end}}{{end}}
{{if notEmptyStr .OutName}}resp, err := s.{{.MethodName}}(req)
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) {
{{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}}
}
{{if notEmptyStr .OutName}}resp, err := s.{{.MethodName}}(c, req)
r.Render{{.MethodName}}(c, resp, err){{else}}r.Render{{.MethodName}}(c, s.{{.MethodName}}(c, req)){{end}}{{else}}{{if notEmptyStr .OutName}}resp, err := s.{{.MethodName}}(c)
r.Render{{.MethodName}}(c, resp, err){{else}}r.Render{{.MethodName}}(c, s.{{.MethodName}}(c)){{end}}{{end}}
{{if notEmptyStr .OutName}}resp, err := s.{{.MethodName}}(req)
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}}{{$field := .}} {
h := func(c *gin.Context) {
{{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}}
}
{{if notEmptyStr .OutName}}resp, err := s.{{.MethodName}}(c, req)
r.Render{{.MethodName}}(c, resp, err){{else}}r.Render{{.MethodName}}(c, s.{{.MethodName}}(c, req)){{end}}{{else}}{{if notEmptyStr .OutName}}resp, err := s.{{.MethodName}}(c)
r.Render{{.MethodName}}(c, resp, err){{else}}r.Render{{.MethodName}}(c, s.{{.MethodName}}(c)){{end}}{{end}}
{{if notEmptyStr .OutName}}resp, err := s.{{.MethodName}}(req)
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}}
}
{{range .AnyHttpMethods}} router.Handle("{{.}}", "{{$field.Path}}", h)
{{end}} }
Expand All @@ -90,7 +90,7 @@ func ({{$unimplementedServant}}){{.Chain}}() gin.HandlersChain {

{{end}}
{{range .Fields}}
func ({{$unimplementedServant}}){{.MethodName}}(c *gin.Context{{if notEmptyStr .InName }}, req *{{ .InName }}{{end}}) {{if notEmptyStr .OutName }}(*{{ .OutName}}, mir.Error){{else}}mir.Error{{end}} {
func ({{$unimplementedServant}}){{.MethodName}}({{if notEmptyStr .InName }}req *{{ .InName }}{{end}}) {{if notEmptyStr .OutName }}(*{{ .OutName}}, mir.Error){{else}}mir.Error{{end}} {
return {{if notEmptyStr .OutName }}nil, {{end}}mir.Errorln(http.StatusNotImplemented, http.StatusText(http.StatusNotImplemented))
}

Expand Down

0 comments on commit e2e2af1

Please sign in to comment.