|
| 1 | +package taggroups |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "reflect" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/grokify/mogo/type/stringsutil" |
| 10 | + "github.com/grokify/spectrum/openapi3" |
| 11 | + "github.com/grokify/spectrum/openapi3edit" |
| 12 | +) |
| 13 | + |
| 14 | +const XTagGroupsPropertyName = "x-tag-groups" |
| 15 | + |
| 16 | +type TagGroupSet struct { |
| 17 | + TagGroups []TagGroup |
| 18 | +} |
| 19 | + |
| 20 | +func NewTagGroupSet() TagGroupSet { |
| 21 | + return TagGroupSet{TagGroups: []TagGroup{}} |
| 22 | +} |
| 23 | + |
| 24 | +func (set *TagGroupSet) Exists(tagName string) bool { |
| 25 | + for _, tg := range set.TagGroups { |
| 26 | + for _, tgTagName := range tg.Tags { |
| 27 | + if tagName == tgTagName { |
| 28 | + return true |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + return false |
| 33 | +} |
| 34 | + |
| 35 | +func (set *TagGroupSet) GetTagGroupNamesForTagNames(wantTagNames ...string) []string { |
| 36 | + tagGroupNames := []string{} |
| 37 | + for _, tg := range set.TagGroups { |
| 38 | + for _, tgTagName := range tg.Tags { |
| 39 | + for _, wantTagName := range wantTagNames { |
| 40 | + if wantTagName == tgTagName { |
| 41 | + tagGroupNames = append(tagGroupNames, tg.Name) |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + return stringsutil.SliceCondenseSpace(tagGroupNames, true, true) |
| 47 | +} |
| 48 | + |
| 49 | +func (set *TagGroupSet) AddToSpec(spec *openapi3.Spec) error { |
| 50 | + if len(set.TagGroups) == 0 { |
| 51 | + return nil |
| 52 | + } |
| 53 | + missing := SpecTagsWithoutGroups(spec, *set) |
| 54 | + if len(missing) > 0 { |
| 55 | + return fmt.Errorf("E_TAGS_WITHOUT_GROUPS [%s]", strings.Join(missing, ",")) |
| 56 | + } |
| 57 | + se := openapi3edit.SpecEdit{} |
| 58 | + se.SpecSet(spec) |
| 59 | + se.ExtensionSet(XTagGroupsPropertyName, set.TagGroups) |
| 60 | + // spec.ExtensionProps.Extensions[XTagGroupsPropertyName] = set.TagGroups |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +// OperationMoreTagGroupNames this function is meant to be used wtih `SpecMore.Table()` |
| 65 | +// and must follow the `OperationMoreStringFunc` interface. |
| 66 | +func (set *TagGroupSet) OperationMoreTagGroupNames(opm *openapi3.OperationMore) string { |
| 67 | + // row = append(row, strings.Join(tgs.GetTagGroupNamesForTagNames(op.Tags...), ", ")) |
| 68 | + if opm == nil || opm.Operation == nil { |
| 69 | + return "" |
| 70 | + } |
| 71 | + return strings.Join(set.GetTagGroupNamesForTagNames(opm.Operation.Tags...), ", ") |
| 72 | +} |
| 73 | + |
| 74 | +type TagGroup struct { |
| 75 | + Name string `json:"name"` |
| 76 | + Popular bool `json:"popular"` |
| 77 | + Tags []string `json:"tags"` |
| 78 | +} |
| 79 | + |
| 80 | +/* |
| 81 | +func (sm *SpecMore) TagsWithoutGroups() ([]string, []string, []string, error) { |
| 82 | + tgs, err := sm.TagGroups() |
| 83 | + if err != nil { |
| 84 | + return []string{}, []string{}, []string{}, err |
| 85 | + } |
| 86 | + allTags := []string{} |
| 87 | +
|
| 88 | + topTags := stringsutil.SliceCondenseSpace(sm.Tags(true, false), true, true) |
| 89 | + allTags = append(allTags, topTags...) |
| 90 | +
|
| 91 | + opsTags := stringsutil.SliceCondenseSpace(sm.Tags(false, true), true, true) |
| 92 | + allTags = append(allTags, opsTags...) |
| 93 | +
|
| 94 | + allTags = stringsutil.SliceCondenseSpace(allTags, true, true) |
| 95 | + return allTags, topTags, opsTags, nil |
| 96 | +} |
| 97 | +*/ |
| 98 | + |
| 99 | +func SpecTagsWithoutGroups(spec *openapi3.Spec, tagGroupSet TagGroupSet) []string { |
| 100 | + missing := []string{} |
| 101 | + for _, tag := range spec.Tags { |
| 102 | + if !tagGroupSet.Exists(tag.Name) { |
| 103 | + missing = append(missing, tag.Name) |
| 104 | + } |
| 105 | + } |
| 106 | + return missing |
| 107 | +} |
| 108 | + |
| 109 | +// SpecTagGroups parses a TagGroupSet from an OpenAPI3 spec. |
| 110 | +func SpecTagGroups(spec *openapi3.Spec) (TagGroupSet, error) { |
| 111 | + sm := openapi3.SpecMore{Spec: spec} |
| 112 | + tgs := NewTagGroupSet() |
| 113 | + iface, ok := sm.Spec.ExtensionProps.Extensions[XTagGroupsPropertyName] |
| 114 | + if !ok { |
| 115 | + return tgs, nil |
| 116 | + } |
| 117 | + |
| 118 | + tagGroups := []TagGroup{} |
| 119 | + if reflect.TypeOf(iface) == reflect.TypeOf(tagGroups) { |
| 120 | + tgs.TagGroups = iface.([]TagGroup) |
| 121 | + return tgs, nil |
| 122 | + } |
| 123 | + |
| 124 | + // message is stored as `json.RawMessage` when the data |
| 125 | + // is read in from JSON, vs. set via code. |
| 126 | + rawMessage := iface.(json.RawMessage) |
| 127 | + err := json.Unmarshal(rawMessage, &tagGroups) |
| 128 | + if err != nil { |
| 129 | + return tgs, err |
| 130 | + } |
| 131 | + tgs.TagGroups = tagGroups |
| 132 | + delete(sm.Spec.ExtensionProps.Extensions, XTagGroupsPropertyName) |
| 133 | + sm.Spec.ExtensionProps.Extensions[XTagGroupsPropertyName] = tagGroups |
| 134 | + return tgs, nil |
| 135 | +} |
0 commit comments