...

Source file src/github.com/ory/x/jsonx/flatten_test.go

Documentation: github.com/ory/x/jsonx

     1  package jsonx
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestFlatten(t *testing.T) {
    13  	f, err := ioutil.ReadFile("./stub/random.json")
    14  	require.NoError(t, err)
    15  
    16  	for k, tc := range []struct {
    17  		raw      []byte
    18  		expected map[string]interface{}
    19  	}{
    20  		{
    21  			raw:      f,
    22  			expected: map[string]interface{}{"fall": "to", "floating.0": -1.273085434e+09, "floating.1": 9.53442581e+08, "floating.2.gray.buy": true, "floating.2.gray.hold.0.0": 1.81518765e+08, "floating.2.gray.hold.0.1.0.flies": -1.571371799e+09, "floating.2.gray.hold.0.1.0.leather": "across", "floating.2.gray.hold.0.1.0.over": 5.12666854e+08, "floating.2.gray.hold.0.1.0.shaking": true, "floating.2.gray.hold.0.1.0.steam.ago": true, "floating.2.gray.hold.0.1.0.steam.appropriate": 1.249911539e+09, "floating.2.gray.hold.0.1.0.steam.box": false, "floating.2.gray.hold.0.1.0.steam.cry": 1.463961818e+09, "floating.2.gray.hold.0.1.0.steam.entirely": -8.51427469e+08, "floating.2.gray.hold.0.1.0.steam.through": 6.95239749e+08, "floating.2.gray.hold.0.1.0.thank": true, "floating.2.gray.hold.0.1.1": "hit", "floating.2.gray.hold.0.1.2": -6.481787444899056e+08, "floating.2.gray.hold.0.1.3": 1.225027271e+09, "floating.2.gray.hold.0.1.4": -1.481507228e+09, "floating.2.gray.hold.0.1.5": true, "floating.2.gray.hold.0.2": -2.114582277e+09, "floating.2.gray.hold.0.3": 1.3900602049360588e+09, "floating.2.gray.hold.0.4": 1.6156026309049141e+09, "floating.2.gray.hold.0.5": "darkness", "floating.2.gray.hold.1": 6.3427197713988304e+07, "floating.2.gray.hold.2": -5.80344963961421e+08, "floating.2.gray.hold.3": "stems", "floating.2.gray.hold.4": 1.016960217612642e+09, "floating.2.gray.hold.5": 1.240918909e+09, "floating.2.gray.parent": "pull", "floating.2.gray.shore": -7.38396277e+08, "floating.2.gray.usually": 1.050049449e+09, "floating.2.gray.wonder": false, "floating.2.joy": "difference", "floating.2.little": "cloud", "floating.2.probably": -4.13625494e+08, "floating.2.ready": "silent", "floating.2.worker": "situation", "floating.3": "grade", "floating.4": false, "floating.5": "thou", "product": "whale", "shop": 1.294397217e+09, "spend": "greatest", "wagon": -1.722583702e+09},
    23  		},
    24  		{raw: []byte(`{"foo":"bar"}`), expected: map[string]interface{}{"foo": "bar"}},
    25  		{raw: []byte(`{"foo":["bar",{"foo":"bar"}]}`), expected: map[string]interface{}{"foo.0": "bar", "foo.1.foo": "bar"}},
    26  		{raw: []byte(`{"foo":"bar","baz":{"bar":"foo"}}`), expected: map[string]interface{}{"foo": "bar", "baz.bar": "foo"}},
    27  		{
    28  			raw:      []byte(`{"foo":"bar","baz":{"bar":"foo"},"bar":["foo","bar","baz"]}`),
    29  			expected: map[string]interface{}{"bar.0": "foo", "bar.1": "bar", "bar.2": "baz", "baz.bar": "foo", "foo": "bar"},
    30  		},
    31  		{raw: []byte(`[]`), expected: nil},
    32  		{raw: []byte(`null`), expected: nil},
    33  		{raw: []byte(`"bar"`), expected: nil},
    34  	} {
    35  		t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
    36  			assert.EqualValues(t, tc.expected, Flatten(tc.raw))
    37  		})
    38  	}
    39  }
    40  

View as plain text