-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.go
49 lines (41 loc) · 1.09 KB
/
helper.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
package gochill
import json "github.com/bitly/go-simplejson"
//CombineMaps used to merge list of maps into one single map
//contain of key string and value interface
func CombineMaps(maps ...map[string]interface{}) map[string]interface{} {
var m map[string]interface{}
m = make(map[string]interface{})
if len(maps) > 0 {
for _, mapped := range maps {
if len(mapped) > 0 {
for k, v := range mapped {
m[k] = v
}
}
}
}
return m
}
//MapToJSON used to convert map to json byte
func MapToJSON(maps map[string]interface{}) []byte {
/*
I'm not using native golang json encoding because
it's very hard to convert map to json because native
golang use struct to marshal and unmarshal data, we
doesn't need any struct here.
I'm using third party library here from bitly/go-simplejson
it's easier to convert map to json, just iterate the map, set
and convert it to json.
*/
jsonEngine := json.New()
if len(maps) > 0 {
for k, v := range maps {
jsonEngine.Set(k, v)
}
}
jsonByte, err := jsonEngine.MarshalJSON()
if err != nil {
return nil
}
return jsonByte
}