Skip to content

Commit

Permalink
Fix support for aliased time.Time types
Browse files Browse the repository at this point in the history
  • Loading branch information
lafriks committed Jan 25, 2022
1 parent 88976f3 commit f3fa470
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 112 deletions.
202 changes: 100 additions & 102 deletions baked_in.go
Expand Up @@ -816,19 +816,18 @@ func isNeField(fl FieldLevel) bool {

fieldType := field.Type()

// Not Same underlying type i.e. struct and time
if fieldType != currentField.Type() {
return true
}

if fieldType == timeType {
if fieldType.ConvertibleTo(timeType) && currentField.Type().ConvertibleTo(timeType) {

t := currentField.Interface().(time.Time)
fieldTime := field.Interface().(time.Time)

return !fieldTime.Equal(t)
}

// Not Same underlying type i.e. struct and time
if fieldType != currentField.Type() {
return true
}
}

// default reflect.String:
Expand Down Expand Up @@ -869,18 +868,18 @@ func isLteCrossStructField(fl FieldLevel) bool {

fieldType := field.Type()

// Not Same underlying type i.e. struct and time
if fieldType != topField.Type() {
return false
}
if fieldType.ConvertibleTo(timeType) && topField.Type().ConvertibleTo(timeType) {

if fieldType == timeType {

fieldTime := field.Interface().(time.Time)
topTime := topField.Interface().(time.Time)
fieldTime := field.Convert(timeType).Interface().(time.Time)
topTime := topField.Convert(timeType).Interface().(time.Time)

return fieldTime.Before(topTime) || fieldTime.Equal(topTime)
}

// Not Same underlying type i.e. struct and time
if fieldType != topField.Type() {
return false
}
}

// default reflect.String:
Expand Down Expand Up @@ -917,18 +916,18 @@ func isLtCrossStructField(fl FieldLevel) bool {

fieldType := field.Type()

// Not Same underlying type i.e. struct and time
if fieldType != topField.Type() {
return false
}

if fieldType == timeType {
if fieldType.ConvertibleTo(timeType) && topField.Type().ConvertibleTo(timeType) {

fieldTime := field.Interface().(time.Time)
topTime := topField.Interface().(time.Time)
fieldTime := field.Convert(timeType).Interface().(time.Time)
topTime := topField.Convert(timeType).Interface().(time.Time)

return fieldTime.Before(topTime)
}

// Not Same underlying type i.e. struct and time
if fieldType != topField.Type() {
return false
}
}

// default reflect.String:
Expand Down Expand Up @@ -964,18 +963,18 @@ func isGteCrossStructField(fl FieldLevel) bool {

fieldType := field.Type()

// Not Same underlying type i.e. struct and time
if fieldType != topField.Type() {
return false
}

if fieldType == timeType {
if fieldType.ConvertibleTo(timeType) && topField.Type().ConvertibleTo(timeType) {

fieldTime := field.Interface().(time.Time)
topTime := topField.Interface().(time.Time)
fieldTime := field.Convert(timeType).Interface().(time.Time)
topTime := topField.Convert(timeType).Interface().(time.Time)

return fieldTime.After(topTime) || fieldTime.Equal(topTime)
}

// Not Same underlying type i.e. struct and time
if fieldType != topField.Type() {
return false
}
}

// default reflect.String:
Expand Down Expand Up @@ -1011,18 +1010,18 @@ func isGtCrossStructField(fl FieldLevel) bool {

fieldType := field.Type()

// Not Same underlying type i.e. struct and time
if fieldType != topField.Type() {
return false
}
if fieldType.ConvertibleTo(timeType) && topField.Type().ConvertibleTo(timeType) {

if fieldType == timeType {

fieldTime := field.Interface().(time.Time)
topTime := topField.Interface().(time.Time)
fieldTime := field.Convert(timeType).Interface().(time.Time)
topTime := topField.Convert(timeType).Interface().(time.Time)

return fieldTime.After(topTime)
}

// Not Same underlying type i.e. struct and time
if fieldType != topField.Type() {
return false
}
}

// default reflect.String:
Expand Down Expand Up @@ -1061,18 +1060,18 @@ func isNeCrossStructField(fl FieldLevel) bool {

fieldType := field.Type()

// Not Same underlying type i.e. struct and time
if fieldType != topField.Type() {
return true
}

if fieldType == timeType {
if fieldType.ConvertibleTo(timeType) && topField.Type().ConvertibleTo(timeType) {

t := field.Interface().(time.Time)
fieldTime := topField.Interface().(time.Time)
t := field.Convert(timeType).Interface().(time.Time)
fieldTime := topField.Convert(timeType).Interface().(time.Time)

return !fieldTime.Equal(t)
}

// Not Same underlying type i.e. struct and time
if fieldType != topField.Type() {
return true
}
}

// default reflect.String:
Expand Down Expand Up @@ -1111,18 +1110,18 @@ func isEqCrossStructField(fl FieldLevel) bool {

fieldType := field.Type()

// Not Same underlying type i.e. struct and time
if fieldType != topField.Type() {
return false
}

if fieldType == timeType {
if fieldType.ConvertibleTo(timeType) && topField.Type().ConvertibleTo(timeType) {

t := field.Interface().(time.Time)
fieldTime := topField.Interface().(time.Time)
t := field.Convert(timeType).Interface().(time.Time)
fieldTime := topField.Convert(timeType).Interface().(time.Time)

return fieldTime.Equal(t)
}

// Not Same underlying type i.e. struct and time
if fieldType != topField.Type() {
return false
}
}

// default reflect.String:
Expand Down Expand Up @@ -1161,19 +1160,18 @@ func isEqField(fl FieldLevel) bool {

fieldType := field.Type()

// Not Same underlying type i.e. struct and time
if fieldType != currentField.Type() {
return false
}
if fieldType.ConvertibleTo(timeType) && currentField.Type().ConvertibleTo(timeType) {

if fieldType == timeType {

t := currentField.Interface().(time.Time)
fieldTime := field.Interface().(time.Time)
t := currentField.Convert(timeType).Interface().(time.Time)
fieldTime := field.Convert(timeType).Interface().(time.Time)

return fieldTime.Equal(t)
}

// Not Same underlying type i.e. struct and time
if fieldType != currentField.Type() {
return false
}
}

// default reflect.String:
Expand Down Expand Up @@ -1677,18 +1675,18 @@ func isGteField(fl FieldLevel) bool {

fieldType := field.Type()

// Not Same underlying type i.e. struct and time
if fieldType != currentField.Type() {
return false
}

if fieldType == timeType {
if fieldType.ConvertibleTo(timeType) && currentField.Type().ConvertibleTo(timeType) {

t := currentField.Interface().(time.Time)
fieldTime := field.Interface().(time.Time)
t := currentField.Convert(timeType).Interface().(time.Time)
fieldTime := field.Convert(timeType).Interface().(time.Time)

return fieldTime.After(t) || fieldTime.Equal(t)
}

// Not Same underlying type i.e. struct and time
if fieldType != currentField.Type() {
return false
}
}

// default reflect.String
Expand Down Expand Up @@ -1724,18 +1722,18 @@ func isGtField(fl FieldLevel) bool {

fieldType := field.Type()

// Not Same underlying type i.e. struct and time
if fieldType != currentField.Type() {
return false
}

if fieldType == timeType {
if fieldType.ConvertibleTo(timeType) && currentField.Type().ConvertibleTo(timeType) {

t := currentField.Interface().(time.Time)
fieldTime := field.Interface().(time.Time)
t := currentField.Convert(timeType).Interface().(time.Time)
fieldTime := field.Convert(timeType).Interface().(time.Time)

return fieldTime.After(t)
}

// Not Same underlying type i.e. struct and time
if fieldType != currentField.Type() {
return false
}
}

// default reflect.String
Expand Down Expand Up @@ -1777,10 +1775,10 @@ func isGte(fl FieldLevel) bool {

case reflect.Struct:

if field.Type() == timeType {
if field.Type().ConvertibleTo(timeType) {

now := time.Now().UTC()
t := field.Interface().(time.Time)
t := field.Convert(timeType).Interface().(time.Time)

return t.After(now) || t.Equal(now)
}
Expand Down Expand Up @@ -1823,9 +1821,9 @@ func isGt(fl FieldLevel) bool {
return field.Float() > p
case reflect.Struct:

if field.Type() == timeType {
if field.Type().ConvertibleTo(timeType) {

return field.Interface().(time.Time).After(time.Now().UTC())
return field.Convert(timeType).Interface().(time.Time).After(time.Now().UTC())
}
}

Expand Down Expand Up @@ -1903,18 +1901,18 @@ func isLteField(fl FieldLevel) bool {

fieldType := field.Type()

// Not Same underlying type i.e. struct and time
if fieldType != currentField.Type() {
return false
}
if fieldType.ConvertibleTo(timeType) && currentField.Type().ConvertibleTo(timeType) {

if fieldType == timeType {

t := currentField.Interface().(time.Time)
fieldTime := field.Interface().(time.Time)
t := currentField.Convert(timeType).Interface().(time.Time)
fieldTime := field.Convert(timeType).Interface().(time.Time)

return fieldTime.Before(t) || fieldTime.Equal(t)
}

// Not Same underlying type i.e. struct and time
if fieldType != currentField.Type() {
return false
}
}

// default reflect.String
Expand Down Expand Up @@ -1950,18 +1948,18 @@ func isLtField(fl FieldLevel) bool {

fieldType := field.Type()

// Not Same underlying type i.e. struct and time
if fieldType != currentField.Type() {
return false
}

if fieldType == timeType {
if fieldType.ConvertibleTo(timeType) && currentField.Type().ConvertibleTo(timeType) {

t := currentField.Interface().(time.Time)
fieldTime := field.Interface().(time.Time)
t := currentField.Convert(timeType).Interface().(time.Time)
fieldTime := field.Convert(timeType).Interface().(time.Time)

return fieldTime.Before(t)
}

// Not Same underlying type i.e. struct and time
if fieldType != currentField.Type() {
return false
}
}

// default reflect.String
Expand Down Expand Up @@ -2003,10 +2001,10 @@ func isLte(fl FieldLevel) bool {

case reflect.Struct:

if field.Type() == timeType {
if field.Type().ConvertibleTo(timeType) {

now := time.Now().UTC()
t := field.Interface().(time.Time)
t := field.Convert(timeType).Interface().(time.Time)

return t.Before(now) || t.Equal(now)
}
Expand Down Expand Up @@ -2050,9 +2048,9 @@ func isLt(fl FieldLevel) bool {

case reflect.Struct:

if field.Type() == timeType {
if field.Type().ConvertibleTo(timeType) {

return field.Interface().(time.Time).Before(time.Now().UTC())
return field.Convert(timeType).Interface().(time.Time).Before(time.Now().UTC())
}
}

Expand Down
8 changes: 3 additions & 5 deletions util.go
Expand Up @@ -82,7 +82,7 @@ BEGIN:
fld := namespace
var ns string

if typ != timeType {
if !typ.ConvertibleTo(timeType) {

idx := strings.Index(namespace, namespaceSeparator)

Expand Down Expand Up @@ -243,12 +243,10 @@ func asIntFromTimeDuration(param string) int64 {
// 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:
if t.ConvertibleTo(timeDurationType) {
return asIntFromTimeDuration(param)
default:
return asInt(param)
}
return asInt(param)
}

// asUint returns the parameter as a uint64
Expand Down

0 comments on commit f3fa470

Please sign in to comment.