-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_value_entry.go
52 lines (43 loc) · 1.58 KB
/
key_value_entry.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
package natsutil
import (
"sync/atomic"
"time"
"github.com/41north/go-async"
"github.com/nats-io/nats.go"
)
// KeyValueEntry provides a generic interface for nats.KeyValueEntry.
type KeyValueEntry[T any] interface {
nats.KeyValueEntry
// UnmarshalValue decodes and returns the retrieve value.
UnmarshalValue() (T, error)
}
// kve is a generic implementation of nats.KeyValueEntry.
type kve[T any] struct {
// encoder defines how to encode T.
encoder nats.Encoder
// value represents the decoded return value.
value atomic.Pointer[async.Result[T]]
// delegate is the underlying nats.KeyValueEntry returned from the nats library.
delegate nats.KeyValueEntry
}
func (e *kve[T]) Bucket() string { return e.delegate.Bucket() }
func (e *kve[T]) Key() string { return e.delegate.Key() }
func (e *kve[T]) Value() []byte { return e.delegate.Value() }
func (e *kve[T]) Revision() uint64 { return e.delegate.Revision() }
func (e *kve[T]) Created() time.Time { return e.delegate.Created() }
func (e *kve[T]) Delta() uint64 { return e.delegate.Delta() }
func (e *kve[T]) Operation() nats.KeyValueOp { return e.delegate.Operation() }
func (e *kve[T]) UnmarshalValue() (T, error) {
// check if we have already unmarshalled the value
v := e.value.Load()
if v != nil {
// value has already been unmarshalled
return (*v).Unwrap()
}
var value T
err := e.encoder.Decode("", e.delegate.Value(), &value)
result := async.NewResult[T](value, err)
// cache the result and return
e.value.CompareAndSwap(nil, &result)
return result.Unwrap()
}