util.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package tsgen
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "strings"
  7. "github.com/tal-tech/go-zero/tools/goctl/api/spec"
  8. apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
  9. "github.com/tal-tech/go-zero/tools/goctl/util"
  10. )
  11. func writeProperty(writer io.Writer, member spec.Member, indent int) error {
  12. writeIndent(writer, indent)
  13. ty, err := goTypeToTs(member.Type, false)
  14. if err != nil {
  15. return err
  16. }
  17. optionalTag := ""
  18. if member.IsOptional() || member.IsOmitEmpty() {
  19. optionalTag = "?"
  20. }
  21. name, err := member.GetPropertyName()
  22. if err != nil {
  23. return err
  24. }
  25. comment := member.GetComment()
  26. if len(comment) > 0 {
  27. comment = strings.TrimPrefix(comment, "//")
  28. comment = " // " + strings.TrimSpace(comment)
  29. }
  30. if len(member.Docs) > 0 {
  31. fmt.Fprintf(writer, "%s\n", strings.Join(member.Docs, ""))
  32. writeIndent(writer, 1)
  33. }
  34. _, err = fmt.Fprintf(writer, "%s%s: %s%s\n", name, optionalTag, ty, comment)
  35. return err
  36. }
  37. func writeIndent(writer io.Writer, indent int) {
  38. for i := 0; i < indent; i++ {
  39. fmt.Fprint(writer, "\t")
  40. }
  41. }
  42. func goTypeToTs(tp spec.Type, fromPacket bool) (string, error) {
  43. switch v := tp.(type) {
  44. case spec.DefineStruct:
  45. return addPrefix(tp, fromPacket), nil
  46. case spec.PrimitiveType:
  47. r, ok := primitiveType(tp.Name())
  48. if !ok {
  49. return "", errors.New("unsupported primitive type " + tp.Name())
  50. }
  51. return r, nil
  52. case spec.MapType:
  53. valueType, err := goTypeToTs(v.Value, fromPacket)
  54. if err != nil {
  55. return "", err
  56. }
  57. return fmt.Sprintf("{ [key: string]: %s }", valueType), nil
  58. case spec.ArrayType:
  59. if tp.Name() == "[]byte" {
  60. return "Blob", nil
  61. }
  62. valueType, err := goTypeToTs(v.Value, fromPacket)
  63. if err != nil {
  64. return "", err
  65. }
  66. return fmt.Sprintf("Array<%s>", valueType), nil
  67. case spec.InterfaceType:
  68. return "any", nil
  69. case spec.PointerType:
  70. return goTypeToTs(v.Type, fromPacket)
  71. }
  72. return "", errors.New("unsupported type " + tp.Name())
  73. }
  74. func addPrefix(tp spec.Type, fromPacket bool) string {
  75. if fromPacket {
  76. return packagePrefix + util.Title(tp.Name())
  77. }
  78. return util.Title(tp.Name())
  79. }
  80. func primitiveType(tp string) (string, bool) {
  81. switch tp {
  82. case "string":
  83. return "string", true
  84. case "int", "int8", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64":
  85. return "number", true
  86. case "float", "float32", "float64":
  87. return "number", true
  88. case "bool":
  89. return "boolean", true
  90. case "[]byte":
  91. return "Blob", true
  92. case "interface{}":
  93. return "any", true
  94. }
  95. return "", false
  96. }
  97. func writeType(writer io.Writer, tp spec.Type) error {
  98. fmt.Fprintf(writer, "export interface %s {\n", util.Title(tp.Name()))
  99. if err := writeMembers(writer, tp, false); err != nil {
  100. return err
  101. }
  102. fmt.Fprintf(writer, "}\n")
  103. return genParamsTypesIfNeed(writer, tp)
  104. }
  105. func genParamsTypesIfNeed(writer io.Writer, tp spec.Type) error {
  106. definedType, ok := tp.(spec.DefineStruct)
  107. if !ok {
  108. return errors.New("no members of type " + tp.Name())
  109. }
  110. members := definedType.GetNonBodyMembers()
  111. if len(members) == 0 {
  112. return nil
  113. }
  114. fmt.Fprintf(writer, "\n")
  115. fmt.Fprintf(writer, "export interface %sParams {\n", util.Title(tp.Name()))
  116. if err := writeMembers(writer, tp, true); err != nil {
  117. return err
  118. }
  119. fmt.Fprintf(writer, "}\n")
  120. return nil
  121. }
  122. func writeMembers(writer io.Writer, tp spec.Type, isParam bool) error {
  123. definedType, ok := tp.(spec.DefineStruct)
  124. if !ok {
  125. pointType, ok := tp.(spec.PointerType)
  126. if ok {
  127. return writeMembers(writer, pointType.Type, isParam)
  128. }
  129. return fmt.Errorf("type %s not supported", tp.Name())
  130. }
  131. members := definedType.GetBodyMembers()
  132. if isParam {
  133. members = definedType.GetNonBodyMembers()
  134. }
  135. for _, member := range members {
  136. if member.IsInline {
  137. if err := writeMembers(writer, member.Type, isParam); err != nil {
  138. return err
  139. }
  140. continue
  141. }
  142. if err := writeProperty(writer, member, 1); err != nil {
  143. return apiutil.WrapErr(err, " type "+tp.Name())
  144. }
  145. }
  146. return nil
  147. }