-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathinfo_diff.go
More file actions
78 lines (66 loc) · 2.51 KB
/
info_diff.go
File metadata and controls
78 lines (66 loc) · 2.51 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
package diff
import (
"github.com/getkin/kin-openapi/openapi3"
)
// InfoDiff describes the changes between a pair of info objects: https://swagger.io/specification/#info-object
type InfoDiff 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"`
TitleDiff *ValueDiff `json:"title,omitempty" yaml:"title,omitempty"`
DescriptionDiff *ValueDiff `json:"description,omitempty" yaml:"description,omitempty"`
TermsOfServiceDiff *ValueDiff `json:"termsOfService,omitempty" yaml:"termsOfService,omitempty"`
ContactDiff *ContactDiff `json:"contact,omitempty" yaml:"contact,omitempty"`
LicenseDiff *LicenseDiff `json:"license,omitempty" yaml:"license,omitempty"`
VersionDiff *ValueDiff `json:"version,omitempty" yaml:"version,omitempty"`
}
// Empty indicates whether a change was found in this element
func (diff *InfoDiff) Empty() bool {
return diff == nil || *diff == InfoDiff{}
}
func getInfoDiff(config *Config, info1, info2 *openapi3.Info) (*InfoDiff, error) {
diff, err := getInfoDiffInternal(config, info1, info2)
if err != nil {
return nil, err
}
if diff.Empty() {
return nil, nil
}
return diff, nil
}
func getInfoDiffInternal(config *Config, info1, info2 *openapi3.Info) (*InfoDiff, error) {
if info1 == nil && info2 == nil {
return nil, nil
}
if info1 == nil && info2 != nil {
return &InfoDiff{
Added: true,
}, nil
}
if info1 != nil && info2 == nil {
return &InfoDiff{
Deleted: true,
}, nil
}
extensionsDiff, err := getExtensionsDiff(config, info1.Extensions, info2.Extensions)
if err != nil {
return nil, err
}
licenseDiff, err := getLicenseDiff(config, info1.License, info2.License)
if err != nil {
return nil, err
}
contactDiff, err := getContactDiff(config, info1.Contact, info2.Contact)
if err != nil {
return nil, err
}
return &InfoDiff{
ExtensionsDiff: extensionsDiff,
TitleDiff: getValueDiffConditional(config.IsExcludeTitle(), info1.Title, info2.Title),
DescriptionDiff: getValueDiffConditional(config.IsExcludeDescription(), info1.Description, info2.Description),
TermsOfServiceDiff: getValueDiff(info1.TermsOfService, info2.TermsOfService),
ContactDiff: contactDiff,
LicenseDiff: licenseDiff,
VersionDiff: getValueDiff(info1.Version, info2.Version),
}, nil
}