-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheme.go
More file actions
49 lines (37 loc) · 848 Bytes
/
scheme.go
File metadata and controls
49 lines (37 loc) · 848 Bytes
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
package srm
import (
"errors"
"github.com/CloudyKit/srm/scheme"
"reflect"
"sync"
"sync/atomic"
)
var (
scmMap = map[reflect.Type]*scheme.Scheme{}
scmRwMutex = sync.RWMutex{}
)
func getScheme(t reflect.Type) *scheme.Scheme {
scmRwMutex.RLock()
_scheme, found := scmMap[t]
scmRwMutex.RUnlock()
if !found {
panic(errors.New("Trying to use an undefined Scheme! call database.Scheme(Type,func("))
}
return _scheme
}
func Scheme(typ IModel, defFn func(*scheme.Def)) *scheme.Def {
scmRwMutex.Lock()
defer scmRwMutex.Unlock()
t := reflect.TypeOf(typ)
scm, found := scmMap[t]
if found {
panic(errors.New("Scheme redefinition is not permited!"))
}
scm = &scheme.Scheme{Type: t}
// interprets the new allocated scheme as type *Def
scmDef := (*scheme.Def)(scm)
defFn(scmDef)
scmDef.Done()
scmMap[t] = scm
return scm
}