Skip to content

Commit

Permalink
fix parser result in error pkg name
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Mar 6, 2020
1 parent c98bb68 commit 1061d4d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
6 changes: 5 additions & 1 deletion core/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ func (d *IfaceDescriptor) SnakeFileName() string {

// SetPkgName set package name
func (d *IfaceDescriptor) SetPkgName(name string) {
d.PkgName = snakeName(name).String()
if name == "" {
d.PkgName = "api"
} else {
d.PkgName = snakeName(name).String()
}
}

func snakeName(name string) *strings.Builder {
Expand Down
21 changes: 5 additions & 16 deletions internal/generator/templates/gin_iface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,24 @@ import (

{{if notEmptyStr .Comment }}// {{.Comment}}{{end}}
type {{.TypeName}} interface {
{{if .IsNeedChain}}
// Chain provide handlers chain for gin
{{if .IsNeedChain}}// Chain provide handlers chain for gin
Chain() gin.HandlersChain
{{end}}
{{range .Fields}}
{{if notEmptyStr .Comment }}// {{.Comment}}{{end}}
{{range .Fields}}{{if notEmptyStr .Comment }}// {{.Comment}}{{end}}
{{.MethodName}}(*gin.Context)
{{end}}
}

// Register{{.TypeName}}Servant register {{.TypeName}} servant to gin
func Register{{.TypeName}}Servant(e *gin.Engine, s {{.TypeName}}) {
{{if notEmptyStr .Group }}
router := e.Group({{.Group}})
{{else}}
router := e
{{end}}
{{if .IsNeedChain}}
// use chain for router
{{if notEmptyStr .Group }}router := e.Group({{.Group}}){{else}}router := e{{end}}
{{if .IsNeedChain}}// use chain for router
middlewares := s.Chain()
router.Use(middlewares...)
{{end}}

// register routes info to router
{{range .Fields}}
{{if notHttpAny .HttpMethod }}
router.Handle({{.HttpMethod}}, {{.Path}}, s.{{.MethodName}})
{{else}}
router.Any({{.Path}}, s.{{.MethodName}})
{{end}}
{{if notHttpAny .HttpMethod }}router.Handle({{.HttpMethod}}, "{{.Path}}", s.{{.MethodName}}){{else}}router.Any("{{.Path}}", s.{{.MethodName}}){{end}}
{{end}}
}
8 changes: 4 additions & 4 deletions internal/generator/templates_gen.go

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

0 comments on commit 1061d4d

Please sign in to comment.