-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathraml.go
233 lines (202 loc) · 5.96 KB
/
raml.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package raml
import (
"container/list"
"context"
"fmt"
"reflect"
)
type HookKey string
// RAML is a store for all fragments and shapes.
// WARNING: Not thread-safe
type RAML struct {
fragmentsCache map[string]Fragment // Library, NamedExample, DataType
fragmentTypes map[string]map[string]*BaseShape
fragmentAnnotationTypes map[string]map[string]*BaseShape
// entryPoint is a Library, NamedExample or DataType fragment that is used as an entry point for the resolution.
entryPoint Fragment
// basePath string
// May be reused for both validation and resolution.
domainExtensions []*DomainExtension
shapes []*BaseShape
// Temporary storage for unresolved shapes.
unresolvedShapes list.List
// idCounter is a counter for generating unique IDs per raml
idCounter int64
// ctx is a context of the RAML, for future use.
ctx context.Context
}
type HookFunc func(ctx context.Context, r *RAML, params ...any) error
func (r *RAML) getHooks(key HookKey) []HookFunc {
if r.ctx == nil {
r.ctx = context.Background()
}
hooks, ok := r.ctx.Value(key).([]HookFunc)
if !ok {
return []HookFunc{}
}
if hooks == nil {
return []HookFunc{}
}
return hooks
}
func (r *RAML) setHooks(key HookKey, hooks []HookFunc) {
if r.ctx == nil {
r.ctx = context.Background()
}
r.ctx = context.WithValue(r.ctx, key, hooks)
}
func (r *RAML) AppendHook(key HookKey, hook HookFunc) {
hooks := r.getHooks(key)
hooks = append(hooks, hook)
r.setHooks(key, hooks)
}
func (r *RAML) PrependHook(key HookKey, hook HookFunc) {
hooks := r.getHooks(key)
hooks = append([]HookFunc{hook}, hooks...)
r.setHooks(key, hooks)
}
func (r *RAML) RemoveHook(key HookKey, hook HookFunc) {
hooks := r.getHooks(key)
for i, h := range hooks {
if reflect.ValueOf(h).Pointer() == reflect.ValueOf(hook).Pointer() {
hooks = append(hooks[:i], hooks[i+1:]...)
break
}
}
r.setHooks(key, hooks)
}
func (r *RAML) ClearHooks(key HookKey) {
r.setHooks(key, []HookFunc{})
}
func (r *RAML) callHooks(key HookKey, params ...any) error {
if r == nil {
return nil
}
hooks := r.getHooks(key)
for _, hook := range hooks {
if hook == nil {
continue
}
err := hook(r.ctx, r, params...)
if err != nil {
return err
}
}
return nil
}
// EntryPoint returns the entry point of the RAML.
func (r *RAML) EntryPoint() Fragment {
return r.entryPoint
}
// SetEntryPoint sets the entry point of the RAML.
func (r *RAML) SetEntryPoint(entryPoint Fragment) *RAML {
r.entryPoint = entryPoint
return r
}
// GetLocation returns the location of the RAML.
func (r *RAML) GetLocation() string {
if r.entryPoint == nil {
return ""
}
return r.entryPoint.GetLocation()
}
// GetAllAnnotationsPtr returns all annotations as pointers.
func (r *RAML) GetAllAnnotationsPtr() []*DomainExtension {
var annotations []*DomainExtension
return append(annotations, r.domainExtensions...)
}
// GetAllAnnotations returns all annotations.
func (r *RAML) GetAllAnnotations() []DomainExtension {
var annotations []DomainExtension
for _, de := range r.domainExtensions {
annotations = append(annotations, *de)
}
return annotations
}
// New creates a new RAML.
func New(ctx context.Context) *RAML {
return &RAML{
fragmentTypes: make(map[string]map[string]*BaseShape),
fragmentAnnotationTypes: make(map[string]map[string]*BaseShape),
fragmentsCache: make(map[string]Fragment),
domainExtensions: make([]*DomainExtension, 0),
ctx: ctx,
}
}
// Shapes returns all shapes.
func (r *RAML) GetShapes() []*BaseShape {
return r.shapes
}
func (r *RAML) PutShape(shape *BaseShape) {
r.shapes = append(r.shapes, shape)
}
// GetFragmentTypePtrs returns fragment shapes as pointers.
func (r *RAML) GetFragmentTypePtrs(location string) map[string]*BaseShape {
return r.fragmentTypes[location]
}
// GetTypeFromFragmentPtr returns a shape from a fragment as a pointer.
func (r *RAML) GetTypeFromFragmentPtr(location string, typeName string) (*BaseShape, error) {
loc, ok := r.fragmentTypes[location]
if !ok {
return nil, fmt.Errorf("location %s not found", location)
}
return loc[typeName], nil
}
// PutTypeIntoFragment puts a shape into a fragment.
func (r *RAML) PutTypeIntoFragment(name string, location string, shape *BaseShape) {
loc, ok := r.fragmentTypes[location]
if !ok {
loc = make(map[string]*BaseShape)
r.fragmentTypes[location] = loc
}
loc[name] = shape
}
// GetTypeFromFragmentPtr returns a shape from a fragment.
func (r *RAML) GetAnnotationTypeFromFragmentPtr(location string, typeName string) (*BaseShape, error) {
loc, ok := r.fragmentAnnotationTypes[location]
if !ok {
return nil, fmt.Errorf("location %s not found", location)
}
return loc[typeName], nil
}
// PutTypeIntoFragment puts a shape into a fragment.
func (r *RAML) PutAnnotationTypeIntoFragment(name string, location string, shape *BaseShape) {
loc, ok := r.fragmentAnnotationTypes[location]
if !ok {
loc = make(map[string]*BaseShape)
r.fragmentAnnotationTypes[location] = loc
}
loc[name] = shape
}
// GetFragment returns a fragment.
func (r *RAML) GetFragment(location string) Fragment {
return r.fragmentsCache[location]
}
// PutFragment puts a fragment.
func (r *RAML) PutFragment(location string, fragment Fragment) {
if _, ok := r.fragmentsCache[location]; !ok {
r.fragmentsCache[location] = fragment
}
}
func (r *RAML) GetReferencedType(refName string, location string) (*BaseShape, error) {
frag := r.GetFragment(location)
if frag == nil {
return nil, fmt.Errorf("fragment not found")
}
ref, err := frag.GetReferenceType(refName)
if err != nil {
return nil, fmt.Errorf("get reference type: %s: %w", refName, err)
}
return ref, nil
}
func (r *RAML) GetReferencedAnnotationType(refName string, location string) (*BaseShape, error) {
frag := r.GetFragment(location)
if frag == nil {
return nil, fmt.Errorf("fragment not found")
}
ref, err := frag.GetReferenceAnnotationType(refName)
if err != nil {
return nil, fmt.Errorf("get reference annotation type: %s: %w", refName, err)
}
return ref, nil
}