Skip to content

Commit

Permalink
feat(client/v2): stringify bytes cosmos.Dec (#16985)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Sep 13, 2023
1 parent 4eb0185 commit 2f4e109
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 24 deletions.
5 changes: 4 additions & 1 deletion client/v2/autocli/flag/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ type Builder struct {

// FileResolver specifies how protobuf file descriptors will be resolved. If it is
// nil protoregistry.GlobalFiles will be used.
FileResolver protodesc.Resolver
FileResolver interface {
protodesc.Resolver
RangeFiles(func(protoreflect.FileDescriptor) bool)
}

messageFlagTypes map[protoreflect.FullName]Type
scalarFlagTypes map[string]Type
Expand Down
47 changes: 38 additions & 9 deletions client/v2/autocli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package autocli

import (
"fmt"
"io"
"time"

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
"cosmossdk.io/x/tx/signing/aminojson"
"github.com/cockroachdb/errors"
"github.com/spf13/cobra"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/reflect/protoreflect"

"cosmossdk.io/client/v2/internal/util"
Expand Down Expand Up @@ -101,17 +103,16 @@ func (b *Builder) BuildQueryMethodCommand(descriptor protoreflect.MethodDescript
serviceDescriptor := descriptor.Parent().(protoreflect.ServiceDescriptor)
methodName := fmt.Sprintf("/%s/%s", serviceDescriptor.FullName(), descriptor.Name())
outputType := util.ResolveMessageType(b.TypeResolver, descriptor.Output())
jsonMarshalOptions := protojson.MarshalOptions{
encoderOptions := aminojson.EncoderOptions{
Indent: " ",
UseProtoNames: true,
UseEnumNumbers: false,
EmitUnpopulated: true,
Resolver: b.TypeResolver,
DoNotSortFields: true,
TypeResolver: b.TypeResolver,
FileResolver: b.FileResolver,
}

cmd, err := b.buildMethodCommandCommon(descriptor, options, func(cmd *cobra.Command, input protoreflect.Message) error {
if noIdent, _ := cmd.Flags().GetBool(flagNoIndent); noIdent {
jsonMarshalOptions.Indent = ""
if noIndent, _ := cmd.Flags().GetBool(flagNoIndent); noIndent {
encoderOptions.Indent = ""
}

clientConn, err := getClientConn(cmd)
Expand All @@ -124,7 +125,8 @@ func (b *Builder) BuildQueryMethodCommand(descriptor protoreflect.MethodDescript
return err
}

bz, err := jsonMarshalOptions.Marshal(output.Interface())
enc := encoder(aminojson.NewEncoder(encoderOptions))
bz, err := enc.Marshal(output.Interface())
if err != nil {
return fmt.Errorf("cannot marshal response %v: %w", output.Interface(), err)
}
Expand All @@ -143,3 +145,30 @@ func (b *Builder) BuildQueryMethodCommand(descriptor protoreflect.MethodDescript

return cmd, nil
}

func encoder(encoder aminojson.Encoder) aminojson.Encoder {
return encoder.DefineTypeEncoding("google.protobuf.Duration", func(_ *aminojson.Encoder, msg protoreflect.Message, w io.Writer) error {
var (
secondsName protoreflect.Name = "seconds"
nanosName protoreflect.Name = "nanos"
)

fields := msg.Descriptor().Fields()
secondsField := fields.ByName(secondsName)
if secondsField == nil {
return fmt.Errorf("expected seconds field")
}

seconds := msg.Get(secondsField).Int()

nanosField := fields.ByName(nanosName)
if nanosField == nil {
return fmt.Errorf("expected nanos field")
}

nanos := msg.Get(nanosField).Int()

_, err := fmt.Fprintf(w, `"%s"`, (time.Duration(seconds)*time.Second + (time.Duration(nanos) * time.Nanosecond)).String())
return err
})
}
2 changes: 1 addition & 1 deletion client/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
cosmossdk.io/core v0.11.0
cosmossdk.io/depinject v1.0.0-alpha.4
cosmossdk.io/log v1.2.1
cosmossdk.io/x/tx v0.9.2-0.20230913111958-e394604f8382
github.com/cockroachdb/errors v1.11.1
github.com/cosmos/cosmos-proto v1.0.0-beta.3
github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230718211500-1d74652f6021
Expand All @@ -24,7 +25,6 @@ require (
cosmossdk.io/errors v1.0.0 // indirect
cosmossdk.io/math v1.1.2 // indirect
cosmossdk.io/store v1.0.0-rc.0 // indirect
cosmossdk.io/x/tx v0.9.1 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions client/v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ cosmossdk.io/math v1.1.2 h1:ORZetZCTyWkI5GlZ6CZS28fMHi83ZYf+A2vVnHNzZBM=
cosmossdk.io/math v1.1.2/go.mod h1:l2Gnda87F0su8a/7FEKJfFdJrM0JZRXQaohlgJeyQh0=
cosmossdk.io/store v1.0.0-rc.0 h1:9DwOjuUYxDtYxn/REkTxGQAmxlIGfRroB35MQ8TrxF4=
cosmossdk.io/store v1.0.0-rc.0/go.mod h1:FtBDOJmwtOZfmKKF65bKZbTYgS3bDNjjo3nP76dAegk=
cosmossdk.io/x/tx v0.9.1 h1:9pmmXA9Vs4qdouOFnzhsdsff2mif0f0kylMq5xTGhRI=
cosmossdk.io/x/tx v0.9.1/go.mod h1:/YFGTXG6+kyihd8YbfuJiXHV4R/mIMm2uvVzo80CIhA=
cosmossdk.io/x/tx v0.9.2-0.20230913111958-e394604f8382 h1:p/wyc5pVTMtx2/RsKP0fUpOKGbDfy6q6WswJtmARdbg=
cosmossdk.io/x/tx v0.9.2-0.20230913111958-e394604f8382/go.mod h1:MKo9/b5wsoL8dd9y9pvD2yOP1CMvzHIWYxi1l2oLPFo=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek=
filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
Expand Down
2 changes: 1 addition & 1 deletion simapp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
cosmossdk.io/x/evidence v0.0.0-20230613133644-0a778132a60f
cosmossdk.io/x/feegrant v0.0.0-20230613133644-0a778132a60f
cosmossdk.io/x/nft v0.0.0-20230613133644-0a778132a60f
cosmossdk.io/x/tx v0.9.1
cosmossdk.io/x/tx v0.9.2-0.20230913111958-e394604f8382
cosmossdk.io/x/upgrade v0.0.0-20230613133644-0a778132a60f
github.com/cometbft/cometbft v0.38.0
github.com/cosmos/cosmos-db v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions simapp/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ cosmossdk.io/math v1.1.2 h1:ORZetZCTyWkI5GlZ6CZS28fMHi83ZYf+A2vVnHNzZBM=
cosmossdk.io/math v1.1.2/go.mod h1:l2Gnda87F0su8a/7FEKJfFdJrM0JZRXQaohlgJeyQh0=
cosmossdk.io/store v1.0.0-rc.0 h1:9DwOjuUYxDtYxn/REkTxGQAmxlIGfRroB35MQ8TrxF4=
cosmossdk.io/store v1.0.0-rc.0/go.mod h1:FtBDOJmwtOZfmKKF65bKZbTYgS3bDNjjo3nP76dAegk=
cosmossdk.io/x/tx v0.9.1 h1:9pmmXA9Vs4qdouOFnzhsdsff2mif0f0kylMq5xTGhRI=
cosmossdk.io/x/tx v0.9.1/go.mod h1:/YFGTXG6+kyihd8YbfuJiXHV4R/mIMm2uvVzo80CIhA=
cosmossdk.io/x/tx v0.9.2-0.20230913111958-e394604f8382 h1:p/wyc5pVTMtx2/RsKP0fUpOKGbDfy6q6WswJtmARdbg=
cosmossdk.io/x/tx v0.9.2-0.20230913111958-e394604f8382/go.mod h1:MKo9/b5wsoL8dd9y9pvD2yOP1CMvzHIWYxi1l2oLPFo=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek=
filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
Expand Down
4 changes: 2 additions & 2 deletions simapp/gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ schema = 3
version = "v1.0.0-rc.0"
hash = "sha256-DgW4ZrDwmgsPtEXajPyAsrQuPjXekw5PfsYFvA5yuiE="
[mod."cosmossdk.io/x/tx"]
version = "v0.9.1"
hash = "sha256-/fopDRDbfIUr8Ub0qqu3k0y6FsLKTAwf51PJaCL18Bk="
version = "v0.9.2-0.20230913111958-e394604f8382"
hash = "sha256-am/+/mfQR+xqjQSTFh2X/AiC6EQZvZ7te6d8MIwd/EA="
[mod."filippo.io/edwards25519"]
version = "v1.0.0"
hash = "sha256-APnPAcmItvtJ5Zsy863lzR2TjEBF9Y66TY1e4M1ap98="
Expand Down
2 changes: 1 addition & 1 deletion tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
cosmossdk.io/x/evidence v0.0.0-20230613133644-0a778132a60f
cosmossdk.io/x/feegrant v0.0.0-20230613133644-0a778132a60f
cosmossdk.io/x/nft v0.0.0-20230613133644-0a778132a60f // indirect
cosmossdk.io/x/tx v0.9.1
cosmossdk.io/x/tx v0.9.2-0.20230913111958-e394604f8382
cosmossdk.io/x/upgrade v0.0.0-20230613133644-0a778132a60f
github.com/cometbft/cometbft v0.38.0
github.com/cosmos/cosmos-db v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ cosmossdk.io/math v1.1.2 h1:ORZetZCTyWkI5GlZ6CZS28fMHi83ZYf+A2vVnHNzZBM=
cosmossdk.io/math v1.1.2/go.mod h1:l2Gnda87F0su8a/7FEKJfFdJrM0JZRXQaohlgJeyQh0=
cosmossdk.io/store v1.0.0-rc.0 h1:9DwOjuUYxDtYxn/REkTxGQAmxlIGfRroB35MQ8TrxF4=
cosmossdk.io/store v1.0.0-rc.0/go.mod h1:FtBDOJmwtOZfmKKF65bKZbTYgS3bDNjjo3nP76dAegk=
cosmossdk.io/x/tx v0.9.1 h1:9pmmXA9Vs4qdouOFnzhsdsff2mif0f0kylMq5xTGhRI=
cosmossdk.io/x/tx v0.9.1/go.mod h1:/YFGTXG6+kyihd8YbfuJiXHV4R/mIMm2uvVzo80CIhA=
cosmossdk.io/x/tx v0.9.2-0.20230913111958-e394604f8382 h1:p/wyc5pVTMtx2/RsKP0fUpOKGbDfy6q6WswJtmARdbg=
cosmossdk.io/x/tx v0.9.2-0.20230913111958-e394604f8382/go.mod h1:MKo9/b5wsoL8dd9y9pvD2yOP1CMvzHIWYxi1l2oLPFo=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek=
filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
Expand Down
2 changes: 1 addition & 1 deletion tests/starship/tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ require (
cosmossdk.io/x/evidence v0.0.0-20230613133644-0a778132a60f // indirect
cosmossdk.io/x/feegrant v0.0.0-20230613133644-0a778132a60f // indirect
cosmossdk.io/x/nft v0.0.0-20230613133644-0a778132a60f // indirect
cosmossdk.io/x/tx v0.9.1 // indirect
cosmossdk.io/x/tx v0.9.2-0.20230913111958-e394604f8382 // indirect
cosmossdk.io/x/upgrade v0.0.0-20230613133644-0a778132a60f // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
Expand Down
4 changes: 2 additions & 2 deletions tests/starship/tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ cosmossdk.io/math v1.1.2 h1:ORZetZCTyWkI5GlZ6CZS28fMHi83ZYf+A2vVnHNzZBM=
cosmossdk.io/math v1.1.2/go.mod h1:l2Gnda87F0su8a/7FEKJfFdJrM0JZRXQaohlgJeyQh0=
cosmossdk.io/store v1.0.0-rc.0 h1:9DwOjuUYxDtYxn/REkTxGQAmxlIGfRroB35MQ8TrxF4=
cosmossdk.io/store v1.0.0-rc.0/go.mod h1:FtBDOJmwtOZfmKKF65bKZbTYgS3bDNjjo3nP76dAegk=
cosmossdk.io/x/tx v0.9.1 h1:9pmmXA9Vs4qdouOFnzhsdsff2mif0f0kylMq5xTGhRI=
cosmossdk.io/x/tx v0.9.1/go.mod h1:/YFGTXG6+kyihd8YbfuJiXHV4R/mIMm2uvVzo80CIhA=
cosmossdk.io/x/tx v0.9.2-0.20230913111958-e394604f8382 h1:p/wyc5pVTMtx2/RsKP0fUpOKGbDfy6q6WswJtmARdbg=
cosmossdk.io/x/tx v0.9.2-0.20230913111958-e394604f8382/go.mod h1:MKo9/b5wsoL8dd9y9pvD2yOP1CMvzHIWYxi1l2oLPFo=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek=
filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
Expand Down

0 comments on commit 2f4e109

Please sign in to comment.