-
Notifications
You must be signed in to change notification settings - Fork 0
/
ore.go
58 lines (47 loc) · 1013 Bytes
/
ore.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
package ore
import (
"context"
"fmt"
"sync"
)
var (
lock = &sync.RWMutex{}
isBuilt = false
container = map[string][]any{}
)
type Creator[T any] interface {
New(ctx context.Context) T
}
// Generates a unique identifier for an entry based on type and key(s)
func typeIdentifier[T any](key []KeyStringer) string {
for _, stringer := range key {
if stringer == nil {
panic(nilKey)
}
}
var mockType *T
customKey := oreKey(key)
tt := fmt.Sprintf("%c:%v", mockType, customKey)
return tt
}
// Appends an entry to the container with type and key
func appendToContainer[T any](entry entry[T], key []KeyStringer) {
if isBuilt {
panic(alreadyBuiltCannotAdd)
}
typeId := typeIdentifier[T](key)
lock.Lock()
container[typeId] = append(container[typeId], entry)
lock.Unlock()
}
func replaceEntry[T any](typeId string, index int, entry entry[T]) {
lock.Lock()
container[typeId][index] = entry
lock.Unlock()
}
func Build() {
if isBuilt {
panic(alreadyBuilt)
}
isBuilt = true
}