diff --git a/internal/command/select_test.go b/internal/command/select_test.go index b584bcfc..cc8b6bf5 100644 --- a/internal/command/select_test.go +++ b/internal/command/select_test.go @@ -175,4 +175,33 @@ c: bar`)), nil, nil, )) + + t.Run("Issue316", runTest( + []string{"-r", "json"}, + []byte(`{ + "a": "alice", + "b": null, + "c": [ + { + "d": 9, + "e": null + }, + null + ] +}`), + newline([]byte(`{ + "a": "alice", + "b": null, + "c": [ + { + "d": 9, + "e": null + }, + null + ] +}`)), + nil, + nil, + )) + } diff --git a/value.go b/value.go index 03c6ffae..b1751220 100644 --- a/value.go +++ b/value.go @@ -387,7 +387,13 @@ func makeAddressable(value reflect.Value) reflect.Value { if isdencodingMap(unpacked) { om := value.Interface().(*dencoding.Map) for _, kv := range om.KeyValues() { - om.Set(kv.Key, makeAddressable(reflect.ValueOf(kv.Value)).Interface()) + var val any + if v := deref(reflect.ValueOf(kv.Value)); v.IsValid() { + val = makeAddressable(v).Interface() + } else { + val = nil + } + om.Set(kv.Key, val) } return value } @@ -422,7 +428,11 @@ func derefMap(value reflect.Value) reflect.Value { res := reflect.MakeMap(unpacked.Type()) for _, key := range unpacked.MapKeys() { - res.SetMapIndex(key, deref(unpacked.MapIndex(key))) + if v := deref(unpacked.MapIndex(key)); v.IsValid() { + res.SetMapIndex(key, v) + } else { + res.SetMapIndex(key, reflect.ValueOf(new(any))) + } } return res @@ -434,7 +444,11 @@ func deref(value reflect.Value) reflect.Value { if isdencodingMap(unpacked) { om := value.Interface().(*dencoding.Map) for _, kv := range om.KeyValues() { - om.Set(kv.Key, deref(reflect.ValueOf(kv.Value)).Interface()) + if v := deref(reflect.ValueOf(kv.Value)); v.IsValid() { + om.Set(kv.Key, v.Interface()) + } else { + om.Set(kv.Key, nil) + } } return value }