1
0
stevenzack 4 жил өмнө
parent
commit
de2f8c06fb

+ 6 - 5
tools/goctl/api/ktgen/cmd.go

@@ -2,6 +2,7 @@ package ktgen
 
 import (
 	"errors"
+
 	"github.com/tal-tech/go-zero/core/lang"
 	"github.com/tal-tech/go-zero/tools/goctl/api/parser"
 	"github.com/urfave/cli"
@@ -25,12 +26,12 @@ func KtCommand(c *cli.Context) error {
 	if e != nil {
 		return e
 	}
-	api,e:=p.Parse()
-	if e!=nil {
-	    return e
+	api, e := p.Parse()
+	if e != nil {
+		return e
 	}
 
-	lang.Must(genBase(dir,pkg,api))
-	lang.Must(genApi(dir,pkg, api))
+	lang.Must(genBase(dir, pkg, api))
+	lang.Must(genApi(dir, pkg, api))
 	return nil
 }

+ 24 - 20
tools/goctl/api/ktgen/funcs.go

@@ -1,17 +1,20 @@
 package ktgen
 
 import (
-	"github.com/tal-tech/go-zero/tools/goctl/api/util"
 	"log"
 	"strings"
 	"text/template"
+
+	"github.com/tal-tech/go-zero/tools/goctl/api/util"
 )
-var funcsMap=template.FuncMap{
-	"lowCamelCase":lowCamelCase,
-	"pathToFuncName":pathToFuncName,
-	"parseType":parseType,
-	"add":add,
+
+var funcsMap = template.FuncMap{
+	"lowCamelCase":   lowCamelCase,
+	"pathToFuncName": pathToFuncName,
+	"parseType":      parseType,
+	"add":            add,
 }
+
 func lowCamelCase(s string) string {
 	if len(s) < 1 {
 		return ""
@@ -25,27 +28,28 @@ func pathToFuncName(path string) string {
 		path = "/" + path
 	}
 
-	path = strings.Replace(path, "/", "_", -1)
-	path = strings.Replace(path, "-", "_", -1)
+	path = strings.ReplaceAll(path, "/", "_")
+	path = strings.ReplaceAll(path, "-", "_")
 
 	camel := util.ToCamelCase(path)
 	return util.ToLower(camel[:1]) + camel[1:]
 }
+
 func parseType(t string) string {
-	t=strings.Replace(t,"*","",-1)
-	if strings.HasPrefix(t,"[]"){
-		return "List<"+parseType(t[2:])+ ">"
+	t = strings.Replace(t, "*", "", -1)
+	if strings.HasPrefix(t, "[]") {
+		return "List<" + parseType(t[2:]) + ">"
 	}
 
-	if strings.HasPrefix(t,"map"){
-		tys,e:=util.DecomposeType(t)
-		if e!=nil{
-		    log.Fatal(e)
+	if strings.HasPrefix(t, "map") {
+		tys, e := util.DecomposeType(t)
+		if e != nil {
+			log.Fatal(e)
 		}
-		if len(tys)!=2{
+		if len(tys) != 2 {
 			log.Fatal("Map type number !=2")
 		}
-		return "Map<String,"+parseType(tys[1])+">"
+		return "Map<String," + parseType(tys[1]) + ">"
 	}
 
 	switch t {
@@ -62,6 +66,6 @@ func parseType(t string) string {
 	}
 }
 
-func add(a,i int)int{
-	return a+i
-}
+func add(a, i int) int {
+	return a + i
+}

+ 18 - 17
tools/goctl/api/ktgen/gen.go

@@ -1,13 +1,14 @@
 package ktgen
 
 import (
-	"github.com/tal-tech/go-zero/core/logx"
-	"github.com/tal-tech/go-zero/tools/goctl/api/spec"
 	"log"
 	"os"
 	"path/filepath"
 	"text/template"
+
 	"github.com/iancoleman/strcase"
+	"github.com/tal-tech/go-zero/core/logx"
+	"github.com/tal-tech/go-zero/tools/goctl/api/spec"
 )
 
 const (
@@ -145,28 +146,28 @@ func genBase(dir, pkg string, api *spec.ApiSpec) error {
 
 func genApi(dir, pkg string, api *spec.ApiSpec) error {
 	path := filepath.Join(dir, strcase.ToCamel(api.Info.Title+"Api")+".kt")
-	api.Info.Title= pkg
+	api.Info.Title = pkg
 
-	e:=os.MkdirAll(dir,0755)
-	if e!=nil {
-	    logx.Error(e)
-	    return e
+	e := os.MkdirAll(dir, 0755)
+	if e != nil {
+		logx.Error(e)
+		return e
 	}
 
-	file,e:=os.OpenFile(path,os.O_WRONLY|os.O_TRUNC|os.O_CREATE,0644)
-	if e!=nil {
-	    logx.Error(e)
-	    return e
+	file, e := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
+	if e != nil {
+		logx.Error(e)
+		return e
 	}
 	defer file.Close()
 
-	t,e:=template.New("api").Funcs(funcsMap).Parse(apiTemplate)
-	if e!=nil{
-	    log.Fatal(e)
+	t, e := template.New("api").Funcs(funcsMap).Parse(apiTemplate)
+	if e != nil {
+		log.Fatal(e)
 	}
-	e=t.Execute(file,api)
-	if e!=nil{
-	    log.Fatal(e)
+	e = t.Execute(file, api)
+	if e != nil {
+		log.Fatal(e)
 	}
 	return nil
 }