-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffer.go
More file actions
207 lines (180 loc) · 4.41 KB
/
differ.go
File metadata and controls
207 lines (180 loc) · 4.41 KB
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
package mapx
import (
"fmt"
"reflect"
"sort"
"strconv"
"strings"
)
const (
// ActionAdd means has added
ActionAdd = "Add"
// ActionChange means has changed
ActionChange = "Change"
// ActionRemove means has removed
ActionRemove = "Remove"
)
const (
// NodeTypeIndex is index type node
NodeTypeIndex = "index"
// NodeTypeKey is key type node
NodeTypeKey = "key"
)
// Node Map tree node
type Node struct {
Type string
Index string
Key string
}
// NewIdxNode ...
func NewIdxNode(index int) Node {
return Node{Type: NodeTypeIndex, Index: strconv.Itoa(index)}
}
// NewKeyNode ...
func NewKeyNode(key string) Node {
return Node{Type: NodeTypeKey, Key: key}
}
// DiffRet Diff Result
type DiffRet struct {
Action string
Dotted string
OldVal any
NewVal any
}
// NewDiffRet ...
func NewDiffRet(action string, nodes []Node, old, new any) DiffRet {
var b strings.Builder
for _, n := range nodes {
switch n.Type {
case NodeTypeKey:
// if key contains ".", add "(" and ")" for distinguish
if strings.Contains(n.Key, ".") {
b.WriteString(".(" + n.Key + ")")
} else {
b.WriteString("." + n.Key)
}
case NodeTypeIndex:
b.WriteString("[" + n.Index + "]")
}
}
dotted := strings.Trim(b.String(), ".")
return DiffRet{Action: action, Dotted: dotted, OldVal: old, NewVal: new}
}
// String change DiffRet to string
func (r *DiffRet) String() string {
ret := fmt.Sprintf("%s %s: ", r.Action, r.Dotted)
switch r.Action {
case ActionAdd:
ret += fmt.Sprintf("%v", r.NewVal)
case ActionChange:
ret += fmt.Sprintf("%v -> %v", r.OldVal, r.NewVal)
case ActionRemove:
ret += fmt.Sprintf("%v", r.OldVal)
}
return ret
}
// DiffRetList Diff Result List
type DiffRetList []DiffRet
// Len ...
func (l DiffRetList) Len() int {
return len(l)
}
// Less sort by type and dotted
func (l DiffRetList) Less(i, j int) bool {
cmpRet := strings.Compare(l[i].Action, l[j].Action)
if cmpRet == 0 {
return strings.Compare(l[i].Dotted, l[j].Dotted) < 0
}
return cmpRet < 0
}
// Swap ...
func (l DiffRetList) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}
// Differ is Map differ, used to compare the keys and values of two maps.
type Differ struct {
old map[string]any
new map[string]any
rets DiffRetList
}
// NewDiffer ...
func NewDiffer(old, new map[string]any) *Differ {
return &Differ{old: old, new: new, rets: DiffRetList{}}
}
// Do execute compare
func (d *Differ) Do() DiffRetList {
d.handleMap(d.old, d.new, []Node{})
sort.Sort(d.rets)
return d.rets
}
func (d *Differ) handleMap(old, new map[string]any, nodes []Node) {
intersection, addition, deletion := []string{}, []string{}, []string{}
for key := range old {
if Exists(new, key) {
intersection = append(intersection, key)
}
}
for key := range new {
if !Exists(old, key) {
addition = append(addition, key)
}
}
for key := range old {
if !Exists(new, key) {
deletion = append(deletion, key)
}
}
// intersection
for _, key := range intersection {
curNodes := append(nodes, NewKeyNode(key))
d.handle(old[key], new[key], curNodes)
}
// addition
for _, key := range addition {
ret := NewDiffRet(ActionAdd, append(nodes, NewKeyNode(key)), nil, new[key])
d.rets = append(d.rets, ret)
}
// deletion
for _, key := range deletion {
ret := NewDiffRet(ActionRemove, append(nodes, NewKeyNode(key)), old[key], nil)
d.rets = append(d.rets, ret)
}
}
func (d *Differ) handleList(old, new []any, nodes []Node) {
oldLen, newLen, minLen := len(old), len(new), len(old)
if newLen < oldLen {
minLen = newLen
}
// intersection
for idx := 0; idx < minLen; idx++ {
d.handle(old[idx], new[idx], append(nodes, NewIdxNode(idx)))
}
// addition
for idx := minLen; idx < newLen; idx++ {
ret := NewDiffRet(ActionAdd, append(nodes, NewIdxNode(idx)), nil, new[idx])
d.rets = append(d.rets, ret)
}
// deletion
for idx := minLen; idx < oldLen; idx++ {
ret := NewDiffRet(ActionRemove, append(nodes, NewIdxNode(idx)), old[idx], nil)
d.rets = append(d.rets, ret)
}
}
func (d *Differ) handle(old, new any, nodes []Node) {
oldMap, oldIsMap := old.(map[string]any)
newMap, newIsMap := new.(map[string]any)
if oldIsMap && newIsMap {
d.handleMap(oldMap, newMap, nodes)
return
}
oldList, oldIsList := old.([]any)
newList, newIsList := new.([]any)
if oldIsList && newIsList {
d.handleList(oldList, newList, nodes)
return
}
if !reflect.DeepEqual(old, new) {
ret := NewDiffRet(ActionChange, nodes, old, new)
d.rets = append(d.rets, ret)
}
}