Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
AsaiYusuke committed Dec 31, 2020
1 parent b7f562e commit cb4bb10
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 32 deletions.
11 changes: 6 additions & 5 deletions syntax_basic_comparator_any_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import "encoding/json"
type syntaxBasicAnyValueComparator struct {
}

func (c *syntaxBasicAnyValueComparator) typeCast(value interface{}) (interface{}, bool) {
if number, ok := value.(json.Number); ok {
if floatNumber, err := number.Float64(); err == nil {
return floatNumber, true
func (c *syntaxBasicAnyValueComparator) typeCast(values map[int]interface{}) {
for index := range values {
if number, ok := values[index].(json.Number); ok {
if floatNumber, err := number.Float64(); err == nil {
values[index] = floatNumber
}
}
}
return value, true
}
21 changes: 12 additions & 9 deletions syntax_basic_comparator_numeric.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import "encoding/json"
type syntaxBasicNumericComparator struct {
}

func (c *syntaxBasicNumericComparator) typeCast(value interface{}) (interface{}, bool) {
switch value.(type) {
case float64:
return value, true
case json.Number:
number := value.(json.Number)
if floatNumber, err := number.Float64(); err == nil {
return floatNumber, true
func (c *syntaxBasicNumericComparator) typeCast(values map[int]interface{}) {
for index, value := range values {
switch value.(type) {
case float64:
continue
case json.Number:
number := value.(json.Number)
if floatNumber, err := number.Float64(); err == nil {
values[index] = floatNumber
}
default:
delete(values, index)
}
}
return 0, false
}
9 changes: 6 additions & 3 deletions syntax_basic_comparator_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package jsonpath
type syntaxBasicStringComparator struct {
}

func (c *syntaxBasicStringComparator) typeCast(value interface{}) (interface{}, bool) {
_, ok := value.(string)
return value, ok
func (c *syntaxBasicStringComparator) typeCast(values map[int]interface{}) {
for index := range values {
if _, ok := values[index].(string); !ok {
delete(values, index)
}
}
}
16 changes: 2 additions & 14 deletions syntax_basic_compare_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,10 @@ func (q *syntaxBasicCompareQuery) compute(
root interface{}, currentMap map[int]interface{}) map[int]interface{} {

leftValues := q.leftParam.get(root, currentMap)
for index := range leftValues {
if cast, ok := q.comparator.typeCast(leftValues[index]); ok {
leftValues[index] = cast
} else {
delete(leftValues, index)
}
}
q.comparator.typeCast(leftValues)

rightValues := q.rightParam.get(root, currentMap)
for index := range rightValues {
if cast, ok := q.comparator.typeCast(rightValues[index]); ok {
rightValues[index] = cast
} else {
delete(rightValues, index)
}
}
q.comparator.typeCast(rightValues)

for leftIndex := range leftValues {
for rightIndex := range rightValues {
Expand Down
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, right interface{}) bool
typeCast(value interface{}) (interface{}, bool)
typeCast(values map[int]interface{})
}

0 comments on commit cb4bb10

Please sign in to comment.