-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathtags_diff.go
More file actions
75 lines (60 loc) · 1.76 KB
/
tags_diff.go
File metadata and controls
75 lines (60 loc) · 1.76 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
package diff
import (
"github.com/getkin/kin-openapi/openapi3"
)
// TagsDiff describes the changes between a pair of lists of tag objects: https://swagger.io/specification/#tag-object
type TagsDiff struct {
Added []string `json:"added,omitempty" yaml:"added,omitempty"`
Deleted []string `json:"deleted,omitempty" yaml:"deleted,omitempty"`
Modified ModifiedTags `json:"modified,omitempty" yaml:"modified,omitempty"`
}
func newTagsDiff() *TagsDiff {
return &TagsDiff{
Added: []string{},
Deleted: []string{},
Modified: ModifiedTags{},
}
}
// ModifiedTags is map of tag names to their respective diffs
type ModifiedTags map[string]*TagDiff
// Empty indicates whether a change was found in this element
func (tagsDiff *TagsDiff) Empty() bool {
if tagsDiff == nil {
return true
}
return len(tagsDiff.Added) == 0 &&
len(tagsDiff.Deleted) == 0 &&
len(tagsDiff.Modified) == 0
}
func getTagsDiff(config *Config, tags1, tags2 openapi3.Tags) *TagsDiff {
diff := getTagsDiffInternal(config, tags1, tags2)
if diff.Empty() {
return nil
}
return diff
}
func getTagsDiffInternal(config *Config, tags1, tags2 openapi3.Tags) *TagsDiff {
result := newTagsDiff()
for _, tag1 := range tags1 {
if tag2 := tags2.Get(tag1.Name); tag2 != nil {
if diff := getTagDiff(config, tag1, tag2); !diff.Empty() {
result.Modified[tag1.Name] = diff
}
} else {
result.Deleted = append(result.Deleted, tag1.Name)
}
}
for _, tag2 := range tags2 {
if tag1 := tags1.Get(tag2.Name); tag1 == nil {
result.Added = append(result.Added, tag2.Name)
}
}
return result
}
func (tagsDiff *TagsDiff) getSummary() *SummaryDetails {
return &SummaryDetails{
Added: len(tagsDiff.Added),
Deleted: len(tagsDiff.Deleted),
Modified: len(tagsDiff.Modified),
}
}