Skip to content

Commit

Permalink
feat(x/tx): add custom type encoder (#17681)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Sep 13, 2023
1 parent 1a62d77 commit e394604
Show file tree
Hide file tree
Showing 8 changed files with 711 additions and 78 deletions.
1 change: 1 addition & 0 deletions x/tx/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Features

* [#17681](https://github.com/cosmos/cosmos-sdk/pull/17681) Add encoder `DefineTypeEncoding` method for defining custom type encodings.
* [#17600](https://github.com/cosmos/cosmos-sdk/pull/17600) Add encoder `DefineScalarEncoding` method for defining custom scalar encodings.
* [#17600](https://github.com/cosmos/cosmos-sdk/pull/17600) Add indent option to encoder.

Expand Down
6 changes: 3 additions & 3 deletions x/tx/signing/aminojson/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
"google.golang.org/protobuf/types/known/anypb"
)

func (enc Encoder) marshalAny(message protoreflect.Message, writer io.Writer) error {
func marshalAny(enc *Encoder, message protoreflect.Message, writer io.Writer) error {
// when a message contains a nested any field, and the top-level message has been unmarshalled into a dyanmicpb,
// the nested any field will also be a dynamicpb. In this case, we must use the dynamicpb API.
_, ok := message.Interface().(*dynamicpb.Message)
if ok {
return enc.marshalDynamic(message, writer)
return marshalDynamic(enc, message, writer)
}

anyMsg, ok := message.Interface().(*anypb.Any)
Expand Down Expand Up @@ -64,7 +64,7 @@ const (
anyValueFieldName = "value"
)

func (enc Encoder) marshalDynamic(message protoreflect.Message, writer io.Writer) error {
func marshalDynamic(enc *Encoder, message protoreflect.Message, writer io.Writer) error {
msgName := message.Get(message.Descriptor().Fields().ByName(anyTypeURLFieldName)).String()[1:]
msgBytes := message.Get(message.Descriptor().Fields().ByName(anyValueFieldName)).Bytes()

Expand Down
5 changes: 5 additions & 0 deletions x/tx/signing/aminojson/internal/testpb/test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ message ABitOfEverything {
// google.protobuf.Duration duration = 24;
}

message Duration {
google.protobuf.Duration duration = 1;
google.protobuf.Timestamp timestamp = 2;
}

message NestedMessage {
option (amino.name) = "NestedMessage";

Expand Down
Loading

0 comments on commit e394604

Please sign in to comment.