Skip to content

Commit 2d28d0b

Browse files
committed
fix cmp_any #7
1 parent f95233d commit 2d28d0b

2 files changed

Lines changed: 42 additions & 14 deletions

File tree

jsonpath.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -526,21 +526,23 @@ func eval_filter(obj, root interface{}, lp, op, rp string) (res bool, err error)
526526
}
527527
}
528528

529-
func isNumber(s string) bool {
530-
dot_cnt := 0
531-
for _, c := range s {
532-
if c == '.' {
533-
dot_cnt += 1
534-
if dot_cnt > 1 {
535-
return false
536-
}
537-
} else if ( c >= '0' && c <= '9') {
538-
continue
529+
func isNumber(o interface{}) bool {
530+
switch v := o.(type) {
531+
case int,int8,int16,int32,int64:
532+
return true
533+
case uint,uint8,uint16,uint32,uint64:
534+
return true
535+
case float32,float64:
536+
return true
537+
case string:
538+
_, err := strconv.ParseFloat(v, 64)
539+
if err == nil {
540+
return true
539541
} else {
540542
return false
541543
}
542544
}
543-
return true
545+
return false
544546
}
545547

546548
func cmp_any(obj1, obj2 interface{}, op string) (bool, error) {
@@ -550,9 +552,8 @@ func cmp_any(obj1, obj2 interface{}, op string) (bool, error) {
550552
return false, fmt.Errorf("op should only be <, <=, ==, >= and >")
551553
}
552554

553-
554555
var exp string
555-
if isNumber(fmt.Sprintf("%s", obj1)) && isNumber(fmt.Sprintf("%s", obj2)) {
556+
if isNumber(obj1) && isNumber(obj2) {
556557
exp = fmt.Sprintf(`%v %s %v`, obj1, op, obj2)
557558
} else {
558559
exp = fmt.Sprintf(`"%v" %s "%v"`, obj1, op, obj2)
@@ -569,5 +570,6 @@ func cmp_any(obj1, obj2 interface{}, op string) (bool, error) {
569570
if res.Value.String() == "true" {
570571
return true, nil
571572
}
573+
572574
return false, nil
573575
}

jsonpath_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,12 +862,18 @@ var tcase_cmp_any = []map[string]interface{}{
862862
"op": "==",
863863
"exp": true,
864864
"err": nil,
865+
},{
866+
"obj1": 20,
867+
"obj2": "100",
868+
"op": ">",
869+
"exp": false,
870+
"err": nil,
865871
},
866872
}
867873

868874
func Test_jsonpath_cmp_any(t *testing.T) {
869875
for idx, tcase := range tcase_cmp_any {
870-
//for idx, tcase := range tcase_cmp_any[6:] {
876+
//for idx, tcase := range tcase_cmp_any[8:] {
871877
t.Logf("idx: %v, %v %v %v, exp: %v", idx, tcase["obj1"], tcase["op"], tcase["obj2"], tcase["exp"])
872878
res, err := cmp_any(tcase["obj1"], tcase["obj2"], tcase["op"].(string))
873879
exp := tcase["exp"].(bool)
@@ -956,4 +962,24 @@ func Test_jsonpath_null_in_the_middle(t *testing.T) {
956962

957963
res, err := JsonPathLookup(j, "$.head_commit.author.username")
958964
t.Log(res, err)
965+
}
966+
967+
func Test_jsonpath_num_cmp(t *testing.T) {
968+
data := `{
969+
"books": [
970+
{ "name": "My First Book", "price": 10 },
971+
{ "name": "My Second Book", "price": 20 }
972+
]
973+
}`
974+
var j interface{}
975+
json.Unmarshal([]byte(data), &j)
976+
res, err := JsonPathLookup(j, "$.books[?(@.price > 100)].name")
977+
if err != nil {
978+
t.Fatal(err)
979+
}
980+
arr := res.([]interface{})
981+
if len(arr) != 0 {
982+
t.Fatal("should return [], got: ", arr)
983+
}
984+
959985
}

0 commit comments

Comments
 (0)