-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmarshal_strings.go
158 lines (138 loc) · 3.94 KB
/
marshal_strings.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
package qs
import (
"fmt"
"net/url"
"reflect"
"strconv"
"time"
)
type ptrMarshaler struct {
Type reflect.Type
ElemMarshaler Marshaler
}
func newPtrMarshaler(t reflect.Type, opts *MarshalOptions) (Marshaler, error) {
if t.Kind() != reflect.Ptr {
return nil, &wrongKindError{Expected: reflect.Ptr, Actual: t}
}
et := t.Elem()
em, err := opts.MarshalerFactory.Marshaler(et, opts)
if err != nil {
return nil, err
}
return &ptrMarshaler{
Type: t,
ElemMarshaler: em,
}, nil
}
func (p *ptrMarshaler) Marshal(v reflect.Value, opts *MarshalOptions) ([]string, error) {
t := v.Type()
if t != p.Type {
return nil, &wrongTypeError{Actual: t, Expected: p.Type}
}
if v.IsNil() {
return nil, nil
}
return p.ElemMarshaler.Marshal(v.Elem(), opts)
}
type arrayAndSliceMarshaler struct {
Type reflect.Type
ElemMarshaler Marshaler
}
func newArrayAndSliceMarshaler(t reflect.Type, opts *MarshalOptions) (Marshaler, error) {
k := t.Kind()
if k != reflect.Array && k != reflect.Slice {
return nil, &wrongKindError{Expected: reflect.Array, Actual: t}
}
em, err := opts.MarshalerFactory.Marshaler(t.Elem(), opts)
if err != nil {
return nil, err
}
return &arrayAndSliceMarshaler{
Type: t,
ElemMarshaler: em,
}, nil
}
func (p *arrayAndSliceMarshaler) Marshal(v reflect.Value, opts *MarshalOptions) ([]string, error) {
t := v.Type()
if t != p.Type {
return nil, &wrongTypeError{Actual: t, Expected: p.Type}
}
vlen := v.Len()
if vlen == 0 {
return nil, nil
}
a := make([]string, vlen)
for i := 0; i < vlen; i++ {
a2, err := p.ElemMarshaler.Marshal(v.Index(i), opts)
if err != nil {
return nil, fmt.Errorf("error marshaling array/slice index %v :: %v", i, err)
}
if len(a2) != 1 {
return nil, fmt.Errorf("marshaler returned a slice of length %v for array/slice index %v", len(a2), i)
}
a[i] = a2[0]
}
return a, nil
}
func marshalString(v reflect.Value, opts *MarshalOptions) (string, error) {
if v.Kind() != reflect.String {
return "", &wrongKindError{Expected: reflect.String, Actual: v.Type()}
}
return v.String(), nil
}
func marshalBool(v reflect.Value, opts *MarshalOptions) (string, error) {
if v.Kind() != reflect.Bool {
return "", &wrongKindError{Expected: reflect.Bool, Actual: v.Type()}
}
return strconv.FormatBool(v.Bool()), nil
}
func marshalInt(v reflect.Value, opts *MarshalOptions) (string, error) {
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(v.Int(), 10), nil
default:
return "", &wrongKindError{Expected: reflect.Int, Actual: v.Type()}
}
}
func marshalUint(v reflect.Value, opts *MarshalOptions) (string, error) {
switch v.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.FormatUint(v.Uint(), 10), nil
default:
return "", &wrongKindError{Expected: reflect.Uint, Actual: v.Type()}
}
}
func marshalFloat(v reflect.Value, opts *MarshalOptions) (string, error) {
var bitSize int
switch v.Kind() {
case reflect.Float32:
bitSize = 32
case reflect.Float64:
bitSize = 64
default:
return "", &wrongKindError{Expected: reflect.Float32, Actual: v.Type()}
}
return strconv.FormatFloat(v.Float(), 'f', -1, bitSize), nil
}
func marshalTime(v reflect.Value, opts *MarshalOptions) (string, error) {
t := v.Type()
if t != timeType {
return "", &wrongTypeError{Actual: t, Expected: timeType}
}
return v.Interface().(time.Time).Format(time.RFC3339), nil
}
func marshalURL(v reflect.Value, opts *MarshalOptions) (string, error) {
t := v.Type()
if t != urlType {
return "", &wrongTypeError{Actual: t, Expected: urlType}
}
u := v.Interface().(url.URL)
return u.String(), nil
}
func marshalWithMarshalQS(v reflect.Value, opts *MarshalOptions) ([]string, error) {
marshalQS, ok := v.Interface().(MarshalQS)
if !ok {
return nil, fmt.Errorf("expected a type that implements MarshalQS, got %v", v.Type())
}
return marshalQS.MarshalQS(opts)
}