-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
192 lines (171 loc) · 4.94 KB
/
helper.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"fmt"
"strings"
"github.com/gobeam/stringy"
)
func toVarDartType(dt string) string {
switch dt {
case "bigint":
return "int"
case "char":
return "String"
case "date":
return "DateTime"
case "datetime":
return "DateTime"
case "double":
return "double"
case "int":
return "int"
case "longtext":
return "String"
case "mediumtext":
return "String"
case "text":
return "String"
case "timestamp":
return "DateTime"
case "tinyint":
return "int"
case "varchar":
return "String"
default:
s := stringy.New(dt)
return s.CamelCase()
}
}
func declaration(isnull, isList bool, _type, varName string) string {
if isList {
if isnull {
return fmt.Sprintf("final List<%s>? %s;", _type, varName)
} else {
return fmt.Sprintf("final List<%s> %s;", _type, varName)
}
}
if isnull {
return fmt.Sprintf("final %s? %s;", _type, varName)
} else {
return fmt.Sprintf("final %s %s;", _type, varName)
}
}
func constructorLine(isnull bool, varName string) string {
if isnull {
return fmt.Sprintf("this.%s,", varName)
} else {
return fmt.Sprintf("required this.%s,", varName)
}
}
func formMapLine(varName, jsonKey, varType string, isNull, isList bool) string {
if !isList {
if isNull {
switch varType {
case "int":
return fmt.Sprintf("%s: map['%s'],", varName, jsonKey)
case "double":
return fmt.Sprintf("%s: map['%s']?.toDouble(),", varName, jsonKey)
case "String":
return fmt.Sprintf("%s: map['%s'],", varName, jsonKey)
case "DateTime":
return fmt.Sprintf("%s: map['%s'] == null ? null : DateTime.parse(map['%s']),", varName, jsonKey, jsonKey)
default:
return fmt.Sprintf("%s: %s.fromMap(map['%s'])", varName, varType, jsonKey)
}
} else {
switch varType {
case "int":
return fmt.Sprintf("%s: map['%s'],", varName, jsonKey)
case "double":
return fmt.Sprintf("%s: map['%s'].toDouble(),", varName, jsonKey)
case "String":
return fmt.Sprintf("%s: map['%s'],", varName, jsonKey)
case "DateTime":
return fmt.Sprintf("%s: DateTime.parse(map['%s']),", varName, jsonKey)
default:
return fmt.Sprintf("%s: %s.fromMap(map['%s'])", varName, varType, jsonKey)
}
}
}
switch varType {
case "int", "double", "String", "DateTime":
panic("unimplemented")
default:
return fmt.Sprintf("%s: map['%s'] == null ? null : List<%s>.from(map['%s']?.map((x) => %s.fromMap(x))),", varName, jsonKey, varType, jsonKey, varType)
}
//
}
func toMapLine(varType, jsonKey, varName string, isnull, isList bool) string {
if isList {
switch varType {
case "int", "double", "String", "DateTime":
panic("unimplemented")
default:
return fmt.Sprintf("'%s': %s?.map((x) => x.toMap()).toList(),", jsonKey, varName)
}
}
switch varType {
case "int", "double", "String":
return fmt.Sprintf("'%s': %s,", jsonKey, varName)
case "DateTime":
if isnull {
return fmt.Sprintf("'%s': %s?.toIso8601String(),", jsonKey, varName)
} else {
return fmt.Sprintf("'%s': %s.toIso8601String(),", jsonKey, varName)
}
default:
if isnull {
return fmt.Sprintf("'%s': %s?.toMap(),", jsonKey, varName)
} else {
return fmt.Sprintf("'%s': %s.toMap(),", jsonKey, varName)
}
}
}
func convertColumnToProperties(dbCol Column) ClassProperties {
var cIn ClassProperties
// membuat varName
str2 := stringy.New(dbCol.ColumnName)
str2 = stringy.New(str2.CamelCase())
cIn.VarName = str2.LcFirst()
cIn.VarName = strings.ReplaceAll(cIn.VarName, "Id", "ID")
if dbCol.IsList {
cIn.VarName = plur.Plural(cIn.VarName)
} else {
cIn.VarName = plur.Singular(cIn.VarName)
}
// Membuat JSONKey
cIn.JsonKey = cIn.VarName
// membuat isnullable
cIn.IsNullable = true
if dbCol.IsNullable == "NO" {
cIn.IsNullable = false
}
// konversi varType
cIn.VarType = toVarDartType(dbCol.DataType)
// Declaration
cIn.Declaration = declaration(cIn.IsNullable, dbCol.IsList, cIn.VarType, cIn.VarName)
// Constructor Line
cIn.ConstructorLine = constructorLine(cIn.IsNullable, cIn.VarName)
// From Map line
cIn.FromMapLine = formMapLine(cIn.VarName, cIn.JsonKey, cIn.VarType, cIn.IsNullable, dbCol.IsList)
// copyWith
if dbCol.IsList {
cIn.CopyWithParamLine = fmt.Sprintf("List<%s>? %s,", cIn.VarType, cIn.VarName)
cIn.CopyWithPassingLine = fmt.Sprintf("%s: %s ?? this.%s,", cIn.VarName, cIn.VarName, cIn.VarName)
} else {
cIn.CopyWithParamLine = fmt.Sprintf("%s? %s,", cIn.VarType, cIn.VarName)
cIn.CopyWithPassingLine = fmt.Sprintf("%s: %s ?? this.%s,", cIn.VarName, cIn.VarName, cIn.VarName)
}
// toMap
cIn.ToMapLine = toMapLine(cIn.VarType, cIn.JsonKey, cIn.VarName, cIn.IsNullable, dbCol.IsList)
// toString
cIn.ToStringLine = fmt.Sprintf("%s: $%s", cIn.VarName, cIn.VarName)
// equality
if dbCol.IsList {
cIn.EqualityLine = fmt.Sprintf("DeepCollectionEquality().equals(other.%s, %s)", cIn.VarName, cIn.VarName)
} else {
cIn.EqualityLine = fmt.Sprintf("other.%s == %s", cIn.VarName, cIn.VarName)
}
// hasCode
cIn.HashCodeLine = fmt.Sprintf("%s.hashCode", cIn.VarName)
return cIn
}