Skip to content

Commit

Permalink
1. group support multi level folder 2. remove force flag (zeromicro#203)
Browse files Browse the repository at this point in the history
* add comment support

* add comment support

* 1. group support multi level folder
2. remove force flag

* bug fix

Co-authored-by: kim <[email protected]>
  • Loading branch information
kingxt and kim authored Nov 12, 2020
1 parent 7b4a5e3 commit ef4d496
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 33 deletions.
9 changes: 4 additions & 5 deletions tools/goctl/api/gogen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,17 @@ var tmpDir = path.Join(os.TempDir(), "goctl")
func GoCommand(c *cli.Context) error {
apiFile := c.String("api")
dir := c.String("dir")
force := c.Bool("force")
if len(apiFile) == 0 {
return errors.New("missing -api")
}
if len(dir) == 0 {
return errors.New("missing -dir")
}

return DoGenProject(apiFile, dir, force)
return DoGenProject(apiFile, dir)
}

func DoGenProject(apiFile, dir string, force bool) error {
func DoGenProject(apiFile, dir string) error {
p, err := parser.NewParser(apiFile)
if err != nil {
return err
Expand All @@ -54,9 +53,9 @@ func DoGenProject(apiFile, dir string, force bool) error {
logx.Must(genConfig(dir, api))
logx.Must(genMain(dir, api))
logx.Must(genServiceContext(dir, api))
logx.Must(genTypes(dir, api, force))
logx.Must(genTypes(dir, api))
logx.Must(genHandlers(dir, api))
logx.Must(genRoutes(dir, api, force))
logx.Must(genRoutes(dir, api))
logx.Must(genLogic(dir, api))

if err := backupAndSweep(apiFile); err != nil {
Expand Down
11 changes: 5 additions & 6 deletions tools/goctl/api/gogen/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Response struct {
@server(
// C0
group: greet
group: greet/s1
)
// C1
service A-api {
Expand All @@ -43,10 +43,8 @@ service A-api {
get /greet/from/:name(Request) returns (Response) // hello
// C4
@server(
handler: NoResponseHandler // C5
)
get /greet/get(Request) returns
@handler NoResponseHandler // C5
get /greet/get(Request)
}
`

Expand Down Expand Up @@ -504,7 +502,8 @@ func TestHasImportApi(t *testing.T) {

func validate(t *testing.T, api string) {
dir := "_go"
err := DoGenProject(api, dir, true)
os.RemoveAll(dir)
err := DoGenProject(api, dir)
defer os.RemoveAll(dir)
assert.Nil(t, err)
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
Expand Down
20 changes: 10 additions & 10 deletions tools/goctl/api/gogen/genroutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gogen
import (
"bytes"
"fmt"
"os"
"path"
"sort"
"strings"
Expand Down Expand Up @@ -61,7 +62,7 @@ type (
}
)

func genRoutes(dir string, api *spec.ApiSpec, force bool) error {
func genRoutes(dir string, api *spec.ApiSpec) error {
var builder strings.Builder
groups, err := getRoutes(api)
if err != nil {
Expand Down Expand Up @@ -121,11 +122,7 @@ func genRoutes(dir string, api *spec.ApiSpec, force bool) error {
}

filename := path.Join(dir, handlerDir, routesFilename)
if !force {
if err := util.RemoveOrQuit(filename); err != nil {
return err
}
}
os.Remove(filename)

fp, created, err := apiutil.MaybeCreateFile(dir, handlerDir, routesFilename)
if err != nil {
Expand Down Expand Up @@ -163,8 +160,7 @@ func genRouteImports(parentPkg string, api *spec.ApiSpec) string {
continue
}
}
importSet.AddStr(fmt.Sprintf("%s \"%s\"", folder,
util.JoinPackages(parentPkg, handlerDir, folder)))
importSet.AddStr(fmt.Sprintf("%s \"%s\"", toPrefix(folder), util.JoinPackages(parentPkg, handlerDir, folder)))
}
}
imports := importSet.KeysStr()
Expand All @@ -187,11 +183,11 @@ func getRoutes(api *spec.ApiSpec) ([]group, error) {
handler = getHandlerBaseName(handler) + "Handler(serverCtx)"
folder, ok := apiutil.GetAnnotationValue(r.Annotations, "server", groupProperty)
if ok {
handler = folder + "." + strings.ToUpper(handler[:1]) + handler[1:]
handler = toPrefix(folder) + "." + strings.ToUpper(handler[:1]) + handler[1:]
} else {
folder, ok = apiutil.GetAnnotationValue(g.Annotations, "server", groupProperty)
if ok {
handler = folder + "." + strings.ToUpper(handler[:1]) + handler[1:]
handler = toPrefix(folder) + "." + strings.ToUpper(handler[:1]) + handler[1:]
}
}
groupedRoutes.routes = append(groupedRoutes.routes, route{
Expand All @@ -215,3 +211,7 @@ func getRoutes(api *spec.ApiSpec) ([]group, error) {

return routes, nil
}

func toPrefix(folder string) string {
return strings.ReplaceAll(folder, "/", "")
}
9 changes: 3 additions & 6 deletions tools/goctl/api/gogen/gentypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"os"
"path"
"strings"
"text/template"
Expand Down Expand Up @@ -42,18 +43,14 @@ func BuildTypes(types []spec.Type) (string, error) {
return builder.String(), nil
}

func genTypes(dir string, api *spec.ApiSpec, force bool) error {
func genTypes(dir string, api *spec.ApiSpec) error {
val, err := BuildTypes(api.Types)
if err != nil {
return err
}

filename := path.Join(dir, typesDir, typesFile)
if !force {
if err := util.RemoveOrQuit(filename); err != nil {
return err
}
}
os.Remove(filename)

fp, created, err := apiutil.MaybeCreateFile(dir, typesDir, typesFile)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/new/newservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ func NewService(c *cli.Context) error {
return err
}

err = gogen.DoGenProject(apiFilePath, abs, true)
err = gogen.DoGenProject(apiFilePath, abs)
return err
}
3 changes: 2 additions & 1 deletion tools/goctl/api/parser/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ memberLoop:
}
switch {
case isSpace(next):
if builder.Len() > 0 {
if builder.Len() > 0 && annoName == "" {
annoName = builder.String()
builder.Reset()
}
case isNewline(next):
if builder.Len() == 0 {
return errors.New("invalid annotation format")
}

if len(annoName) > 0 {
value := builder.String()
if value != string(leftParenthesis) {
Expand Down
4 changes: 0 additions & 4 deletions tools/goctl/goctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ var (
Name: "api",
Usage: "the api file",
},
cli.BoolFlag{
Name: "force",
Usage: "force override the exist files",
},
},
Action: gogen.GoCommand,
},
Expand Down

0 comments on commit ef4d496

Please sign in to comment.