(t *testing.T)
| 7 | ) |
| 8 | |
| 9 | func TestSimpleExample(t *testing.T) { |
| 10 | // build a map from a JSON object |
| 11 | o := objx.MustFromJSON(`{"name":"Mat","foods":["indian","chinese"], "location":{"county":"hobbiton","city":"the shire"}}`) |
| 12 | |
| 13 | // Map can be used as a straight map[string]interface{} |
| 14 | assert.Equal(t, o["name"], "Mat") |
| 15 | |
| 16 | // Get an Value object |
| 17 | v := o.Get("name") |
| 18 | require.NotNil(t, v) |
| 19 | |
| 20 | // Test the contained value |
| 21 | assert.False(t, v.IsInt()) |
| 22 | assert.False(t, v.IsBool()) |
| 23 | assert.True(t, v.IsStr()) |
| 24 | |
| 25 | // Get the contained value |
| 26 | assert.Equal(t, v.Str(), "Mat") |
| 27 | |
| 28 | // Get a default value if the contained value is not of the expected type or does not exist |
| 29 | assert.Equal(t, 1, v.Int(1)) |
| 30 | |
| 31 | // Get a value by using array notation |
| 32 | assert.Equal(t, "indian", o.Get("foods[0]").Data()) |
| 33 | |
| 34 | // Set a value by using array notation |
| 35 | o.Set("foods[0]", "italian") |
| 36 | assert.Equal(t, "italian", o.Get("foods[0]").Str()) |
| 37 | |
| 38 | // Get a value by using dot notation |
| 39 | assert.Equal(t, "hobbiton", o.Get("location.county").Str()) |
| 40 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…