-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmarshaler_cache.go
61 lines (52 loc) · 1.21 KB
/
marshaler_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 newValuesMarshalerCache(wrapped ValuesMarshalerFactory) ValuesMarshalerFactory {
return &valuesMarshalerCache{
wrapped: wrapped,
cache: newSyncMap(),
}
}
type valuesMarshalerCache struct {
wrapped ValuesMarshalerFactory
cache syncMap
}
func (o *valuesMarshalerCache) ValuesMarshaler(t reflect.Type, opts *MarshalOptions) (ValuesMarshaler, error) {
if item, ok := o.cache.Load(t); ok {
if m, ok := item.(ValuesMarshaler); ok {
return m, nil
}
return nil, item.(error)
}
m, err := o.wrapped.ValuesMarshaler(t, opts)
if err != nil {
o.cache.Store(t, err)
} else {
o.cache.Store(t, m)
}
return m, err
}
func newMarshalerCache(wrapped MarshalerFactory) MarshalerFactory {
return &marshalerCache{
wrapped: wrapped,
cache: newSyncMap(),
}
}
type marshalerCache struct {
wrapped MarshalerFactory
cache syncMap
}
func (o *marshalerCache) Marshaler(t reflect.Type, opts *MarshalOptions) (Marshaler, error) {
if item, ok := o.cache.Load(t); ok {
if m, ok := item.(Marshaler); ok {
return m, nil
}
return nil, item.(error)
}
m, err := o.wrapped.Marshaler(t, opts)
if err != nil {
o.cache.Store(t, err)
} else {
o.cache.Store(t, m)
}
return m, err
}