From 5d28ac67f7f131ad8d9d44027c02ab0c98684754 Mon Sep 17 00:00:00 2001 From: joeybloggs Date: Thu, 4 Aug 2016 21:20:58 -0400 Subject: [PATCH] remove validatable logic as it does't really work, both with clarity and pointer vs non-pointer receivers --- cache.go | 28 +++++++++++------ validator_test.go | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 9 deletions(-) diff --git a/cache.go b/cache.go index 7534ea7..4900087 100644 --- a/cache.go +++ b/cache.go @@ -2,7 +2,6 @@ package validator import ( "fmt" - "log" "reflect" "strings" "sync" @@ -27,7 +26,7 @@ const ( ) // var ( -// validatable = reflect.ValueOf((*Validatable)(nil)).Elem() +// validatable = reflect.TypeOf((*Validatable)(nil)).Elem() // ) type structCache struct { @@ -115,14 +114,25 @@ func (v *Validate) extractStructCache(current reflect.Value, sName string) *cStr cs = &cStruct{Name: sName, fields: make(map[int]*cField), fn: v.structLevelFuncs[typ]} - if vable, ok := reflect.PtrTo(typ).(Validatable); ok { + // newVal := reflect.New(typ) - if cs.fn != nil { - log.Println("warning: struct level validation overriding 'Validatabe' interface function") - } else { - cs.fn = vable.Validate - } - } + // // fmt.Println("implements:", reflect.PtrTo(typ).Implements(validatable)) + // tv1, tv2 := newVal.Interface().(Validatable) + // // tv1, tv2 := reflect.PtrTo(typ).Elem().(Validatable) + // fmt.Println("implements2:", tv1, tv2) + // // fmt.Println("Testing if Validatable", tv1, tv2, reflect.PtrTo(typ), current, current.Type(), typ) + + // if vable, ok := reflect.New(typ).Interface().(Validatable); ok { + + // // fmt.Println("Validatable", cs.fn) + + // if cs.fn != nil { + // log.Println("warning: struct level validation overriding 'Validatabe' interface function") + // } else { + // cs.fn = vable.Validate + // // fmt.Println(cs.fn) + // } + // } numFields := current.NumField() diff --git a/validator_test.go b/validator_test.go index d125068..0d2226b 100644 --- a/validator_test.go +++ b/validator_test.go @@ -304,6 +304,86 @@ type TestStructReturnValidationErrors struct { Inner1 *TestStructReturnValidationErrorsInner1 `json:"Inner1JSON"` } +// type TestValidatablePtr struct { +// Value string +// } + +// func (tv *TestValidatablePtr) Validate(sl StructLevel) { + +// fmt.Printf("HERE *********** %p %#v\n", tv, tv) +// if len(tv.Value) == 0 { +// sl.ReportError(reflect.ValueOf(tv.Value), "Value", "Value", "required") +// } +// } + +// type TestValidatableNoPtr struct { +// Value string +// } + +// func (tv TestValidatableNoPtr) Validate(sl StructLevel) { + +// fmt.Println("HERE2 ***********") +// if len(tv.Value) == 0 { +// sl.ReportError(reflect.ValueOf(tv.Value), "Value", "Value", "required") +// } +// } + +// var _ Validatable = new(TestValidatablePtr) + +// func TestStructLevelValidatableInterfaceValidations(t *testing.T) { + +// // validate := New() + +// // tv := &TestValidatablePtr{Value: "tst3"} + +// // fmt.Println("VALIDATING***************") +// // err := validate.Struct(tv) +// // NotEqual(t, err, nil) + +// // var tv2 TestValidatableNoPtr + +// // fmt.Println("VALIDATING***************") +// // err = validate.Struct(tv2) +// // NotEqual(t, err, nil) + +// // tv3 := &TestValidatablePtr{Value: "test"} + +// // fmt.Println("VALIDATING***************") +// // err = validate.Struct(tv3) +// // Equal(t, err, nil) + +// // v1 := New() +// // v1.RegisterStructValidation(StructValidationTestStruct, TestStruct{}) + +// // tst := &TestStruct{ +// // String: "good value", +// // } + +// // errs := v1.Struct(tst) +// // NotEqual(t, errs, nil) +// // AssertError(t, errs, "TestStruct.StringVal", "TestStruct.String", "StringVal", "String", "badvalueteststruct") + +// // v2 := New() +// // v2.RegisterStructValidation(StructValidationNoTestStructCustomName, TestStruct{}) + +// // errs = v2.Struct(tst) +// // NotEqual(t, errs, nil) +// // AssertError(t, errs, "TestStruct.String", "TestStruct.String", "String", "String", "badvalueteststruct") + +// // v3 := New() +// // v3.RegisterStructValidation(StructValidationTestStructInvalid, TestStruct{}) + +// // errs = v3.Struct(tst) +// // NotEqual(t, errs, nil) +// // AssertError(t, errs, "TestStruct.StringVal", "TestStruct.String", "StringVal", "String", "badvalueteststruct") + +// // v4 := New() +// // v4.RegisterStructValidation(StructValidationTestStructSuccess, TestStruct{}) + +// // errs = v4.Struct(tst) +// // Equal(t, errs, nil) +// } + func TestNameNamespace(t *testing.T) { type Inner2Namespace struct {