forked from Tufin/oasdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_diff.go
70 lines (53 loc) · 1.98 KB
/
server_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
package diff
import (
"github.com/rjmohammad/kin-openapi/openapi3"
)
// ServerDiff describes the changes between a pair of server objects: https://swagger.io/specification/#server-object
type ServerDiff struct {
Added bool `json:"added,omitempty" yaml:"added,omitempty"`
Deleted bool `json:"deleted,omitempty" yaml:"deleted,omitempty"`
ExtensionsDiff *ExtensionsDiff `json:"extensions,omitempty" yaml:"extensions,omitempty"`
URLDiff *ValueDiff `json:"urlType,omitempty" yaml:"urlType,omitempty"`
DescriptionDiff *ValueDiff `json:"description,omitempty" yaml:"description,omitempty"`
VariablesDiff *VariablesDiff `json:"variables,omitempty" yaml:"variables,omitempty"`
}
// Empty indicates whether a change was found in this element
func (diff *ServerDiff) Empty() bool {
return diff == nil || *diff == ServerDiff{}
}
func (diff *ServerDiff) removeNonBreaking() {
if diff.Empty() {
return
}
diff.ExtensionsDiff = nil
diff.DescriptionDiff = nil
}
func getServerDiff(config *Config, state *state, value1, value2 *openapi3.Server) *ServerDiff {
diff := getServerDiffInternal(config, state, value1, value2)
if config.BreakingOnly {
diff.removeNonBreaking()
}
if diff.Empty() {
return nil
}
return diff
}
func getServerDiffInternal(config *Config, state *state, value1, value2 *openapi3.Server) *ServerDiff {
if value1 == nil && value2 == nil {
return nil
}
result := ServerDiff{}
if value1 == nil && value2 != nil {
result.Added = true
return &result
}
if value1 != nil && value2 == nil {
result.Deleted = true
return &result
}
result.ExtensionsDiff = getExtensionsDiff(config, state, value1.ExtensionProps, value2.ExtensionProps)
result.URLDiff = getValueDiff(value1.URL, value2.URL)
result.DescriptionDiff = getValueDiffConditional(config.ExcludeDescription, value1.Description, value2.Description)
result.VariablesDiff = getVariablesDiff(config, state, value1.Variables, value2.Variables)
return &result
}