Преглед изворни кода

fix: Fix issue #1810 (#1811)

* Fix #1810

* Remove go embed

* Format code

* Remove useless code

Co-authored-by: anqiansong <anqiansong@bytedance.com>
anqiansong пре 3 година
родитељ
комит
305587aa81

+ 4 - 0
tools/goctl/api/dartgen/gen.go

@@ -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 + "/"

+ 4 - 0
tools/goctl/api/gogen/gen.go

@@ -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

+ 4 - 0
tools/goctl/api/javagen/gen.go

@@ -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))

+ 4 - 0
tools/goctl/api/ktgen/cmd.go

@@ -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 {

+ 7 - 0
tools/goctl/api/parser/parser_test.go

@@ -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 - 0
tools/goctl/api/spec/validate.go

@@ -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 - 0
tools/goctl/api/tsgen/gen.go

@@ -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))

+ 6 - 1
tools/goctl/api/validate/validate.go

@@ -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"))
 	}