github.com/Finschia/finschia-sdk@v0.48.1/codec/amino_codec.go (about) 1 package codec 2 3 import ( 4 "github.com/gogo/protobuf/proto" 5 ) 6 7 // AminoCodec defines a codec that utilizes Codec for both binary and JSON 8 // encoding. 9 type AminoCodec struct { 10 *LegacyAmino 11 } 12 13 var _ Codec = &AminoCodec{} 14 15 // NewAminoCodec returns a reference to a new AminoCodec 16 func NewAminoCodec(codec *LegacyAmino) *AminoCodec { 17 return &AminoCodec{LegacyAmino: codec} 18 } 19 20 // Marshal implements BinaryMarshaler.Marshal method. 21 func (ac *AminoCodec) Marshal(o ProtoMarshaler) ([]byte, error) { 22 return ac.LegacyAmino.Marshal(o) 23 } 24 25 // MustMarshal implements BinaryMarshaler.MustMarshal method. 26 func (ac *AminoCodec) MustMarshal(o ProtoMarshaler) []byte { 27 return ac.LegacyAmino.MustMarshal(o) 28 } 29 30 // MarshalLengthPrefixed implements BinaryMarshaler.MarshalLengthPrefixed method. 31 func (ac *AminoCodec) MarshalLengthPrefixed(o ProtoMarshaler) ([]byte, error) { 32 return ac.LegacyAmino.MarshalLengthPrefixed(o) 33 } 34 35 // MustMarshalLengthPrefixed implements BinaryMarshaler.MustMarshalLengthPrefixed method. 36 func (ac *AminoCodec) MustMarshalLengthPrefixed(o ProtoMarshaler) []byte { 37 return ac.LegacyAmino.MustMarshalLengthPrefixed(o) 38 } 39 40 // Unmarshal implements BinaryMarshaler.Unmarshal method. 41 func (ac *AminoCodec) Unmarshal(bz []byte, ptr ProtoMarshaler) error { 42 return ac.LegacyAmino.Unmarshal(bz, ptr) 43 } 44 45 // MustUnmarshal implements BinaryMarshaler.MustUnmarshal method. 46 func (ac *AminoCodec) MustUnmarshal(bz []byte, ptr ProtoMarshaler) { 47 ac.LegacyAmino.MustUnmarshal(bz, ptr) 48 } 49 50 // UnmarshalLengthPrefixed implements BinaryMarshaler.UnmarshalLengthPrefixed method. 51 func (ac *AminoCodec) UnmarshalLengthPrefixed(bz []byte, ptr ProtoMarshaler) error { 52 return ac.LegacyAmino.UnmarshalLengthPrefixed(bz, ptr) 53 } 54 55 // MustUnmarshalLengthPrefixed implements BinaryMarshaler.MustUnmarshalLengthPrefixed method. 56 func (ac *AminoCodec) MustUnmarshalLengthPrefixed(bz []byte, ptr ProtoMarshaler) { 57 ac.LegacyAmino.MustUnmarshalLengthPrefixed(bz, ptr) 58 } 59 60 // MarshalJSON implements JSONCodec.MarshalJSON method, 61 // it marshals to JSON using legacy amino codec. 62 func (ac *AminoCodec) MarshalJSON(o proto.Message) ([]byte, error) { 63 return ac.LegacyAmino.MarshalJSON(o) 64 } 65 66 // MustMarshalJSON implements JSONCodec.MustMarshalJSON method, 67 // it executes MarshalJSON except it panics upon failure. 68 func (ac *AminoCodec) MustMarshalJSON(o proto.Message) []byte { 69 return ac.LegacyAmino.MustMarshalJSON(o) 70 } 71 72 // UnmarshalJSON implements JSONCodec.UnmarshalJSON method, 73 // it unmarshals from JSON using legacy amino codec. 74 func (ac *AminoCodec) UnmarshalJSON(bz []byte, ptr proto.Message) error { 75 return ac.LegacyAmino.UnmarshalJSON(bz, ptr) 76 } 77 78 // MustUnmarshalJSON implements JSONCodec.MustUnmarshalJSON method, 79 // it executes UnmarshalJSON except it panics upon failure. 80 func (ac *AminoCodec) MustUnmarshalJSON(bz []byte, ptr proto.Message) { 81 ac.LegacyAmino.MustUnmarshalJSON(bz, ptr) 82 } 83 84 // MarshalInterface is a convenience function for amino marshaling interfaces. 85 // The `i` must be an interface. 86 // NOTE: to marshal a concrete type, you should use Marshal instead 87 func (ac *AminoCodec) MarshalInterface(i proto.Message) ([]byte, error) { 88 if err := assertNotNil(i); err != nil { 89 return nil, err 90 } 91 return ac.LegacyAmino.Marshal(i) 92 } 93 94 // UnmarshalInterface is a convenience function for amino unmarshaling interfaces. 95 // `ptr` must be a pointer to an interface. 96 // NOTE: to unmarshal a concrete type, you should use Unmarshal instead 97 // 98 // Example: 99 // 100 // var x MyInterface 101 // err := cdc.UnmarshalInterface(bz, &x) 102 func (ac *AminoCodec) UnmarshalInterface(bz []byte, ptr interface{}) error { 103 return ac.LegacyAmino.Unmarshal(bz, ptr) 104 } 105 106 // MarshalInterfaceJSON is a convenience function for amino marshaling interfaces. 107 // The `i` must be an interface. 108 // NOTE: to marshal a concrete type, you should use MarshalJSON instead 109 func (ac *AminoCodec) MarshalInterfaceJSON(i proto.Message) ([]byte, error) { 110 if err := assertNotNil(i); err != nil { 111 return nil, err 112 } 113 return ac.LegacyAmino.MarshalJSON(i) 114 } 115 116 // UnmarshalInterfaceJSON is a convenience function for amino unmarshaling interfaces. 117 // `ptr` must be a pointer to an interface. 118 // NOTE: to unmarshal a concrete type, you should use UnmarshalJSON instead 119 // 120 // Example: 121 // 122 // var x MyInterface 123 // err := cdc.UnmarshalInterfaceJSON(bz, &x) 124 func (ac *AminoCodec) UnmarshalInterfaceJSON(bz []byte, ptr interface{}) error { 125 return ac.LegacyAmino.UnmarshalJSON(bz, ptr) 126 }