-
Notifications
You must be signed in to change notification settings - Fork 0
/
providerService.go
387 lines (344 loc) · 10.6 KB
/
providerService.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package anysdk
import (
"fmt"
"reflect"
"github.com/getkin/kin-openapi/jsoninfo"
"github.com/getkin/kin-openapi/openapi3"
"github.com/stackql/stackql-parser/go/sqltypes"
)
var (
_ ProviderService = &standardProviderService{}
_ ITable = &standardProviderService{}
)
type ProviderService interface {
ITable
GetQueryTransposeAlgorithm() string
GetProvider() (Provider, bool)
GetService() (Service, error)
GetRequestTranslateAlgorithm() string
GetResourcesShallow() (ResourceRegister, error)
GetPaginationRequestTokenSemantic() (TokenSemantic, bool)
GetPaginationResponseTokenSemantic() (TokenSemantic, bool)
ConditionIsValid(lhs string, rhs interface{}) bool
GetID() string
GetServiceFragment(resourceKey string) (Service, error)
GetResourcesRefRef() string
PeekServiceFragment(resourceKey string) (Service, bool)
SetServiceRefVal(Service) bool
IsPreferred() bool
GetTitle() string
GetVersion() string
GetDescription() string
//
getResourcesShallowWithRegistry(registry RegistryAPI) (ResourceRegister, error)
getServiceRefRef() string
getResourcesRefRef() string
setService(svc Service)
getServiceWithRegistry(registry RegistryAPI) (Service, error)
getServiceDocRef(rr ResourceRegister, rsc Resource) ServiceRef
setProvider(provider Provider)
}
type standardProviderService struct {
openapi3.ExtensionProps
ID string `json:"id" yaml:"id"` // Required
Name string `json:"name" yaml:"name"` // Required
Title string `json:"title" yaml:"title"` // Required
Version string `json:"version" yaml:"version"` // Required
Description string `json:"description" yaml:"description"`
Preferred bool `json:"preferred" yaml:"preferred"`
ServiceRef *ServiceRef `json:"service,omitempty" yaml:"service,omitempty"` // will be lazy evaluated
ResourcesRef *ResourcesRef `json:"resources,omitempty" yaml:"resources,omitempty"` // will be lazy evaluated
Provider Provider `json:"-" yaml:"-"` // upwards traversal
StackQLConfig *standardStackQLConfig `json:"config,omitempty" yaml:"config,omitempty"`
Service Service `json:"-" yaml:"-"`
}
func NewEmptyProviderService() ProviderService {
return &standardProviderService{}
}
func (sv *standardProviderService) GetTitle() string {
return sv.Title
}
func (sv *standardProviderService) GetVersion() string {
return sv.Version
}
func (sv *standardProviderService) GetDescription() string {
return sv.Description
}
func (sv *standardProviderService) IsPreferred() bool {
return sv.Preferred
}
func (sv *standardProviderService) SetServiceRefVal(svc Service) bool {
switch svc := svc.(type) {
case *standardService:
sv.ServiceRef.Value = svc
return true
default:
return false
}
}
func (sv *standardProviderService) setProvider(provider Provider) {
sv.Provider = provider
}
func (sv *standardProviderService) GetID() string {
return sv.ID
}
func (sv *standardProviderService) setService(svc Service) {
sv.Service = svc
}
func (sv *standardProviderService) getServiceRefRef() string {
if sv.ServiceRef == nil {
return ""
}
return sv.ServiceRef.Ref
}
func (sv *standardProviderService) GetResourcesRefRef() string {
return sv.getResourcesRefRef()
}
func (sv *standardProviderService) getResourcesRefRef() string {
if sv.ResourcesRef == nil {
return ""
}
return sv.ResourcesRef.Ref
}
func (sv *standardProviderService) GetProvider() (Provider, bool) {
return sv.Provider, sv.Provider != nil
}
func (sv *standardProviderService) GetQueryTransposeAlgorithm() string {
if sv.StackQLConfig != nil {
qt, qtExists := sv.StackQLConfig.GetQueryTranspose()
if qtExists {
return qt.GetAlgorithm()
}
}
return ""
}
func (sv *standardProviderService) GetRequestTranslateAlgorithm() string {
if sv.StackQLConfig == nil || sv.StackQLConfig.RequestTranslate == nil {
return ""
}
return sv.StackQLConfig.RequestTranslate.Algorithm
}
func (sv *standardProviderService) GetPaginationRequestTokenSemantic() (TokenSemantic, bool) {
if sv.StackQLConfig == nil || sv.StackQLConfig.Pagination == nil || sv.StackQLConfig.Pagination.RequestToken == nil {
return nil, false
}
return sv.StackQLConfig.Pagination.RequestToken, true
}
func (sv *standardProviderService) GetPaginationResponseTokenSemantic() (TokenSemantic, bool) {
if sv.StackQLConfig == nil || sv.StackQLConfig.Pagination == nil || sv.StackQLConfig.Pagination.ResponseToken == nil {
return nil, false
}
return sv.StackQLConfig.Pagination.ResponseToken, true
}
func (sv *standardProviderService) ConditionIsValid(lhs string, rhs interface{}) bool {
elem := sv.ToMap()[lhs]
return reflect.TypeOf(elem) == reflect.TypeOf(rhs)
}
func extractService(ps ProviderService) (Service, error) {
b, err := getServiceDocBytes(ps.getServiceRefRef())
if err != nil {
return nil, err
}
return LoadServiceDocFromBytes(ps, b)
}
func getResourcesShallow(ps ProviderService) (ResourceRegister, error) {
b, err := getServiceDocBytes(ps.getResourcesRefRef())
if err != nil {
return nil, err
}
return loadResourcesShallow(ps, b)
}
func (pr *standardProviderService) MarshalJSON() ([]byte, error) {
return jsoninfo.MarshalStrictStruct(pr)
}
func (pr *standardProviderService) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalStrictStruct(data, pr)
}
func (ps *standardProviderService) FilterBy(predicate func(interface{}) (ITable, error)) (ITable, error) {
return predicate(ps)
}
func (ps *standardProviderService) ToMap() map[string]interface{} {
retVal := make(map[string]interface{})
retVal["id"] = ps.ID
retVal["name"] = ps.Name
retVal["title"] = ps.Title
retVal["description"] = ps.Description
retVal["version"] = ps.Version
return retVal
}
func (ps *standardProviderService) GetKeyAsSqlVal(lhs string) (sqltypes.Value, error) {
val, ok := ps.ToMap()[lhs]
rv, err := InterfaceToSQLType(val)
if !ok {
return rv, fmt.Errorf("key '%s' no preset in providerService", lhs)
}
return rv, err
}
func (ps *standardProviderService) GetKey(lhs string) (interface{}, error) {
val, ok := ps.ToMap()[lhs]
if !ok {
return nil, fmt.Errorf("key '%s' no preset in providerService", lhs)
}
return val, nil
}
func (ps *standardProviderService) getServiceWithRegistry(registry RegistryAPI) (Service, error) {
if ps.ServiceRef.Value != nil {
return ps.ServiceRef.Value, nil
}
if registry != nil {
return registry.GetService(ps)
}
svc, err := extractService(ps)
if err != nil {
return nil, err
}
ps.Service = svc
return ps.Service, nil
}
func (ps *standardProviderService) GetService() (Service, error) {
if ps.Service != nil {
return ps.Service, nil
}
if ps.ServiceRef.Value != nil {
return ps.ServiceRef.Value, nil
}
svc, err := extractService(ps)
if err != nil {
return nil, err
}
ps.Service = svc
return ps.Service, nil
}
func (ps *standardProviderService) extractService() (Service, error) {
if ps.ServiceRef.Value != nil {
return ps.ServiceRef.Value, nil
}
svc, err := extractService(ps)
if err != nil {
return nil, err
}
ps.Service = svc
return ps.Service, nil
}
func (ps *standardProviderService) getServiceDocRef(rr ResourceRegister, rsc Resource) ServiceRef {
var rv ServiceRef
if ps.ServiceRef != nil && ps.ServiceRef.Ref != "" {
rv = *ps.ServiceRef
}
if rr.GetServiceDocPath() != nil && rr.GetServiceDocPath().Ref != "" {
rv = *(rr.GetServiceDocPath())
}
if rsc.GetServiceDocPath() != nil && rsc.GetServiceDocPath().Ref != "" {
rv = *(rsc.GetServiceDocPath())
}
return rv
}
func (ps *standardProviderService) GetServiceFragment(resourceKey string) (Service, error) {
if ps.ResourcesRef == nil || ps.ResourcesRef.Ref == "" {
return ps.GetService()
}
rr, err := ps.GetResourcesShallow()
if err != nil {
return nil, err
}
rsc, ok := rr.GetResource(resourceKey)
if !ok {
return nil, fmt.Errorf("cannot locate resource for key = '%s'", resourceKey)
}
sdRef := ps.getServiceDocRef(rr, rsc)
if sdRef.Ref == "" {
return nil, fmt.Errorf("no service doc available for resourceKey = '%s'", resourceKey)
}
if sdRef.Value != nil {
return sdRef.Value, nil
}
sb, err := getServiceDocBytes(sdRef.Ref)
if err != nil {
return nil, err
}
svc, err := LoadServiceSubsetDocFromBytes(rr, resourceKey, sb)
if err != nil {
return nil, err
}
ps.Service = svc
return ps.Service, nil
}
func (ps *standardProviderService) PeekServiceFragment(resourceKey string) (Service, bool) {
if ps.ServiceRef == nil || ps.ServiceRef.Value == nil || ps.ServiceRef.Value.rsc == nil {
return nil, false
}
_, ok := ps.ServiceRef.Value.rsc[resourceKey]
if !ok {
return nil, false
}
return ps.ServiceRef.Value, true
}
func (ps *standardProviderService) getResourcesShallowWithRegistry(registry RegistryAPI) (ResourceRegister, error) {
if ps.ResourcesRef == nil || ps.ResourcesRef.Ref == "" {
if ps.ServiceRef != nil || ps.ServiceRef.Ref != "" {
svc, err := ps.getServiceWithRegistry(registry)
if err != nil {
return nil, err
}
resources, err := svc.GetResources()
rscCast := make(map[string]*standardResource, len(resources))
if err != nil {
return nil, err
}
for k, v := range resources {
rscCast[k] = v.(*standardResource)
}
rv := &standardResourceRegister{
ServiceDocPath: ps.ServiceRef,
Resources: rscCast,
}
return rv, nil
}
return nil, fmt.Errorf("cannot resolve shallow resources")
}
if ps.ResourcesRef.Value != nil {
return ps.ResourcesRef.Value, nil
}
if registry != nil {
return registry.GetResourcesShallowFromURL(ps)
}
return getResourcesShallow(ps)
}
func (ps *standardProviderService) GetResourcesShallow() (ResourceRegister, error) {
if ps.ResourcesRef == nil || ps.ResourcesRef.Ref == "" {
if ps.ServiceRef != nil || ps.ServiceRef.Ref != "" {
svc, err := ps.GetService()
if err != nil {
return nil, err
}
resources, err := svc.GetResources()
if err != nil {
return nil, err
}
rscCast := make(map[string]*standardResource, len(resources))
for k, v := range resources {
rscCast[k] = v.(*standardResource)
}
rv := &standardResourceRegister{
ServiceDocPath: ps.ServiceRef,
Resources: rscCast,
}
return rv, nil
}
return nil, fmt.Errorf("cannot resolve shallow resources")
}
if ps.ResourcesRef.Value != nil {
return ps.ResourcesRef.Value, nil
}
return getResourcesShallow(ps)
}
func (ps *standardProviderService) GetName() string {
return ps.Name
}
func (ps *standardProviderService) GetRequiredParameters() map[string]Addressable {
return nil
}
func (ps *standardProviderService) KeyExists(lhs string) bool {
_, ok := ps.ToMap()[lhs]
return ok
}