You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/tools/goctl/api/tsgen/util.go

170 lines
3.9 KiB
Go

4 years ago
package tsgen
import (
"errors"
4 years ago
"fmt"
"io"
"strings"
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
"github.com/tal-tech/go-zero/tools/goctl/util"
4 years ago
)
func writeProperty(writer io.Writer, member spec.Member, indent int) error {
4 years ago
writeIndent(writer, indent)
ty, err := goTypeToTs(member.Type, false)
if err != nil {
return err
}
4 years ago
optionalTag := ""
if member.IsOptional() || member.IsOmitempty() {
optionalTag = "?"
}
name, err := member.GetPropertyName()
if err != nil {
return err
}
4 years ago
comment := member.GetComment()
if len(comment) > 0 {
comment = strings.TrimPrefix(comment, "//")
comment = " // " + strings.TrimSpace(comment)
}
if len(member.Docs) > 0 {
fmt.Fprintf(writer, "%s\n", strings.Join(member.Docs, ""))
4 years ago
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 spec.Type, fromPacket bool) (string, error) {
switch v := tp.(type) {
case spec.DefineStruct:
return addPrefix(tp, fromPacket), nil
case spec.PrimitiveType:
r, ok := primitiveType(tp.Name())
if !ok {
return "", errors.New("unsupported primitive type " + tp.Name())
4 years ago
}
return r, nil
case spec.MapType:
valueType, err := goTypeToTs(v.Value, fromPacket)
4 years ago
if err != nil {
return "", err
}
return fmt.Sprintf("{ [key: string]: %s }", valueType), nil
case spec.ArrayType:
if tp.Name() == "[]byte" {
return "Blob", nil
4 years ago
}
valueType, err := goTypeToTs(v.Value, fromPacket)
4 years ago
if err != nil {
return "", err
}
return fmt.Sprintf("Array<%s>", valueType), nil
case spec.InterfaceType:
return "any", nil
case spec.PointerType:
return goTypeToTs(v.Type, fromPacket)
4 years ago
}
return "", errors.New("unsupported type " + tp.Name())
4 years ago
}
func addPrefix(tp spec.Type, fromPacket bool) string {
if fromPacket {
return packagePrefix + util.Title(tp.Name())
4 years ago
}
return util.Title(tp.Name())
4 years ago
}
func primitiveType(tp string) (string, bool) {
switch tp {
case "string":
return "string", true
case "int", "int8", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64":
4 years ago
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) error {
fmt.Fprintf(writer, "export interface %s {\n", util.Title(tp.Name()))
if err := writeMembers(writer, tp, false); err != nil {
4 years ago
return err
}
4 years ago
fmt.Fprintf(writer, "}\n")
return genParamsTypesIfNeed(writer, tp)
4 years ago
}
func genParamsTypesIfNeed(writer io.Writer, tp spec.Type) error {
definedType, ok := tp.(spec.DefineStruct)
if !ok {
return errors.New("no members of type " + tp.Name())
}
members := definedType.GetNonBodyMembers()
4 years ago
if len(members) == 0 {
return nil
}
fmt.Fprintf(writer, "\n")
fmt.Fprintf(writer, "export interface %sParams {\n", util.Title(tp.Name()))
if err := writeMembers(writer, tp, true); err != nil {
4 years ago
return err
}
4 years ago
fmt.Fprintf(writer, "}\n")
return nil
}
func writeMembers(writer io.Writer, tp spec.Type, isParam bool) error {
definedType, ok := tp.(spec.DefineStruct)
if !ok {
pointType, ok := tp.(spec.PointerType)
if ok {
return writeMembers(writer, pointType.Type, isParam)
}
return errors.New(fmt.Sprintf("type %s not supported", tp.Name()))
}
members := definedType.GetBodyMembers()
4 years ago
if isParam {
members = definedType.GetNonBodyMembers()
4 years ago
}
for _, member := range members {
if member.IsInline {
if err := writeMembers(writer, member.Type, isParam); err != nil {
4 years ago
return err
}
continue
}
if err := writeProperty(writer, member, 1); err != nil {
return apiutil.WrapErr(err, " type "+tp.Name())
4 years ago
}
}
return nil
}