/
dashboard_tree_item_diffs.go
194 lines (175 loc) · 4.7 KB
/
dashboard_tree_item_diffs.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
package modconfig
import (
"github.com/turbot/go-kit/helpers"
"github.com/turbot/pipe-fittings/utils"
"golang.org/x/exp/maps"
)
// DashboardTreeItemDiffs is a struct representing the differences between 2 DashboardTreeItems (of same type)
type DashboardTreeItemDiffs struct {
Name string
Item ModTreeItem
ChangedProperties []string
AddedItems []string
RemovedItems []string
}
func (d *DashboardTreeItemDiffs) AddPropertyDiff(propertyName string) {
if !helpers.StringSliceContains(d.ChangedProperties, propertyName) {
d.ChangedProperties = append(d.ChangedProperties, propertyName)
}
}
func (d *DashboardTreeItemDiffs) AddAddedItem(name string) {
d.AddedItems = append(d.AddedItems, name)
}
func (d *DashboardTreeItemDiffs) AddRemovedItem(name string) {
d.RemovedItems = append(d.RemovedItems, name)
}
func (d *DashboardTreeItemDiffs) populateChildDiffs(old ModTreeItem, new ModTreeItem) {
// build map of child names
oldChildMap := make(map[string]ModTreeItem)
newChildMap := make(map[string]ModTreeItem)
oldChildren := old.GetChildren()
newChildren := new.GetChildren()
for i, child := range oldChildren {
// check for child ordering
if i < len(newChildren) && newChildren[i].Name() != child.Name() {
d.AddPropertyDiff("Children")
}
oldChildMap[child.Name()] = child
}
for _, child := range newChildren {
newChildMap[child.Name()] = child
}
for childName, prevChild := range oldChildMap {
if child, existInNew := newChildMap[childName]; !existInNew {
d.AddRemovedItem(childName)
} else {
// so this resource exists on old and new
// TACTICAL
// some child resources are not added to the mod but we must consider them for the diff
var childDiff = &DashboardTreeItemDiffs{}
switch t := child.(type) {
case *DashboardWith:
childDiff = t.Diff(prevChild.(*DashboardWith))
case *DashboardNode:
childDiff = t.Diff(prevChild.(*DashboardNode))
case *DashboardEdge:
childDiff = t.Diff(prevChild.(*DashboardEdge))
}
if childDiff.HasChanges() {
d.AddPropertyDiff("Children")
}
}
}
for childName := range newChildMap {
if _, existsInOld := oldChildMap[childName]; !existsInOld {
d.AddAddedItem(childName)
}
}
}
func (d *DashboardTreeItemDiffs) HasChanges() bool {
return len(d.ChangedProperties)+
len(d.AddedItems)+
len(d.RemovedItems) > 0
}
func (d *DashboardTreeItemDiffs) queryProviderDiff(l QueryProvider, r QueryProvider) {
// sql
if !utils.SafeStringsEqual(l.GetSQL(), r.GetSQL()) {
d.AddPropertyDiff("SQL")
}
// args
if lArgs := l.GetArgs(); lArgs == nil {
if r.GetArgs() != nil {
d.AddPropertyDiff("Args")
}
} else {
// we have args
if rArgs := r.GetArgs(); rArgs == nil {
d.AddPropertyDiff("Args")
} else if !lArgs.Equals(rArgs) {
d.AddPropertyDiff("Args")
}
}
// query
if lQuery := l.GetQuery(); lQuery == nil {
if r.GetQuery() != nil {
d.AddPropertyDiff("Query")
}
} else {
// we have query
if rQuery := r.GetQuery(); rQuery == nil {
d.AddPropertyDiff("Query")
} else if !lQuery.Equals(rQuery) {
d.AddPropertyDiff("Query")
}
}
// params
lParams := l.GetParams()
rParams := r.GetParams()
if len(lParams) != len(rParams) {
d.AddPropertyDiff("Params")
} else {
for i, lParam := range lParams {
if !lParam.Equals(rParams[i]) {
d.AddPropertyDiff("Params")
}
}
}
// with
if lwp, ok := l.(WithProvider); ok {
rwp := r.(WithProvider)
lWiths := lwp.GetWiths()
rWiths := rwp.GetWiths()
if len(lWiths) != len(rWiths) {
d.AddPropertyDiff("With")
} else {
for i, lWith := range lWiths {
if !lWith.Equals(rWiths[i]) {
d.AddPropertyDiff("With")
}
}
}
// have BASE withs changed
lbase := l.GetBase()
rbase := r.GetBase()
var lbaseWiths []*DashboardWith
var rbaseWiths []*DashboardWith
if lbase != nil {
lbaseWiths = lbase.(WithProvider).GetWiths()
}
if rbase != nil {
rbaseWiths = rbase.(WithProvider).GetWiths()
}
if len(lbaseWiths) != len(rbaseWiths) {
d.AddPropertyDiff("With")
} else {
for i, lBaseWith := range lbaseWiths {
if !lBaseWith.Equals(rbaseWiths[i]) {
d.AddPropertyDiff("With")
}
}
}
}
}
func (d *DashboardTreeItemDiffs) dashboardLeafNodeDiff(l DashboardLeafNode, r DashboardLeafNode) {
if l.Name() != r.Name() {
d.AddPropertyDiff("Name")
}
if l.GetTitle() != r.GetTitle() {
d.AddPropertyDiff("Title")
}
if l.GetWidth() != r.GetWidth() {
d.AddPropertyDiff("Width")
}
if l.GetDisplay() != r.GetDisplay() {
d.AddPropertyDiff("Display")
}
if l.GetDocumentation() != r.GetDocumentation() {
d.AddPropertyDiff("Documentation")
}
if l.GetType() != r.GetType() {
d.AddPropertyDiff("Type")
}
if !maps.Equal(l.GetTags(), r.GetTags()) {
d.AddPropertyDiff("Tags")
}
}