@@ -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
546548func 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}
0 commit comments