-
Notifications
You must be signed in to change notification settings - Fork 0
/
refs.go
157 lines (126 loc) · 3.47 KB
/
refs.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package anysdk
import (
"context"
"fmt"
"strings"
"github.com/getkin/kin-openapi/jsoninfo"
"github.com/getkin/kin-openapi/openapi3"
"github.com/go-openapi/jsonpointer"
)
type OperationRef struct {
Ref string `json:"$ref" yaml:"$ref"`
Value *openapi3.Operation
}
func (opr OperationRef) ExtractPathItem() string {
return opr.extractPathItem()
}
func (opr OperationRef) extractPathItem() string {
s := opr.extractFragment()
elems := strings.Split(strings.TrimPrefix(s, "/paths/"), "/")
toUse := elems
if len(elems) > 1 {
toUse = elems[0 : len(elems)-1]
}
s2 := strings.Join(toUse, "/")
return strings.ReplaceAll(s2, "~1", "/")
}
func (opr OperationRef) ExtractMethodItem() string {
return opr.extractMethodItem()
}
func (opr OperationRef) extractMethodItem() string {
return extractSuffix(opr.Ref)
}
func (opr OperationRef) ExtractServiceDocPath() string {
return opr.extractServiceDocPath()
}
func (opr OperationRef) extractServiceDocPath() string {
s := opr.Ref
elems := strings.Split(s, "#")
if len(elems) > 1 {
return elems[0]
}
return s
}
func extractFragment(s string) string {
if strings.HasPrefix(s, "#") {
return s[1:]
}
elems := strings.Split(s, "#")
if len(elems) > 2 {
return strings.Join(elems[2:], "#")
}
return elems[len(elems)-1]
}
func extractSuffix(s string) string {
sf := extractFragment(s)
elems := strings.Split(sf, "/")
return elems[len(elems)-1]
}
func (opr OperationRef) extractFragment() string {
return extractFragment(opr.Ref)
}
type OperationStoreRef struct {
Ref string `json:"$ref" yaml:"$ref"`
Value *standardOperationStore
}
func (osr *OperationStoreRef) hasValue() bool {
return osr.Value != nil
}
func (osr *OperationStoreRef) extractMethodItem() string {
return extractSuffix(osr.Ref)
}
type PathItemRef struct {
Ref string `json:"$ref" yaml:"$ref"`
Value *openapi3.PathItem
}
type ServiceRef struct {
Ref string `json:"$ref" yaml:"$ref"`
Value *standardService
}
type ResourcesRef struct {
Ref string `json:"$ref" yaml:"$ref"`
Value *standardResourceRegister
}
var _ jsonpointer.JSONPointable = (*OperationRef)(nil)
func (value *OperationRef) MarshalJSON() ([]byte, error) {
return jsoninfo.MarshalRef(value.Ref, value.Value)
}
func (value *OperationRef) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalRef(data, &value.Ref, &value.Value)
}
func (value *OperationRef) Validate(ctx context.Context) error {
if v := value.Value; v != nil {
return v.Validate(ctx)
}
return foundUnresolvedRef(value.Ref)
}
func (value OperationRef) JSONLookup(token string) (interface{}, error) {
if token == "$ref" {
return value.Ref, nil
}
ptr, _, err := jsonpointer.GetForToken(value.Value, token)
return ptr, err
}
var _ jsonpointer.JSONPointable = (*OperationStoreRef)(nil)
func (value *OperationStoreRef) MarshalJSON() ([]byte, error) {
return jsoninfo.MarshalRef(value.Ref, value.Value)
}
func (value *OperationStoreRef) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalRef(data, &value.Ref, &value.Value)
}
// func (value *OperationStoreRef) Validate(ctx context.Context) error {
// if v := value.Value; v != nil {
// return v.Validate(ctx)
// }
// return foundUnresolvedRef(value.Ref)
// }
func (value OperationStoreRef) JSONLookup(token string) (interface{}, error) {
if token == "$ref" {
return value.Ref, nil
}
ptr, _, err := jsonpointer.GetForToken(value.Value, token)
return ptr, err
}
func foundUnresolvedRef(ref string) error {
return fmt.Errorf("found unresolved ref: %q", ref)
}