-
Notifications
You must be signed in to change notification settings - Fork 312
/
rpc.go
752 lines (652 loc) · 24 KB
/
rpc.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
package encoding
import (
"fmt"
"reflect"
"slices"
"sort"
"strings"
"github.com/cockroachdb/errors"
"github.com/golang/protobuf/proto"
"encr.dev/pkg/idents"
meta "encr.dev/proto/encore/parser/meta/v1"
schema "encr.dev/proto/encore/parser/schema/v1"
)
// ParameterLocation is the request/response home of the parameter
type ParameterLocation string
const (
Undefined ParameterLocation = "undefined" // Parameter location is Undefined
Header ParameterLocation = "header" // Parameter is placed in the HTTP header
Query ParameterLocation = "query" // Parameter is placed in the query string
Body ParameterLocation = "body" // Parameter is placed in the body
Cookie ParameterLocation = "cookie" // Parameter is placed in cookies
)
var (
QueryTag = tagDescription{
location: Query,
overrideDefault: true,
}
QsTag = QueryTag
HeaderTag = tagDescription{
location: Header,
overrideDefault: true,
wireFormatter: strings.ToLower,
}
JSONTag = tagDescription{
location: Body,
omitEmptyOption: "omitempty",
overrideDefault: false,
}
CookieTag = tagDescription{
location: Cookie,
omitEmptyOption: "omitempty",
overrideDefault: true,
}
)
// authTags is a description of tags used for auth
var authTags = map[string]tagDescription{
"query": QueryTag,
"header": HeaderTag,
"cookie": CookieTag,
}
// requestTags is a description of tags used for requests
var requestTags = map[string]tagDescription{
"query": QueryTag,
"qs": QsTag,
"header": HeaderTag,
"json": JSONTag,
}
// responseTags is a description of tags used for responses
var responseTags = map[string]tagDescription{
"header": HeaderTag,
"json": JSONTag,
}
// tagDescription is used to map struct field tags to param locations
// if overrideDefault is set, tagDescription.location will be used instead of encodingHints.defaultLocation
// if the tag matches the paramLocation, the param name will be replaced with the
// tag name
type tagDescription struct {
location ParameterLocation
overrideDefault bool
omitEmptyOption string
wireFormatter func(name string) string
}
// encodingHints is used to determine the default location and applicable tag overrides for http
// request/response encoding
type encodingHints struct {
defaultLocation ParameterLocation
tags map[string]tagDescription
options *Options
}
// RPCEncoding expresses how an RPC should be encoded on the wire for both the request and responses.
type RPCEncoding struct {
Name string `json:"name"`
Doc string `json:"doc"`
AccessType string `json:"access_type"`
Proto string `json:"proto"`
Path *meta.Path `json:"path"`
HttpMethods []string `json:"http_methods"`
DefaultMethod string `json:"default_method"`
// Expresses how the default request encoding and method should be
// Note: DefaultRequestEncoding.HTTPMethods will always be a slice with length 1
DefaultRequestEncoding *RequestEncoding `json:"request_encoding"`
// Expresses all the different ways the request can be encoded for this RPC
RequestEncoding []*RequestEncoding `json:"all_request_encodings"`
// Expresses how the response to this RPC will be encoded
ResponseEncoding *ResponseEncoding `json:"response_encoding"`
}
// RequestEncodingForMethod returns the request encoding required for the given HTTP method.
// If the method is not supported by the RPC it reports nil.
func (e *RPCEncoding) RequestEncodingForMethod(method string) *RequestEncoding {
var wildcardOption *RequestEncoding
for _, reqEnc := range e.RequestEncoding {
for _, m := range reqEnc.HTTPMethods {
if strings.EqualFold(m, method) {
return reqEnc
}
if m == "*" {
wildcardOption = reqEnc
}
}
}
return wildcardOption
}
// AuthEncoding expresses how a response should be encoded on the wire.
type AuthEncoding struct {
// LegacyTokenFormat specifies whether the auth encoding uses the legacy format of
// "just give us a token as a string". If true, the other parameters are all empty.
LegacyTokenFormat bool
// Contains metadata about how to marshal an HTTP parameter
HeaderParameters []*ParameterEncoding `json:"header_parameters"`
QueryParameters []*ParameterEncoding `json:"query_parameters"`
CookieParameters []*ParameterEncoding `json:"cookie_parameters"`
}
// ParameterEncodingMap returns the parameter encodings as a map, keyed by SrcName.
func (e *AuthEncoding) ParameterEncodingMap() map[string]*ParameterEncoding {
return toEncodingMap(srcNameKey, e.HeaderParameters, e.QueryParameters, e.CookieParameters)
}
// ParameterEncodingMapByName returns the parameter encodings as a map, keyed by Name.
// Conflicts result in an undefined encoding getting set.
func (e *AuthEncoding) ParameterEncodingMapByName() map[string][]*ParameterEncoding {
return toEncodingMultiMap(nameKey, e.HeaderParameters, e.QueryParameters, e.CookieParameters)
}
// ResponseEncoding expresses how a response should be encoded on the wire
type ResponseEncoding struct {
// Contains metadata about how to marshal an HTTP parameter
HeaderParameters []*ParameterEncoding `json:"header_parameters"`
BodyParameters []*ParameterEncoding `json:"body_parameters"`
}
// ParameterEncodingMap returns the parameter encodings as a map, keyed by SrcName.
func (e *ResponseEncoding) ParameterEncodingMap() map[string]*ParameterEncoding {
return toEncodingMap(srcNameKey, e.HeaderParameters, e.BodyParameters)
}
// ParameterEncodingMapByName returns the parameter encodings as a map, keyed by Name.
// Conflicts result in an undefined encoding getting set.
func (e *ResponseEncoding) ParameterEncodingMapByName() map[string][]*ParameterEncoding {
return toEncodingMultiMap(nameKey, e.HeaderParameters, e.BodyParameters)
}
// RequestEncoding expresses how a request should be encoded for an explicit set of HTTPMethods
type RequestEncoding struct {
// The HTTP methods these field configurations can be used for
HTTPMethods []string `json:"http_methods"`
// Contains metadata about how to marshal an HTTP parameter
HeaderParameters []*ParameterEncoding `json:"header_parameters"`
QueryParameters []*ParameterEncoding `json:"query_parameters"`
BodyParameters []*ParameterEncoding `json:"body_parameters"`
}
// ParameterEncodingMap returns the parameter encodings as a map, keyed by SrcName.
func (e *RequestEncoding) ParameterEncodingMap() map[string]*ParameterEncoding {
return toEncodingMap(srcNameKey, e.HeaderParameters, e.QueryParameters, e.BodyParameters)
}
// ParameterEncodingMapByName returns the parameter encodings as a map, keyed by Name.
// Conflicts result in an undefined encoding getting set.
func (e *RequestEncoding) ParameterEncodingMapByName() map[string][]*ParameterEncoding {
return toEncodingMultiMap(nameKey, e.HeaderParameters, e.QueryParameters, e.BodyParameters)
}
// ParameterEncoding expresses how a parameter should be encoded on the wire
type ParameterEncoding struct {
// The location specific name of the parameter (e.g. cheeseEater, cheese-eater, X-Cheese-Eater)
Name string `json:"name"`
// Location is the location this encoding is for.
Location ParameterLocation `json:"location"`
// OmitEmpty specifies whether the parameter should be omitted if it's empty.
OmitEmpty bool `json:"omit_empty"`
// SrcName is the name of the struct field
SrcName string `json:"src_name"`
// Doc is the documentation of the struct field
Doc string `json:"doc"`
// Type is the field's type description.
Type *schema.Type `json:"type"`
// RawTag specifies the raw, unparsed struct tag for the field.
RawTag string `json:"raw_tag"`
// WireFormat is the wire format of the parameter.
WireFormat string `json:"wire_format"`
// Optional indicates whether the field is optional.
Optional bool `json:"optional"`
}
type Options struct {
// SrcNameTag, if set, specifies which source tag should be used to determine
// the value of the SrcName field in the returned parameter descriptions.
//
// If the given SrcNameTag is not present on the field, SrcName will be set
// to the Go field name instead.
//
// If SrcNameTag is empty, SrcName is set to the Go field name.
SrcNameTag string
}
type APIEncoding struct {
Services []*ServiceEncoding `json:"services"`
Authorization *AuthEncoding `json:"authorization"`
}
type ServiceEncoding struct {
Name string `json:"name"`
Doc string `json:"doc"`
RPCs []*RPCEncoding `json:"rpcs"`
}
func DescribeAPI(meta *meta.Data) *APIEncoding {
api := &APIEncoding{Services: make([]*ServiceEncoding, len(meta.Svcs))}
for i, s := range meta.Svcs {
api.Services[i] = DescribeService(meta, s)
}
if meta.AuthHandler == nil {
return api
}
var err error
api.Authorization, err = DescribeAuth(meta, meta.AuthHandler.Params, nil)
if err != nil {
panic(fmt.Sprintf("Invalid auth definition: %s: %v", meta.AuthHandler.Name, err))
}
return api
}
func findDoc(relPath string, meta *meta.Data) string {
for _, p := range meta.Pkgs {
if p.RelPath == relPath {
return p.Doc
}
}
return ""
}
func DescribeService(meta *meta.Data, svc *meta.Service) *ServiceEncoding {
service := &ServiceEncoding{Name: svc.Name, Doc: findDoc(svc.RelPath, meta), RPCs: make([]*RPCEncoding, len(svc.Rpcs))}
for i, r := range svc.Rpcs {
rpc, err := DescribeRPC(meta, r, nil)
if err != nil {
panic(fmt.Sprintf("invalid rpc: %v", err))
}
service.RPCs[i] = rpc
}
return service
}
// DescribeRPC expresses how to encode an RPCs request and response objects for the wire.
func DescribeRPC(appMetaData *meta.Data, rpc *meta.RPC, options *Options) (*RPCEncoding, error) {
encoding := &RPCEncoding{
DefaultMethod: DefaultClientHttpMethod(rpc),
Name: rpc.Name,
AccessType: rpc.AccessType.String(),
Proto: rpc.Proto.String(),
Path: rpc.Path,
Doc: findDoc(rpc.Doc, appMetaData),
}
var err error
// Work out the request encoding
encoding.RequestEncoding, err = DescribeRequest(appMetaData, rpc.RequestSchema, options, rpc.HttpMethods...)
if err != nil {
return nil, errors.Wrap(err, "request encoding")
}
// Work out the response encoding
encoding.ResponseEncoding, err = DescribeResponse(appMetaData, rpc.ResponseSchema, options)
if err != nil {
return nil, errors.Wrap(err, "request encoding")
}
if encoding.RequestEncoding != nil {
// Setup the default request encoding
defaultEncoding := encoding.RequestEncodingForMethod(encoding.DefaultMethod)
encoding.DefaultRequestEncoding = &RequestEncoding{
HTTPMethods: []string{encoding.DefaultMethod},
HeaderParameters: defaultEncoding.HeaderParameters,
BodyParameters: defaultEncoding.BodyParameters,
QueryParameters: defaultEncoding.QueryParameters,
}
}
return encoding, nil
}
// GetConcreteStructType returns a construct Struct object for the given schema. This means any generic types
// in the struct will be resolved to their concrete types and there will be no generic parameters in the struct object.
// However, any nested structs may still contain generic types.
//
// If a nil schema is provided, a nil struct is returned.
func GetConcreteStructType(appDecls []*schema.Decl, typ *schema.Type, typeArgs []*schema.Type) (*schema.Struct, error) {
// dereference pointers
pointer := typ.GetPointer()
for pointer != nil {
typ = pointer.Base
pointer = typ.GetPointer()
}
typ, err := GetConcreteType(appDecls, typ, typeArgs)
if err != nil {
return nil, err
}
struc := typ.GetStruct()
if struc == nil {
return nil, errors.Newf("unsupported type %+v", reflect.TypeOf(typ.Typ))
}
return struc, nil
}
// GetConcreteType returns a concrete type for the given schema. This means any generic types
// in the top level type will be resolved to their concrete types and there will be no generic parameters in returned typ.
// However, any nested types may still contain generic types.
//
// If a nil schema is provided, a nil is returned.
func GetConcreteType(appDecls []*schema.Decl, originalType *schema.Type, typeArgs []*schema.Type) (*schema.Type, error) {
if originalType == nil {
// If there's no schema type, we want to shortcut
return nil, nil
}
switch typ := originalType.Typ.(type) {
case *schema.Type_Struct:
// If there are no type arguments, we've got a concrete type
if len(typeArgs) == 0 {
return originalType, nil
}
// Deep copy the original struct
struc, ok := proto.Clone(typ.Struct).(*schema.Struct)
if !ok {
return nil, errors.New("failed to clone struct")
}
// replace any type parameters with the type argument
for _, field := range struc.Fields {
field.Typ = resolveTypeParams(field.Typ, typeArgs)
}
return &schema.Type{Typ: &schema.Type_Struct{Struct: struc}}, nil
case *schema.Type_Map:
// If there are no type arguments, we've got a concrete type
if len(typeArgs) == 0 {
return originalType, nil
}
// Deep copy the original struct
mapType, ok := proto.Clone(typ.Map).(*schema.Map)
if !ok {
return nil, errors.New("failed to clone map")
}
return resolveTypeParams(&schema.Type{Typ: &schema.Type_Map{Map: mapType}}, typeArgs), nil
case *schema.Type_List:
// If there are no type arguments, we've got a concrete type
if len(typeArgs) == 0 {
return originalType, nil
}
// Deep copy the original struct
list, ok := proto.Clone(typ.List).(*schema.List)
if !ok {
return nil, errors.New("failed to clone list type")
}
// replace any type parameters with the type argument
return resolveTypeParams(&schema.Type{Typ: &schema.Type_List{List: list}}, typeArgs), nil
case *schema.Type_Pointer:
// If there are no type arguments, we've got a concrete type
if len(typeArgs) == 0 {
return originalType, nil
}
// Deep copy the original struct
pointer, ok := proto.Clone(typ.Pointer).(*schema.Pointer)
if !ok {
return nil, errors.New("failed to clone pointer type")
}
var err error
pointer.Base, err = GetConcreteType(appDecls, pointer.Base, typeArgs)
if err != nil {
return nil, err
}
// replace any type parameters with the type argument
return resolveTypeParams(&schema.Type{Typ: &schema.Type_Pointer{Pointer: pointer}}, typeArgs), nil
case *schema.Type_Config:
// If there are no type arguments, we've got a concrete type
if len(typeArgs) == 0 {
return originalType, nil
}
// Deep copy the original struct
config, ok := proto.Clone(typ.Config).(*schema.ConfigValue)
if !ok {
return nil, errors.New("failed to clone config type")
}
// replace any type parameters with the type argument
return resolveTypeParams(&schema.Type{Typ: &schema.Type_Config{Config: config}}, typeArgs), nil
case *schema.Type_Named:
decl := appDecls[typ.Named.Id]
return GetConcreteType(appDecls, decl.Type, typ.Named.TypeArguments)
case *schema.Type_Builtin:
return originalType, nil
default:
return nil, errors.Newf("unsupported type %+v", reflect.TypeOf(typ))
}
}
// resolveTypeParams resolves any type parameters in the given type to the given type arguments.
// only at the top level object - so nested type arguments are not resolved
func resolveTypeParams(typ *schema.Type, typeArgs []*schema.Type) *schema.Type {
switch t := typ.Typ.(type) {
case *schema.Type_TypeParameter:
return typeArgs[t.TypeParameter.ParamIdx]
case *schema.Type_Struct:
for _, field := range t.Struct.Fields {
field.Typ = resolveTypeParams(field.Typ, typeArgs)
}
case *schema.Type_List:
t.List.Elem = resolveTypeParams(t.List.Elem, typeArgs)
case *schema.Type_Map:
t.Map.Key = resolveTypeParams(t.Map.Key, typeArgs)
t.Map.Value = resolveTypeParams(t.Map.Value, typeArgs)
case *schema.Type_Config:
t.Config.Elem = resolveTypeParams(t.Config.Elem, typeArgs)
case *schema.Type_Pointer:
t.Pointer.Base = resolveTypeParams(t.Pointer.Base, typeArgs)
case *schema.Type_Named:
for i, param := range t.Named.TypeArguments {
t.Named.TypeArguments[i] = resolveTypeParams(param, typeArgs)
}
}
return typ
}
// DefaultClientHttpMethod works out the default HTTP method a client should use for a given RPC.
// When possible we will default to POST either when no method has been specified on the API or when
// then is a selection of methods and POST is one of them. If POST is not allowed as a method then
// we will use the first specified method.
func DefaultClientHttpMethod(rpc *meta.RPC) string {
if rpc.HttpMethods[0] == "*" {
return "POST"
}
for _, httpMethod := range rpc.HttpMethods {
if httpMethod == "POST" {
return "POST"
}
}
return rpc.HttpMethods[0]
}
// DescribeAuth generates a ParameterEncoding per field of the auth struct and returns it as
// the AuthEncoding. If authSchema is nil it returns nil, nil.
func DescribeAuth(appMetaData *meta.Data, authSchema *schema.Type, options *Options) (*AuthEncoding, error) {
if authSchema == nil {
return nil, nil
}
switch t := authSchema.Typ.(type) {
case *schema.Type_Builtin:
if t.Builtin != schema.Builtin_STRING {
return nil, errors.Newf("unsupported auth parameter %v", errors.Safe(t.Builtin))
}
return &AuthEncoding{LegacyTokenFormat: true}, nil
case *schema.Type_Named:
case *schema.Type_Pointer:
default:
return nil, errors.Newf("unsupported auth parameter type %T", errors.Safe(t))
}
authStruct, err := GetConcreteStructType(appMetaData.Decls, authSchema, nil)
if err != nil {
return nil, errors.Wrap(err, "auth struct")
}
fields, err := describeParams(&encodingHints{Undefined, authTags, options}, authStruct)
if err != nil {
return nil, err
}
if locationDiff := keyDiff(fields, Header, Query, Cookie); len(locationDiff) > 0 {
return nil, errors.Newf("auth must only contain query, header, and cookie parameters. Found: %v", locationDiff)
}
return &AuthEncoding{
QueryParameters: fields[Query],
HeaderParameters: fields[Header],
CookieParameters: fields[Cookie],
}, nil
}
// DescribeResponse generates a ParameterEncoding per field of the response struct and returns it as
// the ResponseEncoding
func DescribeResponse(appMetaData *meta.Data, responseSchema *schema.Type, options *Options) (*ResponseEncoding, error) {
if responseSchema == nil {
return nil, nil
}
responseStruct, err := GetConcreteStructType(appMetaData.Decls, responseSchema, nil)
if err != nil {
return nil, errors.Wrap(err, "response struct")
}
fields, err := describeParams(&encodingHints{Body, responseTags, options}, responseStruct)
if err != nil {
return nil, err
}
if keys := keyDiff(fields, Header, Body); len(keys) > 0 {
return nil, errors.Newf("response must only contain body and header parameters. Found: %v", keys)
}
return &ResponseEncoding{
BodyParameters: fields[Body],
HeaderParameters: fields[Header],
}, nil
}
// keyDiff returns the diff between src.keys and keys
func keyDiff[T comparable, V any](src map[T]V, keys ...T) (diff []T) {
for k := range src {
if !slices.Contains(keys, k) {
diff = append(diff, k)
}
}
return diff
}
// DescribeRequest groups the provided httpMethods by default ParameterLocation and returns a RequestEncoding
// per ParameterLocation
func DescribeRequest(appMetaData *meta.Data, requestSchema *schema.Type, options *Options, httpMethods ...string) ([]*RequestEncoding, error) {
methodsByDefaultLocation := make(map[ParameterLocation][]string)
for _, m := range httpMethods {
switch m {
case "GET", "HEAD", "DELETE":
methodsByDefaultLocation[Query] = append(methodsByDefaultLocation[Query], m)
case "*":
methodsByDefaultLocation[Body] = []string{"POST", "PUT", "PATCH"}
methodsByDefaultLocation[Query] = []string{"GET", "HEAD", "DELETE"}
default:
methodsByDefaultLocation[Body] = append(methodsByDefaultLocation[Body], m)
}
}
var requestStruct *schema.Struct
var err error
if requestSchema != nil {
requestStruct, err = GetConcreteStructType(appMetaData.Decls, requestSchema, nil)
if err != nil {
return nil, errors.Wrap(err, "request struct")
}
}
var reqs []*RequestEncoding
for location, methods := range methodsByDefaultLocation {
var fields map[ParameterLocation][]*ParameterEncoding
if requestStruct != nil {
fields, err = describeParams(&encodingHints{location, requestTags, options}, requestStruct)
if err != nil {
return nil, err
}
}
if keys := keyDiff(fields, Query, Header, Body); len(keys) > 0 {
return nil, errors.Newf("request must only contain Query, Body and Header parameters. Found: %v", keys)
}
reqs = append(reqs, &RequestEncoding{
HTTPMethods: methods,
QueryParameters: fields[Query],
HeaderParameters: fields[Header],
BodyParameters: fields[Body],
})
}
// Sort by first method to get a deterministic order (list is randomized by map above)
sort.Slice(reqs, func(i, j int) bool {
return reqs[i].HTTPMethods[0] < reqs[j].HTTPMethods[0]
})
return reqs, nil
}
// describeParams calls describeParam() for each field in the payload struct
func describeParams(encodingHints *encodingHints, payload *schema.Struct) (fields map[ParameterLocation][]*ParameterEncoding, err error) {
paramByLocation := make(map[ParameterLocation][]*ParameterEncoding)
for _, f := range payload.GetFields() {
f, err := describeParam(encodingHints, f)
if err != nil {
return nil, err
}
if f != nil {
paramByLocation[f.Location] = append(paramByLocation[f.Location], f)
}
}
return paramByLocation, nil
}
// formatName formats a parameter name with the default formatting for the location (e.g. snakecase for query)
func formatName(location ParameterLocation, name string) string {
switch location {
case Query:
return idents.Convert(name, idents.SnakeCase)
default:
return name
}
}
// IgnoreField returns true if the field name is "-" is any of the valid request or response tags
func IgnoreField(field *schema.Field) bool {
for _, tag := range field.Tags {
if _, found := requestTags[tag.Key]; found && tag.Name == "-" {
return true
}
}
return false
}
// describeParam returns the ParameterEncoding which uses field tags to describe how the parameter
// (e.g. qs, query, header) should be encoded in HTTP (name and location).
//
// It returns nil, nil if the field is not to be encoded.
func describeParam(encodingHints *encodingHints, field *schema.Field) (*ParameterEncoding, error) {
location := encodingHints.defaultLocation
name := formatName(encodingHints.defaultLocation, field.Name)
param := ParameterEncoding{
Name: name,
OmitEmpty: false,
SrcName: field.Name,
Doc: field.Doc,
Type: field.Typ,
RawTag: field.RawTag,
Optional: field.Optional,
WireFormat: name,
}
var usedOverrideTag string
for _, tag := range field.Tags {
if IgnoreField(field) {
return nil, nil
}
tagHint, ok := encodingHints.tags[tag.Key]
if !ok {
continue
}
if tagHint.overrideDefault {
if usedOverrideTag != "" {
return nil, errors.Newf("tag conflict: %s cannot be combined with %s", usedOverrideTag, tag.Key)
}
location = tagHint.location
usedOverrideTag = tag.Key
}
if tagHint.location == location {
param.Name = tag.Name
if tagHint.wireFormatter != nil {
param.WireFormat = tagHint.wireFormatter(tag.Name)
} else {
param.WireFormat = tag.Name
}
}
if tagHint.omitEmptyOption != "" {
for _, o := range tag.Options {
if o == tagHint.omitEmptyOption {
param.OmitEmpty = true
}
}
}
if encodingHints.options != nil && tag.Key == encodingHints.options.SrcNameTag {
param.SrcName = tag.Name
}
}
if param.Name == "-" {
return nil, nil
}
param.Location = location
return ¶m, nil
}
// toEncodingMap returns a map from SrcName to parameter encodings.
func toEncodingMap(keyFunc func(e *ParameterEncoding) string, encodings ...[]*ParameterEncoding) map[string]*ParameterEncoding {
res := make(map[string]*ParameterEncoding)
for _, e := range encodings {
for _, param := range e {
res[keyFunc(param)] = param
}
}
return res
}
// toEncodingMultiMap returns a map from a key to the list of parameter encodings
// matching that key.
func toEncodingMultiMap(keyFunc func(e *ParameterEncoding) string, encodings ...[]*ParameterEncoding) map[string][]*ParameterEncoding {
res := make(map[string][]*ParameterEncoding)
for _, e := range encodings {
for _, param := range e {
key := keyFunc(param)
res[key] = append(res[key], param)
}
}
return res
}
func srcNameKey(e *ParameterEncoding) string {
return e.SrcName
}
func nameKey(e *ParameterEncoding) string {
return e.Name
}