Skip to content

Commit

Permalink
Fixed issue 197 (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
ohler55 authored Jan 9, 2025
1 parent c53cf6d commit 54cef83
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

The structure and content of this file follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [1.26.1] - 2025-01-09
### Fixed
- Fixed issue #197 where nested array elements were not filled when using `oj.Match()`.

## [1.26.0] - 2024-12-31
### Added
- Support for non-selector scripts added to the jp package. See the
Expand Down
31 changes: 31 additions & 0 deletions jp/match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
package jp_test

import (
"fmt"
"testing"

"github.com/ohler55/ojg/jp"
"github.com/ohler55/ojg/oj"
"github.com/ohler55/ojg/pretty"
"github.com/ohler55/ojg/sen"
"github.com/ohler55/ojg/tt"
)

Expand Down Expand Up @@ -54,3 +58,30 @@ func TestPathMatchDoubleRoot(t *testing.T) {
func TestPathMatchSkipBracket(t *testing.T) {
tt.Equal(t, true, jp.PathMatch(jp.B().C("a"), jp.C("a")))
}

func TestMatchNestedMapArray(t *testing.T) {
var buf []byte
err := oj.Match([]byte(`[{"a":[1, 2]}]`), func(path jp.Expr, data any) {
buf = fmt.Appendf(buf, "%s: %s", path, pretty.SEN(data))
}, jp.MustParseString("$[*]"))
tt.Nil(t, err)
tt.Equal(t, "$[0]: {a: [1 2]}", string(buf))
}

func TestMatchNestedArrays(t *testing.T) {
var buf []byte
err := oj.Match([]byte(`[[[1, [2]]]]`), func(path jp.Expr, data any) {
buf = fmt.Appendf(buf, "%s: %s", path, sen.Bytes(data))
}, jp.MustParseString("$[*]"))
tt.Nil(t, err)
tt.Equal(t, "$[0]: [[1 [2]]]", string(buf))
}

func TestMatchNestedMap(t *testing.T) {
var buf []byte
err := oj.Match([]byte(`{"a": {"b": 1, "c": {"d": 2}}}`), func(path jp.Expr, data any) {
buf = fmt.Appendf(buf, "%s: %s", path, pretty.SEN(data))
}, jp.MustParseString("$[*]"))
tt.Nil(t, err)
tt.Equal(t, "$.a: {b: 1 c: {d: 2}}", string(buf))
}
11 changes: 10 additions & 1 deletion jp/matchhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (h *MatchHandler) ArrayEnd() {
h.objArrayEnd()
}

// AddValue is called when a leave value is encountered.
// AddValue is called when a leaf value is encountered.
func (h *MatchHandler) AddValue(v any) {
if 0 < len(h.Stack) {
switch ts := h.Stack[len(h.Stack)-1].(type) {
Expand Down Expand Up @@ -142,7 +142,16 @@ func (h *MatchHandler) objArrayEnd() {
h.OnData(p, v)
}
}
v := h.Stack[len(h.Stack)-1]
h.Stack = h.Stack[:len(h.Stack)-1]
if 0 < len(h.Stack) {
switch ts := h.Stack[len(h.Stack)-1].(type) {
case map[string]any:
ts[string(h.Path[len(h.Path)-1].(Child))] = v
case []any:
ts[h.Path[len(h.Path)-1].(Nth)] = v
}
}
}
h.incNth()
}
Expand Down
7 changes: 7 additions & 0 deletions oj/tokenizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,10 @@ func TestTokenizerMany(t *testing.T) {
}
}
}

func TestTokenizerNesting(t *testing.T) {
var h testHandler
err := oj.TokenizeString(`[{"a":[1, 2]}]`, &h)
tt.Nil(t, err)
tt.Equal(t, "[ { a: [ 1 2 ] } ] ", string(h.buf))
}

0 comments on commit 54cef83

Please sign in to comment.