Skip to content

Commit

Permalink
add direct equal comparator (#39)
Browse files Browse the repository at this point in the history
* add direct equal comparator
  • Loading branch information
AsaiYusuke authored Oct 26, 2023
1 parent bf6c344 commit 02aaf3c
Show file tree
Hide file tree
Showing 18 changed files with 179 additions and 59 deletions.
58 changes: 56 additions & 2 deletions jsonpath_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,13 +555,67 @@ func (p *jsonPathParser) _createBasicCompareQuery(

func (p *jsonPathParser) pushCompareEQ(
leftParam, rightParam *syntaxBasicCompareParameter) {
p.push(p._createBasicCompareQuery(leftParam, rightParam, &syntaxCompareEQ{}))
if rightLiteralParam, ok := rightParam.param.(*syntaxQueryParamLiteral); ok {
switch rightLiteralParam.literal[0].(type) {
case float64:
p.push(p._createBasicCompareQuery(leftParam, rightParam, &syntaxCompareDirectEQ{
syntaxTypeValidator: &syntaxBasicNumericTypeValidator{},
}))
case bool:
p.push(p._createBasicCompareQuery(leftParam, rightParam, &syntaxCompareDirectEQ{
syntaxTypeValidator: &syntaxBasicBoolTypeValidator{},
}))
case string:
p.push(p._createBasicCompareQuery(leftParam, rightParam, &syntaxCompareDirectEQ{
syntaxTypeValidator: &syntaxBasicStringTypeValidator{},
}))
case nil:
p.push(p._createBasicCompareQuery(leftParam, rightParam, &syntaxCompareDirectEQ{
syntaxTypeValidator: &syntaxBasicNilTypeValidator{},
}))
}

return
}

p.push(p._createBasicCompareQuery(leftParam, rightParam, &syntaxCompareDeepEQ{}))
}

func (p *jsonPathParser) pushCompareNE(
leftParam, rightParam *syntaxBasicCompareParameter) {
if rightLiteralParam, ok := rightParam.param.(*syntaxQueryParamLiteral); ok {
switch rightLiteralParam.literal[0].(type) {
case float64:
p.push(&syntaxLogicalNot{
query: p._createBasicCompareQuery(leftParam, rightParam, &syntaxCompareDirectEQ{
syntaxTypeValidator: &syntaxBasicNumericTypeValidator{},
}),
})
case bool:
p.push(&syntaxLogicalNot{
query: p._createBasicCompareQuery(leftParam, rightParam, &syntaxCompareDirectEQ{
syntaxTypeValidator: &syntaxBasicBoolTypeValidator{},
}),
})
case string:
p.push(&syntaxLogicalNot{
query: p._createBasicCompareQuery(leftParam, rightParam, &syntaxCompareDirectEQ{
syntaxTypeValidator: &syntaxBasicStringTypeValidator{},
}),
})
case nil:
p.push(&syntaxLogicalNot{
query: p._createBasicCompareQuery(leftParam, rightParam, &syntaxCompareDirectEQ{
syntaxTypeValidator: &syntaxBasicNilTypeValidator{},
}),
})
}

return
}

p.push(&syntaxLogicalNot{
query: p._createBasicCompareQuery(leftParam, rightParam, &syntaxCompareEQ{}),
query: p._createBasicCompareQuery(leftParam, rightParam, &syntaxCompareDeepEQ{}),
})
}

Expand Down
21 changes: 0 additions & 21 deletions syntax_basic_comparator_any_value.go

This file was deleted.

6 changes: 3 additions & 3 deletions syntax_basic_compare_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ func (q *syntaxBasicCompareQuery) compute(
root interface{}, currentList []interface{}) []interface{} {

leftValues := q.leftParam.compute(root, currentList)
leftFound := q.comparator.typeCast(leftValues)
leftFound := q.comparator.validate(leftValues)

rightValues := q.rightParam.compute(root, currentList)
rightFound := q.comparator.typeCast(rightValues)
rightFound := q.comparator.validate(rightValues)

if leftFound && rightFound {
// The syntax parser always results in a literal value on the right side as input.
Expand All @@ -25,7 +25,7 @@ func (q *syntaxBasicCompareQuery) compute(

// leftFound == false && rightFound == false
if leftFound == rightFound {
if _, ok := q.comparator.(*syntaxCompareEQ); ok {
if _, ok := q.comparator.(*syntaxCompareDeepEQ); ok {
return currentList
}
}
Expand Down
14 changes: 14 additions & 0 deletions syntax_basic_type_validator_any_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package jsonpath

type syntaxBasicAnyValueTypeValidator struct {
}

func (c *syntaxBasicAnyValueTypeValidator) validate(values []interface{}) bool {
var foundValue bool
for index := range values {
if values[index] != emptyEntity {
foundValue = true
}
}
return foundValue
}
18 changes: 18 additions & 0 deletions syntax_basic_type_validator_bool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package jsonpath

type syntaxBasicBoolTypeValidator struct {
}

func (c *syntaxBasicBoolTypeValidator) validate(values []interface{}) bool {
var foundValue bool
for index := range values {
switch values[index].(type) {
case bool:
foundValue = true
case struct{}:
default:
values[index] = emptyEntity
}
}
return foundValue
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package jsonpath

type syntaxBasicStringComparator struct {
type syntaxBasicNilTypeValidator struct {
}

func (c *syntaxBasicStringComparator) typeCast(values []interface{}) bool {
func (c *syntaxBasicNilTypeValidator) validate(values []interface{}) bool {
var foundValue bool
for index := range values {
switch values[index].(type) {
case string:
case nil:
foundValue = true
case struct{}:
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package jsonpath

import "encoding/json"

type syntaxBasicNumericComparator struct {
type syntaxBasicNumericTypeValidator struct {
}

func (c *syntaxBasicNumericComparator) typeCast(values []interface{}) bool {
func (c *syntaxBasicNumericTypeValidator) validate(values []interface{}) bool {
var foundValue bool
for index := range values {
switch typedValue := values[index].(type) {
Expand Down
18 changes: 18 additions & 0 deletions syntax_basic_type_validator_string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package jsonpath

type syntaxBasicStringTypeValidator struct {
}

func (c *syntaxBasicStringTypeValidator) validate(values []interface{}) bool {
var foundValue bool
for index := range values {
switch values[index].(type) {
case string:
foundValue = true
case struct{}:
default:
values[index] = emptyEntity
}
}
return foundValue
}
2 changes: 1 addition & 1 deletion syntax_if_comparator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package jsonpath

type syntaxComparator interface {
comparator(left []interface{}, right interface{}) bool
typeCast(values []interface{}) bool
validate(values []interface{}) bool
}
5 changes: 5 additions & 0 deletions syntax_if_type_validater.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package jsonpath

type syntaxTypeValidator interface {
validate(values []interface{}) bool
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package jsonpath

import "reflect"

type syntaxCompareEQ struct {
*syntaxBasicAnyValueComparator
type syntaxCompareDeepEQ struct {
*syntaxBasicAnyValueTypeValidator
}

func (c *syntaxCompareEQ) comparator(left []interface{}, right interface{}) bool {
func (c *syntaxCompareDeepEQ) comparator(left []interface{}, right interface{}) bool {
var hasValue bool
for leftIndex := range left {
if left[leftIndex] == emptyEntity {
Expand Down
17 changes: 17 additions & 0 deletions syntax_query_compare_comparator_direct_eq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package jsonpath

type syntaxCompareDirectEQ struct {
syntaxTypeValidator
}

func (c *syntaxCompareDirectEQ) comparator(left []interface{}, right interface{}) bool {
var hasValue bool
for leftIndex := range left {
if left[leftIndex] == right {
hasValue = true
} else {
left[leftIndex] = emptyEntity
}
}
return hasValue
}
2 changes: 1 addition & 1 deletion syntax_query_compare_comparator_ge.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package jsonpath

type syntaxCompareGE struct {
*syntaxBasicNumericComparator
*syntaxBasicNumericTypeValidator
}

func (c *syntaxCompareGE) comparator(left []interface{}, right interface{}) bool {
Expand Down
2 changes: 1 addition & 1 deletion syntax_query_compare_comparator_gt.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package jsonpath

type syntaxCompareGT struct {
*syntaxBasicNumericComparator
*syntaxBasicNumericTypeValidator
}

func (c *syntaxCompareGT) comparator(left []interface{}, right interface{}) bool {
Expand Down
2 changes: 1 addition & 1 deletion syntax_query_compare_comparator_le.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package jsonpath

type syntaxCompareLE struct {
*syntaxBasicNumericComparator
*syntaxBasicNumericTypeValidator
}

func (c *syntaxCompareLE) comparator(left []interface{}, right interface{}) bool {
Expand Down
2 changes: 1 addition & 1 deletion syntax_query_compare_comparator_lt.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package jsonpath

type syntaxCompareLT struct {
*syntaxBasicNumericComparator
*syntaxBasicNumericTypeValidator
}

func (c *syntaxCompareLT) comparator(left []interface{}, right interface{}) bool {
Expand Down
2 changes: 1 addition & 1 deletion syntax_query_compare_comparator_regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package jsonpath
import "regexp"

type syntaxCompareRegex struct {
*syntaxBasicStringComparator
*syntaxBasicStringTypeValidator

regex *regexp.Regexp
}
Expand Down
Loading

0 comments on commit 02aaf3c

Please sign in to comment.