Add function asIntFromType

pull/642/head
Elias Rodrigues 4 years ago
parent 33ffa4a31f
commit 1cbd3081e4
  1. 48
      baked_in.go
  2. 11
      util.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

@ -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 {

Loading…
Cancel
Save