forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collections.go
124 lines (99 loc) · 3.07 KB
/
collections.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package types
import (
"github.com/verzth/cosmos-sdk/collections"
collcodec "github.com/verzth/cosmos-sdk/collections/codec"
"github.com/verzth/cosmos-sdk/math"
)
var (
// AccAddressKey follows the same semantics of collections.BytesKey.
// It just uses humanised format for the String() and EncodeJSON().
AccAddressKey collcodec.KeyCodec[AccAddress] = genericAddressKey[AccAddress]{
stringDecoder: AccAddressFromBech32,
keyType: "sdk.AccAddress",
}
// ValAddressKey follows the same semantics as AccAddressKey.
ValAddressKey collcodec.KeyCodec[ValAddress] = genericAddressKey[ValAddress]{
stringDecoder: ValAddressFromBech32,
keyType: "sdk.ValAddress",
}
// ConsAddressKey follows the same semantics as ConsAddressKey.
ConsAddressKey collcodec.KeyCodec[ConsAddress] = genericAddressKey[ConsAddress]{
stringDecoder: ConsAddressFromBech32,
keyType: "sdk.ConsAddress",
}
// IntValue represents a collections.ValueCodec to work with Int.
IntValue collcodec.ValueCodec[math.Int] = intValueCodec{}
)
type addressUnion interface {
AccAddress | ValAddress | ConsAddress
String() string
}
type genericAddressKey[T addressUnion] struct {
stringDecoder func(string) (T, error)
keyType string
}
func (a genericAddressKey[T]) Encode(buffer []byte, key T) (int, error) {
return collections.BytesKey.Encode(buffer, key)
}
func (a genericAddressKey[T]) Decode(buffer []byte) (int, T, error) {
return collections.BytesKey.Decode(buffer)
}
func (a genericAddressKey[T]) Size(key T) int {
return collections.BytesKey.Size(key)
}
func (a genericAddressKey[T]) EncodeJSON(value T) ([]byte, error) {
return collections.StringKey.EncodeJSON(value.String())
}
func (a genericAddressKey[T]) DecodeJSON(b []byte) (v T, err error) {
s, err := collections.StringKey.DecodeJSON(b)
if err != nil {
return
}
v, err = a.stringDecoder(s)
return
}
func (a genericAddressKey[T]) Stringify(key T) string {
return key.String()
}
func (a genericAddressKey[T]) KeyType() string {
return a.keyType
}
func (a genericAddressKey[T]) EncodeNonTerminal(buffer []byte, key T) (int, error) {
return collections.BytesKey.EncodeNonTerminal(buffer, key)
}
func (a genericAddressKey[T]) DecodeNonTerminal(buffer []byte) (int, T, error) {
return collections.BytesKey.DecodeNonTerminal(buffer)
}
func (a genericAddressKey[T]) SizeNonTerminal(key T) int {
return collections.BytesKey.SizeNonTerminal(key)
}
// Collection Codecs
type intValueCodec struct{}
func (i intValueCodec) Encode(value math.Int) ([]byte, error) {
return value.Marshal()
}
func (i intValueCodec) Decode(b []byte) (math.Int, error) {
v := new(Int)
err := v.Unmarshal(b)
if err != nil {
return Int{}, err
}
return *v, nil
}
func (i intValueCodec) EncodeJSON(value math.Int) ([]byte, error) {
return value.MarshalJSON()
}
func (i intValueCodec) DecodeJSON(b []byte) (Int, error) {
v := new(Int)
err := v.UnmarshalJSON(b)
if err != nil {
return Int{}, err
}
return *v, nil
}
func (i intValueCodec) Stringify(value Int) string {
return value.String()
}
func (i intValueCodec) ValueType() string {
return "math.Int"
}