From 5989727cf7560f406f7097a0aecc7a82c3c943cd Mon Sep 17 00:00:00 2001 From: joeybloggs Date: Fri, 14 Aug 2015 21:31:16 -0400 Subject: [PATCH] complete test coverage for IsGteField after conversion --- baked_in.go | 92 ++++++++--------------------------------------- validator_test.go | 10 ++++++ 2 files changed, 25 insertions(+), 77 deletions(-) diff --git a/baked_in.go b/baked_in.go index 8519e94..0c6ca4f 100644 --- a/baked_in.go +++ b/baked_in.go @@ -460,109 +460,47 @@ func hasValue(v *Validate, topStruct reflect.Value, currentStruct reflect.Value, } } -func isGteField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool { +func isGteField(v *Validate, topStruct reflect.Value, currentStruct reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool { - if !current.IsValid() { - panic("struct not passed for cross validation") - } - - if current.Kind() == reflect.Ptr && !current.IsNil() { - current = current.Elem() - } - - switch current.Kind() { - - case reflect.Struct: - - if current.Type() == timeType || current.Type() == timePtrType { - break - } - - current = current.FieldByName(param) - - if current.Kind() == reflect.Invalid { - panic(fmt.Sprintf("Field \"%s\" not found in struct", param)) - } - } - - if current.Kind() == reflect.Ptr && !current.IsNil() { - current = current.Elem() + currentField, currentKind, ok := v.getStructFieldOK(currentStruct, param) + if !ok || currentKind != fieldKind { + return false } switch fieldKind { 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: - return field.Uint() >= current.Uint() + return field.Uint() >= currentField.Uint() case reflect.Float32, reflect.Float64: - return field.Float() >= current.Float() + return field.Float() >= currentField.Float() case reflect.Struct: - if field.Type() == timeType || field.Type() == timePtrType { + // Not Same underlying type i.e. struct and time + if fieldType != currentField.Type() { + return false + } - if current.Type() != timeType && current.Type() != timePtrType { - panic("Bad Top Level field type") - } + if fieldType == timeType { - t := current.Interface().(time.Time) + t := currentField.Interface().(time.Time) fieldTime := field.Interface().(time.Time) return fieldTime.After(t) || fieldTime.Equal(t) } } - panic(fmt.Sprintf("Bad field type %T", field.Interface())) + // default reflect.String + return len(field.String()) >= len(currentField.String()) } -// func isEqField(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 { - -// case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: -// return field.Int() == currentField.Int() - -// case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: -// return field.Uint() == currentField.Uint() - -// case reflect.Float32, reflect.Float64: -// return field.Float() == currentField.Float() - -// case reflect.Slice, reflect.Map, reflect.Array: -// return int64(field.Len()) == int64(currentField.Len()) - -// case reflect.Struct: - -// // Not Same underlying type i.e. struct and time -// 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) diff --git a/validator_test.go b/validator_test.go index 07a7678..e9a9fb6 100644 --- a/validator_test.go +++ b/validator_test.go @@ -2882,6 +2882,16 @@ func TestGteField(t *testing.T) { NotEqual(t, errs, nil) AssertError(t, errs, "", "", "gtefield") + errs = validate.FieldWithValue(timeTest, &start, "gtefield") + NotEqual(t, errs, nil) + AssertError(t, errs, "", "", "gtefield") + + errs = validate.FieldWithValue("test", "test", "gtefield") + Equal(t, errs, nil) + + errs = validate.FieldWithValue("test", "test bigger", "gtefield") + Equal(t, errs, nil) + type IntTest struct { Val1 int `validate:"required"` Val2 int `validate:"required,gtefield=Val1"`