Core

/app/util/diff.go (4.7 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
package util

import (
"fmt"
"strings"

"github.com/samber/lo"
)

type Diff struct {
Path string `json:"path"`
Old string `json:"o,omitempty"`
New string `json:"n"`
}

func NewDiff(p string, o string, n string) *Diff {
return &Diff{Path: p, Old: o, New: n}
}

func (d Diff) String() string {
return d.Path
}

func (d Diff) StringVerbose() string {
return fmt.Sprintf("%s (%q != %q)", d.Path, d.Old, d.New)
}

type Diffs []*Diff

func (d Diffs) String() string {
return strings.Join(lo.Map(d, func(x *Diff, _ int) string {
return x.String()
}), "; ")
}

func (d Diffs) StringVerbose() string {
return strings.Join(lo.Map(d, func(x *Diff, _ int) string {
return x.StringVerbose()
}), "; ")
}

func DiffObjects(l any, r any, path ...string) Diffs {
return DiffObjectsIgnoring(l, r, nil, path...)
}

func DiffObjectsIgnoring(l any, r any, ignored []string, path ...string) Diffs {
if len(path) > 0 && lo.Contains(ignored, path[len(path)-1]) {
return nil
}
if l == nil {
return Diffs{NewDiff(strings.Join(path, "."), "", fmt.Sprint(r))}
}
if r == nil {
return Diffs{NewDiff(strings.Join(path, "."), fmt.Sprint(l), "")}
}
if lt, rt := fmt.Sprintf("%T", l), fmt.Sprintf("%T", r); lt != rt {
return Diffs{NewDiff(strings.Join(path, "."), ToJSONCompact(l), ToJSONCompact(r))}
}
return diffType(l, r, ignored, false, path...)
}

func diffType(l any, r any, ignored []string, recursed bool, path ...string) Diffs {
var ret Diffs
switch t := l.(type) {
case ValueMap:
ret = append(ret, diffMaps(t, r, ignored, path...)...)
case map[string]any:
ret = append(ret, diffMaps(t, r, ignored, path...)...)
case map[string]int:
ret = append(ret, diffIntMaps(t, r, ignored, path...)...)
case []any:
ret = append(ret, diffArrays(t, r, ignored, path...)...)
case Diffs:
rm, _ := r.(Diffs)
lo.ForEach(t, func(v *Diff, idx int) {
rv := rm[idx]
ret = append(ret, DiffObjectsIgnoring(v, rv, ignored, append([]string{}, path...)...)...)
})
case int64:
i, _ := r.(int64)
if t != i {
ret = append(ret, NewDiff(strings.Join(path, "."), fmt.Sprint(t), fmt.Sprint(i)))
}
case int:
i, _ := r.(int)
if t != i {
ret = append(ret, NewDiff(strings.Join(path, "."), fmt.Sprint(t), fmt.Sprint(i)))
}
case float64:
i, _ := r.(float64)
if t != i {
ret = append(ret, NewDiff(strings.Join(path, "."), fmt.Sprint(t), fmt.Sprint(i)))
}
case string:
s, _ := r.(string)
if t != s {
ret = append(ret, NewDiff(strings.Join(path, "."), t, s))
}
default:
lj, rj := ToJSONCompact(l), ToJSONCompact(r)
if !recursed && (strings.HasPrefix(lj, "{") || strings.HasPrefix(lj, "[")) {
var lx, rx any
_ = FromJSON([]byte(lj), &lx)
_ = FromJSON([]byte(rj), &rx)
ret = append(ret, diffType(lx, rx, ignored, true, path...)...)
} else if lj != rj {
ret = append(ret, NewDiff(strings.Join(path, "."), lj, rj))
}
}
return ret
}

func diffArrays(l []any, r any, ignored []string, path ...string) Diffs {
var ret Diffs
rm, _ := r.([]any)
lo.ForEach(l, func(v any, idx int) {
if len(rm) > idx {
rv := rm[idx]
ret = append(ret, DiffObjectsIgnoring(v, rv, ignored, append(append([]string{}, path...), fmt.Sprint(idx))...)...)
} else {
ret = append(ret, DiffObjectsIgnoring(v, nil, ignored, append(append([]string{}, path...), fmt.Sprint(idx))...)...)
}
})
if len(rm) > len(l) {
for i := len(l); i < len(rm); i++ {
ret = append(ret, DiffObjectsIgnoring(nil, rm[i], ignored, append(append([]string{}, path...), fmt.Sprint(i))...)...)
}
}
return ret
}

func diffMaps(l map[string]any, r any, ignored []string, path ...string) Diffs {
var ret Diffs
rm, ok := r.(map[string]any)
if !ok {
rm, _ = r.(ValueMap)
}
for k, v := range l {
if lo.Contains(ignored, k) {
continue
}
rv := rm[k]
ret = append(ret, DiffObjectsIgnoring(v, rv, ignored, append(append([]string{}, path...), k)...)...)
}
for k, v := range rm {
if lo.Contains(ignored, k) {
continue
}
if _, exists := l[k]; !exists {
ret = append(ret, DiffObjectsIgnoring(nil, v, ignored, append(append([]string{}, path...), k)...)...)
}
}
return ret
}

func diffIntMaps(l map[string]int, r any, ignored []string, path ...string) Diffs {
var ret Diffs
rm, _ := r.(map[string]int)
for k, v := range l {
if lo.Contains(ignored, k) {
continue
}
rv := rm[k]
ret = append(ret, DiffObjectsIgnoring(v, rv, ignored, append(append([]string{}, path...), k)...)...)
}
for k, v := range rm {
if lo.Contains(ignored, k) {
continue
}
if _, exists := l[k]; !exists {
ret = append(ret, DiffObjectsIgnoring(nil, v, ignored, append(append([]string{}, path...), k)...)...)
}
}
return ret
}