-
Notifications
You must be signed in to change notification settings - Fork 3
/
registry.go
81 lines (67 loc) · 2.14 KB
/
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Package store provides internal interfaces for dbServices and models to publish out changes
package store
import (
"reflect"
"strings"
"sync"
"github.com/globalsign/mgo"
)
var registry sync.Map
var registryHistory sync.Map
var Version string
type collectionStore interface {
ById(objectID interface{}, joins []string) (value reflect.Value, err error)
ByFilter(filter map[string]interface{}, inFilter map[string]interface{}, excludeFilter map[string]interface{}, joins []string) (value reflect.Value, err error)
CountByFilter(filter map[string]interface{}, inFilter map[string]interface{}, excludeFilter map[string]interface{}, joins []string) (count int, err error)
ReflectByFieldName(fieldName string, x interface{}) (value reflect.Value, err error)
ReflectBaseTypeByFieldName(fieldName string, x interface{}) (value reflect.Value, err error)
NewByReflection() (value reflect.Value)
SetCollection(mdb *mgo.Database)
Bootstrap() error
BootStrapComplete()
Index() error
}
type collectionHistoryStore interface {
SetCollection(mdb *mgo.Database)
Index() error
}
//RegisterStore will register a new store to the store registry.
func RegisterStore(x interface{}) {
key := strings.Replace(getType(x), "model", "", -1)
registry.Store(key, x)
}
//GetCollection will return the collection by key string.
func GetCollection(key string) (x collectionStore, ok bool) {
obj, ok := registry.Load(key)
if ok {
x = obj.(collectionStore)
}
return
}
//RegisterStore will register a new store to the store registry.
func RegisterHistoryStore(x interface{}) {
key := strings.Replace(getType(x), "model", "", -1)
registryHistory.Store(key, x)
}
//GetCollection will return the collection by key string.
func GetCollectionHistory(key string) (x collectionHistoryStore, ok bool) {
obj, ok := registryHistory.Load(key)
if ok {
x = obj.(collectionHistoryStore)
}
return
}
func getRegistry(key string) (x collectionStore, ok bool) {
obj, ok := registry.Load(key)
if ok {
x = obj.(collectionStore)
}
return
}
func getType(myvar interface{}) string {
if t := reflect.TypeOf(myvar); t.Kind() == reflect.Ptr {
return t.Elem().Name()
} else {
return t.Name()
}
}