From f581a2d517e569f6c333a8f8e69b7637da28be6a Mon Sep 17 00:00:00 2001 From: joeybloggs Date: Fri, 5 Aug 2016 22:26:20 -0400 Subject: [PATCH] reuse field --- validator.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/validator.go b/validator.go index 8a7e686..0173bd0 100644 --- a/validator.go +++ b/validator.go @@ -205,30 +205,35 @@ OUTER: switch kind { case reflect.Slice, reflect.Array: - var nm string + var i64 int64 + reusableCF := &cField{} // TODO: cache pool &cField for i := 0; i < current.Len(); i++ { + i64 = int64(i) + v.misc = append(v.misc[0:0], cf.Name...) v.misc = append(v.misc, '[') - v.misc = strconv.AppendInt(v.misc, int64(i), 10) + v.misc = strconv.AppendInt(v.misc, i64, 10) v.misc = append(v.misc, ']') - nm = string(v.misc) + reusableCF.Name = string(v.misc) v.misc = append(v.misc[0:0], cf.AltName...) v.misc = append(v.misc, '[') - v.misc = strconv.AppendInt(v.misc, int64(i), 10) + v.misc = strconv.AppendInt(v.misc, i64, 10) v.misc = append(v.misc, ']') - v.traverseField(parent, current.Index(i), ns, structNs, &cField{Name: nm, AltName: string(v.misc)}, ct) + reusableCF.AltName = string(v.misc) + + v.traverseField(parent, current.Index(i), ns, structNs, reusableCF, ct) } case reflect.Map: - var nm string var pv string + reusableCF := &cField{} for _, key := range current.MapKeys() { @@ -239,14 +244,16 @@ OUTER: v.misc = append(v.misc, pv...) v.misc = append(v.misc, ']') - nm = string(v.misc) + reusableCF.Name = string(v.misc) v.misc = append(v.misc[0:0], cf.AltName...) v.misc = append(v.misc, '[') v.misc = append(v.misc, pv...) v.misc = append(v.misc, ']') - v.traverseField(parent, current.MapIndex(key), ns, structNs, &cField{Name: nm, AltName: string(v.misc)}, ct) + reusableCF.AltName = string(v.misc) + + v.traverseField(parent, current.MapIndex(key), ns, structNs, reusableCF, ct) } default: