-
Notifications
You must be signed in to change notification settings - Fork 5
/
ref_index.go
66 lines (57 loc) · 1.59 KB
/
ref_index.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
package vervet
import (
"reflect"
"github.com/getkin/kin-openapi/openapi3"
"github.com/mitchellh/reflectwalk"
)
// RefIndex indexes the distinct references used in an OpenAPI document.
type RefIndex struct {
refs map[string]struct{}
}
// NewRefIndex returns a new reference index on an OpenAPI document.
func NewRefIndex(doc *openapi3.T) (*RefIndex, error) {
ix := &RefIndex{refs: map[string]struct{}{}}
if err := ix.index(doc); err != nil {
return nil, err
}
return ix, nil
}
func (ix *RefIndex) index(doc *openapi3.T) error {
return reflectwalk.Walk(doc, ix)
}
// HasRef returns whether the indexed document contains the given ref.
func (ix *RefIndex) HasRef(ref string) bool {
_, ok := ix.refs[ref]
return ok
}
// Struct implements reflectwalk.StructWalker
func (ix *RefIndex) Struct(v reflect.Value) error {
if !v.CanInterface() {
return nil
}
switch val := v.Addr().Interface().(type) {
case *openapi3.SchemaRef:
ix.refs[val.Ref] = struct{}{}
case *openapi3.ParameterRef:
ix.refs[val.Ref] = struct{}{}
case *openapi3.HeaderRef:
ix.refs[val.Ref] = struct{}{}
case *openapi3.RequestBodyRef:
ix.refs[val.Ref] = struct{}{}
case *openapi3.ResponseRef:
ix.refs[val.Ref] = struct{}{}
case *openapi3.SecuritySchemeRef:
ix.refs[val.Ref] = struct{}{}
case *openapi3.ExampleRef:
ix.refs[val.Ref] = struct{}{}
case *openapi3.LinkRef:
ix.refs[val.Ref] = struct{}{}
case *openapi3.CallbackRef:
ix.refs[val.Ref] = struct{}{}
}
return nil
}
// StructField implements reflectwalk.StructWalker
func (*RefIndex) StructField(field reflect.StructField, v reflect.Value) error {
return nil
}