Skip to content

Commit

Permalink
fix: Fix issue zeromicro#1810 (zeromicro#1811)
Browse files Browse the repository at this point in the history
* Fix zeromicro#1810

* Remove go embed

* Format code

* Remove useless code

Co-authored-by: anqiansong <anqiansong@bytedance.com>
  • Loading branch information
kesonan and anqiansong authored Apr 21, 2022
1 parent 2cdff97 commit 305587a
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 1 deletion.
4 changes: 4 additions & 0 deletions tools/goctl/api/dartgen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func DartCommand(c *cli.Context) error {
return err
}

if err := api.Validate(); err != nil {
return err
}

api.Service = api.Service.JoinPrefix()
if !strings.HasSuffix(dir, "/") {
dir = dir + "/"
Expand Down
4 changes: 4 additions & 0 deletions tools/goctl/api/gogen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func DoGenProject(apiFile, dir, style string) error {
return err
}

if err := api.Validate(); err != nil {
return err
}

cfg, err := config.NewConfig(style)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions tools/goctl/api/javagen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func JavaCommand(c *cli.Context) error {
return err
}

if err := api.Validate(); err != nil {
return err
}

api.Service = api.Service.JoinPrefix()
packetName := strings.TrimSuffix(api.Service.Name, "-api")
logx.Must(pathx.MkdirIfNotExist(dir))
Expand Down
4 changes: 4 additions & 0 deletions tools/goctl/api/ktgen/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func KtCommand(c *cli.Context) error {
return e
}

if err := api.Validate(); err != nil {
return err
}

api.Service = api.Service.JoinPrefix()
e = genBase(dir, pkg, api)
if e != nil {
Expand Down
7 changes: 7 additions & 0 deletions tools/goctl/api/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ func TestParseContent(t *testing.T) {
}
}
}

func TestMissingService(t *testing.T) {
sp, err := ParseContent("")
assert.Nil(t, err)
err = sp.Validate()
assert.Equal(t, spec.ErrMissingService, err)
}
16 changes: 16 additions & 0 deletions tools/goctl/api/spec/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package spec

import "errors"

var ErrMissingService = errors.New("missing service")

// Validate validates Validate the integrity of the spec.
func (s *ApiSpec) Validate() error {
if len(s.Service.Name) == 0 {
return ErrMissingService
}
if len(s.Service.Groups) == 0 {
return ErrMissingService
}
return nil
}
4 changes: 4 additions & 0 deletions tools/goctl/api/tsgen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func TsCommand(c *cli.Context) error {
return err
}

if err := api.Validate(); err != nil {
return err
}

api.Service = api.Service.JoinPrefix()
logx.Must(pathx.MkdirIfNotExist(dir))
logx.Must(genHandler(dir, webAPI, caller, api, unwrapAPI))
Expand Down
7 changes: 6 additions & 1 deletion tools/goctl/api/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ func GoValidateApi(c *cli.Context) error {
return errors.New("missing -api")
}

_, err := parser.Parse(apiFile)
spec, err := parser.Parse(apiFile)
if err != nil {
return err
}

err = spec.Validate()
if err == nil {
fmt.Println(aurora.Green("api format ok"))
}
Expand Down

0 comments on commit 305587a

Please sign in to comment.