From de2f8c06fb1c41d02c0b372fd3b49aca5617c575 Mon Sep 17 00:00:00 2001 From: stevenzack Date: Fri, 14 Aug 2020 10:35:35 +0800 Subject: [PATCH] fix-break-line --- tools/goctl/api/ktgen/cmd.go | 11 +++++---- tools/goctl/api/ktgen/funcs.go | 44 ++++++++++++++++++---------------- tools/goctl/api/ktgen/gen.go | 35 ++++++++++++++------------- 3 files changed, 48 insertions(+), 42 deletions(-) diff --git a/tools/goctl/api/ktgen/cmd.go b/tools/goctl/api/ktgen/cmd.go index b8e382e8..cd91e18d 100644 --- a/tools/goctl/api/ktgen/cmd.go +++ b/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 } diff --git a/tools/goctl/api/ktgen/funcs.go b/tools/goctl/api/ktgen/funcs.go index 9a301c66..60c7f53e 100644 --- a/tools/goctl/api/ktgen/funcs.go +++ b/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" + return "Map" } switch t { @@ -62,6 +66,6 @@ func parseType(t string) string { } } -func add(a,i int)int{ - return a+i -} \ No newline at end of file +func add(a, i int) int { + return a + i +} diff --git a/tools/goctl/api/ktgen/gen.go b/tools/goctl/api/ktgen/gen.go index fd9923f4..e6842fb4 100644 --- a/tools/goctl/api/ktgen/gen.go +++ b/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 }