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/dartgen/gendata.go

150 lines
3.9 KiB
Go

4 years ago
package dartgen
import (
"os"
"strings"
4 years ago
"text/template"
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
4 years ago
)
const dataTemplate = `// --{{with .Info}}{{.Title}}{{end}}--
{{ range .Types}}
class {{.Name}}{
{{range .Members}}
/// {{.Comment}}
final {{if isNumberType .Type.Name}}num{{else}}{{.Type.Name}}{{end}} {{lowCamelCase .Name}};
4 years ago
{{end}}
{{.Name}}({ {{range .Members}}
this.{{lowCamelCase .Name}},{{end}}
});
factory {{.Name}}.fromJson(Map<String,dynamic> m) {
return {{.Name}}({{range .Members}}
{{lowCamelCase .Name}}: {{if isDirectType .Type.Name}}m['{{getPropertyFromMember .}}']{{else if isClassListType .Type.Name}}(m['{{getPropertyFromMember .}}'] as List<dynamic>).map((i) => {{getCoreType .Type.Name}}.fromJson(i)){{else}}{{.Type.Name}}.fromJson(m['{{getPropertyFromMember .}}']){{end}},{{end}}
4 years ago
);
}
Map<String,dynamic> toJson() {
return { {{range .Members}}
'{{getPropertyFromMember .}}': {{if isDirectType .Type.Name}}{{lowCamelCase .Name}}{{else if isClassListType .Type.Name}}{{lowCamelCase .Name}}.map((i) => i.toJson()){{else}}{{lowCamelCase .Name}}.toJson(){{end}},{{end}}
4 years ago
};
}
}
{{end}}
`
const dataTemplateV2 = `// --{{with .Info}}{{.Title}}{{end}}--
{{ range .Types}}
class {{.Name}} {
{{range .Members}}
{{if .Comment}}{{.Comment}}{{end}}
final {{if isNumberType .Type.Name}}num{{else}}{{.Type.Name}}{{end}} {{lowCamelCase .Name}};
{{end}}{{.Name}}({{if .Members}}{
{{range .Members}} required this.{{lowCamelCase .Name}},
{{end}}}{{end}});
factory {{.Name}}.fromJson(Map<String,dynamic> m) {
return {{.Name}}(
{{range .Members}}
{{lowCamelCase .Name}}: {{appendNullCoalescing .}}
{{if isDirectType .Type.Name}}
m['{{getPropertyFromMember .}}'] {{appendDefaultEmptyValue .Type.Name}}
{{else if isClassListType .Type.Name}}
((m['{{getPropertyFromMember .}}'] {{appendDefaultEmptyValue .Type.Name}}) as List<dynamic>).map((i) => {{getCoreType .Type.Name}}.fromJson(i)).toList()
{{else}}
{{.Type.Name}}.fromJson(m['{{getPropertyFromMember .}}']){{end}}
,{{end}}
);
}
Map<String,dynamic> toJson() {
return { {{range .Members}}
'{{getPropertyFromMember .}}':
{{if isDirectType .Type.Name}}
{{lowCamelCase .Name}}
{{else if isClassListType .Type.Name}}
{{lowCamelCase .Name}}{{if isNullableType .Type.Name}}?{{end}}.map((i) => i{{if isListItemsNullable .Type.Name}}?{{end}}.toJson())
{{else}}
{{lowCamelCase .Name}}{{if isNullableType .Type.Name}}?{{end}}.toJson()
{{end}}
,{{end}}
};
}
}
{{end}}`
func genData(dir string, api *spec.ApiSpec, isLegacy bool) error {
err := os.MkdirAll(dir, 0o755)
if err != nil {
return err
4 years ago
}
err = genTokens(dir, isLegacy)
if err != nil {
return err
4 years ago
}
file, err := os.OpenFile(dir+strings.ToLower(api.Service.Name+".dart"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
return err
4 years ago
}
defer file.Close()
t := template.New("dataTemplate")
t = t.Funcs(funcMap)
tpl := dataTemplateV2
if isLegacy {
tpl = dataTemplate
}
t, err = t.Parse(tpl)
if err != nil {
return err
4 years ago
}
err = convertDataType(api)
if err != nil {
return err
}
4 years ago
return t.Execute(file, api)
}
func genTokens(dir string, isLeagcy bool) error {
4 years ago
path := dir + "tokens.dart"
if fileExists(path) {
return nil
}
tokensFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
return err
4 years ago
}
4 years ago
defer tokensFile.Close()
tpl := tokensFileContentV2
if isLeagcy {
tpl = tokensFileContent
}
_, err = tokensFile.WriteString(tpl)
return err
4 years ago
}
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
}