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>
This commit is contained in:
@@ -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)
|
||||
* 可以在servicecontext.go里面传递依赖给logic,比如mysql, redis等
|
||||
* 在api定义的get/post/put/delete等请求对应的logic里增加业务处理逻辑
|
||||
|
||||
@@ -16,6 +16,7 @@ const mainTemplate = `package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
{{.importPackages}}
|
||||
)
|
||||
@@ -33,6 +34,8 @@ func main() {
|
||||
defer server.Stop()
|
||||
|
||||
handler.RegisterHandlers(server, ctx)
|
||||
|
||||
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||
server.Start()
|
||||
}
|
||||
`
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"text/template"
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/gogen"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@@ -28,18 +29,24 @@ service {{.name}}-api {
|
||||
|
||||
func NewService(c *cli.Context) error {
|
||||
args := c.Args()
|
||||
name := "greet"
|
||||
dirName := "greet"
|
||||
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
|
||||
}
|
||||
|
||||
filename := name + ".api"
|
||||
apiFilePath := filepath.Join(location, filename)
|
||||
err = util.MkdirIfNotExist(abs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dirName = filepath.Base(filepath.Clean(abs))
|
||||
filename := dirName + ".api"
|
||||
apiFilePath := filepath.Join(abs, filename)
|
||||
fp, err := os.Create(apiFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -48,11 +55,11 @@ func NewService(c *cli.Context) error {
|
||||
defer fp.Close()
|
||||
t := template.Must(template.New("template").Parse(apiTemplate))
|
||||
if err := t.Execute(fp, map[string]string{
|
||||
"name": name,
|
||||
"name": dirName,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = gogen.DoGenProject(apiFilePath, location)
|
||||
err = gogen.DoGenProject(apiFilePath, abs)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -170,9 +170,9 @@ func parseTag(basicLit *ast.BasicLit) string {
|
||||
}
|
||||
|
||||
// returns
|
||||
// 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
|
||||
// resp3:error
|
||||
// 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
|
||||
// resp3: error
|
||||
func parseType(expr ast.Expr) (interface{}, string, error) {
|
||||
if expr == nil {
|
||||
return nil, "", ErrUnSupportType
|
||||
@@ -214,14 +214,17 @@ func parseType(expr ast.Expr) (interface{}, string, error) {
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
value, valueStringExpr, err := parseType(v.Value)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
keyType, ok := key.(*spec.BasicType)
|
||||
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)
|
||||
return &spec.MapType{
|
||||
Key: keyType.Name,
|
||||
@@ -233,16 +236,17 @@ func parseType(expr ast.Expr) (interface{}, string, error) {
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
e := fmt.Sprintf("[]%s", stringExpr)
|
||||
return &spec.ArrayType{ArrayType: arrayType, StringExpr: e}, e, nil
|
||||
case *ast.InterfaceType:
|
||||
return &spec.InterfaceType{StringExpr: interfaceExpr}, interfaceExpr, nil
|
||||
case *ast.ChanType:
|
||||
return nil, "", errors.New("[chan] - unsupport type")
|
||||
return nil, "", errors.New("[chan] - unsupported type")
|
||||
case *ast.FuncType:
|
||||
return nil, "", errors.New("[func] - unsupport type")
|
||||
return nil, "", errors.New("[func] - unsupported type")
|
||||
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:
|
||||
x := v.X
|
||||
sel := v.Sel
|
||||
@@ -252,6 +256,7 @@ func parseType(expr ast.Expr) (interface{}, string, error) {
|
||||
if name != "time" && sel.Name != "Time" {
|
||||
return nil, "", fmt.Errorf("[outter package] - package:%s, unsupport type", name)
|
||||
}
|
||||
|
||||
tm := fmt.Sprintf("time.Time")
|
||||
return &spec.TimeType{
|
||||
StringExpr: tm,
|
||||
|
||||
@@ -73,6 +73,7 @@ type (
|
||||
StringExpr string
|
||||
Name string
|
||||
}
|
||||
|
||||
PointerType struct {
|
||||
StringExpr string
|
||||
// it can be asserted as BasicType: int、bool、
|
||||
|
||||
Reference in New Issue
Block a user