|
47 | 47 | emptyStructPtr = new(struct{}) |
48 | 48 | ) |
49 | 49 |
|
50 | | -type cachedTag struct { |
51 | | - isOmitEmpty bool |
52 | | - isNoStructLevel bool |
53 | | - isStructOnly bool |
54 | | - diveTag string |
55 | | - tags []*tagVals |
56 | | -} |
57 | | - |
58 | | -type tagVals struct { |
59 | | - tagVals [][]string |
60 | | - isOrVal bool |
61 | | - isAlias bool |
62 | | - tag string |
63 | | -} |
64 | | - |
65 | | -type tagCacheMap struct { |
66 | | - lock sync.RWMutex |
67 | | - m map[string]*cachedTag |
68 | | -} |
69 | | - |
70 | | -func (s *tagCacheMap) Get(key string) (*cachedTag, bool) { |
71 | | - s.lock.RLock() |
72 | | - value, ok := s.m[key] |
73 | | - s.lock.RUnlock() |
74 | | - return value, ok |
75 | | -} |
76 | | - |
77 | | -func (s *tagCacheMap) Set(key string, value *cachedTag) { |
78 | | - s.lock.Lock() |
79 | | - s.m[key] = value |
80 | | - s.lock.Unlock() |
81 | | -} |
82 | | - |
83 | 50 | // StructLevel contains all of the information and helper methods |
84 | 51 | // for reporting errors during struct level validation |
85 | 52 | type StructLevel struct { |
@@ -154,7 +121,8 @@ type Validate struct { |
154 | 121 | hasCustomFuncs bool |
155 | 122 | hasAliasValidators bool |
156 | 123 | hasStructLevelFuncs bool |
157 | | - tagsCache *tagCacheMap |
| 124 | + tagCache *tagCacheMap |
| 125 | + structCache *structCacheMap |
158 | 126 | errsPool *sync.Pool |
159 | 127 | } |
160 | 128 |
|
@@ -227,7 +195,8 @@ func New(config *Config) *Validate { |
227 | 195 | v := &Validate{ |
228 | 196 | tagName: config.TagName, |
229 | 197 | fieldNameTag: config.FieldNameTag, |
230 | | - tagsCache: &tagCacheMap{m: map[string]*cachedTag{}}, |
| 198 | + tagCache: &tagCacheMap{m: map[string]*cachedTag{}}, |
| 199 | + structCache: &structCacheMap{m: map[string]*cachedStruct{}}, |
231 | 200 | errsPool: &sync.Pool{New: func() interface{} { |
232 | 201 | return ValidationErrors{} |
233 | 202 | }}} |
@@ -545,11 +514,10 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect. |
545 | 514 | return |
546 | 515 | } |
547 | 516 |
|
548 | | - cTag, isCached := v.tagsCache.Get(tag) |
| 517 | + cTag, isCached := v.tagCache.Get(tag) |
549 | 518 |
|
550 | 519 | if !isCached { |
551 | 520 | cTag = v.parseTags(tag, name) |
552 | | - v.tagsCache.Set(tag, cTag) |
553 | 521 | } |
554 | 522 |
|
555 | 523 | current, kind := v.ExtractType(current) |
|
0 commit comments