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/javagen/util.go

135 lines
2.6 KiB
Go

4 years ago
package javagen
import (
"errors"
"fmt"
"io"
"strings"
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
"github.com/tal-tech/go-zero/tools/goctl/util"
4 years ago
)
func writeProperty(writer io.Writer, member spec.Member, indent int) error {
writeIndent(writer, indent)
ty, err := specTypeToJava(member.Type)
4 years ago
ty = strings.Replace(ty, "*", "", 1)
if err != nil {
return err
}
4 years ago
name, err := member.GetPropertyName()
if err != nil {
return err
}
4 years ago
_, err = fmt.Fprintf(writer, "private %s %s", ty, name)
if err != nil {
return err
}
err = writeDefaultValue(writer, member)
if err != nil {
return err
}
4 years ago
fmt.Fprint(writer, ";\n")
return err
}
func writeDefaultValue(writer io.Writer, member spec.Member) error {
javaType, err := specTypeToJava(member.Type)
if err != nil {
return err
}
if javaType == "String" {
4 years ago
_, err := fmt.Fprintf(writer, " = \"\"")
return err
}
return nil
}
func writeIndent(writer io.Writer, indent int) {
for i := 0; i < indent; i++ {
fmt.Fprint(writer, "\t")
}
}
func indentString(indent int) string {
var result = ""
for i := 0; i < indent; i++ {
result += "\t"
}
return result
}
func specTypeToJava(tp spec.Type) (string, error) {
switch v := tp.(type) {
case spec.DefineStruct:
return util.Title(tp.Name()), nil
case spec.PrimitiveType:
r, ok := primitiveType(tp.Name())
if !ok {
return "", errors.New("unsupported primitive type " + tp.Name())
}
return r, nil
case spec.MapType:
valueType, err := specTypeToJava(v.Value)
4 years ago
if err != nil {
return "", err
}
return fmt.Sprintf("java.util.HashMap<String, %s>", util.Title(valueType)), nil
case spec.ArrayType:
if tp.Name() == "[]byte" {
return "byte[]", nil
4 years ago
}
valueType, err := specTypeToJava(v.Value)
4 years ago
if err != nil {
return "", err
}
switch valueType {
case "int":
return "Integer[]", nil
case "long":
return "Long[]", nil
case "float":
return "Float[]", nil
case "double":
return "Double[]", nil
case "boolean":
return "Boolean[]", nil
}
return fmt.Sprintf("java.util.ArrayList<%s>", util.Title(valueType)), nil
case spec.InterfaceType:
return "Object", nil
case spec.PointerType:
return specTypeToJava(v.Type)
}
return "", errors.New("unsupported primitive type " + tp.Name())
}
func primitiveType(tp string) (string, bool) {
switch tp {
case "string":
return "String", true
case "int64", "uint64":
return "long", true
case "int", "int8", "int32", "uint", "uint8", "uint16", "uint32":
return "int", true
case "float", "float32":
return "float", true
case "float64":
return "double", true
case "bool":
return "boolean", true
4 years ago
}
return "", false
4 years ago
}