util.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package javagen
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "strings"
  7. "github.com/tal-tech/go-zero/tools/goctl/api/spec"
  8. "github.com/tal-tech/go-zero/tools/goctl/util"
  9. )
  10. func writeProperty(writer io.Writer, member spec.Member, indent int) error {
  11. if len(member.Comment) > 0 {
  12. writeIndent(writer, indent)
  13. fmt.Fprint(writer, member.Comment+util.NL)
  14. }
  15. writeIndent(writer, indent)
  16. ty, err := specTypeToJava(member.Type)
  17. ty = strings.Replace(ty, "*", "", 1)
  18. if err != nil {
  19. return err
  20. }
  21. name, err := member.GetPropertyName()
  22. if err != nil {
  23. return err
  24. }
  25. _, err = fmt.Fprintf(writer, "private %s %s", ty, name)
  26. if err != nil {
  27. return err
  28. }
  29. err = writeDefaultValue(writer, member)
  30. if err != nil {
  31. return err
  32. }
  33. fmt.Fprint(writer, ";\n")
  34. return err
  35. }
  36. func writeDefaultValue(writer io.Writer, member spec.Member) error {
  37. javaType, err := specTypeToJava(member.Type)
  38. if err != nil {
  39. return err
  40. }
  41. if javaType == "String" {
  42. _, err := fmt.Fprintf(writer, " = \"\"")
  43. return err
  44. }
  45. return nil
  46. }
  47. func writeIndent(writer io.Writer, indent int) {
  48. for i := 0; i < indent; i++ {
  49. fmt.Fprint(writer, "\t")
  50. }
  51. }
  52. func indentString(indent int) string {
  53. result := ""
  54. for i := 0; i < indent; i++ {
  55. result += "\t"
  56. }
  57. return result
  58. }
  59. func specTypeToJava(tp spec.Type) (string, error) {
  60. switch v := tp.(type) {
  61. case spec.DefineStruct:
  62. return util.Title(tp.Name()), nil
  63. case spec.PrimitiveType:
  64. r, ok := primitiveType(tp.Name())
  65. if !ok {
  66. return "", errors.New("unsupported primitive type " + tp.Name())
  67. }
  68. return r, nil
  69. case spec.MapType:
  70. valueType, err := specTypeToJava(v.Value)
  71. if err != nil {
  72. return "", err
  73. }
  74. return fmt.Sprintf("java.util.HashMap<String, %s>", util.Title(valueType)), nil
  75. case spec.ArrayType:
  76. if tp.Name() == "[]byte" {
  77. return "byte[]", nil
  78. }
  79. valueType, err := specTypeToJava(v.Value)
  80. if err != nil {
  81. return "", err
  82. }
  83. s := getBaseType(valueType)
  84. if len(s) == 0 {
  85. return s, errors.New("unsupported primitive type " + tp.Name())
  86. }
  87. return fmt.Sprintf("java.util.ArrayList<%s>", util.Title(valueType)), nil
  88. case spec.InterfaceType:
  89. return "Object", nil
  90. case spec.PointerType:
  91. return specTypeToJava(v.Type)
  92. }
  93. return "", errors.New("unsupported primitive type " + tp.Name())
  94. }
  95. func getBaseType(valueType string) string {
  96. switch valueType {
  97. case "int":
  98. return "Integer[]"
  99. case "long":
  100. return "Long[]"
  101. case "float":
  102. return "Float[]"
  103. case "double":
  104. return "Double[]"
  105. case "boolean":
  106. return "Boolean[]"
  107. default:
  108. return ""
  109. }
  110. }
  111. func primitiveType(tp string) (string, bool) {
  112. switch tp {
  113. case "string":
  114. return "String", true
  115. case "int64", "uint64":
  116. return "long", true
  117. case "int", "int8", "int32", "uint", "uint8", "uint16", "uint32":
  118. return "int", true
  119. case "float", "float32":
  120. return "float", true
  121. case "float64":
  122. return "double", true
  123. case "bool":
  124. return "boolean", true
  125. }
  126. return "", false
  127. }