Repository: zeromicro/go-zero
Pull request #1811 (2022-04-21 06:56:15)

fix: Fix issue #1810


Patch:
diff --git a/tools/goctl/api/dartgen/gen.go b/tools/goctl/api/dartgen/gen.go
index c7078bf2..a3fafb42 100644
--- a/tools/goctl/api/dartgen/gen.go
+++ b/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 + "/"
diff --git a/tools/goctl/api/gogen/gen.go b/tools/goctl/api/gogen/gen.go
index 14b81fcd..fa9a31cc 100644
--- a/tools/goctl/api/gogen/gen.go
+++ b/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
diff --git a/tools/goctl/api/javagen/gen.go b/tools/goctl/api/javagen/gen.go
index 4c99269e..2515eded 100644
--- a/tools/goctl/api/javagen/gen.go
+++ b/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))
diff --git a/tools/goctl/api/ktgen/cmd.go b/tools/goctl/api/ktgen/cmd.go
index 65a81e0b..fe07dabf 100644
--- a/tools/goctl/api/ktgen/cmd.go
+++ b/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 {
diff --git a/tools/goctl/api/spec/validate.go b/tools/goctl/api/spec/validate.go
new file mode 100644
index 00000000..f3c8bc7e
--- /dev/null
+++ b/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
+}
diff --git a/tools/goctl/api/tsgen/gen.go b/tools/goctl/api/tsgen/gen.go
index 063f5f59..88c021f3 100644
--- a/tools/goctl/api/tsgen/gen.go
+++ b/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))
diff --git a/tools/goctl/api/validate/validate.go b/tools/goctl/api/validate/validate.go
index ca430253..ad5d9333 100644
--- a/tools/goctl/api/validate/validate.go
+++ b/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"))
 	}


Test patch:
diff --git a/tools/goctl/api/parser/parser_test.go b/tools/goctl/api/parser/parser_test.go
index 16b84c70..272c32d4 100644
--- a/tools/goctl/api/parser/parser_test.go
+++ b/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)
+}