-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparams_parser.go
More file actions
64 lines (57 loc) · 1.59 KB
/
params_parser.go
File metadata and controls
64 lines (57 loc) · 1.59 KB
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
package resque_status
import (
"encoding/json"
"fmt"
"strconv"
)
func ParseInt(params map[string]interface{}, name string) (int, error) {
jsonNumber := params[name]
intValue, err := jsonNumber.(json.Number).Int64()
if err != nil {
return 0, err
}
return int(intValue), nil
}
func ParseIntArray(params map[string]interface{}, name string) ([]int, error) {
jsonNumbers := params[name].([]interface{})
intNumbers := []int{}
for _, jsonNumber := range jsonNumbers {
intValue, err := jsonNumber.(json.Number).Int64()
if err != nil {
return nil, err
}
intNumbers = append(intNumbers, int(intValue))
}
return intNumbers, nil
}
func ParseJsonParam(params map[string]interface{}, name string) ([]map[string]string, error) {
parsedJson := []map[string]string{}
parsedInterfaces := []map[string]interface{}{}
err := json.Unmarshal([]byte(params[name].(string)), &parsedInterfaces)
if err != nil {
return nil, err
}
for _, productData := range parsedInterfaces {
productDataMap := map[string]string{}
for productAttr, productAttrVal := range productData {
switch v := productAttrVal.(type) {
case int:
productDataMap[productAttr] = strconv.FormatInt(int64(v), 10)
case float64:
productDataMap[productAttr] = strconv.FormatFloat(v, 'f', -1, 64)
case string:
productDataMap[productAttr] = v
case bool:
if v {
productDataMap[productAttr] = "true"
} else {
productDataMap[productAttr] = "false"
}
default:
productDataMap[productAttr] = fmt.Sprintf("%v", v)
}
}
parsedJson = append(parsedJson, productDataMap)
}
return parsedJson, nil
}