File tree Expand file tree Collapse file tree 2 files changed +39
-9
lines changed Expand file tree Collapse file tree 2 files changed +39
-9
lines changed Original file line number Diff line number Diff line change @@ -141,18 +141,24 @@ var (
141141 }
142142)
143143
144- var oneofValCache = map [string ][]string {}
145- var oneofValCacheLock = sync.Mutex {}
144+ var oneofValsCache = map [string ][]string {}
145+ var oneofValsCacheRWLock = sync.RWMutex {}
146146
147- func isOneOf ( fl FieldLevel ) bool {
148- param := fl . Param ()
149- oneofValCacheLock . Lock ()
150- vals , ok := oneofValCache [ param ]
147+ func parseOneOfParam2 ( s string ) [] string {
148+ oneofValsCacheRWLock . RLock ()
149+ vals , ok := oneofValsCache [ s ]
150+ oneofValsCacheRWLock . RUnlock ()
151151 if ! ok {
152- vals = strings .Fields (param )
153- oneofValCache [param ] = vals
152+ oneofValsCacheRWLock .Lock ()
153+ vals = strings .Fields (s )
154+ oneofValsCache [s ] = vals
155+ oneofValsCacheRWLock .Unlock ()
154156 }
155- oneofValCacheLock .Unlock ()
157+ return vals
158+ }
159+
160+ func isOneOf (fl FieldLevel ) bool {
161+ vals := parseOneOfParam2 (fl .Param ())
156162
157163 field := fl .Field ()
158164
Original file line number Diff line number Diff line change @@ -1184,3 +1184,27 @@ func BenchmarkStructComplexFailureParallel(b *testing.B) {
11841184 }
11851185 })
11861186}
1187+
1188+ type TestOneof struct {
1189+ Color string `validate:"oneof=red green"`
1190+ }
1191+
1192+ func BenchmarkOneof (b * testing.B ) {
1193+ w := & TestOneof {Color : "green" }
1194+ val := New ()
1195+ for i := 0 ; i < b .N ; i ++ {
1196+ val .Struct (w )
1197+ }
1198+ }
1199+
1200+ func BenchmarkOneofParallel (b * testing.B ) {
1201+ w := & TestOneof {Color : "green" }
1202+ val := New ()
1203+
1204+ b .ResetTimer ()
1205+ b .RunParallel (func (pb * testing.PB ) {
1206+ for pb .Next () {
1207+ val .Struct (w )
1208+ }
1209+ })
1210+ }
You can’t perform that action at this time.
0 commit comments