forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amino.go
195 lines (162 loc) · 4.8 KB
/
amino.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package codec
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
amino "github.com/tendermint/go-amino"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/cosmos/cosmos-sdk/codec/types"
)
// deprecated: LegacyAmino defines a wrapper for an Amino codec that properly handles protobuf
// types with Any's
type LegacyAmino struct {
Amino *amino.Codec
}
func (cdc *LegacyAmino) Seal() {
cdc.Amino.Seal()
}
func NewLegacyAmino() *LegacyAmino {
return &LegacyAmino{amino.NewCodec()}
}
// RegisterEvidences registers Tendermint evidence types with the provided Amino
// codec.
func RegisterEvidences(cdc *LegacyAmino) {
cdc.Amino.RegisterInterface((*tmtypes.Evidence)(nil), nil)
cdc.Amino.RegisterConcrete(&tmtypes.DuplicateVoteEvidence{}, "tendermint/DuplicateVoteEvidence", nil)
}
// MarshalJSONIndent provides a utility for indented JSON encoding of an object
// via an Amino codec. It returns an error if it cannot serialize or indent as
// JSON.
func MarshalJSONIndent(cdc *LegacyAmino, obj interface{}) ([]byte, error) {
bz, err := cdc.MarshalJSON(obj)
if err != nil {
return nil, err
}
var out bytes.Buffer
if err = json.Indent(&out, bz, "", " "); err != nil {
return nil, err
}
return out.Bytes(), nil
}
// MustMarshalJSONIndent executes MarshalJSONIndent except it panics upon failure.
func MustMarshalJSONIndent(cdc *LegacyAmino, obj interface{}) []byte {
bz, err := MarshalJSONIndent(cdc, obj)
if err != nil {
panic(fmt.Sprintf("failed to marshal JSON: %s", err))
}
return bz
}
func (cdc *LegacyAmino) marshalAnys(o interface{}) error {
return types.UnpackInterfaces(o, types.AminoPacker{Cdc: cdc.Amino})
}
func (cdc *LegacyAmino) unmarshalAnys(o interface{}) error {
return types.UnpackInterfaces(o, types.AminoUnpacker{Cdc: cdc.Amino})
}
func (cdc *LegacyAmino) jsonMarshalAnys(o interface{}) error {
return types.UnpackInterfaces(o, types.AminoJSONPacker{Cdc: cdc.Amino})
}
func (cdc *LegacyAmino) jsonUnmarshalAnys(o interface{}) error {
return types.UnpackInterfaces(o, types.AminoJSONUnpacker{Cdc: cdc.Amino})
}
func (cdc *LegacyAmino) MarshalBinaryBare(o interface{}) ([]byte, error) {
err := cdc.marshalAnys(o)
if err != nil {
return nil, err
}
return cdc.Amino.MarshalBinaryBare(o)
}
func (cdc *LegacyAmino) MustMarshalBinaryBare(o interface{}) []byte {
bz, err := cdc.MarshalBinaryBare(o)
if err != nil {
panic(err)
}
return bz
}
func (cdc *LegacyAmino) MarshalBinaryLengthPrefixed(o interface{}) ([]byte, error) {
err := cdc.marshalAnys(o)
if err != nil {
return nil, err
}
return cdc.Amino.MarshalBinaryLengthPrefixed(o)
}
func (cdc *LegacyAmino) MustMarshalBinaryLengthPrefixed(o interface{}) []byte {
bz, err := cdc.MarshalBinaryLengthPrefixed(o)
if err != nil {
panic(err)
}
return bz
}
func (cdc *LegacyAmino) UnmarshalBinaryBare(bz []byte, ptr interface{}) error {
err := cdc.Amino.UnmarshalBinaryBare(bz, ptr)
if err != nil {
return err
}
return cdc.unmarshalAnys(ptr)
}
func (cdc *LegacyAmino) MustUnmarshalBinaryBare(bz []byte, ptr interface{}) {
err := cdc.UnmarshalBinaryBare(bz, ptr)
if err != nil {
panic(err)
}
}
func (cdc *LegacyAmino) UnmarshalBinaryLengthPrefixed(bz []byte, ptr interface{}) error {
err := cdc.Amino.UnmarshalBinaryLengthPrefixed(bz, ptr)
if err != nil {
return err
}
return cdc.unmarshalAnys(ptr)
}
func (cdc *LegacyAmino) MustUnmarshalBinaryLengthPrefixed(bz []byte, ptr interface{}) {
err := cdc.UnmarshalBinaryLengthPrefixed(bz, ptr)
if err != nil {
panic(err)
}
}
func (cdc *LegacyAmino) MarshalJSON(o interface{}) ([]byte, error) {
err := cdc.jsonMarshalAnys(o)
if err != nil {
return nil, err
}
return cdc.Amino.MarshalJSON(o)
}
func (cdc *LegacyAmino) MustMarshalJSON(o interface{}) []byte {
bz, err := cdc.MarshalJSON(o)
if err != nil {
panic(err)
}
return bz
}
func (cdc *LegacyAmino) UnmarshalJSON(bz []byte, ptr interface{}) error {
err := cdc.Amino.UnmarshalJSON(bz, ptr)
if err != nil {
return err
}
return cdc.jsonUnmarshalAnys(ptr)
}
func (cdc *LegacyAmino) MustUnmarshalJSON(bz []byte, ptr interface{}) {
err := cdc.UnmarshalJSON(bz, ptr)
if err != nil {
panic(err)
}
}
func (*LegacyAmino) UnpackAny(*types.Any, interface{}) error {
return errors.New("AminoCodec can't handle unpack protobuf Any's")
}
func (cdc *LegacyAmino) RegisterInterface(ptr interface{}, iopts *amino.InterfaceOptions) {
cdc.Amino.RegisterInterface(ptr, iopts)
}
func (cdc *LegacyAmino) RegisterConcrete(o interface{}, name string, copts *amino.ConcreteOptions) {
cdc.Amino.RegisterConcrete(o, name, copts)
}
func (cdc *LegacyAmino) MarshalJSONIndent(o interface{}, prefix, indent string) ([]byte, error) {
err := cdc.jsonMarshalAnys(o)
if err != nil {
panic(err)
}
return cdc.Amino.MarshalJSONIndent(o, prefix, indent)
}
func (cdc *LegacyAmino) PrintTypes(out io.Writer) error {
return cdc.Amino.PrintTypes(out)
}