forked from thousandeyes/thousandeyes-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
83 lines (66 loc) · 2.25 KB
/
utils.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
package thousandeyes
import (
"encoding/json"
"reflect"
"strings"
)
// Bool is a helper routine that allocates a new bool value
// to store v and returns a pointer to it.
func Bool(v bool) *bool { return &v }
// Int is a helper routine that allocates a new int value
// to store v and returns a pointer to it.
func Int(v int) *int { return &v }
// Int64 is a helper routine that allocates a new int64 value
// to store v and returns a pointer to it.
func Int64(v int64) *int64 { return &v }
// String is a helper routine that allocates a new string value
// to store v and returns a pointer to it.
func String(v string) *string { return &v }
// booleanFieldsFromStruct receives a struct pointer and returns the
// JSON keys for fields tagged with "te:int-bool".
func booleanFieldsFromStruct(structPtr interface{}) map[string]bool {
booleanFields := map[string]bool{}
v := reflect.ValueOf(structPtr).Elem()
t := reflect.TypeOf(v.Interface())
for i := 0; i < t.NumField(); i++ {
if tag, ok := t.Field(i).Tag.Lookup("te"); ok && tag == "int-bool" {
jsonKey := strings.Split(t.Field(i).Tag.Get("json"), ",")[0]
booleanFields[jsonKey] = true
}
}
return booleanFields
}
// jsonBoolToInt is a helper routine that transforms ThousandEyes
// boolean fields to an int value. It is used by JSON marshalers.
func jsonBoolToInt(structPtr interface{}, data []byte) ([]byte, error) {
booleanFields := booleanFieldsFromStruct(structPtr)
var jsonMap map[string]interface{}
if err := json.Unmarshal(data, &jsonMap); err != nil {
return nil, err
}
for jsonKey, jsonValue := range jsonMap {
if booleanFields[jsonKey] {
if jsonValue.(bool) {
jsonMap[jsonKey] = 1
} else {
jsonMap[jsonKey] = 0
}
}
}
return json.Marshal(jsonMap)
}
// jsonIntToBool is a helper routine that transforms ThousandEyes int
// fields to a boolean value. It is used by JSON unmarshalers.
func jsonIntToBool(structPtr interface{}, data []byte) ([]byte, error) {
booleanFields := booleanFieldsFromStruct(structPtr)
var jsonMap map[string]interface{}
if err := json.Unmarshal(data, &jsonMap); err != nil {
return nil, err
}
for jsonKey, jsonValue := range jsonMap {
if booleanFields[jsonKey] {
jsonMap[jsonKey] = jsonValue.(float64) == 1
}
}
return json.Marshal(jsonMap)
}