-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.go
54 lines (45 loc) · 959 Bytes
/
registry.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
package sqlfunc
import (
"database/sql"
"reflect"
"sync"
"sync/atomic"
)
// Ř is the private registry used by the sqlfunc monomorphizer.
// var Ř privateRegistry
var registry privateRegistry
func init() {
registry.ForEach.r = make(map[reflect.Type]funcForEach)
}
type privateRegistry struct {
ForEach registryForEach
}
type funcForEach = func(*sql.Rows, interface{}) error
type registryForEach struct {
disabled uint32
m sync.RWMutex
r map[reflect.Type]funcForEach
}
func (r *registryForEach) Disable(ig bool) {
v := uint32(0)
if ig {
v = 1
}
atomic.StoreUint32(&r.disabled, v)
}
func (r *registryForEach) Get(typ reflect.Type) funcForEach {
if atomic.LoadUint32(&r.disabled) != 0 {
return nil
}
r.m.RLock()
defer r.m.RUnlock()
return r.r[typ]
}
func (r *registryForEach) Register(t interface{}, f funcForEach) {
if f == nil {
return // panic?
}
r.m.Lock()
defer r.m.Unlock()
r.r[reflect.TypeOf(t)] = f
}