Handles keys that dont exit

This commit is contained in:
Mike Farah 2015-10-03 17:06:33 +10:00
parent d1c545cca0
commit 690442d02e
2 changed files with 8 additions and 2 deletions

View File

@ -30,6 +30,8 @@ func recurse(value interface{}, head string, tail []string) interface{} {
log.Fatalf("Error accessing array: %v", err)
}
return readArray(value.([]interface{}), index, tail)
case nil:
return nil
default:
return readMap(value.(map[interface{}]interface{}), head, tail)
}

View File

@ -31,11 +31,15 @@ func TestReadMap_simple(t *testing.T) {
assertResult(t, 2, readMap(parsedData, "b", []string{"c"}))
}
func TestReadMap_array(t *testing.T) {
func TestReadMap_key_doesnt_exist(t *testing.T) {
assertResult(t, nil, readMap(parsedData, "b.x.f", []string{"c"}))
}
func TestReadMap_with_array(t *testing.T) {
assertResult(t, 4, readMap(parsedData, "b", []string{"d", "1"}))
}
func TestReadMap_array_out_of_bounds(t *testing.T) {
func TestReadMap_with_array_out_of_bounds(t *testing.T) {
assertResult(t, nil, readMap(parsedData, "b", []string{"d", "3"}))
}