-
Notifications
You must be signed in to change notification settings - Fork 28
/
categories.go
99 lines (85 loc) · 2.6 KB
/
categories.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
package taxonomy
import (
"errors"
"fmt"
"os"
mapslice "github.com/ake-persson/mapslice-json"
oas3 "github.com/getkin/kin-openapi/openapi3"
"github.com/grokify/mogo/encoding/jsonutil"
"github.com/grokify/mogo/errors/errorsutil"
"github.com/grokify/spectrum/openapi3"
"sigs.k8s.io/yaml"
)
const XTaxonomy = "x-taxonomy"
type Category struct {
Key string `json:"-"`
Slug string `json:"slug,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Tags []oas3.Tag `json:"tags,omitempty" yaml:"tags,omitempty"`
}
func (cat *Category) Taxonomy(catsFilepath string) (Taxonomy, error) {
if len(cat.Key) == 0 {
return Taxonomy{}, errors.New("categgory `Key` not set")
}
return Taxonomy{
Category: CategoryRef{Ref: fmt.Sprintf("%s#/%s", catsFilepath, cat.Key)},
Slug: cat.Slug}, nil
}
type Categories []Category
var ErrCategoryNotFound = errors.New("category not found")
// SpecAddXTaxonomy expects `catTitle` to be the same as the OAS3 `tag` name.
func (cats *Categories) SpecAddXTaxonomy(spec *openapi3.Spec, catTitle, catsFilepath string) error {
if spec == nil {
return openapi3.ErrSpecNotSet
}
cat, err := cats.Category(catTitle)
if err != nil {
return errorsutil.Wrapf(ErrCategoryNotFound, "category (%s)", catTitle)
}
tax, err := cat.Taxonomy(catsFilepath)
if err != nil {
return err
}
tax.AddToSpec(spec)
return nil
}
func (cats *Categories) Category(title string) (Category, error) {
for _, cat := range *cats {
if cat.Title == title {
return cat, nil
}
}
return Category{}, errorsutil.Wrapf(ErrCategoryNotFound, "category (%s)", title)
}
func (cats *Categories) MapSlice() mapslice.MapSlice {
ms := mapslice.MapSlice{}
for _, cat := range *cats {
ms = append(ms, mapslice.MapItem{Key: cat.Key, Value: cat})
}
return ms
}
func (cats *Categories) MarshalJSON(prefix, indent string) ([]byte, error) {
return jsonutil.MarshalSimple(cats.MapSlice(), prefix, indent)
}
func (cats *Categories) MarshalYAML() ([]byte, error) {
if jbytes, err := cats.MarshalJSON("", ""); err != nil {
return []byte{}, err
} else {
return yaml.JSONToYAML(jbytes)
}
}
func (cats *Categories) WriteFileJSON(filename string, perm os.FileMode, prefix, indent string) error {
if jsonData, err := cats.MarshalJSON(prefix, indent); err != nil {
return err
} else {
return os.WriteFile(filename, jsonData, perm)
}
}
func (cats *Categories) WriteFileYAML(filename string, perm os.FileMode) error {
if ybytes, err := cats.MarshalYAML(); err != nil {
return err
} else {
return os.WriteFile(filename, ybytes, perm)
}
}