From 838492087493baa0e9ab22337777d0111b3c4295 Mon Sep 17 00:00:00 2001 From: semihbkgr Date: Sat, 6 May 2023 23:43:10 +0300 Subject: [PATCH 1/3] set nil value to the map when the value is invalid --- value.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/value.go b/value.go index 03c6ffae..57ccd954 100644 --- a/value.go +++ b/value.go @@ -422,7 +422,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 From e5bc0e07196e37da2576b43d1abbf298455f8ac0 Mon Sep 17 00:00:00 2001 From: semihbkgr Date: Sun, 7 May 2023 18:36:58 +0300 Subject: [PATCH 2/3] adding select command test --- internal/command/select_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) 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, + )) + } From e039cb6525f5a43e652a923ac2fe6190842e25a8 Mon Sep 17 00:00:00 2001 From: Tom Wright Date: Fri, 12 May 2023 16:01:39 +0100 Subject: [PATCH 3/3] Fix null value in ordered maps --- value.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/value.go b/value.go index 57ccd954..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 } @@ -438,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 }