-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathitem.go
67 lines (57 loc) · 2.36 KB
/
item.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
package collections
import (
"bytes"
"context"
"fmt"
"cosmossdk.io/collections/codec"
)
// Item is a type declaration based on Map
// with a non-existent key.
type Item[V any] Map[noKey, V]
// NewItem instantiates a new Item instance, given the value encoder of the item V.
// Name and prefix must be unique within the schema and name must match the format specified by NameRegex, or
// else this method will panic.
func NewItem[V any](
schema *SchemaBuilder,
prefix Prefix,
name string,
valueCodec codec.ValueCodec[V],
) Item[V] {
item := (Item[V])(NewMap[noKey](schema, prefix, name, noKey{}, valueCodec))
return item
}
// Get gets the item, if it is not set it returns an ErrNotFound error.
// If value decoding fails then an ErrEncoding is returned.
func (i Item[V]) Get(ctx context.Context) (V, error) {
return (Map[noKey, V])(i).Get(ctx, noKey{})
}
// Set sets the item in the store. If Value encoding fails then an ErrEncoding is returned.
func (i Item[V]) Set(ctx context.Context, value V) error {
return (Map[noKey, V])(i).Set(ctx, noKey{}, value)
}
// Has reports whether the item exists in the store or not.
// Returns an error in case
func (i Item[V]) Has(ctx context.Context) (bool, error) {
return (Map[noKey, V])(i).Has(ctx, noKey{})
}
// Remove removes the item in the store.
func (i Item[V]) Remove(ctx context.Context) error {
return (Map[noKey, V])(i).Remove(ctx, noKey{})
}
// noKey defines a KeyCodec which decodes nothing.
type noKey struct{}
func (noKey) Stringify(_ noKey) string { return "no_key" }
func (noKey) KeyType() string { return "no_key" }
func (noKey) Size(_ noKey) int { return 0 }
func (noKey) Encode(_ []byte, _ noKey) (int, error) { return 0, nil }
func (noKey) Decode(_ []byte) (int, noKey, error) { return 0, noKey{}, nil }
func (noKey) EncodeJSON(_ noKey) ([]byte, error) { return []byte(`"item"`), nil }
func (noKey) DecodeJSON(b []byte) (noKey, error) {
if !bytes.Equal(b, []byte(`"item"`)) {
return noKey{}, fmt.Errorf("%w: invalid item json key bytes", ErrEncoding)
}
return noKey{}, nil
}
func (k noKey) EncodeNonTerminal(_ []byte, _ noKey) (int, error) { panic("must not be called") }
func (k noKey) DecodeNonTerminal(_ []byte) (int, noKey, error) { panic("must not be called") }
func (k noKey) SizeNonTerminal(_ noKey) int { panic("must not be called") }