-
Notifications
You must be signed in to change notification settings - Fork 28
/
folders.go
73 lines (68 loc) · 2.1 KB
/
folders.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
package openapi3postman2
import (
"fmt"
"strings"
oas3 "github.com/getkin/kin-openapi/openapi3"
"github.com/grokify/mogo/net/http/httputilmore"
"github.com/grokify/spectrum/ext/taggroups"
"github.com/grokify/spectrum/openapi3"
"github.com/grokify/spectrum/postman2"
)
func CreateTagsAndTagGroups(pman postman2.Collection, spec *openapi3.Spec) (postman2.Collection, error) {
// oas3specMore := openapi3.SpecMore{Spec: spec}
tagGroupSet, err := taggroups.SpecTagGroups(spec)
// tagGroupSet, err := openapi3.SpecTagGroups(spec)
if err != nil {
return pman, err
}
if len(tagGroupSet.TagGroups) > 0 {
return addFoldersFromTagGroups(pman, tagGroupSet, spec.Tags)
}
return addFoldersFromTags(pman, spec.Tags), nil
}
func addFoldersFromTagGroups(pman postman2.Collection, tgSet taggroups.TagGroupSet, tags oas3.Tags) (postman2.Collection, error) {
tagsMore := openapi3.TagsMore{Tags: tags}
for _, tg := range tgSet.TagGroups {
tg.Name = strings.TrimSpace(tg.Name)
if len(tg.Name) == 0 && len(tg.Tags) > 0 {
return pman, fmt.Errorf("E_TAG_GROUP_EMPTY_NAME TAGS [%s]", strings.Join(tg.Tags, ","))
}
topFolder := pman.GetOrNewFolder(tg.Name)
if topFolder.Item == nil {
topFolder.Item = []*postman2.Item{}
}
for _, tagName := range tg.Tags {
tagName = strings.TrimSpace(tagName)
if len(tagName) == 0 {
continue
}
subFolder := &postman2.Item{Name: tagName}
tag := tagsMore.Get(tagName)
if tag != nil {
subFolder.Description = &postman2.Description{
Content: strings.TrimSpace(tag.Description),
Type: httputilmore.ContentTypeTextPlain}
}
topFolder.UpsertSubItem(subFolder)
}
pman.SetFolder(topFolder)
}
return pman, nil
}
func addFoldersFromTags(pman postman2.Collection, tags oas3.Tags) postman2.Collection {
for _, tag := range tags {
if tag == nil {
continue
}
folder := &postman2.Item{
Name: strings.TrimSpace(tag.Name),
Description: &postman2.Description{
Content: strings.TrimSpace(tag.Description),
Type: httputilmore.ContentTypeTextPlain}}
if len(folder.Name) == 0 {
continue
}
pman.SetFolder(folder)
}
return pman
}