You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ke...@apache.org on 2021/10/08 04:41:10 UTC
[skywalking-infra-e2e] branch main updated: Support float compare
when type not match (#52)
This is an automated email from the ASF dual-hosted git repository.
kezhenxu94 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-infra-e2e.git
The following commit(s) were added to refs/heads/main by this push:
new d37840f Support float compare when type not match (#52)
d37840f is described below
commit d37840fb22f7c78f03ab5932bbdccf925cb5ef73
Author: mrproliu <74...@qq.com>
AuthorDate: Fri Oct 8 12:41:04 2021 +0800
Support float compare when type not match (#52)
---
third-party/go/template/funcs.go | 26 ++++++++++++++++++++------
third-party/go/template/funcs_test.go | 16 +++++++++++++---
2 files changed, 33 insertions(+), 9 deletions(-)
diff --git a/third-party/go/template/funcs.go b/third-party/go/template/funcs.go
index 689118a..a4f0149 100644
--- a/third-party/go/template/funcs.go
+++ b/third-party/go/template/funcs.go
@@ -11,6 +11,7 @@ import (
"io"
"net/url"
"reflect"
+ "strconv"
"strings"
"sync"
"unicode"
@@ -516,6 +517,19 @@ func ne(arg1, arg2 reflect.Value) (bool, error) {
return !equal, err
}
+func toFloat64(val reflect.Value, k kind) (float64, error) {
+ switch k {
+ case intKind:
+ return float64(val.Int()), nil
+ case uintKind:
+ return strconv.ParseFloat(strconv.FormatUint(val.Uint(), 10), 10)
+ case floatKind:
+ return val.Float(), nil
+ default:
+ return 0, errors.New("not a number")
+ }
+}
+
// lt evaluates the comparison a < b.
func lt2bool(arg1, arg2 reflect.Value) (bool, error) {
arg1 = indirectInterface(arg1)
@@ -531,14 +545,14 @@ func lt2bool(arg1, arg2 reflect.Value) (bool, error) {
truth := false
if k1 != k2 {
// Special case: Can compare integer values regardless of type's sign.
- switch {
- case k1 == intKind && k2 == uintKind:
- truth = arg1.Int() < 0 || uint64(arg1.Int()) < arg2.Uint()
- case k1 == uintKind && k2 == intKind:
- truth = arg2.Int() >= 0 && arg1.Uint() < uint64(arg2.Int())
- default:
+ var v1, v2 float64
+ if v1, err = toFloat64(arg1, k1); err != nil {
+ return false, errBadComparison
+ }
+ if v2, err = toFloat64(arg2, k2); err != nil {
return false, errBadComparison
}
+ return v1 < v2, nil
} else {
switch k1 {
case boolKind, complexKind:
diff --git a/third-party/go/template/funcs_test.go b/third-party/go/template/funcs_test.go
index a5c14e9..df8f635 100644
--- a/third-party/go/template/funcs_test.go
+++ b/third-party/go/template/funcs_test.go
@@ -7,10 +7,11 @@ import (
func TestCompare(t *testing.T) {
tests := []struct {
- arg0 int
- arg1 int
+ arg0 interface{}
+ arg1 interface{}
compareResults []bool
}{
+ // same type compare
{
5, 10,
[]bool{5 < 10, 5 <= 10, 5 >= 10, 5 > 10},
@@ -23,6 +24,15 @@ func TestCompare(t *testing.T) {
15, 10,
[]bool{15 < 10, 15 <= 10, 15 >= 10, 15 > 10},
},
+ // deference type compare
+ {
+ 5, 10.10,
+ []bool{5 < 10.10, 5 <= 10.10, 5 >= 10.10, 5 > 10.10},
+ },
+ {
+ 5, uint(10),
+ []bool{5 < uint(10), 5 <= uint(10), 5 >= uint(10), 5 > uint(10)},
+ },
}
type compare func(ar0, ar1 reflect.Value) (interface{}, error)
@@ -40,7 +50,7 @@ func TestCompare(t *testing.T) {
// need be true
if err == nil {
if data.compareResults[inx] {
- validateSuccess = data.arg0 == int(res.(reflect.Value).Int())
+ validateSuccess = reflect.DeepEqual(reflect.ValueOf(data.arg0), res)
} else {
validateSuccess = reflect.TypeOf(res).Kind() == reflect.String
}