-
Notifications
You must be signed in to change notification settings - Fork 28
/
extension_taggroups.go
120 lines (103 loc) · 2.94 KB
/
extension_taggroups.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package openapi3
import (
"encoding/json"
"fmt"
"reflect"
"strings"
oas3 "github.com/getkin/kin-openapi/openapi3"
"github.com/grokify/simplego/type/stringsutil"
)
const XTagGroupsPropertyName = "x-tag-groups"
type TagGroupSet struct {
TagGroups []TagGroup
}
func NewTagGroupSet() TagGroupSet {
return TagGroupSet{TagGroups: []TagGroup{}}
}
func (set *TagGroupSet) Exists(tagName string) bool {
for _, tg := range set.TagGroups {
for _, tgTagName := range tg.Tags {
if tagName == tgTagName {
return true
}
}
}
return false
}
func (set *TagGroupSet) GetTagGroupNamesForTagNames(wantTagNames ...string) []string {
tagGroupNames := []string{}
for _, tg := range set.TagGroups {
for _, tgTagName := range tg.Tags {
for _, wantTagName := range wantTagNames {
if wantTagName == tgTagName {
tagGroupNames = append(tagGroupNames, tg.Name)
}
}
}
}
return stringsutil.SliceCondenseSpace(tagGroupNames, true, true)
}
func (set *TagGroupSet) AddToSpec(spec *oas3.Swagger) error {
if len(set.TagGroups) == 0 {
return nil
}
missing := TagsWithoutGroups(spec, *set)
if len(missing) > 0 {
return fmt.Errorf("E_TAGS_WITHOUT_GROUPS [%s]", strings.Join(missing, ","))
}
spec.ExtensionProps.Extensions[XTagGroupsPropertyName] = set.TagGroups
return nil
}
type TagGroup struct {
Name string `json:"name"`
Popular bool `json:"popular"`
Tags []string `json:"tags"`
}
/*
func (sm *SpecMore) TagsWithoutGroups() ([]string, []string, []string, error) {
tgs, err := sm.TagGroups()
if err != nil {
return []string{}, []string{}, []string{}, err
}
allTags := []string{}
topTags := stringsutil.SliceCondenseSpace(sm.Tags(true, false), true, true)
allTags = append(allTags, topTags...)
opsTags := stringsutil.SliceCondenseSpace(sm.Tags(false, true), true, true)
allTags = append(allTags, opsTags...)
allTags = stringsutil.SliceCondenseSpace(allTags, true, true)
return allTags, topTags, opsTags, nil
}
*/
func TagsWithoutGroups(spec *oas3.Swagger, tagGroupSet TagGroupSet) []string {
missing := []string{}
for _, tag := range spec.Tags {
if !tagGroupSet.Exists(tag.Name) {
missing = append(missing, tag.Name)
}
}
return missing
}
// TagGroups parses a TagGroupSet from an OpenAPI3 spec.
func (sm *SpecMore) TagGroups() (TagGroupSet, error) {
tgs := NewTagGroupSet()
iface, ok := sm.Spec.ExtensionProps.Extensions[XTagGroupsPropertyName]
if !ok {
return tgs, nil
}
tagGroups := []TagGroup{}
if reflect.TypeOf(iface) == reflect.TypeOf(tagGroups) {
tgs.TagGroups = iface.([]TagGroup)
return tgs, nil
}
// message is stored as `json.RawMessage` when the data
// is read in from JSON, vs. set via code.
rawMessage := iface.(json.RawMessage)
err := json.Unmarshal(rawMessage, &tagGroups)
if err != nil {
return tgs, err
}
tgs.TagGroups = tagGroups
delete(sm.Spec.ExtensionProps.Extensions, XTagGroupsPropertyName)
sm.Spec.ExtensionProps.Extensions[XTagGroupsPropertyName] = tagGroups
return tgs, nil
}