forked from Tufin/oasdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact.go
54 lines (41 loc) · 1.72 KB
/
contact.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
package diff
import "github.com/rjmohammad/kin-openapi/openapi3"
// ContactDiff describes the changes between a pair of contact objects: https://swagger.io/specification/#contact-object
type ContactDiff 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"`
NameDiff *ValueDiff `json:"name,omitempty" yaml:"name,omitempty"`
URLDiff *ValueDiff `json:"url,omitempty" yaml:"url,omitempty"`
EmailDiff *ValueDiff `json:"email,omitempty" yaml:"email,omitempty"`
}
// Empty indicates whether a change was found in this element
func (diff *ContactDiff) Empty() bool {
return diff == nil || *diff == ContactDiff{}
}
func getContactDiff(config *Config, state *state, contact1, contact2 *openapi3.Contact) *ContactDiff {
diff := getContactDiffInternal(config, state, contact1, contact2)
if diff.Empty() {
return nil
}
return diff
}
func getContactDiffInternal(config *Config, state *state, contact1, contact2 *openapi3.Contact) *ContactDiff {
result := ContactDiff{}
if contact1 == nil && contact2 == nil {
return &result
}
if contact1 == nil && contact2 != nil {
result.Added = true
return &result
}
if contact1 != nil && contact2 == nil {
result.Deleted = true
return &result
}
result.ExtensionsDiff = getExtensionsDiff(config, state, contact1.ExtensionProps, contact2.ExtensionProps)
result.NameDiff = getValueDiff(contact1.Name, contact2.Name)
result.URLDiff = getValueDiff(contact1.URL, contact2.URL)
result.EmailDiff = getValueDiff(contact1.Email, contact2.Email)
return &result
}