diff --git a/tools/goctl/api/dartgen/gendata.go b/tools/goctl/api/dartgen/gendata.go index 49c5f0a6..90d97ab2 100644 --- a/tools/goctl/api/dartgen/gendata.go +++ b/tools/goctl/api/dartgen/gendata.go @@ -43,7 +43,9 @@ class {{.Name}} { {{end}}}{{end}}); factory {{.Name}}.fromJson(Map m) { return {{.Name}}({{range .Members}} - {{lowCamelCase .Name}}: {{if isDirectType .Type.Name}}m['{{getPropertyFromMember .}}']{{else if isClassListType .Type.Name}}(m['{{getPropertyFromMember .}}'] as List).map((i) => {{getCoreType .Type.Name}}.fromJson(i)){{else}}{{.Type.Name}}.fromJson(m['{{getPropertyFromMember .}}']){{end}},{{end}} + {{lowCamelCase .Name}}: {{if isDirectType .Type.Name}}m['{{getPropertyFromMember .}}'] + {{else if isClassListType .Type.Name}}(m['{{getPropertyFromMember .}}'] as List).map((i) => {{getCoreType .Type.Name}}.fromJson(i)).toList() + {{else}}{{.Type.Name}}.fromJson(m['{{getPropertyFromMember .}}']){{end}},{{end}} ); } Map toJson() { diff --git a/tools/goctl/api/dartgen/util.go b/tools/goctl/api/dartgen/util.go index 0cccf36d..b6a52f15 100644 --- a/tools/goctl/api/dartgen/util.go +++ b/tools/goctl/api/dartgen/util.go @@ -128,11 +128,10 @@ func specTypeToDart(tp spec.Type) (string, error) { } s := getBaseType(valueType) - if len(s) == 0 { - return s, errors.New("unsupported primitive type " + tp.Name()) + if len(s) != 0 { + return s, nil } - - return s, nil + return fmt.Sprintf("List<%s>", valueType), nil case spec.InterfaceType: return "Object", nil case spec.PointerType: diff --git a/tools/goctl/api/dartgen/util_test.go b/tools/goctl/api/dartgen/util_test.go index 68a221fd..437b1960 100644 --- a/tools/goctl/api/dartgen/util_test.go +++ b/tools/goctl/api/dartgen/util_test.go @@ -37,3 +37,35 @@ func Test_getPropertyFromMember(t *testing.T) { }) } } + +func Test_specTypeToDart(t *testing.T) { + tests := []struct { + name string + specType spec.Type + want string + wantErr bool + }{ + { + name: "[]string should return List", + specType: spec.ArrayType{RawName: "[]string", Value: spec.PrimitiveType{RawName: "string"}}, + want: "List", + }, + { + name: "[]Foo should return List", + specType: spec.ArrayType{RawName: "[]Foo", Value: spec.DefineStruct{RawName: "Foo"}}, + want: "List", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := specTypeToDart(tt.specType) + if (err != nil) != tt.wantErr { + t.Errorf("specTypeToDart() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("specTypeToDart() = %v, want %v", got, tt.want) + } + }) + } +}