74b22649 by Wyatt Fang Committed by astaxie

remove the double isStruct/isStructPtr check

1 parent 42556305
...@@ -342,6 +342,7 @@ func (v *Validation) Valid(obj interface{}) (b bool, err error) { ...@@ -342,6 +342,7 @@ func (v *Validation) Valid(obj interface{}) (b bool, err error) {
342 // Anonymous fields will be ignored 342 // Anonymous fields will be ignored
343 func (v *Validation) RecursiveValid(objc interface{}) (bool, error) { 343 func (v *Validation) RecursiveValid(objc interface{}) (bool, error) {
344 //Step 1: validate obj itself firstly 344 //Step 1: validate obj itself firstly
345 // fails if objc is not struct
345 pass, err := v.Valid(objc) 346 pass, err := v.Valid(objc)
346 if err != nil || false == pass { 347 if err != nil || false == pass {
347 return pass, err // Stop recursive validation 348 return pass, err // Stop recursive validation
...@@ -349,23 +350,22 @@ func (v *Validation) RecursiveValid(objc interface{}) (bool, error) { ...@@ -349,23 +350,22 @@ func (v *Validation) RecursiveValid(objc interface{}) (bool, error) {
349 // Step 2: Validate struct's struct fields 350 // Step 2: Validate struct's struct fields
350 objT := reflect.TypeOf(objc) 351 objT := reflect.TypeOf(objc)
351 objV := reflect.ValueOf(objc) 352 objV := reflect.ValueOf(objc)
352 if isStruct(objT) || isStructPtr(objT) {
353 353
354 if isStructPtr(objT) { 354 if isStructPtr(objT) {
355 objT = objT.Elem() 355 objT = objT.Elem()
356 objV = objV.Elem() 356 objV = objV.Elem()
357 } 357 }
358 358
359 for i := 0; i < objT.NumField(); i++ { 359 for i := 0; i < objT.NumField(); i++ {
360 360
361 t := objT.Field(i).Type 361 t := objT.Field(i).Type
362 362
363 if isStruct(t) || isStructPtr(t) { 363 // Recursive applies to struct or pointer to structs fields
364 // Step 3: do the recursive validation 364 if isStruct(t) || isStructPtr(t) {
365 // Only valid the Public field recursively 365 // Step 3: do the recursive validation
366 if objV.Field(i).CanInterface() { 366 // Only valid the Public field recursively
367 pass, err = v.RecursiveValid(objV.Field(i).Interface()) 367 if objV.Field(i).CanInterface() {
368 } 368 pass, err = v.RecursiveValid(objV.Field(i).Interface())
369 } 369 }
370 } 370 }
371 } 371 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!