From 2baa4bd3535434e7679808385ac2752a332ac59d Mon Sep 17 00:00:00 2001 From: joeybloggs Date: Sun, 19 Jul 2015 21:39:58 -0400 Subject: [PATCH] Minor code cleanup --- validator.go | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/validator.go b/validator.go index 6c44f2b..6a527b2 100644 --- a/validator.go +++ b/validator.go @@ -303,7 +303,7 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect. if kind == reflect.Struct { - // required passed validationa above so stop here + // required passed validation above so stop here // if only validating the structs existance. if strings.Contains(tag, structOnlyTag) { return @@ -404,14 +404,7 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect. func (v *Validate) traverseSlice(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, tag string, name string) { for i := 0; i < current.Len(); i++ { - - idxField := current.Index(i) - - if idxField.Kind() == reflect.Ptr && !idxField.IsNil() { - idxField = idxField.Elem() - } - - v.traverseField(topStruct, currentStruct, idxField, errPrefix, errs, false, tag, fmt.Sprintf(arrayIndexFieldName, name, i)) + v.traverseField(topStruct, currentStruct, current.Index(i), errPrefix, errs, false, tag, fmt.Sprintf(arrayIndexFieldName, name, i)) } } @@ -419,27 +412,23 @@ func (v *Validate) traverseSlice(topStruct reflect.Value, currentStruct reflect. func (v *Validate) traverseMap(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, tag string, name string) { for _, key := range current.MapKeys() { - - idxField := current.MapIndex(key) - - if idxField.Kind() == reflect.Ptr && !idxField.IsNil() { - idxField = idxField.Elem() - } - - v.traverseField(topStruct, currentStruct, idxField, errPrefix, errs, false, tag, fmt.Sprintf(mapIndexFieldName, name, key.Interface())) + v.traverseField(topStruct, currentStruct, current.MapIndex(key), errPrefix, errs, false, tag, fmt.Sprintf(mapIndexFieldName, name, key.Interface())) } } // validateField validates a field based on the provided tag's key and param values and returns true if there is an error or false if all ok func (v *Validate) validateField(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, currentType reflect.Type, currentKind reflect.Kind, errPrefix string, errs ValidationErrors, cTag *tagCache, name string) bool { + var valFunc Func + var ok bool + if cTag.isOrVal { errTag := "" for _, val := range cTag.tagVals { - valFunc, ok := v.config.ValidationFuncs[val[0]] + valFunc, ok = v.config.ValidationFuncs[val[0]] if !ok { panic(strings.TrimSpace(fmt.Sprintf(undefinedValidation, name))) } @@ -462,7 +451,7 @@ func (v *Validate) validateField(topStruct reflect.Value, currentStruct reflect. return true } - valFunc, ok := v.config.ValidationFuncs[cTag.tagVals[0][0]] + valFunc, ok = v.config.ValidationFuncs[cTag.tagVals[0][0]] if !ok { panic(strings.TrimSpace(fmt.Sprintf(undefinedValidation, name))) }