@@ -163,7 +163,7 @@ func TestToMap(t *testing.T) {
163163 }
164164}
165165
166- func TestUnmarhalAndMarshalJSON (t * testing.T ) {
166+ func TestUnmarhalValueAsAnyAndMarshalJSON (t * testing.T ) {
167167 t .Parallel ()
168168
169169 tests := []struct {
@@ -225,32 +225,51 @@ func TestUnmarhalAndMarshalJSON(t *testing.T) {
225225 }
226226}
227227
228- func TestUnmarhalAndMarshalJSONForSlice (t * testing.T ) {
228+ func TestUnmarhalAndMarshalJSON (t * testing.T ) {
229229 t .Parallel ()
230230
231231 tests := []struct {
232- name string
233- json string
232+ name string
233+ json string
234+ unmarshal func (string ) (string , error )
234235 }{
235236 {
236- name : "empty array" ,
237+ name : "float64" ,
238+ json : `{"test":3.14}` ,
239+ unmarshal : func (data string ) (string , error ) {
240+ var m orderedmap.OrderedMap [string , float64 ]
241+ err := json .Unmarshal (json .RawMessage (data ), & m )
242+ return m .String (), err
243+ },
244+ },
245+ {
246+ name : "empty slice" ,
237247 json : `{"a1":[]}` ,
248+ unmarshal : func (data string ) (string , error ) {
249+ var m orderedmap.OrderedMap [string , []string ]
250+ err := json .Unmarshal (json .RawMessage (data ), & m )
251+ return m .String (), err
252+ },
238253 },
239254 {
240- name : "strings array " ,
255+ name : "string slice " ,
241256 json : `{"a1":["t1","t3","t2"]}` ,
257+ unmarshal : func (data string ) (string , error ) {
258+ var m orderedmap.OrderedMap [string , []string ]
259+ err := json .Unmarshal (json .RawMessage (data ), & m )
260+ return m .String (), err
261+ },
242262 },
243263 }
244264
245265 for _ , tt := range tests {
246266 t .Run (tt .name , func (t * testing.T ) {
247267 t .Parallel ()
248- var m orderedmap. OrderedMap [ string , [] string ]
249- if err := json . Unmarshal ( json . RawMessage ( tt . json ), & m ); err != nil {
268+ actual , err := tt . unmarshal ( tt . json )
269+ if err != nil {
250270 t .Error (err )
251271 return
252272 }
253- actual := m .String ()
254273 if diff := cmp .Diff (tt .json , actual ); diff != "" {
255274 t .Error (diff )
256275 return
@@ -284,16 +303,16 @@ func TestErrorUnmarhalAndMarshalJSON(t *testing.T) {
284303 var m orderedmap.OrderedMap [string , int ]
285304 return json .Unmarshal (json .RawMessage (data ), & m )
286305 },
287- expected : errors .New ("failed to decode key/value: invalid value type : failed to assert 5 (float64) as int" ),
306+ expected : errors .New ("failed to decode key/value: failed to decode value : failed to assert 5 (float64) as int" ),
288307 },
289308 {
290- name : "wrong key type" ,
291- json : `{"a ":"test"}` ,
309+ name : "not string key type" ,
310+ json : `{"3 ":"test"}` ,
292311 unmarshal : func (data string ) error {
293312 var m orderedmap.OrderedMap [int , string ]
294313 return json .Unmarshal (json .RawMessage (data ), & m )
295314 },
296- expected : errors .New ("failed to decode key/value: invalid key type: failed to assert a ( string) as int " ),
315+ expected : errors .New ("failed to validate type: key type must be string" ),
297316 },
298317 {
299318 name : "wrong value type" ,
@@ -302,7 +321,7 @@ func TestErrorUnmarhalAndMarshalJSON(t *testing.T) {
302321 m := orderedmap .New [string , json.RawMessage ]()
303322 return json .Unmarshal ([]byte (data ), m )
304323 },
305- expected : errors .New ("failed to decode key/value: invalid value type : failed to assert b (string) as slice" ),
324+ expected : errors .New ("failed to decode key/value: failed to decode value : failed to assert b (string) as slice" ),
306325 },
307326 {
308327 name : "wrong the type of slice" ,
@@ -311,25 +330,56 @@ func TestErrorUnmarhalAndMarshalJSON(t *testing.T) {
311330 var m orderedmap.OrderedMap [string , []int ]
312331 return json .Unmarshal (json .RawMessage (data ), & m )
313332 },
314- expected : errors .New ("failed to decode key/value: invalid value type: failed to convert slice: failed to assert 2 (float64) as int" ),
333+ expected : errors .New ("failed to decode key/value: failed to decode value: failed to convert slice: failed to assert 2 (float64) as int" ),
334+ },
335+ {
336+ name : "unsupported the map type" ,
337+ json : `{"value":{"k":1}}` ,
338+ unmarshal : func (data string ) error {
339+ var m orderedmap.OrderedMap [string , map [string ]int ]
340+ return json .Unmarshal (json .RawMessage (data ), & m )
341+ },
342+ expected : errors .New ("failed to validate type: unsupported map, use any" ),
343+ },
344+ {
345+ name : "unsupported the struct type" ,
346+ json : `{"value":{"k":1}}` ,
347+ unmarshal : func (data string ) error {
348+ type KeyValue struct {
349+ Key string
350+ Value int
351+ }
352+ var m orderedmap.OrderedMap [string , KeyValue ]
353+ return json .Unmarshal (json .RawMessage (data ), & m )
354+ },
355+ expected : errors .New ("failed to validate type: unsupported struct, use any" ),
315356 },
316357 {
317358 name : "unsupported the pointer type" ,
318- json : `{"values ": 5}` ,
359+ json : `{"value ": 5}` ,
319360 unmarshal : func (data string ) error {
320361 var m orderedmap.OrderedMap [string , * int ]
321362 return json .Unmarshal (json .RawMessage (data ), & m )
322363 },
323- expected : errors .New ("failed to decode key/value: invalid value type: failed to assert 5 (float64) as *int " ),
364+ expected : errors .New ("failed to validate type: unsupported pointer, use any " ),
324365 },
325366 {
326367 name : "unsupported the pointer slice type" ,
327- json : `{"values ": 5}` ,
368+ json : `{"value ": 5}` ,
328369 unmarshal : func (data string ) error {
329370 var m orderedmap.OrderedMap [string , * []int ]
330371 return json .Unmarshal (json .RawMessage (data ), & m )
331372 },
332- expected : errors .New ("failed to decode key/value: invalid value type: failed to assert 5 (float64) as *[]int" ),
373+ expected : errors .New ("failed to validate type: unsupported pointer, use any" ),
374+ },
375+ {
376+ name : "unsupported the array type" ,
377+ json : `{"a1":["t1","t3"]}` ,
378+ unmarshal : func (data string ) error {
379+ var m orderedmap.OrderedMap [string , [2 ]string ]
380+ return json .Unmarshal (json .RawMessage (data ), & m )
381+ },
382+ expected : errors .New ("failed to validate type: unsupported array, use slice" ),
333383 },
334384 }
335385
0 commit comments