-
Notifications
You must be signed in to change notification settings - Fork 5
/
marshaler_factory.go
160 lines (132 loc) · 5.47 KB
/
marshaler_factory.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
package qs
import (
"net/url"
"reflect"
)
// ValuesMarshaler can marshal a value into a url.Values.
type ValuesMarshaler interface {
// MarshalValues marshals the given v value using opts into a url.Values.
MarshalValues(v reflect.Value, opts *MarshalOptions) (url.Values, error)
}
// ValuesMarshalerFactory can create ValuesMarshaler objects for various types.
type ValuesMarshalerFactory interface {
// ValuesMarshaler returns a ValuesMarshaler object for the given t type and
// opts options.
ValuesMarshaler(t reflect.Type, opts *MarshalOptions) (ValuesMarshaler, error)
}
// Marshaler can marshal a reflect.Value into a []string.
type Marshaler interface {
// Marshal marshals the given v value using opts into a []string.
// Note that []string is the value type of the standard url.Values which is
// a map[string][]string.
Marshal(v reflect.Value, opts *MarshalOptions) ([]string, error)
}
// MarshalerFactory can create Marshaler objects for various types.
type MarshalerFactory interface {
// Marshaler returns a Marshaler object for the given t type and opts
// options.
Marshaler(t reflect.Type, opts *MarshalOptions) (Marshaler, error)
}
// MarshalQS is an interface that can be implemented by any type that
// wants to handle its own marshaling instead of relying on the default
// marshaling provided by this package.
type MarshalQS interface {
// MarshalQS is essentially the same as the Marshaler.Marshal
// method without its v parameter.
MarshalQS(opts *MarshalOptions) ([]string, error)
}
func newValuesMarshalerFactory() ValuesMarshalerFactory {
return &valuesMarshalerFactory{
KindSubRegistries: map[reflect.Kind]ValuesMarshalerFactory{
reflect.Ptr: valuesMarshalerFactoryFunc(newPtrValuesMarshaler),
reflect.Struct: valuesMarshalerFactoryFunc(newStructMarshaler),
reflect.Map: valuesMarshalerFactoryFunc(newMapMarshaler),
},
}
}
func newMarshalerFactory() MarshalerFactory {
return &marshalerFactory{
Types: map[reflect.Type]Marshaler{
timeType: primitiveMarshalerFunc(marshalTime),
urlType: primitiveMarshalerFunc(marshalURL),
},
KindSubRegistries: map[reflect.Kind]MarshalerFactory{
reflect.Ptr: marshalerFactoryFunc(newPtrMarshaler),
reflect.Array: marshalerFactoryFunc(newArrayAndSliceMarshaler),
reflect.Slice: marshalerFactoryFunc(newArrayAndSliceMarshaler),
},
Kinds: map[reflect.Kind]Marshaler{
reflect.String: primitiveMarshalerFunc(marshalString),
reflect.Bool: primitiveMarshalerFunc(marshalBool),
reflect.Int: primitiveMarshalerFunc(marshalInt),
reflect.Int8: primitiveMarshalerFunc(marshalInt),
reflect.Int16: primitiveMarshalerFunc(marshalInt),
reflect.Int32: primitiveMarshalerFunc(marshalInt),
reflect.Int64: primitiveMarshalerFunc(marshalInt),
reflect.Uint: primitiveMarshalerFunc(marshalUint),
reflect.Uint8: primitiveMarshalerFunc(marshalUint),
reflect.Uint16: primitiveMarshalerFunc(marshalUint),
reflect.Uint32: primitiveMarshalerFunc(marshalUint),
reflect.Uint64: primitiveMarshalerFunc(marshalUint),
reflect.Float32: primitiveMarshalerFunc(marshalFloat),
reflect.Float64: primitiveMarshalerFunc(marshalFloat),
},
}
}
// valuesMarshalerFactory implements the ValuesMarshalerFactory interface.
type valuesMarshalerFactory struct {
KindSubRegistries map[reflect.Kind]ValuesMarshalerFactory
}
func (p *valuesMarshalerFactory) ValuesMarshaler(t reflect.Type, opts *MarshalOptions) (ValuesMarshaler, error) {
if subFactory, ok := p.KindSubRegistries[t.Kind()]; ok {
return subFactory.ValuesMarshaler(t, opts)
}
return nil, &unhandledTypeError{Type: t}
}
// marshalerFactory implements the MarshalerFactory interface.
type marshalerFactory struct {
Types map[reflect.Type]Marshaler
KindSubRegistries map[reflect.Kind]MarshalerFactory
Kinds map[reflect.Kind]Marshaler
}
var marshalQSInterfaceType = reflect.TypeOf((*MarshalQS)(nil)).Elem()
func (p *marshalerFactory) Marshaler(t reflect.Type, opts *MarshalOptions) (Marshaler, error) {
if marshaler, ok := p.Types[t]; ok {
return marshaler, nil
}
if t.Implements(marshalQSInterfaceType) {
return marshalerFunc(marshalWithMarshalQS), nil
}
k := t.Kind()
if subFactory, ok := p.KindSubRegistries[k]; ok {
return subFactory.Marshaler(t, opts)
}
if marshaler, ok := p.Kinds[k]; ok {
return marshaler, nil
}
return nil, &unhandledTypeError{Type: t}
}
// valuesMarshalerFactoryFunc implements the ValuesMarshalerFactory interface.
type valuesMarshalerFactoryFunc func(t reflect.Type, opts *MarshalOptions) (ValuesMarshaler, error)
func (f valuesMarshalerFactoryFunc) ValuesMarshaler(t reflect.Type, opts *MarshalOptions) (ValuesMarshaler, error) {
return f(t, opts)
}
// marshalerFactoryFunc implements the MarshalerFactory interface.
type marshalerFactoryFunc func(t reflect.Type, opts *MarshalOptions) (Marshaler, error)
func (f marshalerFactoryFunc) Marshaler(t reflect.Type, opts *MarshalOptions) (Marshaler, error) {
return f(t, opts)
}
// marshalerFunc implements the Marshaler interface.
type marshalerFunc func(v reflect.Value, opts *MarshalOptions) ([]string, error)
func (f marshalerFunc) Marshal(v reflect.Value, opts *MarshalOptions) ([]string, error) {
return f(v, opts)
}
// primitiveMarshalerFunc implements the Marshaler interface.
type primitiveMarshalerFunc func(v reflect.Value, opts *MarshalOptions) (string, error)
func (f primitiveMarshalerFunc) Marshal(v reflect.Value, opts *MarshalOptions) ([]string, error) {
s, err := f(v, opts)
if err != nil {
return nil, err
}
return []string{s}, nil
}