goctl added
This commit is contained in:
44
tools/goctl/api/tsgen/gen.go
Normal file
44
tools/goctl/api/tsgen/gen.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package tsgen
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"zero/core/lang"
|
||||
"zero/tools/goctl/api/parser"
|
||||
"zero/tools/goctl/util"
|
||||
|
||||
"github.com/logrusorgru/aurora"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
func TsCommand(c *cli.Context) error {
|
||||
apiFile := c.String("api")
|
||||
dir := c.String("dir")
|
||||
webApi := c.String("webapi")
|
||||
caller := c.String("caller")
|
||||
unwrapApi := c.Bool("unwrap")
|
||||
if len(apiFile) == 0 {
|
||||
return errors.New("missing -api")
|
||||
}
|
||||
if len(dir) == 0 {
|
||||
return errors.New("missing -dir")
|
||||
}
|
||||
|
||||
p, err := parser.NewParser(apiFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
api, err := p.Parse()
|
||||
if err != nil {
|
||||
fmt.Println(aurora.Red("Failed"))
|
||||
return err
|
||||
}
|
||||
|
||||
lang.Must(util.MkdirIfNotExist(dir))
|
||||
lang.Must(genHandler(dir, webApi, caller, api, unwrapApi))
|
||||
lang.Must(genComponents(dir, api))
|
||||
|
||||
fmt.Println(aurora.Green("Done."))
|
||||
return nil
|
||||
}
|
||||
79
tools/goctl/api/tsgen/gencomponents.go
Normal file
79
tools/goctl/api/tsgen/gencomponents.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package tsgen
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"path"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"zero/tools/goctl/api/spec"
|
||||
apiutil "zero/tools/goctl/api/util"
|
||||
"zero/tools/goctl/util"
|
||||
)
|
||||
|
||||
const (
|
||||
componentsTemplate = `// DO NOT EDIT, generated by goctl
|
||||
|
||||
{{.componentTypes}}
|
||||
`
|
||||
)
|
||||
|
||||
func genComponents(dir string, api *spec.ApiSpec) error {
|
||||
types := apiutil.GetSharedTypes(api)
|
||||
if len(types) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
val, err := buildTypes(types, func(name string) (*spec.Type, error) {
|
||||
for _, ty := range api.Types {
|
||||
if strings.ToLower(ty.Name) == strings.ToLower(name) {
|
||||
return &ty, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("inline type " + name + " not exist, please correct api file")
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
outputFile := apiutil.ComponentName(api) + ".ts"
|
||||
filename := path.Join(dir, outputFile)
|
||||
if err := util.RemoveIfExist(filename); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fp, created, err := apiutil.MaybeCreateFile(dir, ".", outputFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !created {
|
||||
return nil
|
||||
}
|
||||
defer fp.Close()
|
||||
|
||||
t := template.Must(template.New("componentsTemplate").Parse(componentsTemplate))
|
||||
return t.Execute(fp, map[string]string{
|
||||
"componentTypes": val,
|
||||
})
|
||||
}
|
||||
|
||||
func buildTypes(types []spec.Type, inlineType func(string) (*spec.Type, error)) (string, error) {
|
||||
var builder strings.Builder
|
||||
first := true
|
||||
for _, tp := range types {
|
||||
if first {
|
||||
first = false
|
||||
} else {
|
||||
builder.WriteString("\n")
|
||||
}
|
||||
if err := writeType(&builder, tp, func(name string) (*spec.Type, error) {
|
||||
return inlineType(name)
|
||||
}, func(tp string) string {
|
||||
return ""
|
||||
}); err != nil {
|
||||
return "", apiutil.WrapErr(err, "Type "+tp.Name+" generate error")
|
||||
}
|
||||
}
|
||||
|
||||
return builder.String(), nil
|
||||
}
|
||||
214
tools/goctl/api/tsgen/genpacket.go
Normal file
214
tools/goctl/api/tsgen/genpacket.go
Normal file
@@ -0,0 +1,214 @@
|
||||
package tsgen
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"zero/tools/goctl/api/spec"
|
||||
apiutil "zero/tools/goctl/api/util"
|
||||
"zero/tools/goctl/util"
|
||||
)
|
||||
|
||||
const (
|
||||
handlerTemplate = `{{.imports}}
|
||||
|
||||
{{.types}}
|
||||
|
||||
{{.apis}}
|
||||
`
|
||||
)
|
||||
|
||||
func genHandler(dir, webApi, caller string, api *spec.ApiSpec, unwrapApi bool) error {
|
||||
filename := strings.Replace(api.Service.Name, "-api", "", 1) + ".ts"
|
||||
if err := util.RemoveIfExist(path.Join(dir, filename)); err != nil {
|
||||
return err
|
||||
}
|
||||
fp, created, err := apiutil.MaybeCreateFile(dir, "", filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !created {
|
||||
return nil
|
||||
}
|
||||
defer fp.Close()
|
||||
|
||||
var localTypes []spec.Type
|
||||
for _, route := range api.Service.Routes {
|
||||
rts := apiutil.GetLocalTypes(api, route)
|
||||
localTypes = append(localTypes, rts...)
|
||||
}
|
||||
|
||||
var prefixForType = func(ty string) string {
|
||||
if _, pri := primitiveType(ty); pri {
|
||||
return ""
|
||||
}
|
||||
for _, item := range localTypes {
|
||||
if util.Title(item.Name) == ty {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
return packagePrefix
|
||||
}
|
||||
|
||||
types, err := genTypes(localTypes, func(name string) (*spec.Type, error) {
|
||||
for _, ty := range api.Types {
|
||||
if strings.ToLower(ty.Name) == strings.ToLower(name) {
|
||||
return &ty, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("inline type " + name + " not exist, please correct api file")
|
||||
}, prefixForType)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
imports := ""
|
||||
if len(caller) == 0 {
|
||||
caller = "webapi"
|
||||
}
|
||||
importCaller := caller
|
||||
if unwrapApi {
|
||||
importCaller = "{ " + importCaller + " }"
|
||||
}
|
||||
if len(webApi) > 0 {
|
||||
imports += `import ` + importCaller + ` from ` + "\"" + webApi + "\""
|
||||
}
|
||||
shardTypes := apiutil.GetSharedTypes(api)
|
||||
if len(shardTypes) != 0 {
|
||||
if len(imports) > 0 {
|
||||
imports += "\n"
|
||||
}
|
||||
outputFile := apiutil.ComponentName(api)
|
||||
imports += fmt.Sprintf(`import * as components from "%s"`, "./"+outputFile)
|
||||
}
|
||||
|
||||
apis, err := genApi(api, localTypes, caller, prefixForType)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t := template.Must(template.New("handlerTemplate").Parse(handlerTemplate))
|
||||
return t.Execute(fp, map[string]string{
|
||||
"webApi": webApi,
|
||||
"types": strings.TrimSpace(types),
|
||||
"imports": imports,
|
||||
"apis": strings.TrimSpace(apis),
|
||||
})
|
||||
}
|
||||
|
||||
func genTypes(localTypes []spec.Type, inlineType func(string) (*spec.Type, error), prefixForType func(string) string) (string, error) {
|
||||
var builder strings.Builder
|
||||
var first bool
|
||||
|
||||
for _, tp := range localTypes {
|
||||
if first {
|
||||
first = false
|
||||
} else {
|
||||
fmt.Fprintln(&builder)
|
||||
}
|
||||
if err := writeType(&builder, tp, func(name string) (s *spec.Type, err error) {
|
||||
return inlineType(name)
|
||||
}, prefixForType); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
types := builder.String()
|
||||
return types, nil
|
||||
}
|
||||
|
||||
func genApi(api *spec.ApiSpec, localTypes []spec.Type, caller string, prefixForType func(string) string) (string, error) {
|
||||
var builder strings.Builder
|
||||
for _, route := range api.Service.Routes {
|
||||
handler, ok := apiutil.GetAnnotationValue(route.Annotations, "server", "handler")
|
||||
if !ok {
|
||||
return "", fmt.Errorf("missing handler annotation for route %q", route.Path)
|
||||
}
|
||||
handler = util.Untitle(handler)
|
||||
handler = strings.Replace(handler, "Handler", "", 1)
|
||||
comment := commentForRoute(route)
|
||||
if len(comment) > 0 {
|
||||
fmt.Fprintf(&builder, "%s\n", comment)
|
||||
}
|
||||
fmt.Fprintf(&builder, "export function %s(%s) {\n", handler, paramsForRoute(route, prefixForType))
|
||||
writeIndent(&builder, 1)
|
||||
responseGeneric := "<null>"
|
||||
if len(route.ResponseType.Name) > 0 {
|
||||
val, err := goTypeToTs(route.ResponseType.Name, prefixForType)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
responseGeneric = fmt.Sprintf("<%s>", val)
|
||||
}
|
||||
fmt.Fprintf(&builder, `return %s.%s%s(%s)`, caller, strings.ToLower(route.Method),
|
||||
util.Title(responseGeneric), callParamsForRoute(route))
|
||||
builder.WriteString("\n}\n\n")
|
||||
}
|
||||
|
||||
apis := builder.String()
|
||||
return apis, nil
|
||||
}
|
||||
|
||||
func paramsForRoute(route spec.Route, prefixForType func(string) string) string {
|
||||
hasParams := pathHasParams(route)
|
||||
hasBody := hasRequestBody(route)
|
||||
rt, err := goTypeToTs(route.RequestType.Name, prefixForType)
|
||||
if err != nil {
|
||||
println(err.Error())
|
||||
return ""
|
||||
}
|
||||
if hasParams && hasBody {
|
||||
return fmt.Sprintf("params: %s, req: %s", rt+"Params", rt)
|
||||
} else if hasParams {
|
||||
return fmt.Sprintf("params: %s", rt+"Params")
|
||||
} else if hasBody {
|
||||
return fmt.Sprintf("req: %s", rt)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func commentForRoute(route spec.Route) string {
|
||||
var builder strings.Builder
|
||||
comment, _ := apiutil.GetAnnotationValue(route.Annotations, "doc", "summary")
|
||||
builder.WriteString("/**")
|
||||
builder.WriteString("\n * @description " + comment)
|
||||
hasParams := pathHasParams(route)
|
||||
hasBody := hasRequestBody(route)
|
||||
if hasParams && hasBody {
|
||||
builder.WriteString("\n * @param params")
|
||||
builder.WriteString("\n * @param req")
|
||||
} else if hasParams {
|
||||
builder.WriteString("\n * @param params")
|
||||
} else if hasBody {
|
||||
builder.WriteString("\n * @param req")
|
||||
}
|
||||
builder.WriteString("\n */")
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
func callParamsForRoute(route spec.Route) string {
|
||||
hasParams := pathHasParams(route)
|
||||
hasBody := hasRequestBody(route)
|
||||
if hasParams && hasBody {
|
||||
return fmt.Sprintf("%s, %s, %s", pathForRoute(route), "params", "req")
|
||||
} else if hasParams {
|
||||
return fmt.Sprintf("%s, %s", pathForRoute(route), "params")
|
||||
} else if hasBody {
|
||||
return fmt.Sprintf("%s, %s", pathForRoute(route), "req")
|
||||
}
|
||||
return pathForRoute(route)
|
||||
}
|
||||
|
||||
func pathForRoute(route spec.Route) string {
|
||||
return "\"" + route.Path + "\""
|
||||
}
|
||||
|
||||
func pathHasParams(route spec.Route) bool {
|
||||
return len(route.RequestType.Members) != len(route.RequestType.GetBodyMembers())
|
||||
}
|
||||
|
||||
func hasRequestBody(route spec.Route) bool {
|
||||
return len(route.RequestType.Name) > 0 && len(route.RequestType.GetBodyMembers()) > 0
|
||||
}
|
||||
167
tools/goctl/api/tsgen/util.go
Normal file
167
tools/goctl/api/tsgen/util.go
Normal file
@@ -0,0 +1,167 @@
|
||||
package tsgen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"zero/tools/goctl/api/spec"
|
||||
apiutil "zero/tools/goctl/api/util"
|
||||
"zero/tools/goctl/util"
|
||||
)
|
||||
|
||||
func writeProperty(writer io.Writer, member spec.Member, indent int, prefixForType func(string) string) error {
|
||||
writeIndent(writer, indent)
|
||||
ty, err := goTypeToTs(member.Type, prefixForType)
|
||||
optionalTag := ""
|
||||
if member.IsOptional() || member.IsOmitempty() {
|
||||
optionalTag = "?"
|
||||
}
|
||||
name, err := member.GetPropertyName()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
comment := member.GetComment()
|
||||
if len(comment) > 0 {
|
||||
comment = strings.TrimPrefix(comment, "//")
|
||||
comment = " // " + strings.TrimSpace(comment)
|
||||
}
|
||||
if len(member.Docs) > 0 {
|
||||
_, err = fmt.Fprintf(writer, "%s\n", strings.Join(member.Docs, ""))
|
||||
writeIndent(writer, 1)
|
||||
}
|
||||
_, err = fmt.Fprintf(writer, "%s%s: %s%s\n", name, optionalTag, ty, comment)
|
||||
return err
|
||||
}
|
||||
|
||||
func writeIndent(writer io.Writer, indent int) {
|
||||
for i := 0; i < indent; i++ {
|
||||
fmt.Fprint(writer, "\t")
|
||||
}
|
||||
}
|
||||
|
||||
func goTypeToTs(tp string, prefixForType func(string) string) (string, error) {
|
||||
if val, pri := primitiveType(tp); pri {
|
||||
return val, nil
|
||||
}
|
||||
if tp == "[]byte" {
|
||||
return "Blob", nil
|
||||
} else if strings.HasPrefix(tp, "[][]") {
|
||||
tys, err := apiutil.DecomposeType(tp)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(tys) == 0 {
|
||||
return "", fmt.Errorf("%s tp parse error", tp)
|
||||
}
|
||||
innerType, err := goTypeToTs(tys[0], prefixForType)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf("Array<Array<%s>>", innerType), nil
|
||||
} else if strings.HasPrefix(tp, "[]") {
|
||||
tys, err := apiutil.DecomposeType(tp)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(tys) == 0 {
|
||||
return "", fmt.Errorf("%s tp parse error", tp)
|
||||
}
|
||||
innerType, err := goTypeToTs(tys[0], prefixForType)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf("Array<%s>", innerType), nil
|
||||
} else if strings.HasPrefix(tp, "map") {
|
||||
tys, err := apiutil.DecomposeType(tp)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(tys) != 2 {
|
||||
return "", fmt.Errorf("%s tp parse error", tp)
|
||||
}
|
||||
innerType, err := goTypeToTs(tys[1], prefixForType)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf("{ [key: string]: %s }", innerType), nil
|
||||
}
|
||||
return addPrefixIfNeed(util.Title(tp), prefixForType), nil
|
||||
}
|
||||
|
||||
func addPrefixIfNeed(tp string, prefixForType func(string) string) string {
|
||||
if val, pri := primitiveType(tp); pri {
|
||||
return val
|
||||
}
|
||||
tp = strings.Replace(tp, "*", "", 1)
|
||||
return prefixForType(tp) + util.Title(tp)
|
||||
}
|
||||
|
||||
func primitiveType(tp string) (string, bool) {
|
||||
switch tp {
|
||||
case "string":
|
||||
return "string", true
|
||||
case "int", "int8", "int32", "int64":
|
||||
return "number", true
|
||||
case "float", "float32", "float64":
|
||||
return "number", true
|
||||
case "bool":
|
||||
return "boolean", true
|
||||
case "[]byte":
|
||||
return "Blob", true
|
||||
case "interface{}":
|
||||
return "any", true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
func writeType(writer io.Writer, tp spec.Type, inlineType func(string) (*spec.Type, error), prefixForType func(string) string) error {
|
||||
fmt.Fprintf(writer, "export interface %s {\n", util.Title(tp.Name))
|
||||
if err := genMembers(writer, tp, false, inlineType, prefixForType); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(writer, "}\n")
|
||||
err := genParamsTypesIfNeed(writer, tp, inlineType, prefixForType)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func genParamsTypesIfNeed(writer io.Writer, tp spec.Type, inlineType func(string) (*spec.Type, error), prefixForType func(string) string) error {
|
||||
members := tp.GetNonBodyMembers()
|
||||
if len(members) == 0 {
|
||||
return nil
|
||||
}
|
||||
fmt.Fprintf(writer, "\n")
|
||||
fmt.Fprintf(writer, "export interface %sParams {\n", util.Title(tp.Name))
|
||||
if err := genMembers(writer, tp, true, inlineType, prefixForType); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(writer, "}\n")
|
||||
return nil
|
||||
}
|
||||
|
||||
func genMembers(writer io.Writer, tp spec.Type, isParam bool, inlineType func(string) (*spec.Type, error), prefixForType func(string) string) error {
|
||||
members := tp.GetBodyMembers()
|
||||
if isParam {
|
||||
members = tp.GetNonBodyMembers()
|
||||
}
|
||||
for _, member := range members {
|
||||
if member.IsInline {
|
||||
// 获取inline类型的成员然后添加到type中
|
||||
it, err := inlineType(strings.TrimPrefix(member.Type, "*"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := genMembers(writer, *it, isParam, inlineType, prefixForType); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
if err := writeProperty(writer, member, 1, prefixForType); err != nil {
|
||||
return apiutil.WrapErr(err, " type "+tp.Name)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
5
tools/goctl/api/tsgen/vars.go
Normal file
5
tools/goctl/api/tsgen/vars.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package tsgen
|
||||
|
||||
const (
|
||||
packagePrefix = "components."
|
||||
)
|
||||
Reference in New Issue
Block a user