-
Notifications
You must be signed in to change notification settings - Fork 1
/
arguments.go
800 lines (642 loc) · 20.3 KB
/
arguments.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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
package smartapi
import (
"context"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"reflect"
"strconv"
"github.com/go-chi/chi"
)
type endpointOptions int
const (
flagArgument endpointOptions = 1 << iota
flagParsesQuery
flagResponseStatus
flagReadsRequestBody
flagWritesResponse
flagError
)
func (e endpointOptions) has(o endpointOptions) bool {
return e&o != 0
}
// EndpointParam is used with endpointData definition
type EndpointParam interface {
options() endpointOptions
}
// Argument represents an argument passed to a function
type Argument interface {
EndpointParam
checkArg(arg reflect.Type) error
getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error)
}
type errorEndpointParam struct {
err error
}
func (e errorEndpointParam) options() endpointOptions {
return flagError
}
type headerArgument struct {
name string
}
func (a headerArgument) options() endpointOptions {
return flagArgument
}
func (a headerArgument) getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
return reflect.ValueOf(r.Header.Get(a.name)), nil
}
func (a headerArgument) checkArg(arg reflect.Type) error {
if arg.Kind() != reflect.String {
return errors.New("expected a string type")
}
return nil
}
// Header reads a header from the request and passes it as string to a function
func Header(name string) EndpointParam {
return headerArgument{name: name}
}
type requiredHeaderArgument struct {
name string
}
func (a requiredHeaderArgument) options() endpointOptions {
return flagArgument
}
func (a requiredHeaderArgument) getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
value := r.Header.Get(a.name)
if len(value) == 0 {
msg := fmt.Sprintf("missing required header %s", a.name)
return reflect.Value{}, Error(http.StatusBadRequest, msg, msg)
}
return reflect.ValueOf(value), nil
}
func (a requiredHeaderArgument) checkArg(arg reflect.Type) error {
if arg.Kind() != reflect.String {
return errors.New("expected a string type")
}
return nil
}
// RequiredHeader reads a header from the request and passes it as string to a function
func RequiredHeader(name string) EndpointParam {
return requiredHeaderArgument{name: name}
}
type jsonBodyArgument struct {
typ reflect.Type
}
func (a jsonBodyArgument) options() endpointOptions {
return flagArgument | flagReadsRequestBody
}
func (a jsonBodyArgument) checkArg(arg reflect.Type) error {
if reflect.PtrTo(a.typ) != arg {
return errors.New("invalid type")
}
return nil
}
func (a jsonBodyArgument) getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
value := reflect.New(a.typ)
obj := value.Interface()
if err := json.NewDecoder(r.Body).Decode(obj); err != nil {
return reflect.Value{}, WrapError(http.StatusBadRequest, err, "cannot unmarshal request")
}
return value, nil
}
// JSONBody reads request's body and unmarshals it into a pointer to a json structure
func JSONBody(v interface{}) EndpointParam {
return jsonBodyArgument{typ: reflect.TypeOf(v)}
}
type jsonBodyDirectArgument struct {
typ reflect.Type
}
func (a jsonBodyDirectArgument) options() endpointOptions {
return flagArgument | flagReadsRequestBody
}
func (a jsonBodyDirectArgument) checkArg(arg reflect.Type) error {
if a.typ != arg {
return errors.New("invalid type")
}
return nil
}
func (a jsonBodyDirectArgument) getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
value := reflect.New(a.typ)
obj := value.Interface()
if err := json.NewDecoder(r.Body).Decode(obj); err != nil {
return reflect.Value{}, WrapError(http.StatusBadRequest, err, "cannot unmarshal request")
}
return value.Elem(), nil
}
// JSONBodyDirect reads request's body and unmarshals it into a json structure
func JSONBodyDirect(v interface{}) EndpointParam {
return jsonBodyDirectArgument{typ: reflect.TypeOf(v)}
}
type xmlBodyArgument struct {
typ reflect.Type
}
func (a xmlBodyArgument) options() endpointOptions {
return flagArgument | flagReadsRequestBody
}
func (a xmlBodyArgument) checkArg(arg reflect.Type) error {
if reflect.PtrTo(a.typ) != arg {
return errors.New("invalid type")
}
return nil
}
func (a xmlBodyArgument) getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
value := reflect.New(a.typ)
obj := value.Interface()
if err := xml.NewDecoder(r.Body).Decode(obj); err != nil {
return reflect.Value{}, WrapError(http.StatusBadRequest, err, "cannot unmarshal request")
}
return value, nil
}
// XMLBody reads request's body and unmarshals it into a pointer to an xml structure
func XMLBody(v interface{}) EndpointParam {
return xmlBodyArgument{typ: reflect.TypeOf(v)}
}
type stringBodyArgument struct{}
func (stringBodyArgument) options() endpointOptions {
return flagArgument | flagReadsRequestBody
}
func (s stringBodyArgument) checkArg(arg reflect.Type) error {
if arg.Kind() != reflect.String {
return errors.New("expected string type")
}
return nil
}
func (s stringBodyArgument) getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
result, err := ioutil.ReadAll(r.Body)
if err != nil {
return reflect.Value{}, WrapError(http.StatusBadRequest, err, "cannot read request")
}
return reflect.ValueOf(string(result)), nil
}
// StringBody reads request's body end passes it as a string
func StringBody() EndpointParam {
return stringBodyArgument{}
}
type byteSliceBodyArgument struct{}
var byteSliceType = reflect.TypeOf([]byte(nil))
func (byteSliceBodyArgument) options() endpointOptions {
return flagArgument | flagReadsRequestBody
}
func (s byteSliceBodyArgument) checkArg(arg reflect.Type) error {
if arg != byteSliceType {
return errors.New("expected a byte slice")
}
return nil
}
func (s byteSliceBodyArgument) getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
result, err := ioutil.ReadAll(r.Body)
if err != nil {
return reflect.Value{}, WrapError(http.StatusBadRequest, err, "cannot read request")
}
return reflect.ValueOf(result), nil
}
// ByteSliceBody reads request's body end passes it as a byte slice.
func ByteSliceBody() EndpointParam {
return byteSliceBodyArgument{}
}
type bodyReaderArgument struct{}
var readerType = reflect.TypeOf((*io.Reader)(nil)).Elem()
func (bodyReaderArgument) options() endpointOptions {
return flagArgument | flagReadsRequestBody
}
func (b bodyReaderArgument) checkArg(arg reflect.Type) error {
if arg != readerType {
return errors.New("expected io.Reader interface")
}
return nil
}
func (b bodyReaderArgument) getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
return reflect.ValueOf(r.Body), nil
}
// BodyReader passes an io.Reader interface to read request's body.
func BodyReader() EndpointParam {
return bodyReaderArgument{}
}
type urlParamArgument struct {
name string
}
func (urlParamArgument) options() endpointOptions {
return flagArgument
}
func (u urlParamArgument) checkArg(arg reflect.Type) error {
if arg.Kind() != reflect.String {
return errors.New("expected a string type")
}
return nil
}
func (u urlParamArgument) getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
return reflect.ValueOf(chi.URLParam(r, u.name)), nil
}
// URLParam reads a url param and passes it as a string
func URLParam(name string) EndpointParam {
return urlParamArgument{name: name}
}
type contextArgument struct {
}
var ctxType = reflect.TypeOf((*context.Context)(nil)).Elem()
func (contextArgument) options() endpointOptions {
return flagArgument
}
func (q contextArgument) checkArg(arg reflect.Type) error {
if arg.Kind() != reflect.Interface || !arg.Implements(ctxType) {
return errors.New("expected context.Context")
}
return nil
}
func (q contextArgument) getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
return reflect.ValueOf(r.Context()), nil
}
// Context passes request's context into the function
func Context() EndpointParam {
return contextArgument{}
}
type responseStatusArgument struct {
status int
}
func (responseStatusArgument) options() endpointOptions {
return flagResponseStatus
}
// ResponseStatus allows to set successful response status
func ResponseStatus(status int) EndpointParam {
return responseStatusArgument{status: status}
}
type queryParamArgument struct {
name string
}
func (queryParamArgument) options() endpointOptions {
return flagArgument | flagParsesQuery
}
func (q queryParamArgument) checkArg(arg reflect.Type) error {
if arg.Kind() != reflect.String {
return errors.New("expected a string type")
}
return nil
}
func (q queryParamArgument) getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
return reflect.ValueOf(r.Form.Get(q.name)), nil
}
// QueryParam reads a query param and passes it as a string
func QueryParam(name string) EndpointParam {
return queryParamArgument{name: name}
}
type requiredQueryParamArgument struct {
name string
}
func (requiredQueryParamArgument) options() endpointOptions {
return flagArgument | flagParsesQuery
}
func (q requiredQueryParamArgument) checkArg(arg reflect.Type) error {
if arg.Kind() != reflect.String {
return errors.New("expected a string type")
}
return nil
}
func (q requiredQueryParamArgument) getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
value := r.Form.Get(q.name)
if len(value) == 0 {
m := fmt.Sprintf("missing required query param %s", q.name)
return reflect.Value{}, Error(http.StatusBadRequest, m, m)
}
return reflect.ValueOf(value), nil
}
// RequiredQueryParam reads a query param and passes it as a string. Returns 400 BAD REQUEST when empty
func RequiredQueryParam(name string) EndpointParam {
return requiredQueryParamArgument{name: name}
}
type requiredPostQueryParamArgument struct {
name string
}
func (requiredPostQueryParamArgument) options() endpointOptions {
return flagArgument | flagParsesQuery
}
func (q requiredPostQueryParamArgument) checkArg(arg reflect.Type) error {
if arg.Kind() != reflect.String {
return errors.New("expected a string type")
}
return nil
}
func (q requiredPostQueryParamArgument) getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
value := r.PostForm.Get(q.name)
if len(value) == 0 {
m := fmt.Sprintf("missing required post query param %s", q.name)
return reflect.Value{}, Error(http.StatusBadRequest, m, m)
}
return reflect.ValueOf(value), nil
}
// RequiredPostQueryParam reads a post query param and passes it as a string. Returns 400 BAD REQUEST if empty.
func RequiredPostQueryParam(name string) EndpointParam {
return requiredPostQueryParamArgument{name: name}
}
type postQueryParamArgument struct {
name string
}
func (postQueryParamArgument) options() endpointOptions {
return flagArgument | flagParsesQuery
}
func (p postQueryParamArgument) checkArg(arg reflect.Type) error {
if arg.Kind() != reflect.String {
return errors.New("expected a string type")
}
return nil
}
func (p postQueryParamArgument) getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
return reflect.ValueOf(r.PostForm.Get(p.name)), nil
}
// PostQueryParam parses query end passes post query param into a string as an argument
func PostQueryParam(name string) EndpointParam {
return postQueryParamArgument{name: name}
}
type cookieArgument struct {
name string
}
func (cookieArgument) options() endpointOptions {
return flagArgument
}
func (c cookieArgument) checkArg(arg reflect.Type) error {
if arg.Kind() != reflect.String {
return errors.New("expected a string type")
}
return nil
}
func (c cookieArgument) getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
cookie, err := r.Cookie(c.name)
if err != nil {
return reflect.ValueOf(""), nil
}
return reflect.ValueOf(cookie.Value), nil
}
// Cookie reads a cookie from the request and passes it as a string
func Cookie(name string) EndpointParam {
return cookieArgument{name: name}
}
type requiredCookieArgument struct {
name string
}
func (requiredCookieArgument) options() endpointOptions {
return flagArgument
}
func (c requiredCookieArgument) checkArg(arg reflect.Type) error {
if arg.Kind() != reflect.String {
return errors.New("expected a string type")
}
return nil
}
func (c requiredCookieArgument) getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
cookie, err := r.Cookie(c.name)
if err != nil {
msg := fmt.Sprintf("missing cookie %s", c.name)
return reflect.Value{}, Error(http.StatusBadRequest, msg, msg)
}
return reflect.ValueOf(cookie.Value), nil
}
// RequiredCookie reads a cookie from the request and passes it as a string
func RequiredCookie(name string) EndpointParam {
return requiredCookieArgument{name: name}
}
type headerSetterArgument struct{}
var headerSetterType = reflect.TypeOf((*Headers)(nil)).Elem()
func (headerSetterArgument) options() endpointOptions {
return flagArgument
}
func (headerSetterArgument) checkArg(arg reflect.Type) error {
if arg != headerSetterType {
return errors.New("argument's type must be smartapi.Headers")
}
return nil
}
func (headerSetterArgument) getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
return reflect.ValueOf(w.Header()), nil
}
// ResponseHeaders passes an interface to set response header values
func ResponseHeaders() EndpointParam {
return headerSetterArgument{}
}
type cookieSetterArgument struct{}
var cookieSetterType = reflect.TypeOf((*Cookies)(nil)).Elem()
func (cookieSetterArgument) options() endpointOptions {
return flagArgument
}
func (c cookieSetterArgument) checkArg(arg reflect.Type) error {
if arg != cookieSetterType {
return errors.New("argument's type must be smartapi.Cookies")
}
return nil
}
func (c cookieSetterArgument) getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
return reflect.ValueOf(cookieSetter{w: w}), nil
}
// ResponseCookies passes an interface to set cookie values
func ResponseCookies() EndpointParam {
return cookieSetterArgument{}
}
type responseWriterArgument struct{}
var responseWriterType = reflect.TypeOf((*http.ResponseWriter)(nil)).Elem()
func (responseWriterArgument) checkArg(arg reflect.Type) error {
if arg != responseWriterType {
return errors.New("argument's type must be http.ResponseWriter")
}
return nil
}
func (responseWriterArgument) getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
return reflect.ValueOf(w), nil
}
func (responseWriterArgument) options() endpointOptions {
return flagArgument | flagWritesResponse
}
// ResponseWriter passes an http.ResponseWriter into a function
func ResponseWriter() EndpointParam {
return responseWriterArgument{}
}
type fullRequestArgument struct{}
var fullRequestType = reflect.TypeOf(&http.Request{})
func (fullRequestArgument) options() endpointOptions {
return flagArgument | flagReadsRequestBody
}
func (fullRequestArgument) checkArg(arg reflect.Type) error {
if arg != fullRequestType {
return errors.New("argument's type must be *http.Request")
}
return nil
}
func (fullRequestArgument) getValue(_ http.ResponseWriter, r *http.Request) (reflect.Value, error) {
return reflect.ValueOf(r), nil
}
// Request passes an *http.Request into a function
func Request() EndpointParam {
return fullRequestArgument{}
}
const smartAPITagName = "smartapi"
type tagStructArgument struct {
structType reflect.Type
flags endpointOptions
arguments []Argument
}
func (t tagStructArgument) options() endpointOptions {
return t.flags
}
func (t tagStructArgument) checkArg(arg reflect.Type) error {
if arg.Kind() != reflect.Ptr {
return errors.New("argument must be a pointer")
}
if t.structType != arg.Elem() {
return errors.New("invalid argument type")
}
return nil
}
func (t tagStructArgument) getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
return constructStruct(t.structType, t.arguments, w, r)
}
type tagStructDirectArgument tagStructArgument
func (t tagStructDirectArgument) options() endpointOptions {
return t.flags
}
func (t tagStructDirectArgument) checkArg(arg reflect.Type) error {
if t.structType != arg {
return errors.New("invalid argument type")
}
return nil
}
func (t tagStructDirectArgument) getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
v, err := constructStruct(t.structType, t.arguments, w, r)
if err != nil {
return reflect.Value{}, err
}
return v.Elem(), nil
}
func constructStruct(structType reflect.Type, args []Argument, w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
vPtr := reflect.New(structType)
vStruct := vPtr.Elem()
for i, a := range args {
if a == nil {
continue
}
fieldValue, err := a.getValue(w, r)
if err != nil {
return reflect.Value{}, err
}
vStruct.Field(i).Set(fieldValue)
}
return vPtr, nil
}
func requestStruct(structType reflect.Type) (tagStructArgument, error) {
if structType.Kind() != reflect.Struct {
return tagStructArgument{}, errors.New("RequestStruct's argument must be a structure")
}
flags := flagArgument
numFields := structType.NumField()
numReadsBody := 0
var arguments []Argument
for i := 0; i < numFields; i++ {
f := structType.Field(i)
tag := f.Tag.Get(smartAPITagName)
if len(tag) == 0 {
arguments = append(arguments, nil)
continue
}
fieldArg, err := parseArgument(tag, f.Type)
if err != nil {
return tagStructArgument{}, fmt.Errorf("(struct field %s) %w", f.Name, err)
}
if err := fieldArg.checkArg(f.Type); err != nil {
return tagStructArgument{}, fmt.Errorf("(struct field %s) %w", f.Name, err)
}
fieldOpts := fieldArg.(EndpointParam).options()
if fieldOpts.has(flagReadsRequestBody) {
numReadsBody++
}
flags |= fieldOpts
arguments = append(arguments, fieldArg)
}
if numReadsBody > 1 {
return tagStructArgument{}, errors.New("only one struct field can read request's body")
}
return tagStructArgument{
structType: structType,
arguments: arguments,
flags: flags,
}, nil
}
// RequestStructDirect passes request's arguments into struct's fields by tags
func RequestStructDirect(s interface{}) EndpointParam {
reqStruct, err := requestStruct(reflect.TypeOf(s))
if err != nil {
return errorEndpointParam{err: err}
}
return tagStructDirectArgument(reqStruct)
}
// RequestStruct passes request's arguments into struct's fields by tags
func RequestStruct(s interface{}) EndpointParam {
reqStruct, err := requestStruct(reflect.TypeOf(s))
if err != nil {
return errorEndpointParam{err: err}
}
return reqStruct
}
type asIntArgument struct {
arg Argument
}
func (a asIntArgument) options() endpointOptions {
return a.arg.options()
}
func (a asIntArgument) checkArg(arg reflect.Type) error {
if arg.Kind() != reflect.Int {
return errors.New("argument must be an int")
}
return nil
}
func (a asIntArgument) getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
v, err := a.arg.getValue(w, r)
if err != nil {
return reflect.Value{}, err
}
intValue, err := strconv.Atoi(v.String())
if err != nil {
return reflect.Value{}, WrapError(http.StatusBadRequest, fmt.Errorf("AsInt(%s) conversion failed: %w", v.String(), err), "integer parse error")
}
return reflect.ValueOf(intValue), nil
}
// AsInt converts an argument and passes it as an int into the function
func AsInt(param EndpointParam) EndpointParam {
if !param.options().has(flagArgument) {
return errorEndpointParam{err: errors.New("AsInt() requires an argument param")}
}
arg := param.(Argument)
if err := arg.checkArg(reflect.TypeOf("")); err != nil {
return errorEndpointParam{err: errors.New("argument must accept a string")}
}
return asIntArgument{arg: arg}
}
type asByteSliceArgument struct {
arg Argument
}
func (a asByteSliceArgument) options() endpointOptions {
return a.arg.options()
}
func (a asByteSliceArgument) checkArg(arg reflect.Type) error {
if arg != byteSliceType {
return errors.New("argument must be a byte slice")
}
return nil
}
func (a asByteSliceArgument) getValue(w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
v, err := a.arg.getValue(w, r)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf([]byte(v.String())), nil
}
// AsByteSlice converts an argument and passes it as a byte slice
func AsByteSlice(param EndpointParam) EndpointParam {
if !param.options().has(flagArgument) {
return errorEndpointParam{err: errors.New("AsByteSlice() requires an argument param")}
}
arg := param.(Argument)
if err := arg.checkArg(reflect.TypeOf("")); err != nil {
return errorEndpointParam{err: errors.New("argument must accept a string")}
}
return asByteSliceArgument{arg: arg}
}