-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_util.go
310 lines (301 loc) · 8.83 KB
/
handler_util.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package search
import (
"encoding/json"
"errors"
"log"
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
"time"
)
func BuildResourceName(s string) string {
s2 := strings.ToLower(s)
s3 := ""
for i := range s {
if s2[i] != s[i] {
s3 += "-" + string(s2[i])
} else {
s3 += string(s2[i])
}
}
if string(s3[0]) == "-" || string(s3[0]) == "_" {
return s3[1:]
}
return s3
}
func UrlToModel(filter interface{}, params url.Values, paramIndex map[string]int, options ...int) interface{} {
value := reflect.Indirect(reflect.ValueOf(filter))
if value.Kind() == reflect.Ptr {
value = reflect.Indirect(value)
}
//TimeRange
timeType := reflect.TypeOf(TimeRange{})
tRange := reflect.New(timeType)
psTime := tRange.Elem()
//Int64Range
int64Type := reflect.TypeOf(Int64Range{})
i64 := reflect.New(int64Type)
psI64 := i64.Elem()
//NumRange
nRangeType := reflect.TypeOf(NumberRange{})
nRange := reflect.New(nRangeType)
psNRange := nRange.Elem()
for paramKey, valueArr := range params {
paramValue := ""
if len(valueArr) > 0 {
paramValue = valueArr[0]
}
if field, err := FindField(value, paramKey, paramIndex, options...); err == nil {
kind := field.Kind()
var v interface{}
// Need handle more case of kind
if kind == reflect.String {
v = paramValue
} else if kind == reflect.Int {
v, _ = strconv.Atoi(paramValue)
} else if kind == reflect.Int64 {
v, _ = strconv.ParseInt(paramValue, 10, 64)
} else if kind == reflect.Float64 {
v, _ = strconv.ParseFloat(paramValue, 64)
} else if kind == reflect.Bool {
v, _ = strconv.ParseBool(paramValue)
} else if kind == reflect.Slice {
sliceKind := reflect.TypeOf(field.Interface()).Elem().Kind()
if sliceKind == reflect.String {
v = strings.Split(paramValue, ",")
} else {
log.Println("Unhandled slice kind:", kind)
continue
}
} else if kind == reflect.Struct {
newModel := reflect.New(reflect.Indirect(field).Type()).Interface()
if errDecode := json.Unmarshal([]byte(paramValue), newModel); errDecode != nil {
panic(errDecode)
}
v = newModel
} else if kind == reflect.Ptr {
ptrKind := field.Interface()
switch ptrKind.(type) {
case *string:
field.Set(reflect.ValueOf(¶mValue))
continue
case *int:
iv, er := strconv.Atoi(paramValue)
if er == nil {
field.Set(reflect.ValueOf(&iv))
}
continue
case *int64:
iv, er := strconv.ParseInt(paramValue, 10, 64)
if er == nil {
field.Set(reflect.ValueOf(&iv))
}
continue
case *float64:
iv, er := strconv.ParseFloat(paramValue, 64)
if er == nil {
field.Set(reflect.ValueOf(&iv))
}
continue
case *bool:
iv, er := strconv.ParseBool(paramValue)
if er == nil {
field.Set(reflect.ValueOf(&iv))
}
continue
case *TimeRange:
keys := strings.Split(paramKey, ".")
f := psTime.FieldByName(strings.Title(keys[1]))
tValue, _ := time.Parse(time.RFC3339, paramValue)
f.Set(reflect.ValueOf(&tValue))
field.Set(reflect.ValueOf(tRange.Interface()))
continue
case *Int64Range:
keys := strings.Split(paramKey, ".")
f := psI64.FieldByName(strings.Title(keys[1]))
i64Value, _ := strconv.ParseInt(paramValue, 10, 64)
f.Set(reflect.ValueOf(&i64Value))
field.Set(reflect.ValueOf(i64.Interface()))
continue
case *NumberRange:
keys := strings.Split(paramKey, ".")
f := psNRange.FieldByName(strings.Title(keys[1]))
nValue, _ := strconv.ParseFloat(paramValue, 64)
f.Set(reflect.ValueOf(&nValue))
field.Set(reflect.ValueOf(nRange.Interface()))
continue
}
} else {
log.Println("Unhandled kind:", kind)
continue
}
field.Set(reflect.Indirect(reflect.ValueOf(v)))
} else {
log.Println(err)
}
}
return filter
}
func FindField(value reflect.Value, paramKey string, paramIndex map[string]int, options ...int) (reflect.Value, error) {
if keys := strings.Split(paramKey, "."); len(keys) > 0 {
paramKey = keys[0]
}
if index, ok := paramIndex[paramKey]; ok {
return value.Field(index), nil
}
filterIndex := -1
if len(options) > 0 && options[0] >= 0 {
filterIndex = options[0]
}
if filterIndex >= 0 {
filterParamIndex := GetFilterParamIndex()
if index, ok := filterParamIndex[paramKey]; ok {
filterField := value.Field(filterIndex)
if filterField.Kind() == reflect.Ptr {
filterField = reflect.Indirect(filterField)
}
return filterField.Field(index), nil
}
}
return value, errors.New("can't find field " + paramKey)
}
func BuildParamIndex(filterType reflect.Type) map[string]int {
params := map[string]int{}
numField := filterType.NumField()
for i := 0; i < numField; i++ {
field := filterType.Field(i)
fullJsonTag := field.Tag.Get("json")
tagDetails := strings.Split(fullJsonTag, ",")
if len(tagDetails) > 0 && len(tagDetails[0]) > 0 {
params[tagDetails[0]] = i
}
}
return params
}
type Parameters struct {
ParamIndex map[string]int
FilterIndex int
CSVIndex map[string]int
}
func CreateParameters(filterType reflect.Type, modelType reflect.Type) *Parameters {
paramIndex, filterIndex, firstLayerIndexes, _ := CreateAttributes(filterType, modelType)
return &Parameters{ParamIndex: paramIndex, FilterIndex: filterIndex, CSVIndex: firstLayerIndexes}
}
func CreateAttributes(filterType reflect.Type, modelType reflect.Type, opts ...string) (map[string]int, int, map[string]int, map[string]int) {
embedField := ""
if len(opts) > 0 {
embedField = opts[0]
}
paramIndex := BuildParamIndex(filterType)
filterIndex := FindFilterIndex(filterType)
model := reflect.New(modelType).Interface()
fields := GetJSONFields(modelType)
firstLayerIndexes, secondLayerIndexes := BuildJsonMap(model, fields, embedField)
return paramIndex, filterIndex, firstLayerIndexes, secondLayerIndexes
}
func BuildAttributes(filterType reflect.Type) (map[string]int, int) {
paramIndex := BuildParamIndex(filterType)
filterIndex := FindFilterIndex(filterType)
return paramIndex, filterIndex
}
func BuildFilter(r *http.Request, filterType reflect.Type, paramIndex map[string]int, userIdName string, options ...int) (interface{}, int, error) {
var filter = CreateFilter(filterType, options...)
method := r.Method
x := 1
if method == http.MethodGet {
ps := r.URL.Query()
fs := ps.Get("fields")
if len(fs) == 0 {
x = -1
}
UrlToModel(filter, ps, paramIndex, options...)
} else if method == http.MethodPost {
if err := json.NewDecoder(r.Body).Decode(&filter); err != nil {
return nil, x, err
}
}
userId := ""
u := r.Context().Value(userIdName)
if u != nil {
u2, ok2 := u.(string)
if ok2 {
userId = u2
}
}
SetUserId(filter, userId)
return filter, x, nil
}
func GetUser(r *http.Request, opt ...string) string {
user := "userId"
if len(opt) > 0 && len(opt[0]) > 0 {
user = opt[0]
}
u := r.Context().Value(user)
if u != nil {
u2, ok2 := u.(string)
if ok2 {
return u2
}
}
return ""
}
func Decode(r *http.Request, filter interface{}, paramIndex map[string]int, options ...int) error {
method := r.Method
if method == http.MethodGet {
ps := r.URL.Query()
UrlToModel(filter, ps, paramIndex, options...)
return nil
} else {
err := json.NewDecoder(r.Body).Decode(&filter)
return err
}
}
func ToFilter(w http.ResponseWriter, r *http.Request, filter interface{}, paramIndex map[string]int, options ...int) error {
err := Decode(r, &filter, paramIndex, options...)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
return err
}
func DecodeAndCheck(w http.ResponseWriter, r *http.Request, filter interface{}, paramIndex map[string]int, options ...int) error {
return ToFilter(w, r, filter, paramIndex, options...)
}
func ResultToCsv(fields []string, models interface{}, count int64, embedField string, opts ...map[string]int) (string, bool) {
if len(fields) > 0 {
result1 := ToCsv(fields, models, count, embedField, opts...)
return result1, true
} else {
return "", false
}
}
func ResultCsv(fields []string, models interface{}, count int64, opts ...map[string]int) (string, bool) {
if len(fields) > 0 {
result1 := ToCsv(fields, models, count, "", opts...)
return result1, true
} else {
return "", false
}
}
func ResultToNextCsv(fields []string, models interface{}, nextPageToken string, embedField string, opts ...map[string]int) (string, bool) {
if len(fields) > 0 {
result1 := ToNextCsv(fields, models, nextPageToken, embedField, opts...)
return result1, true
} else {
return "", false
}
}
func ResultNextCsv(fields []string, models interface{}, nextPageToken string, opts ...map[string]int) (string, bool) {
if len(fields) > 0 {
result1 := ToNextCsv(fields, models, nextPageToken, "", opts...)
return result1, true
} else {
return "", false
}
}
func CSV(w http.ResponseWriter, code int, out string) (int, error) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
return w.Write([]byte(out))
}