-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexample_custom_marshaler_factory_test.go
150 lines (129 loc) · 4.88 KB
/
example_custom_marshaler_factory_test.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
package qs_test
import (
"encoding/hex"
"fmt"
"reflect"
"time"
"github.com/pasztorpisti/qs"
)
// This example shows how to create QSMarshaler and QSUnmarshaler objects
// that have custom marshaler and unmarshaler factories to provide custom
// marshaling and unmarshaling for some types.
//
// In this example we change the default marshaling and unmarshaling of the
// []byte type and we compare our custom marshaler with the default one. You can
// not only change the behavior of already supported types (like []byte) but you
// can also add types that aren't supported by default - in this example we
// add time.Duration as one such type.
//
// Builtin unnamed golang types (like []byte) can't implement the MarshalQS and
// UnmarshalQS interfaces to provide their own marshaling, this is why we have
// to create custom QSMarshaler and QSUnmarshaler with custom factories for them.
func Example_customMarshalerFactory() {
customMarshaler := qs.NewMarshaler(&qs.MarshalOptions{
MarshalerFactory: &marshalerFactory{qs.NewDefaultMarshalOptions().MarshalerFactory},
})
customUnmarshaler := qs.NewUnmarshaler(&qs.UnmarshalOptions{
UnmarshalerFactory: &unmarshalerFactory{qs.NewDefaultUnmarshalOptions().UnmarshalerFactory},
})
performSliceTest("Default", qs.DefaultMarshaler, qs.DefaultUnmarshaler)
performSliceTest("Custom", customMarshaler, customUnmarshaler)
performDurationTest(customMarshaler, customUnmarshaler)
// Output:
// Default-Marshal-Result: a=0&a=1&a=2&b=3&b=4&b=5 <nil>
// Default-Unmarshal-Result: len=2 a=[0 1 2] b=[3 4 5] <nil>
// Custom-Marshal-Result: a=000102&b=030405 <nil>
// Custom-Unmarshal-Result: len=2 a=[0 1 2] b=[3 4 5] <nil>
// Duration-Marshal-Result: duration=1m1.2s <nil>
// Duration-Unmarshal-Result: len=1 duration=1m1.2s <nil>
}
func performSliceTest(name string, m *qs.QSMarshaler, um *qs.QSUnmarshaler) {
queryStr, err := m.Marshal(map[string][]byte{
"a": {0, 1, 2},
"b": {3, 4, 5},
})
fmt.Printf("%v-Marshal-Result: %v %v\n", name, queryStr, err)
var query map[string][]byte
err = um.Unmarshal(&query, queryStr)
fmt.Printf("%v-Unmarshal-Result: len=%v a=%v b=%v %v\n",
name, len(query), query["a"], query["b"], err)
}
func performDurationTest(m *qs.QSMarshaler, um *qs.QSUnmarshaler) {
queryStr, err := m.Marshal(map[string]time.Duration{
"duration": time.Millisecond * (61*1000 + 200),
})
fmt.Printf("Duration-Marshal-Result: %v %v\n", queryStr, err)
var query map[string]time.Duration
err = um.Unmarshal(&query, queryStr)
fmt.Printf("Duration-Unmarshal-Result: len=%v duration=%v %v\n",
len(query), query["duration"].String(), err)
}
var byteSliceType = reflect.TypeOf([]byte(nil))
var durationType = reflect.TypeOf(time.Duration(0))
// marshalerFactory implements the MarshalerFactory interface and provides
// custom Marshaler for the []byte type.
type marshalerFactory struct {
orig qs.MarshalerFactory
}
func (f *marshalerFactory) Marshaler(t reflect.Type, opts *qs.MarshalOptions) (qs.Marshaler, error) {
switch t {
case byteSliceType:
return byteSliceMarshaler{}, nil
case durationType:
return durationMarshalerInstance, nil
default:
return f.orig.Marshaler(t, opts)
}
}
// unmarshalerFactory implements the UnmarshalerFactory interface and provides
// custom Unmarshaler for the []byte type.
type unmarshalerFactory struct {
orig qs.UnmarshalerFactory
}
func (f *unmarshalerFactory) Unmarshaler(t reflect.Type, opts *qs.UnmarshalOptions) (qs.Unmarshaler, error) {
switch t {
case byteSliceType:
return byteSliceMarshaler{}, nil
case durationType:
return durationMarshalerInstance, nil
default:
return f.orig.Unmarshaler(t, opts)
}
}
// byteSliceMarshaler implements the Marshaler and Unmarshaler interfaces to
// provide custom marshaling and unmarshaling for the []byte type.
type byteSliceMarshaler struct{}
func (byteSliceMarshaler) Marshal(v reflect.Value, opts *qs.MarshalOptions) ([]string, error) {
return []string{hex.EncodeToString(v.Interface().([]byte))}, nil
}
func (byteSliceMarshaler) Unmarshal(v reflect.Value, a []string, opts *qs.UnmarshalOptions) error {
s, err := opts.SliceToString(a)
if err != nil {
return err
}
b, err := hex.DecodeString(s)
if err != nil {
return err
}
v.Set(reflect.ValueOf(b))
return nil
}
var durationMarshalerInstance = &durationMarshaler{}
// durationMarshaler implements the Marshaler and Unmarshaler interfaces to
// provide custom marshaling and unmarshaling for the time.Duration type.
type durationMarshaler struct{}
func (o *durationMarshaler) Marshal(v reflect.Value, opts *qs.MarshalOptions) ([]string, error) {
return []string{v.Interface().(time.Duration).String()}, nil
}
func (o *durationMarshaler) Unmarshal(v reflect.Value, a []string, opts *qs.UnmarshalOptions) error {
s, err := opts.SliceToString(a)
if err != nil {
return err
}
t, err := time.ParseDuration(s)
if err != nil {
return fmt.Errorf("unsupported time format: %v", s)
}
v.Set(reflect.ValueOf(t))
return nil
}