forked from blablacar/ggn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merger.go
102 lines (94 loc) · 2.63 KB
/
merger.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package utils
import (
"errors"
"fmt"
"io/ioutil"
"strconv"
"github.com/n0rad/go-erlog/data"
"github.com/n0rad/go-erlog/errs"
"github.com/peterbourgon/mergemap"
"gopkg.in/yaml.v2"
)
func MergeAttributesFilesForMap(omap map[string]interface{}, files []string) (map[string]interface{}, error) {
newMap := make(map[string]interface{})
newMap["default"] = omap
// loop over attributes files
// merge override files to default files
for _, file := range files {
var contentData interface{}
yml, err := ioutil.ReadFile(file)
if err != nil {
return newMap, errs.WithEF(err, data.WithField("file", file), "Failed to read file")
}
// yaml to contentData
err = yaml.Unmarshal(yml, &contentData)
if err != nil {
return newMap, errs.WithEF(err, data.WithField("file", file), "Failed to unmarshal file")
}
contentData, err = transformYampToJson(contentData)
if err != nil {
return newMap, errs.WithEF(err, data.WithField("file", file), "Failed to transform file to json")
}
// contentData to map
json := contentData.(map[string]interface{})
omap = mergemap.Merge(newMap, json)
}
return ProcessOverride(newMap), nil
}
func MergeAttributesFiles(files []string) (map[string]interface{}, error) {
omap := make(map[string]interface{})
return MergeAttributesFilesForMap(omap, files)
}
func ProcessOverride(omap map[string]interface{}) map[string]interface{} {
// merge override to default inside the file
_, okd := omap["default"]
if okd == false {
omap["default"] = make(map[string]interface{}) //init if default doesn't exist
}
_, oko := omap["override"]
if oko == true {
omap = mergemap.Merge(omap["default"].(map[string]interface{}), omap["override"].(map[string]interface{}))
} else {
omap = omap["default"].(map[string]interface{})
}
return omap
}
// transformYampToJson YAML to JSON
func transformYampToJson(in interface{}) (_ interface{}, err error) {
switch in.(type) {
case map[interface{}]interface{}:
o := make(map[string]interface{})
for k, v := range in.(map[interface{}]interface{}) {
sk := ""
switch k.(type) {
case string:
sk = k.(string)
case int:
sk = strconv.Itoa(k.(int))
default:
return nil, errors.New(
fmt.Sprintf("type not match: expect map key string or int get: %T", k))
}
v, err = transformYampToJson(v)
if err != nil {
return nil, err
}
o[sk] = v
}
return o, nil
case []interface{}:
in1 := in.([]interface{})
len1 := len(in1)
o := make([]interface{}, len1)
for i := 0; i < len1; i++ {
o[i], err = transformYampToJson(in1[i])
if err != nil {
return nil, err
}
}
return o, nil
default:
return in, nil
}
return in, nil
}