From 1e1804eae3c902c505727d20d178ed49da2426f7 Mon Sep 17 00:00:00 2001 From: Eric Chlebek Date: Thu, 31 May 2018 09:23:02 -0700 Subject: [PATCH] Upgrade govaluate to release 3.1.0. Closes #922 Signed-off-by: Eric Chlebek --- Gopkg.lock | 6 +- Gopkg.toml | 2 +- .../sensu/govaluate/evaluationStage.go | 60 ++++++++++++++++--- 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 516ea2d9c7..52dc39f2dd 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -540,10 +540,10 @@ revision = "2315d5715e36303a941d907f038da7f7c44c773b" [[projects]] - branch = "nested-parameters" name = "github.com/sensu/govaluate" packages = ["."] - revision = "8f7cba01497bd9fba915e5a310cbc3b1a98ff77a" + revision = "43cb04169dc67512be53aafaad8fbacf9fa3af9f" + version = "v3.1.0" [[projects]] name = "github.com/shirou/gopsutil" @@ -804,6 +804,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "d55f632f7bd0a3ba73b6ec57b8fc1a334cab1543fd97245d1dee2c1c9c68ad61" + inputs-digest = "6797d51548bb5f6d1c8d7b602a4886c6c1b04a7f72e2d25f13744464c14ad47b" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index 29942e494c..cf4c6bbcf5 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -23,7 +23,7 @@ required = [ # https://github.com/Knetic/govaluate/issues/61 [[constraint]] name = "github.com/sensu/govaluate" - branch = "nested-parameters" + version = "3.1.0" [[constraint]] name = "github.com/sirupsen/logrus" diff --git a/vendor/github.com/sensu/govaluate/evaluationStage.go b/vendor/github.com/sensu/govaluate/evaluationStage.go index 4f23809e1f..688cdf7819 100644 --- a/vendor/github.com/sensu/govaluate/evaluationStage.go +++ b/vendor/github.com/sensu/govaluate/evaluationStage.go @@ -431,15 +431,63 @@ func separatorStage(left interface{}, right interface{}, parameters Parameters) } func inStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) { - - for _, value := range right.([]interface{}) { - if left == value { + v := reflect.ValueOf(right) + for i := 0; i < v.Len(); i++ { + value := reflect.Indirect(v.Index(i)).Interface() + if valuesEqual(left, value) { return true, nil } } return false, nil } +func coerceFloat(v reflect.Value, kind reflect.Kind) float64 { + switch kind { + case reflect.Float32, reflect.Float64: + return v.Float() + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + return float64(v.Uint()) + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return float64(v.Int()) + default: + panic(fmt.Sprintf("not a number: %s", kind)) + } +} + +func isNumber(k reflect.Kind) bool { + switch k { + case reflect.Int: + case reflect.Int8: + case reflect.Int16: + case reflect.Int32: + case reflect.Int64: + case reflect.Uint: + case reflect.Uint8: + case reflect.Uint16: + case reflect.Uint32: + case reflect.Uint64: + case reflect.Float32: + case reflect.Float64: + default: + return false + } + return true +} + +func valuesEqual(x, y interface{}) bool { + v1 := reflect.ValueOf(x) + v2 := reflect.ValueOf(y) + v1Kind := v1.Kind() + v2Kind := v2.Kind() + if v1Kind == v2Kind { + return reflect.DeepEqual(x, y) + } + if isNumber(v1Kind) && isNumber(v2Kind) { + return coerceFloat(v1, v1Kind) == coerceFloat(v2, v2Kind) + } + return false +} + // func isString(value interface{}) bool { @@ -509,11 +557,7 @@ func comparatorTypeCheck(left interface{}, right interface{}) bool { } func isArray(value interface{}) bool { - switch value.(type) { - case []interface{}: - return true - } - return false + return reflect.ValueOf(value).Kind() == reflect.Slice } /*