/
codec_adapter.go
161 lines (133 loc) · 5.58 KB
/
codec_adapter.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package codec
import (
"github.com/gogo/protobuf/proto"
"github.com/furyaxyz/fuxchain/libs/cosmos-sdk/codec/types"
)
type (
// Marshaler defines the interface module codecs must implement in order to support
// backwards compatibility with Amino while allowing custom Protobuf-based
// serialization. Note, Amino can still be used without any dependency on
// Protobuf. There are two typical implementations that fulfill this contract:
//
// 1. AminoCodec: Provides full Amino serialization compatibility.
// 2. ProtoCodec: Provides full Protobuf serialization compatibility.
Marshaler interface {
BinaryMarshaler
JSONMarshaler
}
BinaryMarshaler interface {
MarshalBinaryBare(o ProtoMarshaler) ([]byte, error)
MustMarshalBinaryBare(o ProtoMarshaler) []byte
MarshalBinaryLengthPrefixed(o ProtoMarshaler) ([]byte, error)
MustMarshalBinaryLengthPrefixed(o ProtoMarshaler) []byte
UnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) error
MustUnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler)
UnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler) error
MustUnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler)
MarshalInterface(i proto.Message) ([]byte, error)
UnmarshalInterface(bz []byte, ptr interface{}) error
types.AnyUnpacker
}
JSONMarshaler interface {
MarshalJSON(o proto.Message) ([]byte, error)
MustMarshalJSON(o proto.Message) []byte
MarshalInterfaceJSON(i proto.Message) ([]byte, error)
UnmarshalInterfaceJSON(bz []byte, ptr interface{}) error
UnmarshalJSON(bz []byte, ptr proto.Message) error
MustUnmarshalJSON(bz []byte, ptr proto.Message)
}
// ProtoMarshaler defines an interface a type must implement as protocol buffer
// defined message.
ProtoMarshaler interface {
proto.Message // for JSON serialization
Marshal() ([]byte, error)
MarshalTo(data []byte) (n int, err error)
MarshalToSizedBuffer(dAtA []byte) (int, error)
Size() int
Unmarshal(data []byte) error
}
// AminoMarshaler defines an interface where Amino marshalling can be
// overridden by custom marshalling.
AminoMarshaler interface {
MarshalAmino() ([]byte, error)
UnmarshalAmino([]byte) error
MarshalAminoJSON() ([]byte, error)
UnmarshalAminoJSON([]byte) error
}
//
IbcCodec interface {
BinaryCodec
JSONCodec
}
BinaryCodec interface {
// Marshal returns binary encoding of v.
Marshal(o ProtoMarshaler) ([]byte, error)
// MustMarshal calls Marshal and panics if error is returned.
MustMarshal(o ProtoMarshaler) []byte
// MarshalLengthPrefixed returns binary encoding of v with bytes length prefix.
MarshalLengthPrefixed(o ProtoMarshaler) ([]byte, error)
// MustMarshalLengthPrefixed calls MarshalLengthPrefixed and panics if
// error is returned.
MustMarshalLengthPrefixed(o ProtoMarshaler) []byte
// Unmarshal parses the data encoded with Marshal method and stores the result
// in the value pointed to by v.
Unmarshal(bz []byte, ptr ProtoMarshaler) error
// MustUnmarshal calls Unmarshal and panics if error is returned.
MustUnmarshal(bz []byte, ptr ProtoMarshaler)
// Unmarshal parses the data encoded with UnmarshalLengthPrefixed method and stores
// the result in the value pointed to by v.
UnmarshalLengthPrefixed(bz []byte, ptr ProtoMarshaler) error
// MustUnmarshalLengthPrefixed calls UnmarshalLengthPrefixed and panics if error
// is returned.
MustUnmarshalLengthPrefixed(bz []byte, ptr ProtoMarshaler)
// MarshalInterface is a helper method which will wrap `i` into `Any` for correct
// binary interface (de)serialization.
MarshalInterface(i proto.Message) ([]byte, error)
// UnmarshalInterface is a helper method which will parse binary enoded data
// into `Any` and unpack any into the `ptr`. It fails if the target interface type
// is not registered in codec, or is not compatible with the serialized data
UnmarshalInterface(bz []byte, ptr interface{}) error
types.AnyUnpacker
}
JSONCodec interface {
// MarshalJSON returns JSON encoding of v.
MarshalJSON(o proto.Message) ([]byte, error)
// MustMarshalJSON calls MarshalJSON and panics if error is returned.
MustMarshalJSON(o proto.Message) []byte
// MarshalInterfaceJSON is a helper method which will wrap `i` into `Any` for correct
// JSON interface (de)serialization.
MarshalInterfaceJSON(i proto.Message) ([]byte, error)
// UnmarshalInterfaceJSON is a helper method which will parse JSON enoded data
// into `Any` and unpack any into the `ptr`. It fails if the target interface type
// is not registered in codec, or is not compatible with the serialized data
UnmarshalInterfaceJSON(bz []byte, ptr interface{}) error
// UnmarshalJSON parses the data encoded with MarshalJSON method and stores the result
// in the value pointed to by v.
UnmarshalJSON(bz []byte, ptr proto.Message) error
// MustUnmarshalJSON calls Unmarshal and panics if error is returned.
MustUnmarshalJSON(bz []byte, ptr proto.Message)
}
)
/////////
var (
_ CdcAbstraction = (*CodecProxy)(nil)
)
type CodecProxy struct {
protoCodec *ProtoCodec
cdc *Codec
}
func (mp *CodecProxy) UnmarshalBinaryLengthPrefixedWithRegisteredUbmarshaller(bz []byte, ptr interface{}) (interface{}, error) {
return mp.cdc.UnmarshalBinaryLengthPrefixedWithRegisteredUbmarshaller(bz, ptr)
}
func (mp *CodecProxy) UnmarshalBinaryLengthPrefixed(bz []byte, ptr interface{}) error {
return mp.cdc.UnmarshalBinaryLengthPrefixed(bz, ptr)
}
func NewCodecProxy(protoCodec *ProtoCodec, cdc *Codec) *CodecProxy {
return &CodecProxy{protoCodec: protoCodec, cdc: cdc}
}
func (mp *CodecProxy) GetCdc() *Codec {
return mp.cdc
}
func (mp *CodecProxy) GetProtocMarshal() *ProtoCodec {
return mp.protoCodec
}