|
|
|
@ -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<String,dynamic> m) {
|
|
|
|
|
return {{.Name}}({{range .Members}}
|
|
|
|
|
{{lowCamelCase .Name}}: {{if isDirectType .Type}}m['{{tagGet .Tag "json"}}']{{else if isClassListType .Type}}(m['{{tagGet .Tag "json"}}'] as List<dynamic>).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<dynamic>).map((i) => {{getCoreType .Type.Name}}.fromJson(i)){{else}}{{.Type.Name}}.fromJson(m['{{tagGet .Tag "json"}}']){{end}},{{end}}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
Map<String,dynamic> 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
|
|
|
|
|
}
|
|
|
|
|