-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.go
85 lines (80 loc) · 2.08 KB
/
json.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
package mongoes
import (
"encoding/json"
"io/ioutil"
"strings"
)
/*ReadJSONFromFile takes a path and pointer to map[string]interface
and return error (if any)
Example of path : ../module/test.json
*/
func ReadJSONFromFile(path string, v *map[string]interface{}) error {
fileBytes, err := ioutil.ReadFile(path)
if err != nil {
return err
}
err = json.Unmarshal(fileBytes, v)
if err != nil {
return err
}
return nil
}
/*GetStringJSON takes a map[string]interface{} and path
and return string
Example of path : mongodb.uri
Path is a string that contains json keys that's splitted by '.'
*/
func GetStringJSON(obj map[string]interface{}, path string) string {
temp := obj
var ret string
// Split the path
splitted := strings.Split(path, ".")
// Iterate over the path
for i, v := range splitted {
if i == len(splitted)-1 {
// on the last path, cast expected Type
ret = temp[v].(string)
} else {
// Still not on the last path, cast to JSON (map[string]interface{})
temp = temp[v].(map[string]interface{})
}
}
return ret
}
/*GetBoolJSON takes a map[string]interface{} and path
and return bool
Example of path : mongodb.uri
Path is a string that contains json keys that's splitted by '.'
*/
func GetBoolJSON(obj map[string]interface{}, path string) bool {
temp := obj
var ret bool
// Split the path
splitted := strings.Split(path, ".")
// Iterate over the path
for i, v := range splitted {
if i == len(splitted)-1 {
// on the last path, cast expected Type
ret = temp[v].(bool)
} else {
// Still not on the last path, cast to JSON (map[string]interface{})
temp = temp[v].(map[string]interface{})
}
}
return ret
}
/*GetObjectJSON takes a map[string]interface{} and path
and return map[string]interface{}
Example of path : mongodb.uri
Path is a string that contains json keys that's splitted by '.'
*/
func GetObjectJSON(obj map[string]interface{}, path string) map[string]interface{} {
ret := obj
// Split the path
splitted := strings.Split(path, ".")
// Iterate over the path
for _, v := range splitted {
ret = ret[v].(map[string]interface{})
}
return ret
}