optimized api new with absolute path like: goctl api new $PWD/xxxx (#67)

* rebase upstream

* rebase

* trim no need line

* trim no need line

* trim no need line

* update doc

* remove update

* optimized api new with absolute path like: goctl api new $PWD/xxxx

* optimized api new with absolute path like: goctl api new $PWD/xxxx

* optimized api new with absolute path like: goctl api new $PWD/xxxx

* optimized api new with absolute path like: goctl api new $PWD/xxxx

Co-authored-by: kingxt <dream4kingxt@163.com>
master
kingxt 4 years ago committed by GitHub
parent fb22589cf5
commit 05df86436f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -131,8 +131,6 @@ go get -u github.com/tal-tech/go-zero
编写业务代码: 编写业务代码:
* 可以在servicecontext.go里面传递依赖给logic比如mysql, redis等
* 在api定义的get/post/put/delete等请求对应的logic里增加业务处理逻辑
* api文件定义了服务对外暴露的路由可参考[api规范](https://github.com/tal-tech/go-zero/blob/master/doc/goctl.md) * api文件定义了服务对外暴露的路由可参考[api规范](https://github.com/tal-tech/go-zero/blob/master/doc/goctl.md)
* 可以在servicecontext.go里面传递依赖给logic比如mysql, redis等 * 可以在servicecontext.go里面传递依赖给logic比如mysql, redis等
* 在api定义的get/post/put/delete等请求对应的logic里增加业务处理逻辑 * 在api定义的get/post/put/delete等请求对应的logic里增加业务处理逻辑

@ -16,6 +16,7 @@ const mainTemplate = `package main
import ( import (
"flag" "flag"
"fmt"
{{.importPackages}} {{.importPackages}}
) )
@ -33,6 +34,8 @@ func main() {
defer server.Stop() defer server.Stop()
handler.RegisterHandlers(server, ctx) handler.RegisterHandlers(server, ctx)
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
server.Start() server.Start()
} }
` `

@ -6,6 +6,7 @@ import (
"text/template" "text/template"
"github.com/tal-tech/go-zero/tools/goctl/api/gogen" "github.com/tal-tech/go-zero/tools/goctl/api/gogen"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -28,18 +29,24 @@ service {{.name}}-api {
func NewService(c *cli.Context) error { func NewService(c *cli.Context) error {
args := c.Args() args := c.Args()
name := "greet" dirName := "greet"
if len(args) > 0 { if len(args) > 0 {
name = args.First() dirName = args.First()
} }
location := name
err := os.MkdirAll(location, os.ModePerm) abs, err := filepath.Abs(dirName)
if err != nil {
return err
}
err = util.MkdirIfNotExist(abs)
if err != nil { if err != nil {
return err return err
} }
filename := name + ".api" dirName = filepath.Base(filepath.Clean(abs))
apiFilePath := filepath.Join(location, filename) filename := dirName + ".api"
apiFilePath := filepath.Join(abs, filename)
fp, err := os.Create(apiFilePath) fp, err := os.Create(apiFilePath)
if err != nil { if err != nil {
return err return err
@ -48,11 +55,11 @@ func NewService(c *cli.Context) error {
defer fp.Close() defer fp.Close()
t := template.Must(template.New("template").Parse(apiTemplate)) t := template.Must(template.New("template").Parse(apiTemplate))
if err := t.Execute(fp, map[string]string{ if err := t.Execute(fp, map[string]string{
"name": name, "name": dirName,
}); err != nil { }); err != nil {
return err return err
} }
err = gogen.DoGenProject(apiFilePath, location) err = gogen.DoGenProject(apiFilePath, abs)
return err return err
} }

@ -170,9 +170,9 @@ func parseTag(basicLit *ast.BasicLit) string {
} }
// returns // returns
// resp1:type can convert to *spec.PointerType|*spec.BasicType|*spec.MapType|*spec.ArrayType|*spec.InterfaceType // resp1: type can convert to *spec.PointerType|*spec.BasicType|*spec.MapType|*spec.ArrayType|*spec.InterfaceType
// resp2:type's string expression,like int、string、[]int64、map[string]User、*User // resp2: type's string expression,like int、string、[]int64、map[string]User、*User
// resp3:error // resp3: error
func parseType(expr ast.Expr) (interface{}, string, error) { func parseType(expr ast.Expr) (interface{}, string, error) {
if expr == nil { if expr == nil {
return nil, "", ErrUnSupportType return nil, "", ErrUnSupportType
@ -214,14 +214,17 @@ func parseType(expr ast.Expr) (interface{}, string, error) {
if err != nil { if err != nil {
return nil, "", err return nil, "", err
} }
value, valueStringExpr, err := parseType(v.Value) value, valueStringExpr, err := parseType(v.Value)
if err != nil { if err != nil {
return nil, "", err return nil, "", err
} }
keyType, ok := key.(*spec.BasicType) keyType, ok := key.(*spec.BasicType)
if !ok { if !ok {
return nil, "", fmt.Errorf("[%+v] - unsupport type of map key", v.Key) return nil, "", fmt.Errorf("[%+v] - unsupported type of map key", v.Key)
} }
e := fmt.Sprintf("map[%s]%s", keyStringExpr, valueStringExpr) e := fmt.Sprintf("map[%s]%s", keyStringExpr, valueStringExpr)
return &spec.MapType{ return &spec.MapType{
Key: keyType.Name, Key: keyType.Name,
@ -233,16 +236,17 @@ func parseType(expr ast.Expr) (interface{}, string, error) {
if err != nil { if err != nil {
return nil, "", err return nil, "", err
} }
e := fmt.Sprintf("[]%s", stringExpr) e := fmt.Sprintf("[]%s", stringExpr)
return &spec.ArrayType{ArrayType: arrayType, StringExpr: e}, e, nil return &spec.ArrayType{ArrayType: arrayType, StringExpr: e}, e, nil
case *ast.InterfaceType: case *ast.InterfaceType:
return &spec.InterfaceType{StringExpr: interfaceExpr}, interfaceExpr, nil return &spec.InterfaceType{StringExpr: interfaceExpr}, interfaceExpr, nil
case *ast.ChanType: case *ast.ChanType:
return nil, "", errors.New("[chan] - unsupport type") return nil, "", errors.New("[chan] - unsupported type")
case *ast.FuncType: case *ast.FuncType:
return nil, "", errors.New("[func] - unsupport type") return nil, "", errors.New("[func] - unsupported type")
case *ast.StructType: // todo can optimize case *ast.StructType: // todo can optimize
return nil, "", errors.New("[struct] - unsupport inline struct type") return nil, "", errors.New("[struct] - unsupported inline struct type")
case *ast.SelectorExpr: case *ast.SelectorExpr:
x := v.X x := v.X
sel := v.Sel sel := v.Sel
@ -252,6 +256,7 @@ func parseType(expr ast.Expr) (interface{}, string, error) {
if name != "time" && sel.Name != "Time" { if name != "time" && sel.Name != "Time" {
return nil, "", fmt.Errorf("[outter package] - package:%s, unsupport type", name) return nil, "", fmt.Errorf("[outter package] - package:%s, unsupport type", name)
} }
tm := fmt.Sprintf("time.Time") tm := fmt.Sprintf("time.Time")
return &spec.TimeType{ return &spec.TimeType{
StringExpr: tm, StringExpr: tm,

@ -73,6 +73,7 @@ type (
StringExpr string StringExpr string
Name string Name string
} }
PointerType struct { PointerType struct {
StringExpr string StringExpr string
// it can be asserted as BasicType: int、bool、 // it can be asserted as BasicType: int、bool、

Loading…
Cancel
Save