-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
215 lines (175 loc) · 4.51 KB
/
main.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
"gopkg.in/yaml.v2"
)
type Comparator struct {
Source interface{}
Target interface{}
Path string
}
type OpDefinition struct {
Type string
Path string
Value string `yaml:",omitempty"`
}
type OpDefinitions []OpDefinition
func (o OpDefinitions) Len() int {
return len(o)
}
func (o OpDefinitions) Swap(i, j int) {
o[i], o[j] = o[j], o[i]
}
func (o OpDefinitions) Less(i, j int) bool {
return o[i].Path < o[j].Path
}
func main() {
sourceFilename := os.Args[1]
targetFilename := os.Args[2]
sourceBytes, _ := ioutil.ReadFile(sourceFilename)
targetBytes, _ := ioutil.ReadFile(targetFilename)
c := Comparator{Path: "/"}
err := yaml.Unmarshal(sourceBytes, &c.Source)
if err != nil {
panic(fmt.Sprintf("unable to unmarshal yaml from `%s`\n", sourceFilename))
}
err = yaml.Unmarshal(targetBytes, &c.Target)
if err != nil {
panic(fmt.Sprintf("unable to unmarshal yaml from `%s`\n", targetFilename))
}
opDefs := compareObjects(c)
sort.Sort(opDefs)
opsfile, _ := yaml.Marshal(opDefs)
fmt.Printf("%s", string(opsfile))
}
func compareObjects(c Comparator) OpDefinitions {
switch source := c.Source.(type) {
case map[interface{}]interface{}:
target, ok := c.Target.(map[interface{}]interface{})
if !ok {
fmt.Fprintf(os.Stderr, "Skipping path %s; replace not yet implemented\n", c.Path)
return OpDefinitions{}
}
return compareMaps(source, target, c.Path)
case []interface{}:
target, ok := c.Target.([]interface{})
if !ok {
fmt.Fprintf(os.Stderr, "Skipping path %s; replace not yet implemented\n", c.Path)
return OpDefinitions{}
}
return compareSlices(source, target, c.Path)
default:
return OpDefinitions{}
}
}
func compareMaps(source, target map[interface{}]interface{}, currentPath string) (opDefs OpDefinitions) {
for key, value := range source {
key := key.(string)
if target[key] == nil {
opDefs = append(opDefs, buildOpDefinition(currentPath, key))
continue
}
comparator := Comparator{
Source: value,
Target: target[key],
Path: currentPath + key + "/",
}
opDefs = append(opDefs, compareObjects(comparator)...)
}
return
}
func compareSlices(source, target []interface{}, currentPath string) (opDefs OpDefinitions) {
sourceIds := findUniqueIds(source)
targetIds := findUniqueIds(target)
nameSourceIds := map[string]int{}
for id, sourceIndex := range sourceIds {
if strings.HasPrefix(id, "name=") {
nameSourceIds[id] = sourceIndex
}
}
comparators := []Comparator{}
matchedSourceIds := map[int]bool{}
var targetIndex int
var ok bool
for id, sourceIndex := range nameSourceIds {
targetIndex, ok = targetIds[id]
if ok {
comparators = append(comparators, Comparator{
Source: source[sourceIndex],
Target: target[targetIndex],
Path: fmt.Sprintf("%s%s/", currentPath, id),
})
matchedSourceIds[sourceIndex] = true
}
}
for id, sourceIndex := range sourceIds {
if matchedSourceIds[sourceIndex] {
continue
}
targetIndex, ok = targetIds[id]
if ok {
comparators = append(comparators, Comparator{
Source: source[sourceIndex],
Target: target[targetIndex],
Path: fmt.Sprintf("%s%s/", currentPath, id),
})
matchedSourceIds[sourceIndex] = true
}
}
for id, sourceIndex := range nameSourceIds {
if !matchedSourceIds[sourceIndex] {
opDefs = append(opDefs, buildOpDefinition(currentPath, id))
matchedSourceIds[sourceIndex] = true
}
}
for id, sourceIndex := range sourceIds {
if !matchedSourceIds[sourceIndex] {
opDefs = append(opDefs, buildOpDefinition(currentPath, id))
matchedSourceIds[sourceIndex] = true
}
}
for _, comparator := range comparators {
opDefs = append(opDefs, compareObjects(comparator)...)
}
return
}
func findUniqueIds(items []interface{}) map[string]int {
idPresence := map[string][]int{}
for i, item := range items {
for _, id := range getIDsForItem(item) {
idPresence[id] = append(idPresence[id], i)
}
}
uniqueIds := map[string]int{}
for id, indices := range idPresence {
if len(indices) == 1 {
uniqueIds[id] = indices[0]
}
}
return uniqueIds
}
func getIDsForItem(rawItem interface{}) []string {
ids := []string{}
item, ok := rawItem.(map[interface{}]interface{})
if !ok {
return ids
}
for key, value := range item {
value, ok := value.(string)
if !ok {
continue
}
ids = append(ids, fmt.Sprintf("%s=%s", key.(string), value))
}
return ids
}
func buildOpDefinition(location, key string) OpDefinition {
return OpDefinition{
Type: "remove",
Path: location + key,
}
}