diff --git a/baked_in.go b/baked_in.go index 0c05daa..8ad1e64 100644 --- a/baked_in.go +++ b/baked_in.go @@ -1129,13 +1129,7 @@ func isEq(fl FieldLevel) bool { return int64(field.Len()) == p case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - var p int64 - - if field.Type() == timeDurationType { - p = asIntFromTimeDuration(param) - } else { - p = asInt(param) - } + p := asIntFromType(field.Type(), param) return field.Int() == p @@ -1547,13 +1541,7 @@ func isGte(fl FieldLevel) bool { return int64(field.Len()) >= p case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - var p int64 - - if field.Type() == timeDurationType { - p = asIntFromTimeDuration(param) - } else { - p = asInt(param) - } + p := asIntFromType(field.Type(), param) return field.Int() >= p @@ -1600,13 +1588,7 @@ func isGt(fl FieldLevel) bool { return int64(field.Len()) > p case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - var p int64 - - if field.Type() == timeDurationType { - p = asIntFromTimeDuration(param) - } else { - p = asInt(param) - } + p := asIntFromType(field.Type(), param) return field.Int() > p @@ -1649,13 +1631,7 @@ func hasLengthOf(fl FieldLevel) bool { return int64(field.Len()) == p case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - var p int64 - - if field.Type() == timeDurationType { - p = asIntFromTimeDuration(param) - } else { - p = asInt(param) - } + p := asIntFromType(field.Type(), param) return field.Int() == p @@ -1791,13 +1767,7 @@ func isLte(fl FieldLevel) bool { return int64(field.Len()) <= p case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - var p int64 - - if field.Type() == timeDurationType { - p = asIntFromTimeDuration(param) - } else { - p = asInt(param) - } + p := asIntFromType(field.Type(), param) return field.Int() <= p @@ -1844,13 +1814,7 @@ func isLt(fl FieldLevel) bool { return int64(field.Len()) < p case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - var p int64 - - if field.Type() == timeDurationType { - p = asIntFromTimeDuration(param) - } else { - p = asInt(param) - } + p := asIntFromType(field.Type(), param) return field.Int() < p diff --git a/util.go b/util.go index cb7f0ee..6e568f8 100644 --- a/util.go +++ b/util.go @@ -239,6 +239,17 @@ func asIntFromTimeDuration(param string) int64 { return int64(d) } +// asIntFromType calls the proper function to parse param as int64, +// given a field's Type t. +func asIntFromType(t reflect.Type, param string) int64 { + switch t { + case timeDurationType: + return asIntFromTimeDuration(param) + default: + return asInt(param) + } +} + // asUint returns the parameter as a uint64 // or panics if it can't convert func asUint(param string) uint64 {