fix-break-line

master
stevenzack 4 years ago committed by Kevin Wan
parent 926d746df5
commit de2f8c06fb

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

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

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

Loading…
Cancel
Save