1. group support multi level folder 2. remove force flag (#203)
* add comment support * add comment support * 1. group support multi level folder 2. remove force flag * bug fix Co-authored-by: kim <xutao@xiaoheiban.cn>
This commit is contained in:
@@ -28,7 +28,6 @@ var tmpDir = path.Join(os.TempDir(), "goctl")
|
|||||||
func GoCommand(c *cli.Context) error {
|
func GoCommand(c *cli.Context) error {
|
||||||
apiFile := c.String("api")
|
apiFile := c.String("api")
|
||||||
dir := c.String("dir")
|
dir := c.String("dir")
|
||||||
force := c.Bool("force")
|
|
||||||
if len(apiFile) == 0 {
|
if len(apiFile) == 0 {
|
||||||
return errors.New("missing -api")
|
return errors.New("missing -api")
|
||||||
}
|
}
|
||||||
@@ -36,10 +35,10 @@ func GoCommand(c *cli.Context) error {
|
|||||||
return errors.New("missing -dir")
|
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)
|
p, err := parser.NewParser(apiFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -54,9 +53,9 @@ func DoGenProject(apiFile, dir string, force bool) error {
|
|||||||
logx.Must(genConfig(dir, api))
|
logx.Must(genConfig(dir, api))
|
||||||
logx.Must(genMain(dir, api))
|
logx.Must(genMain(dir, api))
|
||||||
logx.Must(genServiceContext(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(genHandlers(dir, api))
|
||||||
logx.Must(genRoutes(dir, api, force))
|
logx.Must(genRoutes(dir, api))
|
||||||
logx.Must(genLogic(dir, api))
|
logx.Must(genLogic(dir, api))
|
||||||
|
|
||||||
if err := backupAndSweep(apiFile); err != nil {
|
if err := backupAndSweep(apiFile); err != nil {
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ type Response struct {
|
|||||||
|
|
||||||
@server(
|
@server(
|
||||||
// C0
|
// C0
|
||||||
group: greet
|
group: greet/s1
|
||||||
)
|
)
|
||||||
// C1
|
// C1
|
||||||
service A-api {
|
service A-api {
|
||||||
@@ -43,10 +43,8 @@ service A-api {
|
|||||||
get /greet/from/:name(Request) returns (Response) // hello
|
get /greet/from/:name(Request) returns (Response) // hello
|
||||||
|
|
||||||
// C4
|
// C4
|
||||||
@server(
|
@handler NoResponseHandler // C5
|
||||||
handler: NoResponseHandler // C5
|
get /greet/get(Request)
|
||||||
)
|
|
||||||
get /greet/get(Request) returns
|
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
@@ -504,7 +502,8 @@ func TestHasImportApi(t *testing.T) {
|
|||||||
|
|
||||||
func validate(t *testing.T, api string) {
|
func validate(t *testing.T, api string) {
|
||||||
dir := "_go"
|
dir := "_go"
|
||||||
err := DoGenProject(api, dir, true)
|
os.RemoveAll(dir)
|
||||||
|
err := DoGenProject(api, dir)
|
||||||
defer os.RemoveAll(dir)
|
defer os.RemoveAll(dir)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package gogen
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -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
|
var builder strings.Builder
|
||||||
groups, err := getRoutes(api)
|
groups, err := getRoutes(api)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -121,11 +122,7 @@ func genRoutes(dir string, api *spec.ApiSpec, force bool) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
filename := path.Join(dir, handlerDir, routesFilename)
|
filename := path.Join(dir, handlerDir, routesFilename)
|
||||||
if !force {
|
os.Remove(filename)
|
||||||
if err := util.RemoveOrQuit(filename); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fp, created, err := apiutil.MaybeCreateFile(dir, handlerDir, routesFilename)
|
fp, created, err := apiutil.MaybeCreateFile(dir, handlerDir, routesFilename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -163,8 +160,7 @@ func genRouteImports(parentPkg string, api *spec.ApiSpec) string {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
importSet.AddStr(fmt.Sprintf("%s \"%s\"", folder,
|
importSet.AddStr(fmt.Sprintf("%s \"%s\"", toPrefix(folder), util.JoinPackages(parentPkg, handlerDir, folder)))
|
||||||
util.JoinPackages(parentPkg, handlerDir, folder)))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
imports := importSet.KeysStr()
|
imports := importSet.KeysStr()
|
||||||
@@ -187,11 +183,11 @@ func getRoutes(api *spec.ApiSpec) ([]group, error) {
|
|||||||
handler = getHandlerBaseName(handler) + "Handler(serverCtx)"
|
handler = getHandlerBaseName(handler) + "Handler(serverCtx)"
|
||||||
folder, ok := apiutil.GetAnnotationValue(r.Annotations, "server", groupProperty)
|
folder, ok := apiutil.GetAnnotationValue(r.Annotations, "server", groupProperty)
|
||||||
if ok {
|
if ok {
|
||||||
handler = folder + "." + strings.ToUpper(handler[:1]) + handler[1:]
|
handler = toPrefix(folder) + "." + strings.ToUpper(handler[:1]) + handler[1:]
|
||||||
} else {
|
} else {
|
||||||
folder, ok = apiutil.GetAnnotationValue(g.Annotations, "server", groupProperty)
|
folder, ok = apiutil.GetAnnotationValue(g.Annotations, "server", groupProperty)
|
||||||
if ok {
|
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{
|
groupedRoutes.routes = append(groupedRoutes.routes, route{
|
||||||
@@ -215,3 +211,7 @@ func getRoutes(api *spec.ApiSpec) ([]group, error) {
|
|||||||
|
|
||||||
return routes, nil
|
return routes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func toPrefix(folder string) string {
|
||||||
|
return strings.ReplaceAll(folder, "/", "")
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
@@ -42,18 +43,14 @@ func BuildTypes(types []spec.Type) (string, error) {
|
|||||||
return builder.String(), nil
|
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)
|
val, err := BuildTypes(api.Types)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
filename := path.Join(dir, typesDir, typesFile)
|
filename := path.Join(dir, typesDir, typesFile)
|
||||||
if !force {
|
os.Remove(filename)
|
||||||
if err := util.RemoveOrQuit(filename); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fp, created, err := apiutil.MaybeCreateFile(dir, typesDir, typesFile)
|
fp, created, err := apiutil.MaybeCreateFile(dir, typesDir, typesFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -58,6 +58,6 @@ func NewService(c *cli.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = gogen.DoGenProject(apiFilePath, abs, true)
|
err = gogen.DoGenProject(apiFilePath, abs)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ memberLoop:
|
|||||||
}
|
}
|
||||||
switch {
|
switch {
|
||||||
case isSpace(next):
|
case isSpace(next):
|
||||||
if builder.Len() > 0 {
|
if builder.Len() > 0 && annoName == "" {
|
||||||
annoName = builder.String()
|
annoName = builder.String()
|
||||||
builder.Reset()
|
builder.Reset()
|
||||||
}
|
}
|
||||||
@@ -84,6 +84,7 @@ memberLoop:
|
|||||||
if builder.Len() == 0 {
|
if builder.Len() == 0 {
|
||||||
return errors.New("invalid annotation format")
|
return errors.New("invalid annotation format")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(annoName) > 0 {
|
if len(annoName) > 0 {
|
||||||
value := builder.String()
|
value := builder.String()
|
||||||
if value != string(leftParenthesis) {
|
if value != string(leftParenthesis) {
|
||||||
|
|||||||
@@ -98,10 +98,6 @@ var (
|
|||||||
Name: "api",
|
Name: "api",
|
||||||
Usage: "the api file",
|
Usage: "the api file",
|
||||||
},
|
},
|
||||||
cli.BoolFlag{
|
|
||||||
Name: "force",
|
|
||||||
Usage: "force override the exist files",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
Action: gogen.GoCommand,
|
Action: gogen.GoCommand,
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user