diff --git a/README.md b/README.md index e823785..269883d 100644 --- a/README.md +++ b/README.md @@ -114,8 +114,9 @@ Example of buffer reuse: ```go parsed, _ := jsonpath.Parse("$[*]") buf := make([]any, 0, 4) -out1, _ := parsed([]any{1}, &buf) // writes into buf -> [1] -out2, _ := parsed([]any{2}, &buf) // reuses the same buf -> now [2] +args := []*[]any{&buf} +out1, _ := parsed([]any{1}, args...) // writes into buf -> [1] +out2, _ := parsed([]any{2}, args...) // reuses the same buf -> now [2] fmt.Println(out1) fmt.Println(out2) fmt.Println(buf) diff --git a/errors/error_basic_runtime.go b/errors/error_basic_runtime.go index 25f8778..20f3b3d 100644 --- a/errors/error_basic_runtime.go +++ b/errors/error_basic_runtime.go @@ -13,3 +13,10 @@ func (e *ErrorBasicRuntime) GetPath() string { func (e *ErrorBasicRuntime) GetRemainingPathLen() int { return e.remainingPathLen } + +func NewErrorBasicRuntime(path string, remainingPathLen int) ErrorBasicRuntime { + return ErrorBasicRuntime{ + path: path, + remainingPathLen: remainingPathLen, + } +} diff --git a/errors/errors.go b/errors/errors.go index f247809..c92c6db 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -81,9 +81,9 @@ func (e ErrorTypeUnmatched) Error() string { return fmt.Sprintf(`type unmatched (path=%s, expected=%s, found=%s)`, e.ErrorBasicRuntime.GetPath(), e.ExpectedType, e.FoundType) } -func NewErrorTypeUnmatched(path string, remainingPathLen int, expected string, found string) ErrorTypeUnmatched { +func NewErrorTypeUnmatched(errBasicRuntime *ErrorBasicRuntime, expected string, found string) ErrorTypeUnmatched { return ErrorTypeUnmatched{ - ErrorBasicRuntime: &ErrorBasicRuntime{path: path, remainingPathLen: remainingPathLen}, + ErrorBasicRuntime: errBasicRuntime, ExpectedType: expected, FoundType: found, } @@ -98,9 +98,9 @@ func (e ErrorMemberNotExist) Error() string { return fmt.Sprintf(`member did not exist (path=%s)`, e.ErrorBasicRuntime.GetPath()) } -func NewErrorMemberNotExist(path string, remainingPathLen int) ErrorMemberNotExist { +func NewErrorMemberNotExist(errBasicRuntime *ErrorBasicRuntime) ErrorMemberNotExist { return ErrorMemberNotExist{ - ErrorBasicRuntime: &ErrorBasicRuntime{path: path, remainingPathLen: remainingPathLen}, + ErrorBasicRuntime: errBasicRuntime, } } diff --git a/internal/syntax/jsonpath.go b/internal/syntax/jsonpath.go index 60dd014..5fd0169 100644 --- a/internal/syntax/jsonpath.go +++ b/internal/syntax/jsonpath.go @@ -1,7 +1,6 @@ package syntax import ( - "regexp" "sync" "github.com/AsaiYusuke/jsonpath/v2/config" @@ -9,7 +8,6 @@ import ( var parseMutex sync.Mutex var parser = pegJSONPathParser[uint32]{} -var unescapeRegex = regexp.MustCompile(`\\(.)`) // Retrieve returns the retrieved JSON using the given JSONPath. func Retrieve(jsonPath string, src any, config ...config.Config) ([]any, error) { @@ -42,8 +40,6 @@ func Parse(jsonPath string, config ...config.Config) (f func(src any, dst ...*[] parser.Reset() } - parser.jsonPathParser.unescapeRegex = unescapeRegex - if len(config) > 0 { parser.jsonPathParser.filterFunctions = config[0].FilterFunctions parser.jsonPathParser.aggregateFunctions = config[0].AggregateFunctions diff --git a/internal/syntax/jsonpath_parser.go b/internal/syntax/jsonpath_parser.go index 77d4dd1..18e5a12 100644 --- a/internal/syntax/jsonpath_parser.go +++ b/internal/syntax/jsonpath_parser.go @@ -8,11 +8,12 @@ import ( "github.com/AsaiYusuke/jsonpath/v2/errors" ) +var unescapeRegex = regexp.MustCompile(`\\(.)`) + type jsonPathParser struct { root syntaxNode paramsList [][]any params []any - unescapeRegex *regexp.Regexp filterFunctions map[string]func(any) (any, error) aggregateFunctions map[string]func([]any) (any, error) accessorMode bool @@ -59,8 +60,8 @@ func (p *jsonPathParser) toFloat(text string) float64 { } func (p *jsonPathParser) unescape(text string) string { - return p.unescapeRegex.ReplaceAllStringFunc(text, func(block string) string { - varBlockSet := p.unescapeRegex.FindStringSubmatch(block) + return unescapeRegex.ReplaceAllStringFunc(text, func(block string) string { + varBlockSet := unescapeRegex.FindStringSubmatch(block) return varBlockSet[1] }) } diff --git a/internal/syntax/syntax_basic_node.go b/internal/syntax/syntax_basic_node.go index 821fb11..b94af88 100644 --- a/internal/syntax/syntax_basic_node.go +++ b/internal/syntax/syntax_basic_node.go @@ -1,18 +1,26 @@ package syntax import ( + "reflect" + "sync" + "github.com/AsaiYusuke/jsonpath/v2/config" "github.com/AsaiYusuke/jsonpath/v2/errors" ) +type syntaxNodeErrState struct { + basicRuntime errors.ErrorBasicRuntime +} + type syntaxBasicNode struct { - path string - remainingPath string - remainingPathLen int - valueGroup bool - next syntaxNode - accessorMode bool - preErrMemberNotExist errors.ErrorMemberNotExist + path string + remainingPath string + remainingPathLen int + valueGroup bool + next syntaxNode + accessorMode bool + errState *syntaxNodeErrState + onceErrState sync.Once } func (i *syntaxBasicNode) setPath(path string) { @@ -52,11 +60,24 @@ func (i *syntaxBasicNode) getNext() syntaxNode { return i.next } +func (i *syntaxBasicNode) ensureErrState() { + i.onceErrState.Do(func() { + i.errState = &syntaxNodeErrState{} + i.errState.basicRuntime = errors.NewErrorBasicRuntime(i.path, i.remainingPathLen) + }) +} + func (i *syntaxBasicNode) newErrMemberNotExist() errors.ErrorMemberNotExist { - if i.preErrMemberNotExist.ErrorBasicRuntime == nil { - i.preErrMemberNotExist = errors.NewErrorMemberNotExist(i.path, i.remainingPathLen) + i.ensureErrState() + return errors.NewErrorMemberNotExist(&i.errState.basicRuntime) +} + +func (i *syntaxBasicNode) newErrTypeUnmatched(expected string, current any) errors.ErrorTypeUnmatched { + i.ensureErrState() + if current != nil { + return errors.NewErrorTypeUnmatched(&i.errState.basicRuntime, expected, reflect.TypeOf(current).String()) } - return i.preErrMemberNotExist + return errors.NewErrorTypeUnmatched(&i.errState.basicRuntime, expected, msgTypeNull) } func (i *syntaxBasicNode) retrieveAnyValueNext( diff --git a/internal/syntax/syntax_compare_query.go b/internal/syntax/syntax_compare_query.go index 9af87ca..b45c757 100644 --- a/internal/syntax/syntax_compare_query.go +++ b/internal/syntax/syntax_compare_query.go @@ -16,14 +16,14 @@ func (q *syntaxCompareQuery) compute( } } - rightValues := q.rightParam.compute(root, currentList) - // The syntax parser always results in a literal value on the right side as input. - if q.comparator.comparator(leftValues, rightValues[0]) { + rightValue := q.rightParam.compute(root, currentList)[0] + + if q.comparator.compare(leftValues, rightValue) { return leftValues } - if len(leftValues) == 1 && leftValues[0] == emptyEntity && rightValues[0] == emptyEntity { + if len(leftValues) == 1 && leftValues[0] == emptyEntity && rightValue == emptyEntity { if _, ok := q.comparator.(*syntaxCompareDeepEQ); ok { return currentList } diff --git a/internal/syntax/syntax_compare_query_comparator_deep_eq.go b/internal/syntax/syntax_compare_query_comparator_deep_eq.go index 36eb216..4576afb 100644 --- a/internal/syntax/syntax_compare_query_comparator_deep_eq.go +++ b/internal/syntax/syntax_compare_query_comparator_deep_eq.go @@ -5,7 +5,7 @@ import "reflect" type syntaxCompareDeepEQ struct { } -func (c *syntaxCompareDeepEQ) comparator(left []any, right any) bool { +func (c *syntaxCompareDeepEQ) compare(left []any, right any) bool { var hasValue bool for leftIndex := range left { if left[leftIndex] == emptyEntity { diff --git a/internal/syntax/syntax_compare_query_comparator_direct_eq.go b/internal/syntax/syntax_compare_query_comparator_direct_eq.go index d239d80..92d9f30 100644 --- a/internal/syntax/syntax_compare_query_comparator_direct_eq.go +++ b/internal/syntax/syntax_compare_query_comparator_direct_eq.go @@ -5,7 +5,7 @@ import "encoding/json" type syntaxCompareDirectEQ struct { } -func (c *syntaxCompareDirectEQ) comparator(left []any, right any) bool { +func (c *syntaxCompareDirectEQ) compare(left []any, right any) bool { var hasValue bool for leftIndex := range left { if left[leftIndex] == emptyEntity { diff --git a/internal/syntax/syntax_compare_query_comparator_ge.go b/internal/syntax/syntax_compare_query_comparator_ge.go index eb9d3d1..aa98deb 100644 --- a/internal/syntax/syntax_compare_query_comparator_ge.go +++ b/internal/syntax/syntax_compare_query_comparator_ge.go @@ -5,7 +5,7 @@ import "encoding/json" type syntaxCompareGE struct { } -func (c *syntaxCompareGE) comparator(left []any, right any) bool { +func (c *syntaxCompareGE) compare(left []any, right any) bool { rightFloatValue, rightIsFloat := right.(float64) rightNumberValue, rightIsNumber := right.(json.Number) rightStringValue, rightIsString := right.(string) diff --git a/internal/syntax/syntax_compare_query_comparator_gt.go b/internal/syntax/syntax_compare_query_comparator_gt.go index 116d241..df8d178 100644 --- a/internal/syntax/syntax_compare_query_comparator_gt.go +++ b/internal/syntax/syntax_compare_query_comparator_gt.go @@ -5,7 +5,7 @@ import "encoding/json" type syntaxCompareGT struct { } -func (c *syntaxCompareGT) comparator(left []any, right any) bool { +func (c *syntaxCompareGT) compare(left []any, right any) bool { rightFloat, rightIsFloat := right.(float64) rightNumber, rightIsNumber := right.(json.Number) rightString, rightIsString := right.(string) diff --git a/internal/syntax/syntax_compare_query_comparator_le.go b/internal/syntax/syntax_compare_query_comparator_le.go index 1bc623f..39164d1 100644 --- a/internal/syntax/syntax_compare_query_comparator_le.go +++ b/internal/syntax/syntax_compare_query_comparator_le.go @@ -5,7 +5,7 @@ import "encoding/json" type syntaxCompareLE struct { } -func (c *syntaxCompareLE) comparator(left []any, right any) bool { +func (c *syntaxCompareLE) compare(left []any, right any) bool { rightFloat, rightIsFloat := right.(float64) rightNumber, rightIsNumber := right.(json.Number) rightString, rightIsString := right.(string) diff --git a/internal/syntax/syntax_compare_query_comparator_lt.go b/internal/syntax/syntax_compare_query_comparator_lt.go index a283c21..2b2928e 100644 --- a/internal/syntax/syntax_compare_query_comparator_lt.go +++ b/internal/syntax/syntax_compare_query_comparator_lt.go @@ -5,7 +5,7 @@ import "encoding/json" type syntaxCompareLT struct { } -func (c *syntaxCompareLT) comparator(left []any, right any) bool { +func (c *syntaxCompareLT) compare(left []any, right any) bool { rightFloat, rightIsFloat := right.(float64) rightNumber, rightIsNumber := right.(json.Number) rightString, rightIsString := right.(string) diff --git a/internal/syntax/syntax_compare_query_comparator_number_ge.go b/internal/syntax/syntax_compare_query_comparator_number_ge.go index b69e5f1..0e4da8a 100644 --- a/internal/syntax/syntax_compare_query_comparator_number_ge.go +++ b/internal/syntax/syntax_compare_query_comparator_number_ge.go @@ -5,7 +5,7 @@ import "encoding/json" type syntaxCompareNumberGE struct { } -func (c *syntaxCompareNumberGE) comparator(left []any, right any) bool { +func (c *syntaxCompareNumberGE) compare(left []any, right any) bool { rightFloatValue, _ := right.(float64) var hasValue bool diff --git a/internal/syntax/syntax_compare_query_comparator_number_gt.go b/internal/syntax/syntax_compare_query_comparator_number_gt.go index c527050..1e8e2c7 100644 --- a/internal/syntax/syntax_compare_query_comparator_number_gt.go +++ b/internal/syntax/syntax_compare_query_comparator_number_gt.go @@ -5,7 +5,7 @@ import "encoding/json" type syntaxCompareNumberGT struct { } -func (c *syntaxCompareNumberGT) comparator(left []any, right any) bool { +func (c *syntaxCompareNumberGT) compare(left []any, right any) bool { rightFloatValue, _ := right.(float64) var hasValue bool diff --git a/internal/syntax/syntax_compare_query_comparator_number_le.go b/internal/syntax/syntax_compare_query_comparator_number_le.go index c1d2796..078fa10 100644 --- a/internal/syntax/syntax_compare_query_comparator_number_le.go +++ b/internal/syntax/syntax_compare_query_comparator_number_le.go @@ -5,7 +5,7 @@ import "encoding/json" type syntaxCompareNumberLE struct { } -func (c *syntaxCompareNumberLE) comparator(left []any, right any) bool { +func (c *syntaxCompareNumberLE) compare(left []any, right any) bool { rightFloatValue, _ := right.(float64) var hasValue bool diff --git a/internal/syntax/syntax_compare_query_comparator_number_lt.go b/internal/syntax/syntax_compare_query_comparator_number_lt.go index 886406e..936fa56 100644 --- a/internal/syntax/syntax_compare_query_comparator_number_lt.go +++ b/internal/syntax/syntax_compare_query_comparator_number_lt.go @@ -5,7 +5,7 @@ import "encoding/json" type syntaxCompareNumberLT struct { } -func (c *syntaxCompareNumberLT) comparator(left []any, right any) bool { +func (c *syntaxCompareNumberLT) compare(left []any, right any) bool { rightFloatValue, _ := right.(float64) var hasValue bool diff --git a/internal/syntax/syntax_compare_query_comparator_regex.go b/internal/syntax/syntax_compare_query_comparator_regex.go index 2b394fc..3ad1300 100644 --- a/internal/syntax/syntax_compare_query_comparator_regex.go +++ b/internal/syntax/syntax_compare_query_comparator_regex.go @@ -6,7 +6,7 @@ type syntaxCompareRegex struct { regex *regexp.Regexp } -func (r *syntaxCompareRegex) comparator(left []any, _ any) bool { +func (r *syntaxCompareRegex) compare(left []any, _ any) bool { var hasValue bool for leftIndex := range left { if left[leftIndex] == emptyEntity { diff --git a/internal/syntax/syntax_compare_query_comparator_string_ge.go b/internal/syntax/syntax_compare_query_comparator_string_ge.go index 2ddb071..382c1a8 100644 --- a/internal/syntax/syntax_compare_query_comparator_string_ge.go +++ b/internal/syntax/syntax_compare_query_comparator_string_ge.go @@ -3,7 +3,7 @@ package syntax type syntaxCompareStringGE struct { } -func (c *syntaxCompareStringGE) comparator(left []any, right any) bool { +func (c *syntaxCompareStringGE) compare(left []any, right any) bool { rightStringValue, _ := right.(string) var hasValue bool diff --git a/internal/syntax/syntax_compare_query_comparator_string_gt.go b/internal/syntax/syntax_compare_query_comparator_string_gt.go index 089b929..195dad5 100644 --- a/internal/syntax/syntax_compare_query_comparator_string_gt.go +++ b/internal/syntax/syntax_compare_query_comparator_string_gt.go @@ -3,7 +3,7 @@ package syntax type syntaxCompareStringGT struct { } -func (c *syntaxCompareStringGT) comparator(left []any, right any) bool { +func (c *syntaxCompareStringGT) compare(left []any, right any) bool { rightStringValue, _ := right.(string) var hasValue bool diff --git a/internal/syntax/syntax_compare_query_comparator_string_le.go b/internal/syntax/syntax_compare_query_comparator_string_le.go index df66594..229b752 100644 --- a/internal/syntax/syntax_compare_query_comparator_string_le.go +++ b/internal/syntax/syntax_compare_query_comparator_string_le.go @@ -3,7 +3,7 @@ package syntax type syntaxCompareStringLE struct { } -func (c *syntaxCompareStringLE) comparator(left []any, right any) bool { +func (c *syntaxCompareStringLE) compare(left []any, right any) bool { rightStringValue, _ := right.(string) var hasValue bool diff --git a/internal/syntax/syntax_compare_query_comparator_string_lt.go b/internal/syntax/syntax_compare_query_comparator_string_lt.go index 9142c98..26dcdef 100644 --- a/internal/syntax/syntax_compare_query_comparator_string_lt.go +++ b/internal/syntax/syntax_compare_query_comparator_string_lt.go @@ -3,7 +3,7 @@ package syntax type syntaxCompareStringLT struct { } -func (c *syntaxCompareStringLT) comparator(left []any, right any) bool { +func (c *syntaxCompareStringLT) compare(left []any, right any) bool { rightStringValue, _ := right.(string) var hasValue bool diff --git a/internal/syntax/syntax_if_comparator.go b/internal/syntax/syntax_if_comparator.go index 0f8eb39..45a5cf7 100644 --- a/internal/syntax/syntax_if_comparator.go +++ b/internal/syntax/syntax_if_comparator.go @@ -1,5 +1,5 @@ package syntax type syntaxComparator interface { - comparator(left []any, right any) bool + compare(left []any, right any) bool } diff --git a/internal/syntax/syntax_node_identifier_child_multi.go b/internal/syntax/syntax_node_identifier_child_multi.go index 600f217..040c41e 100644 --- a/internal/syntax/syntax_node_identifier_child_multi.go +++ b/internal/syntax/syntax_node_identifier_child_multi.go @@ -1,8 +1,6 @@ package syntax import ( - "reflect" - "github.com/AsaiYusuke/jsonpath/v2/errors" ) @@ -30,12 +28,7 @@ func (i *syntaxChildMultiIdentifier) retrieve( return i.retrieveMap(root, srcMap, results) } - if current != nil { - return errors.NewErrorTypeUnmatched( - i.path, i.remainingPathLen, msgTypeObject, reflect.TypeOf(current).String()) - } - return errors.NewErrorTypeUnmatched( - i.path, i.remainingPathLen, msgTypeObject, msgTypeNull) + return i.newErrTypeUnmatched(msgTypeObject, current) } func (i *syntaxChildMultiIdentifier) retrieveMap( diff --git a/internal/syntax/syntax_node_identifier_child_single.go b/internal/syntax/syntax_node_identifier_child_single.go index 22c9f58..69fc9df 100644 --- a/internal/syntax/syntax_node_identifier_child_single.go +++ b/internal/syntax/syntax_node_identifier_child_single.go @@ -1,8 +1,6 @@ package syntax import ( - "reflect" - "github.com/AsaiYusuke/jsonpath/v2/errors" ) @@ -19,10 +17,5 @@ func (i *syntaxChildSingleIdentifier) retrieve( return i.retrieveMapNext(root, srcMap, i.identifier, results) } - if current != nil { - return errors.NewErrorTypeUnmatched( - i.path, i.remainingPathLen, msgTypeObject, reflect.TypeOf(current).String()) - } - return errors.NewErrorTypeUnmatched( - i.path, i.remainingPathLen, msgTypeObject, msgTypeNull) + return i.newErrTypeUnmatched(msgTypeObject, current) } diff --git a/internal/syntax/syntax_node_identifier_child_wildcard.go b/internal/syntax/syntax_node_identifier_child_wildcard.go index e1a32ac..ca87a60 100644 --- a/internal/syntax/syntax_node_identifier_child_wildcard.go +++ b/internal/syntax/syntax_node_identifier_child_wildcard.go @@ -1,8 +1,6 @@ package syntax import ( - "reflect" - "github.com/AsaiYusuke/jsonpath/v2/errors" ) @@ -21,12 +19,7 @@ func (i *syntaxChildWildcardIdentifier) retrieve( return i.retrieveList(root, typedNodes, results) default: - if current != nil { - return errors.NewErrorTypeUnmatched( - i.path, i.remainingPathLen, msgTypeObjectOrArray, reflect.TypeOf(current).String()) - } - return errors.NewErrorTypeUnmatched( - i.path, i.remainingPathLen, msgTypeObjectOrArray, msgTypeNull) + return i.newErrTypeUnmatched(msgTypeObjectOrArray, current) } } diff --git a/internal/syntax/syntax_node_identifier_recursive_child.go b/internal/syntax/syntax_node_identifier_recursive_child.go index 2e1d898..7724fcd 100644 --- a/internal/syntax/syntax_node_identifier_recursive_child.go +++ b/internal/syntax/syntax_node_identifier_recursive_child.go @@ -1,7 +1,6 @@ package syntax import ( - "reflect" "slices" "github.com/AsaiYusuke/jsonpath/v2/errors" @@ -20,12 +19,7 @@ func (i *syntaxRecursiveChildIdentifier) retrieve( switch current.(type) { case map[string]any, []any: default: - if current != nil { - return errors.NewErrorTypeUnmatched( - i.path, i.remainingPathLen, msgTypeObjectOrArray, reflect.TypeOf(current).String()) - } - return errors.NewErrorTypeUnmatched( - i.path, i.remainingPathLen, msgTypeObjectOrArray, msgTypeNull) + return i.newErrTypeUnmatched(msgTypeObjectOrArray, current) } var deepestError errors.ErrorRuntime diff --git a/internal/syntax/syntax_node_qualifier_filter.go b/internal/syntax/syntax_node_qualifier_filter.go index dbeb6b6..2531470 100644 --- a/internal/syntax/syntax_node_qualifier_filter.go +++ b/internal/syntax/syntax_node_qualifier_filter.go @@ -1,8 +1,6 @@ package syntax import ( - "reflect" - "github.com/AsaiYusuke/jsonpath/v2/errors" ) @@ -23,12 +21,7 @@ func (f *syntaxFilterQualifier) retrieve( return f.retrieveList(root, typedNodes, results) default: - if current != nil { - return errors.NewErrorTypeUnmatched( - f.path, f.remainingPathLen, msgTypeObjectOrArray, reflect.TypeOf(current).String()) - } - return errors.NewErrorTypeUnmatched( - f.path, f.remainingPathLen, msgTypeObjectOrArray, msgTypeNull) + return f.newErrTypeUnmatched(msgTypeObjectOrArray, current) } } @@ -39,8 +32,6 @@ func (f *syntaxFilterQualifier) retrieveMap( return f.newErrMemberNotExist() } - var deepestError errors.ErrorRuntime - sortKeys, keyLength := getSortedKeys(srcMap) buf := getNodeSlice() @@ -65,6 +56,8 @@ func (f *syntaxFilterQualifier) retrieveMap( } } + var deepestError errors.ErrorRuntime + for index := range *sortKeys { if isEachResult { if valueList[index] == emptyEntity { @@ -96,8 +89,6 @@ func (f *syntaxFilterQualifier) retrieveList( return f.newErrMemberNotExist() } - var deepestError errors.ErrorRuntime - valueList := f.query.compute(root, srcList) isEachResult := len(valueList) == len(srcList) @@ -108,6 +99,8 @@ func (f *syntaxFilterQualifier) retrieveList( } } + var deepestError errors.ErrorRuntime + for index := range srcList { if isEachResult { if valueList[index] == emptyEntity { diff --git a/internal/syntax/syntax_node_qualifier_union.go b/internal/syntax/syntax_node_qualifier_union.go index 4cfffc0..1becff9 100644 --- a/internal/syntax/syntax_node_qualifier_union.go +++ b/internal/syntax/syntax_node_qualifier_union.go @@ -1,8 +1,6 @@ package syntax import ( - "reflect" - "github.com/AsaiYusuke/jsonpath/v2/errors" ) @@ -17,18 +15,13 @@ func (u *syntaxUnionQualifier) retrieve( srcArray, ok := current.([]any) if !ok { - if current != nil { - return errors.NewErrorTypeUnmatched( - u.path, u.remainingPathLen, msgTypeArray, reflect.TypeOf(current).String()) - } - return errors.NewErrorTypeUnmatched( - u.path, u.remainingPathLen, msgTypeArray, msgTypeNull) + return u.newErrTypeUnmatched(msgTypeArray, current) } var deepestError errors.ErrorRuntime + srcLen := len(srcArray) for _, subscript := range u.subscripts { - srcLen := len(srcArray) for ord := range subscript.count(srcLen) { if err := u.retrieveListNext(root, srcArray, subscript.indexAt(srcLen, ord), results); len(*results) == 0 && err != nil { deepestError = u.getMostResolvedError(err, deepestError) diff --git a/internal/tests/test_helpers.go b/internal/tests/test_helpers.go index 46520a0..e999260 100644 --- a/internal/tests/test_helpers.go +++ b/internal/tests/test_helpers.go @@ -31,11 +31,13 @@ func createErrorFunctionFailed(functionName string, errorString string) errors.E } func createErrorMemberNotExist(path string) errors.ErrorMemberNotExist { - return errors.NewErrorMemberNotExist(path, len(path)) + errBasicError := errors.NewErrorBasicRuntime(path, len(path)) + return errors.NewErrorMemberNotExist(&errBasicError) } func createErrorTypeUnmatched(path string, expected string, found string) errors.ErrorTypeUnmatched { - return errors.NewErrorTypeUnmatched(path, len(path), expected, found) + errBasicError := errors.NewErrorBasicRuntime(path, len(path)) + return errors.NewErrorTypeUnmatched(&errBasicError, expected, found) } func createErrorInvalidSyntax(position int, reason string, near string) errors.ErrorInvalidSyntax { diff --git a/internal/tests/test_jsonpath_benchmark_test.go b/internal/tests/test_jsonpath_benchmark_test.go index f3a803d..03e2a36 100644 --- a/internal/tests/test_jsonpath_benchmark_test.go +++ b/internal/tests/test_jsonpath_benchmark_test.go @@ -21,9 +21,10 @@ func execParserFunc(jsonPath, srcJSON string, b *testing.B) { } buf := make([]any, 0, 256) + args := []*[]any{&buf} for b.Loop() { - if _, err := parserFunc(src, &buf); err != nil { + if _, err := parserFunc(src, args...); err != nil { b.Errorf(`%s`, err) } diff --git a/test_jsonpath_example_test.go b/test_jsonpath_example_test.go index 3465d60..6945627 100644 --- a/test_jsonpath_example_test.go +++ b/test_jsonpath_example_test.go @@ -78,12 +78,13 @@ func ExampleParse_reuseBuffer() { json.Unmarshal([]byte(srcJSON1), &src1) json.Unmarshal([]byte(srcJSON2), &src2) buf := make([]any, 0, 4) - output1, err := jsonPathParser(src1, &buf) + args := []*[]any{&buf} + output1, err := jsonPathParser(src1, args...) if err != nil { fmt.Printf(`type: %v, value: %v`, reflect.TypeOf(err), err) return } - output2, err := jsonPathParser(src2, &buf) + output2, err := jsonPathParser(src2, args...) if err != nil { fmt.Printf(`type: %v, value: %v`, reflect.TypeOf(err), err) return