1package test
2
3import (
4	"bytes"
5	"encoding/json"
6	"time"
7)
8
9func init() {
10	var pString = func(val string) *string {
11		return &val
12	}
13	epoch := time.Unix(0, 0)
14	unmarshalCases = append(unmarshalCases, unmarshalCase{
15		ptr: (*struct {
16			Field interface{}
17		})(nil),
18		input: `{"Field": "hello"}`,
19	}, unmarshalCase{
20		ptr: (*struct {
21			Field interface{}
22		})(nil),
23		input: `{"Field": "hello"}       `,
24	}, unmarshalCase{
25		ptr: (*struct {
26			Field int `json:"field"`
27		})(nil),
28		input: `{"field": null}`,
29	}, unmarshalCase{
30		ptr: (*struct {
31			ID      int                    `json:"id"`
32			Payload map[string]interface{} `json:"payload"`
33			buf     *bytes.Buffer
34		})(nil),
35		input: ` {"id":1, "payload":{"account":"123","password":"456"}}`,
36	}, unmarshalCase{
37		ptr: (*struct {
38			Field1 string
39		})(nil),
40		input: `{"Field\"1":"hello"}`,
41	}, unmarshalCase{
42		ptr: (*struct {
43			Field1 string
44		})(nil),
45		input: `{"\u0046ield1":"hello"}`,
46	}, unmarshalCase{
47		ptr: (*struct {
48			Field1 *string
49			Field2 *string
50		})(nil),
51		input: `{"field1": null, "field2": "world"}`,
52	}, unmarshalCase{
53		ptr: (*struct {
54			Field1 string
55			Field2 json.RawMessage
56		})(nil),
57		input: `{"field1": "hello", "field2":[1,2,3]}`,
58	}, unmarshalCase{
59		ptr: (*struct {
60			a int
61			b <-chan int
62			C int
63			d *time.Timer
64		})(nil),
65		input: `{"a": 444, "b":"bad", "C":256, "d":{"not":"a timer"}}`,
66	}, unmarshalCase{
67		ptr: (*struct {
68			A string
69			B string
70			C string
71			D string
72			E string
73			F string
74			G string
75			H string
76			I string
77			J string
78			K string
79		})(nil),
80		input: `{"a":"1","b":"2","c":"3","d":"4","e":"5","f":"6","g":"7","h":"8","i":"9","j":"10","k":"11"}`,
81	}, unmarshalCase{
82		ptr: (*struct {
83			T float64 `json:"T"`
84		})(nil),
85		input: `{"t":10.0}`,
86	}, unmarshalCase{
87		ptr: (*struct {
88			T float64 `json:"T"`
89		})(nil),
90		input: `{"T":10.0}`,
91	}, unmarshalCase{
92		ptr: (*struct {
93			T float64 `json:"t"`
94		})(nil),
95		input: `{"T":10.0}`,
96	}, unmarshalCase{
97		ptr: (*struct {
98			KeyString string       `json:"key_string"`
99			Type      string       `json:"type"`
100			Asks      [][2]float64 `json:"asks"`
101		})(nil),
102		input: `{"key_string": "KEYSTRING","type": "TYPE","asks": [[1e+66,1]]}`,
103	})
104	marshalCases = append(marshalCases,
105		struct {
106			Field map[string]interface{}
107		}{
108			map[string]interface{}{"hello": "world"},
109		},
110		struct {
111			Field  map[string]interface{}
112			Field2 string
113		}{
114			map[string]interface{}{"hello": "world"}, "",
115		},
116		struct {
117			Field interface{}
118		}{
119			1024,
120		},
121		struct {
122			Field MyInterface
123		}{
124			MyString("hello"),
125		},
126		struct {
127			F *float64
128		}{},
129		struct {
130			*time.Time
131		}{&epoch},
132		struct {
133			*StructVarious
134		}{&StructVarious{}},
135		struct {
136			*StructVarious
137			Field int
138		}{nil, 10},
139		struct {
140			Field1 int
141			Field2 [1]*float64
142		}{},
143		struct {
144			Field interface{} `json:"field,omitempty"`
145		}{},
146		struct {
147			Field MyInterface `json:"field,omitempty"`
148		}{},
149		struct {
150			Field MyInterface `json:"field,omitempty"`
151		}{MyString("hello")},
152		struct {
153			Field json.Marshaler `json:"field"`
154		}{},
155		struct {
156			Field MyInterface `json:"field"`
157		}{},
158		struct {
159			Field MyInterface `json:"field"`
160		}{MyString("hello")},
161		struct {
162			Field1 string `json:"field-1,omitempty"`
163			Field2 func() `json:"-"`
164		}{},
165		structRecursive{},
166		struct {
167			*CacheItem
168
169			// Omit bad keys
170			OmitMaxAge omit `json:"cacheAge,omitempty"`
171
172			// Add nice keys
173			MaxAge int `json:"max_age"`
174		}{
175			CacheItem: &CacheItem{
176				Key:    "value",
177				MaxAge: 100,
178			},
179			MaxAge: 20,
180		},
181		structOrder{},
182		struct {
183			Field1 *string
184			Field2 *string
185		}{Field2: pString("world")},
186		struct {
187			a int
188			b <-chan int
189			C int
190			d *time.Timer
191		}{
192			a: 42,
193			b: make(<-chan int, 10),
194			C: 21,
195			d: time.NewTimer(10 * time.Second),
196		},
197	)
198}
199
200type StructVarious struct {
201	Field0 string
202	Field1 []string
203	Field2 map[string]interface{}
204}
205
206type structRecursive struct {
207	Field1 string
208	Me     *structRecursive
209}
210
211type omit *struct{}
212type CacheItem struct {
213	Key    string `json:"key"`
214	MaxAge int    `json:"cacheAge"`
215}
216
217type orderA struct {
218	Field2 string
219}
220
221type orderC struct {
222	Field5 string
223}
224
225type orderB struct {
226	Field4 string
227	orderC
228	Field6 string
229}
230
231type structOrder struct {
232	Field1 string
233	orderA
234	Field3 string
235	orderB
236	Field7 string
237}
238