diff --git a/tools/goctl/api/dartgen/gendata.go b/tools/goctl/api/dartgen/gendata.go index f3d3d83f..4b5b6afb 100644 --- a/tools/goctl/api/dartgen/gendata.go +++ b/tools/goctl/api/dartgen/gendata.go @@ -12,19 +12,19 @@ const dataTemplate = `// --{{with .Info}}{{.Title}}{{end}}-- class {{.Name}}{ {{range .Members}} /// {{.Comment}} - final {{.Type}} {{lowCamelCase .Name}}; + final {{.Type.Name}} {{lowCamelCase .Name}}; {{end}} {{.Name}}({ {{range .Members}} this.{{lowCamelCase .Name}},{{end}} }); factory {{.Name}}.fromJson(Map m) { return {{.Name}}({{range .Members}} - {{lowCamelCase .Name}}: {{if isDirectType .Type}}m['{{tagGet .Tag "json"}}']{{else if isClassListType .Type}}(m['{{tagGet .Tag "json"}}'] as List).map((i) => {{getCoreType .Type}}.fromJson(i)){{else}}{{.Type}}.fromJson(m['{{tagGet .Tag "json"}}']){{end}},{{end}} + {{lowCamelCase .Name}}: {{if isDirectType .Type.Name}}m['{{tagGet .Tag "json"}}']{{else if isClassListType .Type.Name}}(m['{{tagGet .Tag "json"}}'] as List).map((i) => {{getCoreType .Type.Name}}.fromJson(i)){{else}}{{.Type.Name}}.fromJson(m['{{tagGet .Tag "json"}}']){{end}},{{end}} ); } Map toJson() { return { {{range .Members}} - '{{tagGet .Tag "json"}}': {{if isDirectType .Type}}{{lowCamelCase .Name}}{{else if isClassListType .Type}}{{lowCamelCase .Name}}.map((i) => i.toJson()){{else}}{{lowCamelCase .Name}}.toJson(){{end}},{{end}} + '{{tagGet .Tag "json"}}': {{if isDirectType .Type.Name}}{{lowCamelCase .Name}}{{else if isClassListType .Type.Name}}{{lowCamelCase .Name}}.map((i) => i.toJson()){{else}}{{lowCamelCase .Name}}.toJson(){{end}},{{end}} }; } } @@ -55,6 +55,11 @@ func genData(dir string, api *spec.ApiSpec) error { return err } + err = convertDataType(api) + if err != nil { + return err + } + return t.Execute(file, api) } @@ -73,3 +78,25 @@ func genTokens(dir string) error { _, err = tokensFile.WriteString(tokensFileContent) return err } + +func convertDataType(api *spec.ApiSpec) error { + types := api.Types + if len(types) == 0 { + return nil + } + + for _, ty := range types { + defineStruct, ok := ty.(spec.DefineStruct) + if ok { + for index, member := range defineStruct.Members { + tp, err := specTypeToDart(member.Type) + if err != nil { + return err + } + defineStruct.Members[index].Type = buildSpecType(member.Type, tp) + } + } + } + + return nil +} diff --git a/tools/goctl/api/dartgen/util.go b/tools/goctl/api/dartgen/util.go index 9c83bd6b..961a5f43 100644 --- a/tools/goctl/api/dartgen/util.go +++ b/tools/goctl/api/dartgen/util.go @@ -1,6 +1,8 @@ package dartgen import ( + "errors" + "fmt" "os" "strings" @@ -82,3 +84,91 @@ func fileExists(path string) bool { _, err := os.Stat(path) return !os.IsNotExist(err) } + +func buildSpecType(tp spec.Type, name string) spec.Type { + switch v := tp.(type) { + case spec.PrimitiveType: + return spec.PrimitiveType{RawName: name} + case spec.MapType: + return spec.MapType{RawName: name, Key: v.Key, Value: v.Value} + case spec.ArrayType: + return spec.ArrayType{RawName: name, Value: v.Value} + case spec.InterfaceType: + return spec.InterfaceType{RawName: name} + case spec.PointerType: + return spec.PointerType{RawName: name, Type: v.Type} + } + return tp +} + +func specTypeToDart(tp spec.Type) (string, error) { + switch v := tp.(type) { + case spec.DefineStruct: + return 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 := specTypeToDart(v.Value) + if err != nil { + return "", err + } + + return fmt.Sprintf("Map", valueType), nil + case spec.ArrayType: + if tp.Name() == "[]byte" { + return "List", nil + } + + valueType, err := specTypeToDart(v.Value) + if err != nil { + return "", err + } + + s := getBaseType(valueType) + if len(s) == 0 { + return s, errors.New("unsupported primitive type " + tp.Name()) + } + + return s, nil + case spec.InterfaceType: + return "Object", nil + case spec.PointerType: + return specTypeToDart(v.Type) + } + + return "", errors.New("unsupported primitive type " + tp.Name()) +} + +func getBaseType(valueType string) string { + switch valueType { + case "int": + return "List" + case "double": + return "List" + case "boolean": + return "List" + case "String": + return "List" + default: + return "" + } +} + +func primitiveType(tp string) (string, bool) { + switch tp { + case "string": + return "String", true + case "int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64", "rune": + return "int", true + case "float32", "float64": + return "double", true + case "bool": + return "bool", true + } + + return "", false +}