Skip to content

Commit

Permalink
feat(client/v2): message binder (backport #17707) (#17829)
Browse files Browse the repository at this point in the history
Co-authored-by: Julien Robert <julien@rbrt.fr>
  • Loading branch information
mergify[bot] and julienrbrt authored Sep 21, 2023
1 parent deea20c commit 3d9ce99
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 22 deletions.
10 changes: 5 additions & 5 deletions client/v2/autocli/flag/messager_binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ func (f fieldBinding) bind(msg protoreflect.Message) error {
return err
}

kind := f.field.Kind()
if !(field.IsList() ||
field.IsMap() ||
kind == protoreflect.MessageKind ||
kind == protoreflect.GroupKind) {
if field.IsMap() {
return nil
}

if msg.IsValid() && val.IsValid() {
msg.Set(f.field, val)
}

Expand Down
95 changes: 78 additions & 17 deletions client/v2/autocli/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ import (
"os"
"strings"
"testing"
"time"

"github.com/spf13/cobra"
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"
"gotest.tools/v3/assert"
"gotest.tools/v3/golden"

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
queryv1beta1 "cosmossdk.io/api/cosmos/base/query/v1beta1"
basev1beta1 "cosmossdk.io/api/cosmos/base/v1beta1"
"cosmossdk.io/client/v2/internal/testpb"
)

Expand Down Expand Up @@ -293,23 +298,8 @@ func TestMap(t *testing.T) {
assert.ErrorContains(t, err, "invalid argument \"bar=123.9\" for \"--map-string-uint32\" flag: strconv.ParseUint: parsing \"123.9\": invalid syntax")
}

func TestMapError(t *testing.T) {
fixture := initFixture(t)

_, err := runCmd(fixture.conn, fixture.b, buildModuleQueryCommand,
"echo",
"1",
"abc",
"1234foo",
"4321bar",
"--map-string-uint32", "bar=123",
"--map-string-coin", "baz=100000foo",
"--map-string-coin", "sec=100000bar",
)
assert.NilError(t, err)
assert.DeepEqual(t, fixture.conn.lastRequest, fixture.conn.lastResponse.(*testpb.EchoResponse).Request, protocmp.Transform())
}

// TestEveything tests all the different types of flags are correctly read and as well as correctly returned
// This tests the flag binding and the message building
func TestEverything(t *testing.T) {
fixture := initFixture(t)

Expand Down Expand Up @@ -357,7 +347,69 @@ func TestEverything(t *testing.T) {
"--uints", "4",
)
assert.NilError(t, err)

expectedResp := &testpb.EchoResponse{
Request: &testpb.EchoRequest{
Positional1: 1,
Positional2: "abc",
Positional3Varargs: []*basev1beta1.Coin{
{Amount: "123.123123124", Denom: "foo"},
// {Amount: "4321", Denom: "bar"}, // TODO fix repeated fields
},
ABool: true,
AnEnum: testpb.Enum_ENUM_ONE,
AMessage: &testpb.AMessage{
Bar: "abc",
Baz: -3,
},
Duration: durationpb.New(4*time.Hour + 3*time.Second),
U32: 27,
U64: 3267246890,
I32: -253,
I64: -234602347,
Str: "def",
Timestamp: &timestamppb.Timestamp{
Seconds: 1546387262,
},
ACoin: &basev1beta1.Coin{
Amount: "100000",
Denom: "foo",
},
AnAddress: "cosmos1y74p8wyy4enfhfn342njve6cjmj5c8dtl6emdk",
AValidatorAddress: "cosmosvaloper1tnh2q55v8wyygtt9srz5safamzdengsn9dsd7z",
AConsensusAddress: "cosmosvalcons16vm0nx49eam4q0xasdnwdzsdl6ymgyjt757sgr",
Bz: []byte("sdgqwefwdgsdg"),
Page: &queryv1beta1.PageRequest{
CountTotal: true,
Key: []byte("1235487sghdas"),
Limit: 1000,
Offset: 10,
Reverse: true,
},
Bools: []bool{true, false, false, true},
Enums: []testpb.Enum{testpb.Enum_ENUM_ONE, testpb.Enum_ENUM_FIVE, testpb.Enum_ENUM_TWO},
Strings: []string{
"abc",
"xyz",
"xyz",
"qrs",
},
Durations: []*durationpb.Duration{
durationpb.New(3 * time.Second),
durationpb.New(5 * time.Second),
durationpb.New(10 * time.Hour),
},
SomeMessages: []*testpb.AMessage{
{},
{Bar: "baz"},
{Baz: -1},
},
Uints: []uint32{1, 2, 3, 4},
},
}

assert.DeepEqual(t, fixture.conn.lastRequest, fixture.conn.lastResponse.(*testpb.EchoResponse).Request, protocmp.Transform())
assert.DeepEqual(t, fixture.conn.lastResponse.(*testpb.EchoResponse), expectedResp, protocmp.Transform())
}

func TestPubKeyParsingConsensusAddress(t *testing.T) {
Expand Down Expand Up @@ -636,3 +688,12 @@ func TestNotFoundErrors(t *testing.T) {
})
assert.ErrorContains(t, err, "can't find field baz")
}

func TestDurationMarshal(t *testing.T) {
fixture := initFixture(t)

out, err := runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, "echo", "1", "abc", "--duration", "1s")
assert.NilError(t, err)
fmt.Println(out.String())
assert.Assert(t, strings.Contains(out.String(), "duration: 1s"))
}

0 comments on commit 3d9ce99

Please sign in to comment.