From e9a296e8bc8f25138ea5910c8b09661f8ae25f10 Mon Sep 17 00:00:00 2001 From: Charlie Stanton Date: Mon, 25 Mar 2024 15:36:31 +0000 Subject: Fix bug in json/write.go inMapAt and inArrayAt --- json/write.go | 8 ++++---- json/write_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/json/write.go b/json/write.go index c2a220e..334dfc0 100644 --- a/json/write.go +++ b/json/write.go @@ -209,7 +209,7 @@ func (writer *JSONWriter) navigateTo(keepLen int, path []walk.PathSegment, state } func (writer *JSONWriter) inMapAt(keepLen int, path []walk.PathSegment) bool { - if keepLen < len(path) { + if len(path) != 0 { return false } @@ -217,12 +217,12 @@ func (writer *JSONWriter) inMapAt(keepLen int, path []walk.PathSegment) bool { return writer.state == JSONWriterStateInMap } - _, isString := writer.path[keepLen].(string) + _, isString := writer.path[0].(string) return isString } func (writer *JSONWriter) inArrayAt(keepLen int, path []walk.PathSegment) bool { - if keepLen < len(path) { + if len(path) != 0 { return false } @@ -230,7 +230,7 @@ func (writer *JSONWriter) inArrayAt(keepLen int, path []walk.PathSegment) bool { return writer.state == JSONWriterStateInArray } - _, isInt := writer.path[keepLen].(int) + _, isInt := writer.path[0].(int) return isInt } diff --git a/json/write_test.go b/json/write_test.go index 05b228e..a0e8c3e 100644 --- a/json/write_test.go +++ b/json/write_test.go @@ -222,6 +222,47 @@ func TestWrite(t *testing.T) { }, expected: `[[100],[],null,[200]]`, }, + { + values: []walk.Value { + walk.MapValue {{ + Key: "a", + Value: walk.MapValue {{ + Key: "b", + Value: walk.StringValue("map"), + }}, + }}, + walk.MapValue {{ + Key: "a", + Value: walk.ArrayValue {{ + Index: 0, + Value: walk.StringValue("array"), + }}, + }}, + }, + expected: `{"a":{"b":"map"},"a":["array"]}`, + }, + { + values: []walk.Value { + walk.ArrayValue {{ + Index: 0, + Value: walk.ArrayValue {{ + Index: 0, + Value: walk.ArrayValue {{ + Index: 1, + Value: walk.StringValue("a"), + }}, + }}, + }}, + walk.ArrayValue {{ + Index: 0, + Value: walk.ArrayValue {{ + Index: 1, + Value: walk.ArrayValue{}, + }}, + }}, + }, + expected: `[[["a"],[]]]`, + }, } for i, test := range tests { -- cgit v1.2.3