From 50de7a42970a4f8c76d923f9827d85c8ac1a8431 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 2 Oct 2018 11:46:18 +0200 Subject: [PATCH] default type check 'int' does not use 'reflect' anymore --- internal/checker/default/int/main.go | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/internal/checker/default/int/main.go b/internal/checker/default/int/main.go index d0c4c2b..859e4b0 100644 --- a/internal/checker/default/int/main.go +++ b/internal/checker/default/int/main.go @@ -2,27 +2,12 @@ package main import ( "git.xdrm.io/go/aicra/driver" - "reflect" + "math" ) func main() {} func Export() driver.Checker { return new(IntChecker) } -var validationTable = map[reflect.Kind]interface{}{ - reflect.Float32: nil, - reflect.Float64: nil, - reflect.Int: nil, - reflect.Int8: nil, - reflect.Int16: nil, - reflect.Int32: nil, - reflect.Int64: nil, - reflect.Uint: nil, - reflect.Uint8: nil, - reflect.Uint16: nil, - reflect.Uint32: nil, - reflect.Uint64: nil, -} - type IntChecker int // Match matches the string 'int' @@ -33,10 +18,13 @@ func (ick IntChecker) Match(name string) bool { // Check returns true for any type from the @validationTable func (ick IntChecker) Check(value interface{}) bool { - kind := reflect.TypeOf(value).Kind() + // check if float (default wrapping type) + floatVal, ok := value.(float64) + if !ok { + return false + } - _, isTypeValid := validationTable[kind] - - return isTypeValid + // check if there is no floating point + return floatVal == math.Floor(floatVal) }