-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_to_dart.go
115 lines (105 loc) · 2.72 KB
/
class_to_dart.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import "fmt"
func classToDart(c Class) string {
// import class
s := "import 'dart:convert';\n\n"
if c.HasList {
s += "import 'package:collection/collection.dart';\n"
}
for _, v := range c.Import {
s += "import '" + v + ".dart';"
}
// class
s += fmt.Sprintf("class %s {\n", c.Name)
// declaration
for _, v := range c.Properties {
s += " " + fmt.Sprintln(v.Declaration)
}
s += "\n"
// constructor
s += fmt.Sprintf(" %s({\n", c.Name)
for _, v := range c.Properties {
s += " " + v.ConstructorLine + "\n"
}
s += " });\n\n"
// checking null safety
s += fmt.Sprintf(" factory %s.fromMap(Map<String, dynamic> map) {\n", c.Name)
for _, v := range c.Properties {
if v.IsNullable {
continue
}
s += fmt.Sprintf(" if (map['%s'] == null) {\n", v.JsonKey)
s += fmt.Sprintf(" throw '%s is required';\n", v.JsonKey)
s += fmt.Sprintln(" }")
}
s += fmt.Sprintf(" return %s(\n", c.Name)
for _, v := range c.Properties {
s += " " + v.FromMapLine + "\n"
}
s += " );\n }\n\n"
// copyWith
s += fmt.Sprintf(" %s copyWith({\n", c.Name)
for _, v := range c.Properties {
s += " " + v.CopyWithParamLine + "\n"
}
s += fmt.Sprintf(" }) {\n return %s(\n", c.Name)
for _, v := range c.Properties {
s += " " + v.CopyWithPassingLine + "\n"
}
s += " );\n"
s += " }\n\n"
// toMap
s += " Map<String, dynamic> toMap() {\n"
s += " return {\n"
for _, v := range c.Properties {
s += " " + v.ToMapLine + "\n"
}
s += " };\n"
s += " }\n\n"
// toJson
s += " String toJson() => json.encode(toMap());\n\n"
// fromJson
s += fmt.Sprintf(" factory %s.fromJson(String source) =>%s.fromMap(json.decode(source));\n\n", c.Name, c.Name)
// toString
s += " @override\n"
s += " String toString() {\n"
s += fmt.Sprintf(" return '%s(", c.Name)
for i, v := range c.Properties {
if i != len(c.Properties)-1 {
s += fmt.Sprintf(" %s,", v.ToStringLine)
} else {
s += fmt.Sprintf("%s)';\n", v.ToStringLine)
}
}
s += " }\n\n"
// equality
s += " @override"
s += " bool operator ==(Object other) {"
s += " if (identical(this, other)) return true;"
s += "\n"
s += fmt.Sprintf(" return other is %s &&\n", c.Name)
for i, v := range c.Properties {
if i < len(c.Properties)-1 {
s += " " + v.EqualityLine + "&&\n"
} else {
s += " " + v.EqualityLine + ";\n"
}
}
s += " }\n\n"
// hashCode
s += " @override\n"
s += " int get hashCode {\n"
for i, v := range c.Properties {
if i == 0 {
s += " return " + v.HashCodeLine + " ^\n"
} else if i == len(c.Properties)-1 {
s += " " + v.HashCodeLine + ";\n"
} else {
s += " " + v.HashCodeLine + " ^\n"
}
}
s += " }\n\n"
// class
s += "}"
return s
}