Update isGtField to user new functions

pull/161/head
joeybloggs 9 years ago
parent 2fe52ca08f
commit 8aea478060
  1. 89
      baked_in.go
  2. 6
      validator_test.go

@ -256,17 +256,11 @@ func isNe(v *Validate, topStruct reflect.Value, currentStruct reflect.Value, fie
func isEqCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool { func isEqCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
// if !topStruct.IsValid() {
// panic("struct or field value not passed for cross validation")
// }
topField, topKind, ok := v.getStructFieldOK(topStruct, param) topField, topKind, ok := v.getStructFieldOK(topStruct, param)
if !ok || topKind != fieldKind { if !ok || topKind != fieldKind {
// fmt.Println("NOT OK:", ok)
return false return false
} }
// fmt.Println("HERE", fieldKind)
switch fieldKind { switch fieldKind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
@ -279,7 +273,6 @@ func isEqCrossStructField(v *Validate, topStruct reflect.Value, current reflect.
return topField.Float() == field.Float() return topField.Float() == field.Float()
case reflect.Slice, reflect.Map, reflect.Array: case reflect.Slice, reflect.Map, reflect.Array:
// fmt.Println(topField.Len(), field.Len())
return int64(topField.Len()) == int64(field.Len()) return int64(topField.Len()) == int64(field.Len())
case reflect.Struct: case reflect.Struct:
@ -304,10 +297,6 @@ func isEqCrossStructField(v *Validate, topStruct reflect.Value, current reflect.
func isEqField(v *Validate, topStruct reflect.Value, currentStruct reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool { func isEqField(v *Validate, topStruct reflect.Value, currentStruct reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
// if !currentStruct.IsValid() {
// panic("struct or field value not passed for cross validation")
// }
currentField, currentKind, ok := v.getStructFieldOK(currentStruct, param) currentField, currentKind, ok := v.getStructFieldOK(currentStruct, param)
if !ok || currentKind != fieldKind { if !ok || currentKind != fieldKind {
return false return false
@ -532,65 +521,87 @@ func isGteField(v *Validate, topStruct reflect.Value, current reflect.Value, fie
panic(fmt.Sprintf("Bad field type %T", field.Interface())) panic(fmt.Sprintf("Bad field type %T", field.Interface()))
} }
func isGtField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool { // func isEqField(v *Validate, topStruct reflect.Value, currentStruct reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
if !current.IsValid() { // currentField, currentKind, ok := v.getStructFieldOK(currentStruct, param)
panic("struct not passed for cross validation") // if !ok || currentKind != fieldKind {
} // return false
// }
if current.Kind() == reflect.Ptr && !current.IsNil() { // switch fieldKind {
current = current.Elem()
}
switch current.Kind() { // case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
// return field.Int() == currentField.Int()
case reflect.Struct: // case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
// return field.Uint() == currentField.Uint()
if current.Type() == timeType || current.Type() == timePtrType { // case reflect.Float32, reflect.Float64:
break // return field.Float() == currentField.Float()
}
current = current.FieldByName(param) // case reflect.Slice, reflect.Map, reflect.Array:
// return int64(field.Len()) == int64(currentField.Len())
if current.Kind() == reflect.Invalid { // case reflect.Struct:
panic(fmt.Sprintf("Field \"%s\" not found in struct", param))
}
}
if current.Kind() == reflect.Ptr && !current.IsNil() { // // Not Same underlying type i.e. struct and time
current = current.Elem() // if fieldType != currentField.Type() {
// return false
// }
// if fieldType == timeType {
// t := currentField.Interface().(time.Time)
// fieldTime := field.Interface().(time.Time)
// return fieldTime.Equal(t)
// }
// }
// // default reflect.String:
// return field.String() == currentField.String()
// }
func isGtField(v *Validate, topStruct reflect.Value, currentStruct reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
currentField, currentKind, ok := v.getStructFieldOK(currentStruct, param)
if !ok || currentKind != fieldKind {
return false
} }
switch fieldKind { switch fieldKind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return field.Int() > current.Int() return field.Int() > currentField.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return field.Uint() > current.Uint() return field.Uint() > currentField.Uint()
case reflect.Float32, reflect.Float64: case reflect.Float32, reflect.Float64:
return field.Float() > current.Float() return field.Float() > currentField.Float()
case reflect.Struct: case reflect.Struct:
if field.Type() == timeType || field.Type() == timePtrType { // Not Same underlying type i.e. struct and time
if fieldType != currentField.Type() {
if current.Type() != timeType && current.Type() != timePtrType { return false
panic("Bad Top Level field type")
} }
t := current.Interface().(time.Time) if fieldType == timeType {
t := currentField.Interface().(time.Time)
fieldTime := field.Interface().(time.Time) fieldTime := field.Interface().(time.Time)
return fieldTime.After(t) return fieldTime.After(t)
} }
} }
panic(fmt.Sprintf("Bad field type %T", field.Interface())) // default reflect.String
return len(field.String()) > len(currentField.String())
} }
func isGte(v *Validate, topStruct reflect.Value, currentStruct reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool { func isGte(v *Validate, topStruct reflect.Value, currentStruct reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {

@ -2459,6 +2459,12 @@ func TestGtField(t *testing.T) {
NotEqual(t, errs, nil) NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "gtfield") AssertError(t, errs, "", "", "gtfield")
errs = validate.FieldWithValue(&timeTest, &end, "gtfield")
NotEqual(t, errs, nil)
errs = validate.FieldWithValue("test", "test bigger", "gtfield")
Equal(t, errs, nil)
type IntTest struct { type IntTest struct {
Val1 int `validate:"required"` Val1 int `validate:"required"`
Val2 int `validate:"required,gtfield=Val1"` Val2 int `validate:"required,gtfield=Val1"`

Loading…
Cancel
Save