Skip to content

Commit e00f5e0

Browse files
author
Dean Karn
committed
split out to function, defer is needed to handle panics
1 parent b962f3d commit e00f5e0

File tree

2 files changed

+20
-39
lines changed

2 files changed

+20
-39
lines changed

cache.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,23 @@ func (v *Validate) parseFieldTagsRecursive(tag string, fieldName string, alias s
315315
current.isBlockEnd = true
316316
}
317317
}
318-
319318
return
320319
}
320+
321+
func (v *Validate) fetchCacheTag(tag string) *cTag {
322+
// find cached tag
323+
ctag, found := v.tagCache.Get(tag)
324+
if !found {
325+
v.tagCache.lock.Lock()
326+
defer v.tagCache.lock.Unlock()
327+
328+
// could have been multiple trying to access, but once first is done this ensures tag
329+
// isn't parsed again.
330+
ctag, found = v.tagCache.Get(tag)
331+
if !found {
332+
ctag, _ = v.parseFieldTagsRecursive(tag, "", "", false)
333+
v.tagCache.Set(tag, ctag)
334+
}
335+
}
336+
return ctag
337+
}

validator_instance.go

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -520,23 +520,8 @@ func (v *Validate) VarCtx(ctx context.Context, field interface{}, tag string) (e
520520
return nil
521521
}
522522

523-
// find cached tag
524-
ctag, ok := v.tagCache.Get(tag)
525-
if !ok {
526-
v.tagCache.lock.Lock()
527-
528-
// could have been multiple trying to access, but once first is done this ensures tag
529-
// isn't parsed again.
530-
ctag, ok = v.tagCache.Get(tag)
531-
if !ok {
532-
ctag, _ = v.parseFieldTagsRecursive(tag, "", "", false)
533-
v.tagCache.Set(tag, ctag)
534-
}
535-
v.tagCache.lock.Unlock()
536-
}
537-
523+
ctag := v.fetchCacheTag(tag)
538524
val := reflect.ValueOf(field)
539-
540525
vd := v.pool.Get().(*validate)
541526
vd.top = val
542527
vd.isPartial = false
@@ -546,9 +531,7 @@ func (v *Validate) VarCtx(ctx context.Context, field interface{}, tag string) (e
546531
err = vd.errs
547532
vd.errs = nil
548533
}
549-
550534
v.pool.Put(vd)
551-
552535
return
553536
}
554537

@@ -589,36 +572,17 @@ func (v *Validate) VarWithValueCtx(ctx context.Context, field interface{}, other
589572
if len(tag) == 0 || tag == skipValidationTag {
590573
return nil
591574
}
592-
593-
// find cached tag
594-
ctag, ok := v.tagCache.Get(tag)
595-
if !ok {
596-
v.tagCache.lock.Lock()
597-
598-
// could have been multiple trying to access, but once first is done this ensures tag
599-
// isn't parsed again.
600-
ctag, ok = v.tagCache.Get(tag)
601-
if !ok {
602-
ctag, _ = v.parseFieldTagsRecursive(tag, "", "", false)
603-
v.tagCache.Set(tag, ctag)
604-
}
605-
v.tagCache.lock.Unlock()
606-
}
607-
575+
ctag := v.fetchCacheTag(tag)
608576
otherVal := reflect.ValueOf(other)
609-
610577
vd := v.pool.Get().(*validate)
611578
vd.top = otherVal
612579
vd.isPartial = false
613-
614580
vd.traverseField(ctx, otherVal, reflect.ValueOf(field), vd.ns[0:0], vd.actualNs[0:0], defaultCField, ctag)
615581

616582
if len(vd.errs) > 0 {
617583
err = vd.errs
618584
vd.errs = nil
619585
}
620-
621586
v.pool.Put(vd)
622-
623587
return
624588
}

0 commit comments

Comments
 (0)