-
Notifications
You must be signed in to change notification settings - Fork 33
/
resolver.go
101 lines (82 loc) · 2.64 KB
/
resolver.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package identity
import (
"sync"
"github.com/stateful/runme/v3/internal/ulid"
)
type LifecycleIdentity int
// LifecycleIdentities are used to determine which object identities should be generated.
// The default is to generate all identities.
//
// The following identities are supported:
// - UnspecifiedLifecycleIdentity: No identity is generated.
// - AllLifecycleIdentity: All identities are generated.
// - DocumentLifecycleIdentity: Document identities are generated.
// - CellLifecycleIdentity: Cell identities are generated.
const (
UnspecifiedLifecycleIdentity LifecycleIdentity = iota
AllLifecycleIdentity
DocumentLifecycleIdentity
CellLifecycleIdentity
)
const DefaultLifecycleIdentity = AllLifecycleIdentity
var documentIdentities = &LifecycleIdentities{
AllLifecycleIdentity,
DocumentLifecycleIdentity,
}
var cellIdentities = &LifecycleIdentities{
AllLifecycleIdentity,
CellLifecycleIdentity,
}
type LifecycleIdentities []LifecycleIdentity
// Contains returns true if the required identity is contained in the provided identities.
func (ids LifecycleIdentities) Contains(id LifecycleIdentity) bool {
for _, v := range ids {
if v == id {
return true
}
}
return false
}
type IdentityResolver struct {
documentIdentity bool
cellIdentity bool
cache *sync.Map
}
func NewResolver(required LifecycleIdentity) *IdentityResolver {
return &IdentityResolver{
documentIdentity: documentIdentities.Contains(required),
cellIdentity: cellIdentities.Contains(required),
cache: &sync.Map{},
}
}
// CellEnabled returns true if the resolver is configured to generate cell identities.
func (ir *IdentityResolver) CellEnabled() bool {
return ir.cellIdentity
}
// DocumentEnabled returns true if the resolver is configured to generate document identities.
func (ir *IdentityResolver) DocumentEnabled() bool {
return ir.documentIdentity
}
// EphemeralDocumentID returns a new document ID which is not persisted.
func (ir *IdentityResolver) EphemeralDocumentID() string {
return ulid.GenerateID()
}
// GetCellID returns a cell ID and a boolean indicating if it's new or from attributes.
func (ir *IdentityResolver) GetCellID(obj any, attributes map[string]string) (string, bool) {
if !ir.cellIdentity {
return "", false
}
// todo(sebastian): are invalid ulid's valid IDs?
// Check for a valid 'id' in attributes;
// if present and valid due to explicit cell identity cache and return it.
if n, ok := attributes["id"]; ok && ulid.ValidID(n) {
ir.cache.Store(obj, n)
return n, true
}
if v, ok := ir.cache.Load(obj); ok {
return v.(string), false
}
id := ulid.GenerateID()
ir.cache.Store(obj, id)
return id, false
}