-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.go
222 lines (203 loc) · 5.68 KB
/
model.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package structdbpostgres
import (
"reflect"
"sort"
stsql "github.com/go-phings/struct-sql-postgres"
)
// GetObjIDInterface returns an interface{} to ID field of an object
func (c *Controller) GetObjIDInterface(obj interface{}) interface{} {
return reflect.ValueOf(obj).Elem().FieldByName("ID").Addr().Interface()
}
// GetObjIDValue returns value of ID field (int64) of an object
func (c *Controller) GetObjIDValue(obj interface{}) int64 {
return reflect.ValueOf(obj).Elem().FieldByName("ID").Int()
}
// GetObjFieldInterfaces return list of interfaces to object's fields
// Argument includeID tells it to include or omit the ID field
func (c Controller) GetObjFieldInterfaces(obj interface{}, includeID bool) []interface{} {
val := reflect.ValueOf(obj).Elem()
var v []interface{}
for i := 0; i < val.NumField(); i++ {
valueField := val.Field(i)
if val.Type().Field(i).Name == "ID" && !includeID {
continue
}
// struct-sql-postgres is used to generate SQL queries so here the same kinds must be supported
if !stsql.IsFieldKindSupported(valueField.Kind()) {
continue
}
v = append(v, valueField.Addr().Interface())
}
return v
}
// GetFiltersInterfaces returns list of interfaces from filters map (used in querying)
func (c Controller) GetFiltersInterfaces(mf map[string]interface{}) []interface{} {
var xi []interface{}
if len(mf) == 0 {
return xi
}
sorted := []string{}
for k := range mf {
if k == "_raw" || k == "_rawConjuction" {
continue
}
sorted = append(sorted, k)
}
sort.Strings(sorted)
for _, v := range sorted {
xi = append(xi, mf[v])
}
// Get pointers to values from raw query
_, ok := mf["_raw"]
if !ok {
return xi
}
rt := reflect.TypeOf(mf["_raw"])
if rt.Kind() != reflect.Slice && rt.Kind() != reflect.Array {
return xi
}
if reflect.ValueOf(mf["_raw"]).Len() < 2 {
return xi
}
for i := 1; i < reflect.ValueOf(mf["_raw"]).Len(); i++ {
rt2 := reflect.TypeOf(mf["_raw"].([]interface{})[i])
if rt2.Kind() == reflect.Slice || rt2.Kind() == reflect.Array {
valInt8s, ok := mf["_raw"].([]interface{})[i].([]int8)
if ok {
for j := 0; j < len(valInt8s); j++ {
xi = append(xi, valInt8s[j])
}
continue
}
valInt16s, ok := mf["_raw"].([]interface{})[i].([]int16)
if ok {
for j := 0; j < len(valInt16s); j++ {
xi = append(xi, valInt16s[j])
}
continue
}
valInt32s, ok := mf["_raw"].([]interface{})[i].([]int32)
if ok {
for j := 0; j < len(valInt32s); j++ {
xi = append(xi, valInt32s[j])
}
continue
}
valInt64s, ok := mf["_raw"].([]interface{})[i].([]int64)
if ok {
for j := 0; j < len(valInt64s); j++ {
xi = append(xi, valInt64s[j])
}
continue
}
valInts, ok := mf["_raw"].([]interface{})[i].([]int)
if ok {
for j := 0; j < len(valInts); j++ {
xi = append(xi, valInts[j])
}
continue
}
valUint8s, ok := mf["_raw"].([]interface{})[i].([]uint8)
if ok {
for j := 0; j < len(valUint8s); j++ {
xi = append(xi, valUint8s[j])
}
continue
}
valUint16s, ok := mf["_raw"].([]interface{})[i].([]uint16)
if ok {
for j := 0; j < len(valUint16s); j++ {
xi = append(xi, valUint16s[j])
}
continue
}
valUint32s, ok := mf["_raw"].([]interface{})[i].([]uint32)
if ok {
for j := 0; j < len(valUint32s); j++ {
xi = append(xi, valUint32s[j])
}
continue
}
valUint64s, ok := mf["_raw"].([]interface{})[i].([]uint64)
if ok {
for j := 0; j < len(valUint64s); j++ {
xi = append(xi, valUint64s[j])
}
continue
}
valUints, ok := mf["_raw"].([]interface{})[i].([]uint)
if ok {
for j := 0; j < len(valUints); j++ {
xi = append(xi, valUints[j])
}
continue
}
valFloat32s, ok := mf["_raw"].([]interface{})[i].([]float32)
if ok {
for j := 0; j < len(valFloat32s); j++ {
xi = append(xi, valFloat32s[j])
}
continue
}
valFloat64s, ok := mf["_raw"].([]interface{})[i].([]float64)
if ok {
for j := 0; j < len(valFloat64s); j++ {
xi = append(xi, valFloat64s[j])
}
continue
}
valBools, ok := mf["_raw"].([]interface{})[i].([]bool)
if ok {
for j := 0; j < len(valBools); j++ {
xi = append(xi, valBools[j])
}
continue
}
valStrings, ok := mf["_raw"].([]interface{})[i].([]string)
if ok {
for j := 0; j < len(valStrings); j++ {
xi = append(xi, valStrings[j])
}
}
} else {
xi = append(xi, mf["_raw"].([]interface{})[i])
}
}
return xi
}
// ResetFields zeroes object's field values
func (c Controller) ResetFields(obj interface{}) {
val := reflect.ValueOf(obj).Elem()
for i := 0; i < val.NumField(); i++ {
f := val.Field(i)
k := f.Kind()
if k == reflect.Ptr {
f.Set(reflect.Zero(f.Type()))
}
if k == reflect.Int || k == reflect.Int8 || k == reflect.Int16 || k == reflect.Int32 || k == reflect.Int64 {
f.SetInt(0)
}
if k == reflect.Uint || k == reflect.Uint8 || k == reflect.Uint16 || k == reflect.Uint32 || k == reflect.Uint64 {
f.SetInt(0)
}
if k == reflect.Float32 || k == reflect.Float64 {
f.SetFloat(0.0)
}
if k == reflect.String {
f.SetString("")
}
if k == reflect.Bool {
f.SetBool(false)
}
}
}
// SetObjCreated sets object's CreatedAt and CreatedBy fields
func (c *Controller) SetObjCreated(obj interface{}, at int64, by int64) {
reflect.ValueOf(obj).Elem().FieldByName("CreatedAt").SetInt(at)
reflect.ValueOf(obj).Elem().FieldByName("CreatedBy").SetInt(by)
}
// SetObjLastModified sets object's LastModifiedAt and LastModifiedBy fields
func (c *Controller) SetObjLastModified(obj interface{}, at int64, by int64) {
reflect.ValueOf(obj).Elem().FieldByName("LastModifiedAt").SetInt(at)
reflect.ValueOf(obj).Elem().FieldByName("LastModifiedBy").SetInt(by)
}