-
Notifications
You must be signed in to change notification settings - Fork 0
/
values.go
77 lines (64 loc) · 2.14 KB
/
values.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
package collections
import (
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gogo/protobuf/proto"
)
var (
AccAddressValueEncoder ValueEncoder[sdk.AccAddress] = accAddressValueEncoder{}
DecValueEncoder ValueEncoder[sdk.Dec] = decValue{}
Uint64ValueEncoder ValueEncoder[uint64] = uint64Value{}
)
// ProtoValueEncoder returns a protobuf value encoder given the codec.BinaryCodec.
// It's used to convert a specific protobuf object into bytes representation and convert
// the protobuf object bytes representation into the concrete object.
func ProtoValueEncoder[V any, PV interface {
*V
codec.ProtoMarshaler
}](cdc codec.BinaryCodec) ValueEncoder[V] {
return protoValueEncoder[V, PV]{
cdc: cdc,
}
}
type protoValueEncoder[V any, PV interface {
*V
codec.ProtoMarshaler
}] struct {
cdc codec.BinaryCodec
}
func (p protoValueEncoder[V, PV]) Name() string { return proto.MessageName(PV(new(V))) }
func (p protoValueEncoder[V, PV]) Encode(value V) []byte { return p.cdc.MustMarshal(PV(&value)) }
func (p protoValueEncoder[V, PV]) Stringify(v V) string { return PV(&v).String() }
func (p protoValueEncoder[V, PV]) Decode(b []byte) V {
v := PV(new(V))
p.cdc.MustUnmarshal(b, v)
return *v
}
type decValue struct{}
func (d decValue) Encode(value sdk.Dec) []byte {
b, err := value.Marshal()
if err != nil {
panic(fmt.Errorf("%w %s", err, HumanizeBytes(b)))
}
return b
}
func (d decValue) Decode(b []byte) sdk.Dec {
dec := new(sdk.Dec)
err := dec.Unmarshal(b)
if err != nil {
panic(fmt.Errorf("%w %s", err, HumanizeBytes(b)))
}
return *dec
}
func (d decValue) Stringify(value sdk.Dec) string {
return value.String()
}
func (d decValue) Name() string {
return "sdk.Dec"
}
type accAddressValueEncoder struct{}
func (a accAddressValueEncoder) Encode(value sdk.AccAddress) []byte { return value }
func (a accAddressValueEncoder) Decode(b []byte) sdk.AccAddress { return b }
func (a accAddressValueEncoder) Stringify(value sdk.AccAddress) string { return value.String() }
func (a accAddressValueEncoder) Name() string { return "sdk.AccAddress" }