Debian Codegraph @

util/encoding.go

raw symbols SCIP
1 package util
2
3 import (
4 "encoding/json"
5 "fmt"
6 "log"
7 "net/url"
8 "reflect"
9 "strconv"
10 "strings"
11 "time"
12 )
13
14 // change instance=["a", "b"]
15 // to instance.1="a" instance.2="b"
16 func FlattenFn(fieldName string, field reflect.Value, values *url.Values) {
17 l := field.Len()
18 if l > 0 {
19 for i := 0; i < l; i++ {
20 str := field.Index(i).String()
21 values.Set(fieldName+"."+strconv.Itoa(i+1), str)
22 }
23 }
24 }
25
26 func Underline2Dot(name string) string {
27 return strings.Replace(name, "_", ".", -1)
28 }
29
30 //ConvertToQueryValues converts the struct to url.Values
31 func ConvertToQueryValues(ifc interface{}) url.Values {
32 values := url.Values{}
33 SetQueryValues(ifc, &values)
34 return values
35 }
36
37 //SetQueryValues sets the struct to existing url.Values following ECS encoding rules
38 func SetQueryValues(ifc interface{}, values *url.Values) {
39 setQueryValues(ifc, values, "")
40 }
41
42 func SetQueryValueByFlattenMethod(ifc interface{}, values *url.Values) {
43 setQueryValuesByFlattenMethod(ifc, values, "")
44 }
45
46 func setQueryValues(i interface{}, values *url.Values, prefix string) {
47 // add to support url.Values
48 mapValues, ok := i.(url.Values)
49 if ok {
50 for k, _ := range mapValues {
51 values.Set(k, mapValues.Get(k))
52 }
53 return
54 }
55
56 elem := reflect.ValueOf(i)
57 if elem.Kind() == reflect.Ptr {
58 elem = elem.Elem()
59 }
60 elemType := elem.Type()
61 for i := 0; i < elem.NumField(); i++ {
62
63 fieldName := elemType.Field(i).Name
64 anonymous := elemType.Field(i).Anonymous
65 tag := elemType.Field(i).Tag.Get("query")
66 argName := elemType.Field(i).Tag.Get("ArgName")
67 field := elem.Field(i)
68 // TODO Use Tag for validation
69 // tag := typ.Field(i).Tag.Get("tagname")
70 kind := field.Kind()
71 isPtr := false
72 if (kind == reflect.Ptr || kind == reflect.Array || kind == reflect.Slice || kind == reflect.Map || kind == reflect.Chan) && field.IsNil() {
73 continue
74 }
75 if kind == reflect.Ptr {
76 field = field.Elem()
77 kind = field.Kind()
78 isPtr = true
79 }
80 var value string
81 //switch field.Interface().(type) {
82 switch kind {
83 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
84 i := field.Int()
85 if i != 0 || isPtr {
86 value = strconv.FormatInt(i, 10)
87 }
88 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
89 i := field.Uint()
90 if i != 0 || isPtr {
91 value = strconv.FormatUint(i, 10)
92 }
93 case reflect.Float32:
94 value = strconv.FormatFloat(field.Float(), 'f', 4, 32)
95 case reflect.Float64:
96 value = strconv.FormatFloat(field.Float(), 'f', 4, 64)
97 case reflect.Bool:
98 value = strconv.FormatBool(field.Bool())
99 case reflect.String:
100 value = field.String()
101 case reflect.Map:
102 ifc := field.Interface()
103 m := ifc.(map[string]string)
104 if m != nil {
105 j := 0
106 for k, v := range m {
107 j++
108 keyName := fmt.Sprintf("%s.%d.Key", fieldName, j)
109 values.Set(keyName, k)
110 valueName := fmt.Sprintf("%s.%d.Value", fieldName, j)
111 values.Set(valueName, v)
112 }
113 }
114 case reflect.Slice:
115 switch field.Type().Elem().Kind() {
116 case reflect.Uint8:
117 value = string(field.Bytes())
118 case reflect.String:
119 l := field.Len()
120 if l > 0 {
121 if tag == "list" {
122 name := argName
123 if argName == "" {
124 name = fieldName
125 }
126 for i := 0; i < l; i++ {
127 valueName := fmt.Sprintf("%s.%d", name, (i + 1))
128 values.Set(valueName, field.Index(i).String())
129 }
130 } else {
131 strArray := make([]string, l)
132 for i := 0; i < l; i++ {
133 strArray[i] = field.Index(i).String()
134 }
135 bytes, err := json.Marshal(strArray)
136 if err == nil {
137 value = string(bytes)
138 } else {
139 log.Printf("Failed to convert JSON: %v", err)
140 }
141 }
142 }
143 default:
144 l := field.Len()
145 for j := 0; j < l; j++ {
146 prefixName := fmt.Sprintf("%s.%d.", fieldName, (j + 1))
147 ifc := field.Index(j).Interface()
148 //log.Printf("%s : %v", prefixName, ifc)
149 if ifc != nil {
150 setQueryValues(ifc, values, prefixName)
151 }
152 }
153 continue
154 }
155
156 default:
157 switch field.Interface().(type) {
158 case ISO6801Time:
159 t := field.Interface().(ISO6801Time)
160 value = t.String()
161 case time.Time:
162 t := field.Interface().(time.Time)
163 value = GetISO8601TimeStamp(t)
164 default:
165 ifc := field.Interface()
166 if ifc != nil {
167 if anonymous {
168 SetQueryValues(ifc, values)
169 } else {
170 prefixName := fieldName + "."
171 setQueryValues(ifc, values, prefixName)
172 }
173 continue
174 }
175 }
176 }
177 if value != "" {
178 name := argName
179 if argName == "" {
180 name = fieldName
181 }
182 if prefix != "" {
183 name = prefix + name
184 }
185 values.Set(name, value)
186 }
187 }
188 }
189
190 func setQueryValuesByFlattenMethod(i interface{}, values *url.Values, prefix string) {
191 // add to support url.Values
192 mapValues, ok := i.(url.Values)
193 if ok {
194 for k, _ := range mapValues {
195 values.Set(k, mapValues.Get(k))
196 }
197 return
198 }
199
200 elem := reflect.ValueOf(i)
201 if elem.Kind() == reflect.Ptr {
202 elem = elem.Elem()
203 }
204 elemType := elem.Type()
205 for i := 0; i < elem.NumField(); i++ {
206
207 fieldName := elemType.Field(i).Name
208 anonymous := elemType.Field(i).Anonymous
209 field := elem.Field(i)
210
211 // TODO Use Tag for validation
212 // tag := typ.Field(i).Tag.Get("tagname")
213 kind := field.Kind()
214
215 isPtr := false
216 if (kind == reflect.Ptr || kind == reflect.Array || kind == reflect.Slice || kind == reflect.Map || kind == reflect.Chan) && field.IsNil() {
217 continue
218 }
219 if kind == reflect.Ptr {
220 field = field.Elem()
221 kind = field.Kind()
222 isPtr = true
223 }
224
225 var value string
226 //switch field.Interface().(type) {
227 switch kind {
228 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
229 i := field.Int()
230 if i != 0 || isPtr {
231 value = strconv.FormatInt(i, 10)
232 }
233 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
234 i := field.Uint()
235 if i != 0 || isPtr {
236 value = strconv.FormatUint(i, 10)
237 }
238 case reflect.Float32:
239 value = strconv.FormatFloat(field.Float(), 'f', 4, 32)
240 case reflect.Float64:
241 value = strconv.FormatFloat(field.Float(), 'f', 4, 64)
242 case reflect.Bool:
243 value = strconv.FormatBool(field.Bool())
244 case reflect.String:
245 value = field.String()
246 case reflect.Map:
247 ifc := field.Interface()
248 m := ifc.(map[string]string)
249 if m != nil {
250 j := 0
251 for k, v := range m {
252 j++
253 keyName := fmt.Sprintf("%s.%d.Key", fieldName, j)
254 values.Set(keyName, k)
255 valueName := fmt.Sprintf("%s.%d.Value", fieldName, j)
256 values.Set(valueName, v)
257 }
258 }
259 case reflect.Slice:
260 if field.Type().Name() == "FlattenArray" {
261 FlattenFn(fieldName, field, values)
262 } else {
263 switch field.Type().Elem().Kind() {
264 case reflect.Uint8:
265 value = string(field.Bytes())
266 case reflect.String:
267 l := field.Len()
268 if l > 0 {
269 strArray := make([]string, l)
270 for i := 0; i < l; i++ {
271 strArray[i] = field.Index(i).String()
272 }
273 bytes, err := json.Marshal(strArray)
274 if err == nil {
275 value = string(bytes)
276 } else {
277 log.Printf("Failed to convert JSON: %v", err)
278 }
279 }
280 default:
281 l := field.Len()
282 for j := 0; j < l; j++ {
283 prefixName := fmt.Sprintf("%s.%d.", fieldName, (j + 1))
284 ifc := field.Index(j).Interface()
285 //log.Printf("%s : %v", prefixName, ifc)
286 if ifc != nil {
287 setQueryValuesByFlattenMethod(ifc, values, prefixName)
288 }
289 }
290 continue
291 }
292 }
293
294 default:
295 switch field.Interface().(type) {
296 case ISO6801Time:
297 t := field.Interface().(ISO6801Time)
298 value = t.String()
299 case time.Time:
300 t := field.Interface().(time.Time)
301 value = GetISO8601TimeStamp(t)
302 default:
303
304 ifc := field.Interface()
305 if ifc != nil {
306 if anonymous {
307 SetQueryValues(ifc, values)
308 } else {
309 prefixName := fieldName + "."
310 setQueryValuesByFlattenMethod(ifc, values, prefixName)
311 }
312 continue
313 }
314 }
315 }
316 if value != "" {
317 name := elemType.Field(i).Tag.Get("ArgName")
318 if name == "" {
319 name = fieldName
320 }
321 if prefix != "" {
322 name = prefix + name
323 }
324 // NOTE: here we will change name to underline style when the type is UnderlineString
325 if field.Type().Name() == "UnderlineString" {
326 name = Underline2Dot(name)
327 }
328 values.Set(name, value)
329 }
330 }
331 }