Skip to content

Commit cab69bd

Browse files
joeybloggsjoeybloggs
authored andcommitted
Add minor optimization for structonly & nostructlevel checks
1 parent 24c1c68 commit cab69bd

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

util.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,22 @@ func (v *Validate) parseTagsRecursive(cTag *cachedTag, tag, fieldName, alias str
271271
}
272272
}
273273

274-
if t == diveTag {
274+
switch t {
275+
276+
case diveTag:
275277
cTag.diveTag = tag
276278
tVals := &tagVals{tagVals: [][]string{{t}}}
277279
cTag.tags = append(cTag.tags, tVals)
278280
return true
279-
}
280281

281-
if t == omitempty {
282+
case omitempty:
282283
cTag.isOmitEmpty = true
284+
285+
case structOnlyTag:
286+
cTag.isStructOnly = true
287+
288+
case noStructLevelTag:
289+
cTag.isNoStructLevel = true
283290
}
284291

285292
// if a pipe character is needed within the param you must use the utf8Pipe representation "0x7C"

validator.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ var (
4848
)
4949

5050
type cachedTag struct {
51-
isOmitEmpty bool
52-
diveTag string
53-
tags []*tagVals
51+
isOmitEmpty bool
52+
isNoStructLevel bool
53+
isStructOnly bool
54+
diveTag string
55+
tags []*tagVals
5456
}
5557

5658
type tagVals struct {
@@ -597,11 +599,11 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.
597599

598600
if typ != timeType {
599601

600-
if strings.Contains(tag, noStructLevelTag) {
602+
if cTag.isNoStructLevel {
601603
return
602604
}
603605

604-
v.tranverseStruct(topStruct, current, current, errPrefix+name+".", errs, false, partial, exclude, includeExclude, strings.Contains(tag, structOnlyTag))
606+
v.tranverseStruct(topStruct, current, current, errPrefix+name+".", errs, false, partial, exclude, includeExclude, cTag.isStructOnly)
605607
return
606608
}
607609
}

0 commit comments

Comments
 (0)