From bc21dd91e076d3a69042043e8be7fcd8a3c9fc3d Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Sun, 8 Dec 2024 00:19:43 -0800 Subject: [PATCH] Assume Names stack is always populated (#69) In #65, we always populate the JSON pointer stack. Thus, there is no need to use the fallback logic of encoding the missing name with the member index. --- jsontext/state.go | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/jsontext/state.go b/jsontext/state.go index aa1b7c9..e2f4d47 100644 --- a/jsontext/state.go +++ b/jsontext/state.go @@ -86,8 +86,6 @@ func (p Pointer) Tokens() iter.Seq[string] { } // appendStackPointer appends a JSON Pointer (RFC 6901) to the current value. -// The returned pointer is only accurate if s.names is populated, -// otherwise it uses the numeric index as the object member name. // // Invariant: Must call s.names.copyQuotedBuffer beforehand. func (s state) appendStackPointer(b []byte) []byte { @@ -100,23 +98,16 @@ func (s state) appendStackPointer(b []byte) []byte { b = append(b, '/') switch { case e.isObject(): - if objectDepth < s.Names.length() { - for _, c := range s.Names.getUnquoted(objectDepth) { - // Per RFC 6901, section 3, escape '~' and '/' characters. - switch c { - case '~': - b = append(b, "~0"...) - case '/': - b = append(b, "~1"...) - default: - b = append(b, c) - } + for _, c := range s.Names.getUnquoted(objectDepth) { + // Per RFC 6901, section 3, escape '~' and '/' characters. + switch c { + case '~': + b = append(b, "~0"...) + case '/': + b = append(b, "~1"...) + default: + b = append(b, c) } - } else { - // Since the names stack is unpopulated, the name is unknown. - // As a best-effort replacement, use the numeric member index. - // While inaccurate, it produces a syntactically valid pointer. - b = strconv.AppendUint(b, uint64((e.Length()-1)/2), 10) } objectDepth++ case e.isArray():