-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathmethod.go
492 lines (436 loc) · 12.2 KB
/
method.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package code
import (
"fmt"
"regexp"
"strings"
"github.com/databricks/databricks-sdk-go/openapi"
)
// Method represents service RPC
type Method struct {
Named
Service *Service
// HTTP method name
Verb string
// Full API Path, including /api/2.x prefix
Path string
// Slice of path params, e.g. permissions/{type}/{id}
PathParts []PathPart
// Request type representation
Request *Entity
// Response type representation
Response *Entity
// The style of the request, either "rpc" or "rest". See the documentation on
// Operation for more details.
PathStyle openapi.PathStyle
// For list APIs, the path of fields in the response entity to follow to get
// the resource ID.
IdFieldPath []*Field
// For list APIs, the path of fields in the response entity to follow to get
// the user-friendly name of the resource.
NameFieldPath []*Field
// If not nil, the field in the request and reponse entities that should be
// mapped to the request/response body.
RequestBodyField *Field
ResponseBodyField *Field
// Expected content type of the request and response
FixedRequestHeaders map[string]string
wait *openapi.Wait
pagination *openapi.Pagination
Operation *openapi.Operation
shortcut bool
}
// Shortcut holds definition of "shortcut" methods, that are generated for
// methods with request entities only with required fields.
type Shortcut struct {
Named
Params []Field
Method *Method
}
// Pagination holds definition of result iteration type per specific RPC.
// Databricks as of now has a couple different types of pagination:
// - next_token/next_page_token + repeated field
// - offset/limit with zero-based offsets + repeated field
// - page/limit with 1-based pages + repeated field
// - repeated inline field
// - repeated field
type Pagination struct {
Offset *Field
Limit *Field
Results *Field
Entity *Entity
Token *Binding
Increment int
}
// NamedIdMap depends on Pagination and is generated, when paginated item
// entity has Identifier and Name fields. End-users usually use this method for
// drop-downs or any other selectors.
type NamedIdMap struct {
Named
IdPath []*Field
NamePath []*Field
Entity *Entity
// if List method returns []Item directly
// without generated iteration wrapper
Direct bool
}
// PathPart represents required field, that is always part of the path
type PathPart struct {
Prefix string
Field *Field
IsAccountId bool
}
var pathPairRE = regexp.MustCompile(`(?m)([^\{]+)(\{(\w+)\})?`)
func (m *Method) pathParams() (params []Field) {
if len(m.PathParts) == 0 {
return nil
}
if !(m.Verb == "GET" || m.Verb == "DELETE" || m.Verb == "HEAD") {
return nil
}
for _, part := range m.PathParts {
if part.Field == nil {
continue
}
params = append(params, *part.Field)
}
return params
}
func (m *Method) ResponseHeaders() (headers []Field) {
if m.Response == nil {
return headers
}
for _, field := range m.Response.Fields() {
if field.IsHeader {
headers = append(headers, *field)
}
}
return headers
}
func (m *Method) allowShortcut() bool {
if m.shortcut {
return true
}
// We do not generate proper pagination (e.g. iterators) for shortcuts,
// so don't emit shortcuts if we have pagination.
if m.pagination != nil {
return false
}
if m.PathStyle == openapi.PathStyleRpc {
return true
}
return false
}
func (m *Method) rpcSingleFields() (params []Field) {
if !m.allowShortcut() {
return nil
}
if m.Request == nil {
return nil
}
if len(m.Request.fields) != 1 {
// TODO: explicitly annotate with x-databricks-shortcut
return nil
}
return []Field{*m.Request.Fields()[0]}
}
func (m *Method) requestShortcutFields() []Field {
pathParams := m.pathParams()
rpcFields := m.rpcSingleFields()
if len(pathParams) == 0 && len(rpcFields) == 0 {
return nil
}
if len(pathParams) > 0 {
return pathParams
}
return rpcFields
}
// Shortcut creates definition from path params and single-field request entities
func (m *Method) Shortcut() *Shortcut {
params := m.requestShortcutFields()
if len(params) == 0 {
return nil
}
nameParts := []string{}
for _, part := range params {
nameParts = append(nameParts, part.PascalName())
}
name := fmt.Sprintf("%sBy%s", m.PascalName(), strings.Join(nameParts, "And"))
return &Shortcut{
Named: Named{name, ""},
Method: m,
Params: params,
}
}
func (m *Method) IsCrudRead() bool {
return m.Operation.Crud == "read"
}
func (m *Method) IsCrudCreate() bool {
return m.Operation.Crud == "create"
}
func (m *Method) IsJsonOnly() bool {
return m.Operation.JsonOnly
}
// MustUseJson indicates whether we have to use
// JSON input to set all required fields for request.
// If we can do so, it means we can only use JSON input passed via --json flag.
func (m *Method) MustUseJson() bool {
// method supports only JSON input
if m.IsJsonOnly() {
return true
}
// if not all required fields are primitive, then fields must be provided in JSON
if m.Request != nil && !m.Request.IsAllRequiredFieldsPrimitive() {
return true
}
// if request is a map, then we have to use JSON input
if m.Request != nil && m.Request.IsMap() {
return true
}
return false
}
// CanUseJson indicates whether the generated command supports --json flag.
// It happens when either method has to use JSON input or not all fields in request
// are primitives. Because such fields are not required, the command has not but
// should support JSON input.
func (m *Method) CanUseJson() bool {
return m.MustUseJson() || (m.Request != nil && m.Request.HasJsonField())
}
func (m *Method) HasRequiredPositionalArguments() bool {
if m.Request == nil {
return false
}
e := m.Request
return e.HasRequiredPathFields() || (!m.MustUseJson() && e.IsAllRequiredFieldsPrimitive())
}
func (m *Method) RequiredPositionalArguments() (fields []*Field) {
if m.Request == nil {
return nil
}
e := m.Request
// Path fields are always required as positional arguments
posArgs := e.RequiredPathFields()
if !m.MustUseJson() && e.IsAllRequiredFieldsPrimitive() {
for _, f := range e.RequiredFields() {
if f.IsPath {
continue
}
posArgs = append(posArgs, f)
}
}
return posArgs
}
func (m *Method) HasIdentifierField() bool {
return len(m.IdFieldPath) > 0
}
func (m *Method) IdentifierField() *Field {
if !m.HasIdentifierField() {
return nil
}
return m.IdFieldPath[len(m.IdFieldPath)-1]
}
func (m *Method) HasNameField() bool {
return len(m.NameFieldPath) > 0
}
// Wait returns definition for long-running operation
func (m *Method) Wait() *Wait {
if m.wait == nil {
return nil
}
// here it's easier to follow the snake_case, as success states are already
// in the CONSTANT_CASE and it's easier to convert from constant to snake,
// than from constant to camel or pascal.
name := m.Service.Singular().SnakeName()
success := strings.ToLower(strings.Join(m.wait.Success, "_or_"))
getStatus, ok := m.Service.methods[m.wait.Poll]
if !ok {
panic(fmt.Errorf("cannot find %s.%s", m.Service.Name, m.wait.Poll))
}
name = fmt.Sprintf("wait_%s_%s_%s", getStatus.SnakeName(), name, success)
return &Wait{
Named: Named{
Name: name,
},
Method: m,
}
}
// Pagination returns definition for possibly multi-request result iterator
func (m *Method) Pagination() *Pagination {
if m.pagination == nil {
return nil
}
if m.Response.ArrayValue != nil {
// we assume that method already returns body-as-array
return nil
}
var token *Binding
if m.pagination.Token != nil {
token = &Binding{ // reuse the same datastructure as for waiters
PollField: m.Request.Field(m.pagination.Token.Request),
Bind: m.Response.Field(m.pagination.Token.Response),
}
}
offset := m.Request.Field(m.pagination.Offset)
limit := m.Request.Field(m.pagination.Limit)
results := m.Response.Field(m.pagination.Results)
if results == nil {
panic(fmt.Errorf("invalid results field '%v': %s",
m.pagination.Results, m.Operation.OperationId))
}
entity := results.Entity.ArrayValue
increment := m.pagination.Increment
return &Pagination{
Results: results,
Token: token,
Entity: entity,
Offset: offset,
Limit: limit,
Increment: increment,
}
}
func (m *Method) paginationItem() *Entity {
if m.pagination == nil {
return nil
}
if m.Response.ArrayValue != nil {
// we assume that method already returns body-as-array
return m.Response.ArrayValue
}
p := m.Pagination()
return p.Entity
}
func (m *Method) NeedsOffsetDedupe() bool {
p := m.Pagination()
return p.Offset != nil && m.HasIdentifierField()
}
func (p *Pagination) MultiRequest() bool {
return p.Offset != nil || p.Token != nil
}
// NamedIdMap returns name-to-id mapping retrieval definition for all
// entities of a type
func (m *Method) NamedIdMap() *NamedIdMap {
entity := m.paginationItem()
if entity == nil || !m.HasIdentifierField() || !m.HasNameField() {
return nil
}
namePath := m.NameFieldPath
nameParts := []string{entity.PascalName()}
for _, v := range namePath {
nameParts = append(nameParts, v.PascalName())
}
nameParts = append(nameParts, "To")
nameParts = append(nameParts, m.IdentifierField().PascalName())
nameParts = append(nameParts, "Map")
return &NamedIdMap{
Named: Named{
Name: strings.Join(nameParts, ""),
},
IdPath: m.IdFieldPath,
NamePath: namePath,
Entity: entity,
Direct: m.Response.ArrayValue != nil,
}
}
func (n *NamedIdMap) Id() *Field {
return n.IdPath[len(n.IdPath)-1]
}
// GetByName returns entity from the same service with x-databricks-crud:read
func (m *Method) GetByName() *Entity {
n := m.NamedIdMap()
if n == nil {
return nil
}
potentialName := "GetBy"
for _, v := range n.NamePath {
potentialName += v.PascalName()
}
for _, other := range m.Service.methods {
shortcut := other.Shortcut()
if shortcut == nil {
continue
}
if shortcut.PascalName() == potentialName {
// we already have the shortcut
return nil
}
}
return n.Entity
}
func (m *Method) CanHaveResponseBody() bool {
return m.TitleVerb() == "Get" || m.TitleVerb() == "Post"
}
func (m *Method) TitleVerb() string {
return strings.Title(strings.ToLower(m.Verb))
}
// IsPrivatePreview flags object being in private preview.
func (m *Method) IsPrivatePreview() bool {
return isPrivatePreview(&m.Operation.Node)
}
// IsPublicPreview flags object being in public preview.
func (m *Method) IsPublicPreview() bool {
return isPublicPreview(&m.Operation.Node)
}
func (m *Method) AsFlat() *Named {
if m.PascalName() == "CreateOboToken" {
return &m.Named
}
methodWords := m.Named.splitASCII()
svc := m.Service.Named
remap := map[string]string{
"ModelRegistry": "Models",
"Libraries": "ClusterLibraries",
"PolicyFamilies": "ClusterPolicyFamilies",
"Workspace": "Notebooks", // or WorkspaceObjects
"OAuthEnrollment": "OauthEnrollment",
"CurrentUser": "",
}
if replace, ok := remap[svc.PascalName()]; ok {
svc = Named{
Name: replace,
}
}
serviceWords := svc.splitASCII()
serviceSingularWords := svc.Singular().splitASCII()
words := []string{}
if len(methodWords) == 1 && strings.ToLower(methodWords[0]) == "list" {
words = append(words, "list")
words = append(words, serviceWords...)
} else if methodWords[0] == "execute" {
words = append(words, methodWords[0])
// command_execution.execute -> execute-command-execution
words = append(words, serviceWords[0])
} else {
words = append(words, methodWords[0])
words = append(words, serviceSingularWords...)
}
words = append(words, methodWords[1:]...)
// warehouses.get_workspace_warehouse_config -> get-warehouse-workspace-config
seen := map[string]bool{}
tmp := []string{}
for _, w := range words {
if seen[w] {
continue
}
tmp = append(tmp, w)
seen[w] = true
}
return &Named{
Name: strings.Join(tmp, "_"),
}
}
func (m *Method) CmdletName(prefix string) string {
words := m.AsFlat().splitASCII()
noun := &Named{
Name: strings.Join(words[1:], "_"),
}
verb := strings.Title(words[0])
prefix = strings.Title(prefix)
return fmt.Sprintf("%s-%s%s", verb, prefix, noun.PascalName())
}
func (m *Method) IsRequestByteStream() bool {
contentType, ok := m.FixedRequestHeaders["Content-Type"]
return m.Request != nil && ok && contentType != string(openapi.MimeTypeJson)
}
func (m *Method) IsResponseByteStream() bool {
accept, ok := m.FixedRequestHeaders["Accept"]
return m.Response != nil && ok && accept != string(openapi.MimeTypeJson)
}