util.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. var 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. switch valueType {
  84. case "int":
  85. return "Integer[]", nil
  86. case "long":
  87. return "Long[]", nil
  88. case "float":
  89. return "Float[]", nil
  90. case "double":
  91. return "Double[]", nil
  92. case "boolean":
  93. return "Boolean[]", nil
  94. }
  95. return fmt.Sprintf("java.util.ArrayList<%s>", util.Title(valueType)), nil
  96. case spec.InterfaceType:
  97. return "Object", nil
  98. case spec.PointerType:
  99. return specTypeToJava(v.Type)
  100. }
  101. return "", errors.New("unsupported primitive type " + tp.Name())
  102. }
  103. func primitiveType(tp string) (string, bool) {
  104. switch tp {
  105. case "string":
  106. return "String", true
  107. case "int64", "uint64":
  108. return "long", true
  109. case "int", "int8", "int32", "uint", "uint8", "uint16", "uint32":
  110. return "int", true
  111. case "float", "float32":
  112. return "float", true
  113. case "float64":
  114. return "double", true
  115. case "bool":
  116. return "boolean", true
  117. }
  118. return "", false
  119. }