forked from pasztorpisti/qs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unmarshaler_cache.go
61 lines (52 loc) · 1.26 KB
/
unmarshaler_cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package qs
import "reflect"
func newValuesUnmarshalerCache(wrapped ValuesUnmarshalerFactory) ValuesUnmarshalerFactory {
return &valuesUnmarshalerCache{
wrapped: wrapped,
cache: newSyncMap(),
}
}
type valuesUnmarshalerCache struct {
wrapped ValuesUnmarshalerFactory
cache syncMap
}
func (o *valuesUnmarshalerCache) ValuesUnmarshaler(t reflect.Type, opts *UnmarshalOptions) (ValuesUnmarshaler, error) {
if item, ok := o.cache.Load(t); ok {
if m, ok := item.(ValuesUnmarshaler); ok {
return m, nil
}
return nil, item.(error)
}
u, err := o.wrapped.ValuesUnmarshaler(t, opts)
if err != nil {
o.cache.Store(t, err)
} else {
o.cache.Store(t, u)
}
return u, err
}
func newUnmarshalerCache(wrapped UnmarshalerFactory) UnmarshalerFactory {
return &unmarshalerCache{
wrapped: wrapped,
cache: newSyncMap(),
}
}
type unmarshalerCache struct {
wrapped UnmarshalerFactory
cache syncMap
}
func (o *unmarshalerCache) Unmarshaler(t reflect.Type, opts *UnmarshalOptions) (Unmarshaler, error) {
if item, ok := o.cache.Load(t); ok {
if m, ok := item.(Unmarshaler); ok {
return m, nil
}
return nil, item.(error)
}
u, err := o.wrapped.Unmarshaler(t, opts)
if err != nil {
o.cache.Store(t, err)
} else {
o.cache.Store(t, u)
}
return u, err
}