forked from Tufin/oasdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variables_diff.go
80 lines (62 loc) · 1.93 KB
/
variables_diff.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
package diff
import "github.com/rjmohammad/kin-openapi/openapi3"
// VariablesDiff describes the changes between a pair of sets of server variable objects: https://swagger.io/specification/#server-variable-object
type VariablesDiff struct {
Added StringList `json:"added,omitempty" yaml:"added,omitempty"`
Deleted StringList `json:"deleted,omitempty" yaml:"deleted,omitempty"`
Modified ModifiedVariables `json:"modified,omitempty" yaml:"modified,omitempty"`
}
// ModifiedVariables is map of variable names to their respective diffs
type ModifiedVariables map[string]*VariableDiff
// Empty indicates whether a change was found in this element
func (diff *VariablesDiff) Empty() bool {
if diff == nil {
return true
}
return len(diff.Added) == 0 &&
len(diff.Deleted) == 0 &&
len(diff.Modified) == 0
}
func (diff *VariablesDiff) removeNonBreaking() {
if diff.Empty() {
return
}
diff.Added = nil
}
func newVariablesDiff() *VariablesDiff {
return &VariablesDiff{
Added: StringList{},
Deleted: StringList{},
Modified: ModifiedVariables{},
}
}
func getVariablesDiff(config *Config, state *state, variables1, variables2 map[string]*openapi3.ServerVariable) *VariablesDiff {
diff := getVariablesDiffInternal(config, state, variables1, variables2)
if config.BreakingOnly {
diff.removeNonBreaking()
}
if diff.Empty() {
return nil
}
return diff
}
func getVariablesDiffInternal(config *Config, state *state, variables1, variables2 map[string]*openapi3.ServerVariable) *VariablesDiff {
result := newVariablesDiff()
for name1, var1 := range variables1 {
var2, ok := variables2[name1]
if !ok {
result.Deleted = append(result.Deleted, name1)
continue
}
if diff := getVariableDiff(config, state, var1, var2); !diff.Empty() {
result.Modified[name1] = diff
}
}
for name2 := range variables2 {
_, ok := variables1[name2]
if !ok {
result.Added = append(result.Added, name2)
}
}
return result
}