-
Notifications
You must be signed in to change notification settings - Fork 106
/
content.go
730 lines (642 loc) · 21.3 KB
/
content.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
/*
Copyright 2017 Caicloud Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package service
import (
"context"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"reflect"
"strconv"
"github.com/caicloud/nirvana/definition"
)
// Consumer handles specifically typed data from a reader and unmarshals it into an object.
type Consumer interface {
// ContentType returns a HTTP MIME type.
ContentType() string
// Consume unmarshals data from r into v.
Consume(r io.Reader, v interface{}) error
}
// Producer marshals an object to specifically typed data and write it into a writer.
type Producer interface {
// ContentType returns a HTTP MIME type.
ContentType() string
// Produce marshals v to data and write to w.
Produce(w io.Writer, v interface{}) error
}
var consumers = map[string]Consumer{
definition.MIMENone: &NoneSerializer{},
definition.MIMEText: NewSimpleSerializer(definition.MIMEText),
definition.MIMEJSON: &JSONSerializer{},
definition.MIMEXML: &XMLSerializer{},
definition.MIMEOctetStream: NewSimpleSerializer(definition.MIMEOctetStream),
definition.MIMEURLEncoded: &URLEncodedConsumer{},
definition.MIMEFormData: &FormDataConsumer{},
}
var producers = map[string]Producer{
definition.MIMENone: &NoneSerializer{},
definition.MIMEText: NewSimpleSerializer(definition.MIMEText),
definition.MIMEJSON: &JSONSerializer{},
definition.MIMEXML: &XMLSerializer{},
definition.MIMEOctetStream: NewSimpleSerializer(definition.MIMEOctetStream),
}
// AllConsumers returns all consumers.
func AllConsumers() []Consumer {
cs := make([]Consumer, 0, len(consumers))
for _, c := range consumers {
cs = append(cs, c)
}
return cs
}
// ConsumerFor gets a consumer for specified content type.
func ConsumerFor(contentType string) Consumer {
return consumers[contentType]
}
// AllProducers returns all producers.
func AllProducers() []Producer {
ps := make([]Producer, 0, len(producers))
// JSON always the first one in producers.
// The first one will be chosen when accept types
// are not recognized.
if p := producers[definition.MIMEJSON]; p != nil {
ps = append(ps, p)
}
for _, p := range producers {
if p.ContentType() == definition.MIMEJSON {
continue
}
ps = append(ps, p)
}
return ps
}
// ProducerFor gets a producer for specified content type.
func ProducerFor(contentType string) Producer {
return producers[contentType]
}
// RegisterConsumer register a consumer. A consumer must not handle "*/*".
func RegisterConsumer(c Consumer) error {
if c.ContentType() == definition.MIMEAll {
return invalidConsumer.Error(definition.MIMEAll)
}
consumers[c.ContentType()] = c
return nil
}
// RegisterProducer register a producer. A producer must not handle "*/*".
func RegisterProducer(p Producer) error {
if p.ContentType() == definition.MIMEAll {
return invalidProducer.Error(definition.MIMEAll)
}
producers[p.ContentType()] = p
return nil
}
// NoneSerializer implements Consumer and Producer for content types
// which can only receive data by io.Reader.
type NoneSerializer struct{}
// ContentType returns none MIME type.
func (s *NoneSerializer) ContentType() string {
return definition.MIMENone
}
// Consume does nothing.
func (s *NoneSerializer) Consume(r io.Reader, v interface{}) error {
return invalidTypeForConsumer.Error(s.ContentType(), reflect.TypeOf(v))
}
// Produce does nothing.
func (s *NoneSerializer) Produce(w io.Writer, v interface{}) error {
return invalidTypeForProducer.Error(s.ContentType(), reflect.TypeOf(v))
}
// RawSerializer implements a raw serializer.
type RawSerializer struct{}
// CanConsumeData checks if raw serializer can consume type v with specified content type.
func (s *RawSerializer) CanConsumeData(contentType string, r io.Reader, v interface{}) bool {
switch v.(type) {
case *string, *[]byte:
return true
}
return false
}
// ConsumeData reads data and converts it to string, []byte.
func (s *RawSerializer) ConsumeData(contentType string, r io.Reader, v interface{}) error {
switch target := v.(type) {
case *string:
data, err := ioutil.ReadAll(r)
if err != nil {
return err
}
*target = string(data)
return nil
case *[]byte:
data, err := ioutil.ReadAll(r)
if err != nil {
return err
}
*target = data
return nil
}
return invalidTypeForConsumer.Error(contentType, reflect.TypeOf(v))
}
// CanProduceData checks if raw serializer can produce data for specified content type from type v.
func (s *RawSerializer) CanProduceData(contentType string, w io.Writer, v interface{}) bool {
if _, ok := v.(io.Reader); ok {
return true
}
switch v.(type) {
case string, []byte:
return true
}
return false
}
// ProduceData writes v to writer. v should be string, []byte, io.Reader.
func (s *RawSerializer) ProduceData(contentType string, w io.Writer, v interface{}) error {
if r, ok := v.(io.Reader); ok {
_, err := io.Copy(w, r)
return err
}
switch source := v.(type) {
case string:
_, err := io.WriteString(w, source)
return err
case []byte:
_, err := w.Write(source)
return err
}
return invalidTypeForProducer.Error(contentType, reflect.TypeOf(v))
}
// SimpleSerializer implements a simple serializer.
type SimpleSerializer struct {
RawSerializer
contentType string
}
// NewSimpleSerializer creates a simple serializer.
func NewSimpleSerializer(contentType string) *SimpleSerializer {
return &SimpleSerializer{
contentType: contentType,
}
}
// ContentType returns plain text MIME type.
func (s *SimpleSerializer) ContentType() string {
return s.contentType
}
// Consume reads data and converts it to string, []byte.
func (s *SimpleSerializer) Consume(r io.Reader, v interface{}) error {
return s.ConsumeData(s.ContentType(), r, v)
}
// Produce writes v to writer. v should be string, []byte, io.Reader.
func (s *SimpleSerializer) Produce(w io.Writer, v interface{}) error {
if s.CanProduceData(s.ContentType(), w, v) {
return s.ProduceData(s.ContentType(), w, v)
}
if r, ok := v.(error); ok {
_, err := io.WriteString(w, r.Error())
return err
}
if r, ok := v.(fmt.Stringer); ok {
_, err := io.WriteString(w, r.String())
return err
}
return invalidTypeForProducer.Error(s.ContentType(), reflect.TypeOf(v))
}
// URLEncodedConsumer implements Consumer for content type "application/x-www-form-urlencoded"
type URLEncodedConsumer struct{ RawSerializer }
// ContentType returns url encoded MIME type.
func (s *URLEncodedConsumer) ContentType() string {
return definition.MIMEURLEncoded
}
// Consume reads data and converts it to string, []byte.
func (s *URLEncodedConsumer) Consume(r io.Reader, v interface{}) error {
return s.ConsumeData(s.ContentType(), r, v)
}
// FormDataConsumer implements Consumer for content type "multipart/form-data"
type FormDataConsumer struct{ RawSerializer }
// ContentType returns form data MIME type.
func (s *FormDataConsumer) ContentType() string {
return definition.MIMEFormData
}
// Consume reads data and converts it to string, []byte.
func (s *FormDataConsumer) Consume(r io.Reader, v interface{}) error {
return s.ConsumeData(s.ContentType(), r, v)
}
// JSONSerializer implements Consumer and Producer for content type "application/json".
type JSONSerializer struct{ RawSerializer }
// ContentType returns json MIME type.
func (s *JSONSerializer) ContentType() string {
return definition.MIMEJSON
}
// Consume unmarshals json from r into v.
func (s *JSONSerializer) Consume(r io.Reader, v interface{}) error {
if s.CanConsumeData(s.ContentType(), r, v) {
return s.ConsumeData(s.ContentType(), r, v)
}
err := json.NewDecoder(r).Decode(v)
if err == io.EOF {
return nil
}
return err
}
// Produce marshals v to json and write to w.
func (s *JSONSerializer) Produce(w io.Writer, v interface{}) error {
if s.CanProduceData(s.ContentType(), w, v) {
return s.ProduceData(s.ContentType(), w, v)
}
return json.NewEncoder(w).Encode(v)
}
// XMLSerializer implements Consumer and Producer for content type "application/xml".
type XMLSerializer struct{ RawSerializer }
// ContentType returns xml MIME type.
func (s *XMLSerializer) ContentType() string {
return definition.MIMEXML
}
// Consume unmarshals xml from r into v.
func (s *XMLSerializer) Consume(r io.Reader, v interface{}) error {
if s.CanConsumeData(s.ContentType(), r, v) {
return s.ConsumeData(s.ContentType(), r, v)
}
err := xml.NewDecoder(r).Decode(v)
if err == io.EOF {
return nil
}
return err
}
// Produce marshals v to xml and write to w.
func (s *XMLSerializer) Produce(w io.Writer, v interface{}) error {
if s.CanProduceData(s.ContentType(), w, v) {
return s.ProduceData(s.ContentType(), w, v)
}
return xml.NewEncoder(w).Encode(v)
}
// Prefab creates instances for internal type. These instances are not
// unmarshaled form http request data.
type Prefab interface {
// Name returns prefab name.
Name() string
// Type is instance type.
Type() reflect.Type
// Make makes an instance.
Make(ctx context.Context) (interface{}, error)
}
var prefabs = map[string]Prefab{
"context": &ContextPrefab{},
}
// PrefabFor gets a prefab by name.
func PrefabFor(name string) Prefab {
return prefabs[name]
}
// RegisterPrefab registers a prefab.
func RegisterPrefab(prefab Prefab) error {
prefabs[prefab.Name()] = prefab
return nil
}
// ContextPrefab returns context from parameter of Make().
// It's usually used for generating the first parameter of api handler.
type ContextPrefab struct{}
// Name returns prefab name.
func (p *ContextPrefab) Name() string {
return "context"
}
// Type is type of context.Context.
func (p *ContextPrefab) Type() reflect.Type {
return reflect.TypeOf((*context.Context)(nil)).Elem()
}
// Make returns context simply.
func (p *ContextPrefab) Make(ctx context.Context) (interface{}, error) {
return ctx, nil
}
// Converter is used to convert []string to specific type. Data must have one
// element at least or it will panic.
type Converter func(ctx context.Context, data []string) (interface{}, error)
var converters = map[reflect.Type]Converter{
reflect.TypeOf(bool(false)): ConvertToBool,
reflect.TypeOf(int(0)): ConvertToInt,
reflect.TypeOf(int8(0)): ConvertToInt8,
reflect.TypeOf(int16(0)): ConvertToInt16,
reflect.TypeOf(int32(0)): ConvertToInt32,
reflect.TypeOf(int64(0)): ConvertToInt64,
reflect.TypeOf(uint(0)): ConvertToUint,
reflect.TypeOf(uint8(0)): ConvertToUint8,
reflect.TypeOf(uint16(0)): ConvertToUint16,
reflect.TypeOf(uint32(0)): ConvertToUint32,
reflect.TypeOf(uint64(0)): ConvertToUint64,
reflect.TypeOf(float32(0)): ConvertToFloat32,
reflect.TypeOf(float64(0)): ConvertToFloat64,
reflect.TypeOf(string("")): ConvertToString,
reflect.TypeOf(new(bool)): ConvertToBoolP,
reflect.TypeOf(new(int)): ConvertToIntP,
reflect.TypeOf(new(int8)): ConvertToInt8P,
reflect.TypeOf(new(int16)): ConvertToInt16P,
reflect.TypeOf(new(int32)): ConvertToInt32P,
reflect.TypeOf(new(int64)): ConvertToInt64P,
reflect.TypeOf(new(uint)): ConvertToUintP,
reflect.TypeOf(new(uint8)): ConvertToUint8P,
reflect.TypeOf(new(uint16)): ConvertToUint16P,
reflect.TypeOf(new(uint32)): ConvertToUint32P,
reflect.TypeOf(new(uint64)): ConvertToUint64P,
reflect.TypeOf(new(float32)): ConvertToFloat32P,
reflect.TypeOf(new(float64)): ConvertToFloat64P,
reflect.TypeOf(new(string)): ConvertToStringP,
reflect.TypeOf([]bool{}): ConvertToBoolSlice,
reflect.TypeOf([]int{}): ConvertToIntSlice,
reflect.TypeOf([]float64{}): ConvertToFloat64Slice,
reflect.TypeOf([]string{}): ConvertToStringSlice,
}
// ConverterFor gets converter for specified type.
func ConverterFor(typ reflect.Type) Converter {
return converters[typ]
}
// RegisterConverter registers a converter for specified type. New converter
// overrides old one.
func RegisterConverter(typ reflect.Type, converter Converter) {
converters[typ] = converter
}
// ConvertToBool converts []string to bool. Only the first data is used.
func ConvertToBool(ctx context.Context, data []string) (interface{}, error) {
origin := data[0]
target, err := strconv.ParseBool(origin)
if err != nil {
return nil, invalidConversion.Error(origin, "bool")
}
return target, nil
}
// ConvertToBoolP converts []string to *bool. Only the first data is used.
func ConvertToBoolP(ctx context.Context, data []string) (interface{}, error) {
ret, err := ConvertToBool(ctx, data)
if err != nil {
return nil, err
}
value := ret.(bool)
return &value, nil
}
// ConvertToInt converts []string to int. Only the first data is used.
func ConvertToInt(ctx context.Context, data []string) (interface{}, error) {
origin := data[0]
target, err := strconv.ParseInt(origin, 10, 0)
if err != nil {
return nil, invalidConversion.Error(origin, "int")
}
return int(target), nil
}
// ConvertToIntP converts []string to *int. Only the first data is used.
func ConvertToIntP(ctx context.Context, data []string) (interface{}, error) {
ret, err := ConvertToInt(ctx, data)
if err != nil {
return nil, err
}
value := ret.(int)
return &value, nil
}
// ConvertToInt8 converts []string to int8. Only the first data is used.
func ConvertToInt8(ctx context.Context, data []string) (interface{}, error) {
origin := data[0]
target, err := strconv.ParseInt(origin, 10, 8)
if err != nil {
return nil, invalidConversion.Error(origin, "int8")
}
return int8(target), nil
}
// ConvertToInt8P converts []string to *int8. Only the first data is used.
func ConvertToInt8P(ctx context.Context, data []string) (interface{}, error) {
ret, err := ConvertToInt8(ctx, data)
if err != nil {
return nil, err
}
value := ret.(int8)
return &value, nil
}
// ConvertToInt16 converts []string to int16. Only the first data is used.
func ConvertToInt16(ctx context.Context, data []string) (interface{}, error) {
origin := data[0]
target, err := strconv.ParseInt(origin, 10, 16)
if err != nil {
return nil, invalidConversion.Error(origin, "int16")
}
return int16(target), nil
}
// ConvertToInt16P converts []string to *int16. Only the first data is used.
func ConvertToInt16P(ctx context.Context, data []string) (interface{}, error) {
ret, err := ConvertToInt16(ctx, data)
if err != nil {
return nil, err
}
value := ret.(int16)
return &value, nil
}
// ConvertToInt32 converts []string to int32. Only the first data is used.
func ConvertToInt32(ctx context.Context, data []string) (interface{}, error) {
origin := data[0]
target, err := strconv.ParseInt(origin, 10, 32)
if err != nil {
return nil, invalidConversion.Error(origin, "int32")
}
return int32(target), nil
}
// ConvertToInt32P converts []string to *int32. Only the first data is used.
func ConvertToInt32P(ctx context.Context, data []string) (interface{}, error) {
ret, err := ConvertToInt32(ctx, data)
if err != nil {
return nil, err
}
value := ret.(int32)
return &value, nil
}
// ConvertToInt64 converts []string to int64. Only the first data is used.
func ConvertToInt64(ctx context.Context, data []string) (interface{}, error) {
origin := data[0]
target, err := strconv.ParseInt(origin, 10, 64)
if err != nil {
return nil, invalidConversion.Error(origin, "int64")
}
return target, nil
}
// ConvertToInt64P converts []string to *int64. Only the first data is used.
func ConvertToInt64P(ctx context.Context, data []string) (interface{}, error) {
ret, err := ConvertToInt64(ctx, data)
if err != nil {
return nil, err
}
value := ret.(int64)
return &value, nil
}
// ConvertToUint converts []string to uint. Only the first data is used.
func ConvertToUint(ctx context.Context, data []string) (interface{}, error) {
origin := data[0]
target, err := strconv.ParseUint(origin, 10, 0)
if err != nil {
return nil, invalidConversion.Error(origin, "uint")
}
return uint(target), nil
}
// ConvertToUintP converts []string to *uint. Only the first data is used.
func ConvertToUintP(ctx context.Context, data []string) (interface{}, error) {
ret, err := ConvertToUint(ctx, data)
if err != nil {
return nil, err
}
value := ret.(uint)
return &value, nil
}
// ConvertToUint8 converts []string to uint8. Only the first data is used.
func ConvertToUint8(ctx context.Context, data []string) (interface{}, error) {
origin := data[0]
target, err := strconv.ParseUint(origin, 10, 8)
if err != nil {
return nil, invalidConversion.Error(origin, "uint8")
}
return uint8(target), nil
}
// ConvertToUint8P converts []string to *uint8. Only the first data is used.
func ConvertToUint8P(ctx context.Context, data []string) (interface{}, error) {
ret, err := ConvertToUint8(ctx, data)
if err != nil {
return nil, err
}
value := ret.(uint8)
return &value, nil
}
// ConvertToUint16 converts []string to uint16. Only the first data is used.
func ConvertToUint16(ctx context.Context, data []string) (interface{}, error) {
origin := data[0]
target, err := strconv.ParseUint(origin, 10, 16)
if err != nil {
return nil, invalidConversion.Error(origin, "uint16")
}
return uint16(target), nil
}
// ConvertToUint16P converts []string to *uint16. Only the first data is used.
func ConvertToUint16P(ctx context.Context, data []string) (interface{}, error) {
ret, err := ConvertToUint16(ctx, data)
if err != nil {
return nil, err
}
value := ret.(uint16)
return &value, nil
}
// ConvertToUint32 converts []string to uint32. Only the first data is used.
func ConvertToUint32(ctx context.Context, data []string) (interface{}, error) {
origin := data[0]
target, err := strconv.ParseUint(origin, 10, 32)
if err != nil {
return nil, invalidConversion.Error(origin, "uint32")
}
return uint32(target), nil
}
// ConvertToUint32P converts []string to *uint32. Only the first data is used.
func ConvertToUint32P(ctx context.Context, data []string) (interface{}, error) {
ret, err := ConvertToUint32(ctx, data)
if err != nil {
return nil, err
}
value := ret.(uint32)
return &value, nil
}
// ConvertToUint64 converts []string to uint64. Only the first data is used.
func ConvertToUint64(ctx context.Context, data []string) (interface{}, error) {
origin := data[0]
target, err := strconv.ParseUint(origin, 10, 64)
if err != nil {
return nil, invalidConversion.Error(origin, "uint64")
}
return target, nil
}
// ConvertToUint64P converts []string to *uint64. Only the first data is used.
func ConvertToUint64P(ctx context.Context, data []string) (interface{}, error) {
ret, err := ConvertToUint64(ctx, data)
if err != nil {
return nil, err
}
value := ret.(uint64)
return &value, nil
}
// ConvertToFloat32 converts []string to float32. Only the first data is used.
func ConvertToFloat32(ctx context.Context, data []string) (interface{}, error) {
origin := data[0]
target, err := strconv.ParseFloat(origin, 32)
if err != nil {
return nil, invalidConversion.Error(origin, "float32")
}
return float32(target), nil
}
// ConvertToFloat32P converts []string to *float32. Only the first data is used.
func ConvertToFloat32P(ctx context.Context, data []string) (interface{}, error) {
ret, err := ConvertToFloat32(ctx, data)
if err != nil {
return nil, err
}
value := ret.(float32)
return &value, nil
}
// ConvertToFloat64 converts []string to float64. Only the first data is used.
func ConvertToFloat64(ctx context.Context, data []string) (interface{}, error) {
origin := data[0]
target, err := strconv.ParseFloat(origin, 64)
if err != nil {
return nil, invalidConversion.Error(origin, "float64")
}
return target, nil
}
// ConvertToFloat64P converts []string to *float64. Only the first data is used.
func ConvertToFloat64P(ctx context.Context, data []string) (interface{}, error) {
ret, err := ConvertToFloat64(ctx, data)
if err != nil {
return nil, err
}
value := ret.(float64)
return &value, nil
}
// ConvertToString return the first element in []string.
func ConvertToString(ctx context.Context, data []string) (interface{}, error) {
return data[0], nil
}
// ConvertToStringP return the first element's pointer in []string.
func ConvertToStringP(ctx context.Context, data []string) (interface{}, error) {
return &data[0], nil
}
// ConvertToBoolSlice converts all elements in data to bool, and return []bool
func ConvertToBoolSlice(ctx context.Context, data []string) (interface{}, error) {
ret := make([]bool, len(data))
for i := range data {
r, err := ConvertToBool(ctx, data[i:i+1])
if err != nil {
return nil, err
}
ret[i] = r.(bool)
}
return ret, nil
}
// ConvertToIntSlice converts all elements in data to int, and return []int
func ConvertToIntSlice(ctx context.Context, data []string) (interface{}, error) {
ret := make([]int, len(data))
for i := range data {
r, err := ConvertToInt(ctx, data[i:i+1])
if err != nil {
return nil, err
}
ret[i] = r.(int)
}
return ret, nil
}
// ConvertToFloat64Slice converts all elements in data to float64, and return []float64
func ConvertToFloat64Slice(ctx context.Context, data []string) (interface{}, error) {
ret := make([]float64, len(data))
for i := range data {
r, err := ConvertToFloat64(ctx, data[i:i+1])
if err != nil {
return nil, err
}
ret[i] = r.(float64)
}
return ret, nil
}
// ConvertToStringSlice return all strings in data.
func ConvertToStringSlice(ctx context.Context, data []string) (interface{}, error) {
return data, nil
}