Skip to content

Commit 1cbd308

Browse files
committed
Add function asIntFromType
1 parent 33ffa4a commit 1cbd308

File tree

2 files changed

+17
-42
lines changed

2 files changed

+17
-42
lines changed

baked_in.go

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,13 +1129,7 @@ func isEq(fl FieldLevel) bool {
11291129
return int64(field.Len()) == p
11301130

11311131
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
1132-
var p int64
1133-
1134-
if field.Type() == timeDurationType {
1135-
p = asIntFromTimeDuration(param)
1136-
} else {
1137-
p = asInt(param)
1138-
}
1132+
p := asIntFromType(field.Type(), param)
11391133

11401134
return field.Int() == p
11411135

@@ -1547,13 +1541,7 @@ func isGte(fl FieldLevel) bool {
15471541
return int64(field.Len()) >= p
15481542

15491543
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
1550-
var p int64
1551-
1552-
if field.Type() == timeDurationType {
1553-
p = asIntFromTimeDuration(param)
1554-
} else {
1555-
p = asInt(param)
1556-
}
1544+
p := asIntFromType(field.Type(), param)
15571545

15581546
return field.Int() >= p
15591547

@@ -1600,13 +1588,7 @@ func isGt(fl FieldLevel) bool {
16001588
return int64(field.Len()) > p
16011589

16021590
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
1603-
var p int64
1604-
1605-
if field.Type() == timeDurationType {
1606-
p = asIntFromTimeDuration(param)
1607-
} else {
1608-
p = asInt(param)
1609-
}
1591+
p := asIntFromType(field.Type(), param)
16101592

16111593
return field.Int() > p
16121594

@@ -1649,13 +1631,7 @@ func hasLengthOf(fl FieldLevel) bool {
16491631
return int64(field.Len()) == p
16501632

16511633
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
1652-
var p int64
1653-
1654-
if field.Type() == timeDurationType {
1655-
p = asIntFromTimeDuration(param)
1656-
} else {
1657-
p = asInt(param)
1658-
}
1634+
p := asIntFromType(field.Type(), param)
16591635

16601636
return field.Int() == p
16611637

@@ -1791,13 +1767,7 @@ func isLte(fl FieldLevel) bool {
17911767
return int64(field.Len()) <= p
17921768

17931769
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
1794-
var p int64
1795-
1796-
if field.Type() == timeDurationType {
1797-
p = asIntFromTimeDuration(param)
1798-
} else {
1799-
p = asInt(param)
1800-
}
1770+
p := asIntFromType(field.Type(), param)
18011771

18021772
return field.Int() <= p
18031773

@@ -1844,13 +1814,7 @@ func isLt(fl FieldLevel) bool {
18441814
return int64(field.Len()) < p
18451815

18461816
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
1847-
var p int64
1848-
1849-
if field.Type() == timeDurationType {
1850-
p = asIntFromTimeDuration(param)
1851-
} else {
1852-
p = asInt(param)
1853-
}
1817+
p := asIntFromType(field.Type(), param)
18541818

18551819
return field.Int() < p
18561820

util.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,17 @@ func asIntFromTimeDuration(param string) int64 {
239239
return int64(d)
240240
}
241241

242+
// asIntFromType calls the proper function to parse param as int64,
243+
// given a field's Type t.
244+
func asIntFromType(t reflect.Type, param string) int64 {
245+
switch t {
246+
case timeDurationType:
247+
return asIntFromTimeDuration(param)
248+
default:
249+
return asInt(param)
250+
}
251+
}
252+
242253
// asUint returns the parameter as a uint64
243254
// or panics if it can't convert
244255
func asUint(param string) uint64 {

0 commit comments

Comments
 (0)