-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunique.go
50 lines (40 loc) · 825 Bytes
/
unique.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
package selo
type uniqueAccessor struct {
f Factory[any]
v any
hasValue bool
}
func (sa *uniqueAccessor) Get() any {
if sa.hasValue == false {
sa.v = sa.f()
}
return sa.v
}
type uniqueAccessorBuilder[T any] struct {
commonAccessorSettings
}
func (b *uniqueAccessorBuilder[T]) SetTag(tag string) AccessorBuilder[T] {
b.tag = tag
return b
}
func (b *uniqueAccessorBuilder[T]) SetFactory(f Factory[T]) AccessorBuilder[T] {
b.f = wrapFactory(f)
return b
}
func (b *uniqueAccessorBuilder[T]) SetLazy(lazy bool) AccessorBuilder[T] {
b.lazy = lazy
return b
}
func (b *uniqueAccessorBuilder[T]) Record() {
a := new(uniqueAccessor)
a.f = b.f
a.hasValue = !b.lazy
if b.lazy == false {
a.v = b.f()
}
if b.tag != "" {
l.set(newTaggedKey[T](b.tag), a)
} else {
l.set((*T)(nil), a)
}
}