Skip to content

Commit

Permalink
fix parser error
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Mar 6, 2020
1 parent b5a84a0 commit c98bb68
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 25 deletions.
5 changes: 5 additions & 0 deletions core/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ func (d Descriptors) GroupFrom(key string) string {
return strings.TrimLeft(key, "_")
}

// SnakeStr return snake string
func (d Descriptors) SnakeStr(s string) string {
return snakeName(s).String()
}

func (d Descriptors) keyFrom(s string) string {
return "_" + s
}
Expand Down
2 changes: 1 addition & 1 deletion examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ build: fmt
generate:
cd mirc
-rm -rf gen
go generate mirc/main.go
go run main.go
cd ..

.PHONY: fmt
Expand Down
2 changes: 0 additions & 2 deletions examples/mirc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
v2 "github.com/alimy/mir/v2/examples/mirc/routes/v2"
)

//go:generate go run main.go

func main() {
log.Println("generate code start")
entries := mirEntries()
Expand Down
4 changes: 2 additions & 2 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ func generate(generatorName string, sinkPath string, ds core.Descriptors) error
FuckErr:
for key, ifaces := range ds {
group := ds.GroupFrom(key)
dirPath = filepath.Join(apiPath, group)
dirPath = filepath.Join(apiPath, ds.SnakeStr(group))
if err = os.MkdirAll(dirPath, 0755); err != nil {
break
}
for _, iface := range ifaces {
filePath = filepath.Join(apiPath, iface.SnakeFileName())
filePath = filepath.Join(dirPath, iface.SnakeFileName())
file, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
break FuckErr
Expand Down
4 changes: 1 addition & 3 deletions internal/generator/templates/chi_iface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
"github.com/go-chi/chi"
)

{{if notEmptyStr .Comment }}
// {{.Comment}}
{{end}}
{{if notEmptyStr .Comment }}// {{.Comment}}{{end}}
type {{.TypeName}} interface {
{{if .IsNeedChain}}
// Chain provide middlewares for chi
Expand Down
8 changes: 2 additions & 6 deletions internal/generator/templates/gin_iface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@ import (
"github.com/gin-gonic/gin"
)

{{if notEmptyStr .Comment }}
// {{.Comment}}
{{end}}
{{if notEmptyStr .Comment }}// {{.Comment}}{{end}}
type {{.TypeName}} interface {
{{if .IsNeedChain}}
// Chain provide handlers chain for gin
Chain() gin.HandlersChain
{{end}}
{{range .Fields}}
{{if notEmptyStr .Comment }}
// {{.Comment}}
{{end}}
{{if notEmptyStr .Comment }}// {{.Comment}}{{end}}
{{.MethodName}}(*gin.Context)
{{end}}
}
Expand Down
16 changes: 8 additions & 8 deletions internal/generator/templates_gen.go

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

3 changes: 3 additions & 0 deletions internal/parser/reflex.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (p mirParser) ifaceFrom(entry interface{}) (*core.IfaceDescriptor, error) {
if !groupSetuped {
groupSetuped = true
inflateGroupInfo(iface, entryValue, tagInfo)
continue
} else {
return nil, errMultGroupInfo
}
Expand All @@ -81,6 +82,7 @@ func (p mirParser) ifaceFrom(entry interface{}) (*core.IfaceDescriptor, error) {
if !chainSetuped {
iface.IsNeedChain = true
chainSetuped = true
continue
} else {
return nil, errMultChainInfo
}
Expand All @@ -103,6 +105,7 @@ func inflateGroupInfo(d *core.IfaceDescriptor, v reflect.Value, t *tagInfo) {
} else {
d.Group = t.group
}
d.SetPkgName(d.Group)
}

// fieldFrom build tagField from entry and tagInfo
Expand Down
2 changes: 1 addition & 1 deletion mirc/cmd/templates/gin_go_mod.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module {{ .PkgName }}
go 1.12

require (
github.com/alimy/mir/v2 v2.0.0-alpha.4
github.com/alimy/mir/v2 v2.0.0-beta.1
github.com/gin-gonic/gin v1.4.0
)
1 change: 0 additions & 1 deletion mirc/cmd/templates/gin_mirc_main.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
v2 "{{ .PkgName }}/mirc/routes/v2"
)

//go:generate go run main.go
func main() {
log.Println("generate code start")
entries := mirEntries()
Expand Down
2 changes: 1 addition & 1 deletion mirc/cmd/templates/makefile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ build: fmt
generate:
cd mirc
-rm -rf gen
go generate mirc/main.go
go run main.go
cd ..

.PHONY: fmt
Expand Down

0 comments on commit c98bb68

Please sign in to comment.