Skip to content

Commit ec83a8f

Browse files
committed
feat: openapi2: add SpecMore{}, NewSpecMetaFilepath(), NewSpecMetaSetFilepaths()
1 parent 38de908 commit ec83a8f

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

openapi2/spec_meta.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package openapi2
2+
3+
import (
4+
"github.com/grokify/spectrum"
5+
)
6+
7+
func NewSpecMetaFilepath(filepath string) (*spectrum.SpecMeta, error) {
8+
if s, err := ReadOpenAPI2KinSpecFile(filepath); err != nil {
9+
return nil, err
10+
} else {
11+
sm := SpecMore{Spec: s}
12+
return sm.Meta(), nil
13+
}
14+
}
15+
16+
func NewSpecMetaSetFilepaths(filepaths []string) (*spectrum.SpecMetaSet, error) {
17+
set := spectrum.NewSpecMetaSet()
18+
for _, fp := range filepaths {
19+
if m, err := NewSpecMetaFilepath(fp); err != nil {
20+
return nil, err
21+
} else {
22+
set.Data[fp] = *m
23+
}
24+
}
25+
return set, nil
26+
}

openapi2/spec_more.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package openapi2
2+
3+
import (
4+
"net/http"
5+
"sort"
6+
7+
"github.com/grokify/spectrum"
8+
)
9+
10+
type SpecMore struct {
11+
Spec *Spec
12+
}
13+
14+
func (m *SpecMore) Meta() *spectrum.SpecMeta {
15+
meta := spectrum.NewSpecMeta()
16+
if m.Spec == nil {
17+
return meta
18+
}
19+
meta.Names = m.Names()
20+
meta.Inflate()
21+
return meta
22+
}
23+
24+
func (more *SpecMore) Names() spectrum.SpecMetaNames {
25+
out := spectrum.SpecMetaNames{
26+
Endpoints: more.Endpoints(),
27+
Models: more.ModelNames(),
28+
Paths: more.PathNames(),
29+
}
30+
return out
31+
}
32+
33+
func (more *SpecMore) Endpoints() []string {
34+
var out []string
35+
if more.Spec == nil {
36+
return out
37+
}
38+
for k, pathItem := range more.Spec.Paths {
39+
if pathItem.Delete != nil {
40+
out = append(out, spectrum.EndpointString(http.MethodDelete, k))
41+
}
42+
if pathItem.Get != nil {
43+
out = append(out, spectrum.EndpointString(http.MethodGet, k))
44+
}
45+
if pathItem.Head != nil {
46+
out = append(out, spectrum.EndpointString(http.MethodHead, k))
47+
}
48+
if pathItem.Options != nil {
49+
out = append(out, spectrum.EndpointString(http.MethodOptions, k))
50+
}
51+
if pathItem.Patch != nil {
52+
out = append(out, spectrum.EndpointString(http.MethodPatch, k))
53+
}
54+
if pathItem.Post != nil {
55+
out = append(out, spectrum.EndpointString(http.MethodPost, k))
56+
}
57+
if pathItem.Put != nil {
58+
out = append(out, spectrum.EndpointString(http.MethodPut, k))
59+
}
60+
}
61+
sort.Strings(out)
62+
return out
63+
}
64+
65+
func (more *SpecMore) ModelNames() []string {
66+
var out []string
67+
for k := range more.Spec.Definitions {
68+
out = append(out, k)
69+
}
70+
sort.Strings(out)
71+
return out
72+
}
73+
74+
func (more *SpecMore) PathNames() []string {
75+
var out []string
76+
for k := range more.Spec.Paths {
77+
out = append(out, k)
78+
}
79+
sort.Strings(out)
80+
return out
81+
}

0 commit comments

Comments
 (0)