Skip to content

Commit

Permalink
Robert/api clientctx (#8107)
Browse files Browse the repository at this point in the history
* rename clientCtx.PrintOutput to PrintObject

* rename clientCtx.PrintOutputLegacy to PrintObjectLegacy

* Changelog update

* Rename PrintObject to PrintProto
  • Loading branch information
robert-zaremba authored Dec 8, 2020
1 parent e843878 commit fe41718
Show file tree
Hide file tree
Showing 21 changed files with 91 additions and 90 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,18 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/genutil) [\#8099](https://github.com/cosmos/cosmos-sdk/pull/8099) `init` now supports a `--recover` flag to recover the private validator key from a given mnemonic

### State Machine Breaking Changes

* (x/upgrade) [\#7979](https://github.com/cosmos/cosmos-sdk/pull/7979) keeper pubkey storage serialization migration from bech32 to protobuf.

### Bug Fixes

* (crypto) [\#7966](https://github.com/cosmos/cosmos-sdk/issues/7966) `Bip44Params` `String()` function now correctly returns the absolute HD path by adding the `m/` prefix.


### API Breaking

* [\#8080](https://github.com/cosmos/cosmos-sdk/pull/8080) Updated the `codec.Marshaler` interface
* Moved `MarshalAny` and `UnmarshalAny` helper functions to `codec.Marshaler` and renamed to `MarshalInterface` and `UnmarshalInterface` respectively. These functions must take interface as a parameter (not a concrete type nor `Any` object). Underneath they use `Any` wrapping for correct protobuf serialization.

* (client) [\#8107](https://github.com/cosmos/cosmos-sdk/pull/8107) Renamed `PrintOutput` and `PrintOutputLegacy` methods of the `context.Client` object to `PrintProto` and `PrintObjectLegacy`.


## [v0.40.0-rc3](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.40.0-rc3) - 2020-11-06
Expand Down
11 changes: 6 additions & 5 deletions client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ func (ctx Context) PrintString(str string) error {
return err
}

// PrintOutput outputs toPrint to the ctx.Output based on ctx.OutputFormat which is
// PrintProto outputs toPrint to the ctx.Output based on ctx.OutputFormat which is
// either text or json. If text, toPrint will be YAML encoded. Otherwise, toPrint
// will be JSON encoded using ctx.JSONMarshaler. An error is returned upon failure.
func (ctx Context) PrintOutput(toPrint proto.Message) error {
func (ctx Context) PrintProto(toPrint proto.Message) error {
// always serialize JSON initially because proto json can't be directly YAML encoded
out, err := ctx.JSONMarshaler.MarshalJSON(toPrint)
if err != nil {
Expand All @@ -220,9 +220,10 @@ func (ctx Context) PrintOutput(toPrint proto.Message) error {
return ctx.printOutput(out)
}

// PrintOutputLegacy is a variant of PrintOutput that doesn't require a proto type
// and uses amino JSON encoding. It will be removed in the near future!
func (ctx Context) PrintOutputLegacy(toPrint interface{}) error {
// PrintObjectLegacy is a variant of PrintProto that doesn't require a proto.Message type
// and uses amino JSON encoding.
// Deprecated: It will be removed in the near future!
func (ctx Context) PrintObjectLegacy(toPrint interface{}) error {
out, err := ctx.LegacyAmino.MarshalJSON(toPrint)
if err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions client/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestMain(m *testing.M) {
os.Exit(m.Run())
}

func TestContext_PrintOutput(t *testing.T) {
func TestContext_PrintObject(t *testing.T) {
ctx := client.Context{}

animal := &testdata.Dog{
Expand All @@ -47,7 +47,7 @@ func TestContext_PrintOutput(t *testing.T) {
buf := &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = "json"
err = ctx.PrintOutput(hasAnimal)
err = ctx.PrintProto(hasAnimal)
require.NoError(t, err)
require.Equal(t,
`{"animal":{"@type":"/testdata.Dog","size":"big","name":"Spot"},"x":"10"}
Expand All @@ -57,7 +57,7 @@ func TestContext_PrintOutput(t *testing.T) {
buf = &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = "text"
err = ctx.PrintOutput(hasAnimal)
err = ctx.PrintProto(hasAnimal)
require.NoError(t, err)
require.Equal(t,
`animal:
Expand All @@ -77,7 +77,7 @@ x: "10"
buf = &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = "json"
err = ctx.PrintOutputLegacy(hasAnimal)
err = ctx.PrintObjectLegacy(hasAnimal)
require.NoError(t, err)
require.Equal(t,
`{"type":"testdata/HasAnimal","value":{"animal":{"type":"testdata/Dog","value":{"size":"big","name":"Spot"}},"x":"10"}}
Expand All @@ -87,7 +87,7 @@ x: "10"
buf = &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = "text"
err = ctx.PrintOutputLegacy(hasAnimal)
err = ctx.PrintObjectLegacy(hasAnimal)
require.NoError(t, err)
require.Equal(t,
`type: testdata/HasAnimal
Expand Down
2 changes: 1 addition & 1 deletion client/rpc/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func ValidatorCommand() *cobra.Command {
return err
}

return clientCtx.PrintOutputLegacy(result)
return clientCtx.PrintObjectLegacy(result)
},
}

Expand Down
2 changes: 1 addition & 1 deletion client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func BroadcastTx(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error {
return err
}

return clientCtx.PrintOutput(res)
return clientCtx.PrintProto(res)
}

// WriteGeneratedTxResponse writes a generated unsigned transaction to the
Expand Down
2 changes: 1 addition & 1 deletion docs/building-modules/module-interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ This query returns the account at a given address. The getter function does the
- The function should first initialize a new client [`Context`](../interfaces/query-lifecycle.md#context) as described in the [previous section](#transaction-commands)
- If applicable, the `Context` is used to retrieve any parameters (e.g. the query originator's address to be used in the query) and marshal them with the query parameter type, in preparation to be relayed to a node. There are no `Context` parameters in this case because the query does not involve any information about the user.
- A new `queryClient` should be initialized using `NewQueryClient(clientCtx)`, this method being generated from `query.proto`. Then it can be used to call the appropriate [query](./messages-and-queries.md#grpc-queries).
- The `clientCtx.PrintOutput` method is used to print the output back to the user.
- The `clientCtx.PrintProto` method is used to format a `proto.Message` object and print it back to the user.
- **Flags.** Add any [flags](#flags) to the command.

Finally, the module also needs a `GetQueryCmd`, which aggregates all of the query commands of the module. Application developers wishing to include the module's queries will call this function to add them as subcommands in their CLI. Its structure is identical to the `GetTxCmd` command shown above.
Expand Down
2 changes: 1 addition & 1 deletion x/auth/client/cli/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ $ <appd> tx broadcast ./mytxn.json
return err
}

return clientCtx.PrintOutput(res)
return clientCtx.PrintProto(res)
},
}

Expand Down
8 changes: 4 additions & 4 deletions x/auth/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ $ <appd> query auth params
return err
}

return clientCtx.PrintOutput(&res.Params)
return clientCtx.PrintProto(&res.Params)
},
}

Expand Down Expand Up @@ -98,7 +98,7 @@ func GetAccountCmd() *cobra.Command {
return err
}

return clientCtx.PrintOutput(res.Account)
return clientCtx.PrintProto(res.Account)
},
}

Expand Down Expand Up @@ -167,7 +167,7 @@ $ %s query txs --%s 'message.sender=cosmos1...&message.action=withdraw_delegator
return err
}

return clientCtx.PrintOutput(txs)
return clientCtx.PrintProto(txs)
},
}

Expand Down Expand Up @@ -203,7 +203,7 @@ func QueryTxCmd() *cobra.Command {
return fmt.Errorf("no transaction found with hash %s", args[0])
}

return clientCtx.PrintOutput(output)
return clientCtx.PrintProto(output)
},
}

Expand Down
8 changes: 4 additions & 4 deletions x/bank/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Example:
if err != nil {
return err
}
return clientCtx.PrintOutput(res)
return clientCtx.PrintProto(res)
}

params := types.NewQueryBalanceRequest(addr, denom)
Expand All @@ -93,7 +93,7 @@ Example:
return err
}

return clientCtx.PrintOutput(res.Balance)
return clientCtx.PrintProto(res.Balance)
},
}

Expand Down Expand Up @@ -140,15 +140,15 @@ To query for the total supply of a specific coin denomination use:
return err
}

return clientCtx.PrintOutput(res)
return clientCtx.PrintProto(res)
}

res, err := queryClient.SupplyOf(context.Background(), &types.QuerySupplyOfRequest{Denom: denom})
if err != nil {
return err
}

return clientCtx.PrintOutput(&res.Amount)
return clientCtx.PrintProto(&res.Amount)
},
}

Expand Down
14 changes: 7 additions & 7 deletions x/distribution/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func GetCmdQueryParams() *cobra.Command {
return err
}

return clientCtx.PrintOutput(&res.Params)
return clientCtx.PrintProto(&res.Params)
},
}

Expand Down Expand Up @@ -103,7 +103,7 @@ $ %s query distribution validator-outstanding-rewards %s1lwjmdnks33xwnmfayc64ycp
return err
}

return clientCtx.PrintOutput(&res.Rewards)
return clientCtx.PrintProto(&res.Rewards)
},
}

Expand Down Expand Up @@ -149,7 +149,7 @@ $ %s query distribution commission %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj
return err
}

return clientCtx.PrintOutput(&res.Commission)
return clientCtx.PrintProto(&res.Commission)
},
}

Expand Down Expand Up @@ -215,7 +215,7 @@ $ %s query distribution slashes %svaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj
return err
}

return clientCtx.PrintOutput(res)
return clientCtx.PrintProto(res)
},
}

Expand Down Expand Up @@ -271,7 +271,7 @@ $ %s query distribution rewards %s1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p %s1ggh
return err
}

return clientCtx.PrintOutput(res)
return clientCtx.PrintProto(res)
}

res, err := queryClient.DelegationTotalRewards(
Expand All @@ -282,7 +282,7 @@ $ %s query distribution rewards %s1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p %s1ggh
return err
}

return clientCtx.PrintOutput(res)
return clientCtx.PrintProto(res)
},
}

Expand Down Expand Up @@ -318,7 +318,7 @@ $ %s query distribution community-pool
return err
}

return clientCtx.PrintOutput(res)
return clientCtx.PrintProto(res)
},
}

Expand Down
6 changes: 3 additions & 3 deletions x/evidence/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func GetQueryCmd() *cobra.Command {
Short: "Query for evidence by hash or for all (paginated) submitted evidence",
Long: strings.TrimSpace(
fmt.Sprintf(`Query for specific submitted evidence by hash or query for all (paginated) evidence:
Example:
$ %s query %s DF0C23E8634E480F84B9D5674A7CDC9816466DEC28A3358F73260F68D28D7660
$ %s query %s --page=2 --limit=50
Expand Down Expand Up @@ -81,7 +81,7 @@ func queryEvidence(clientCtx client.Context, hash string) error {
return err
}

return clientCtx.PrintOutput(res.Evidence)
return clientCtx.PrintProto(res.Evidence)
}

func queryAllEvidence(clientCtx client.Context, pageReq *query.PageRequest) error {
Expand All @@ -96,5 +96,5 @@ func queryAllEvidence(clientCtx client.Context, pageReq *query.PageRequest) erro
return err
}

return clientCtx.PrintOutput(res)
return clientCtx.PrintProto(res)
}
24 changes: 12 additions & 12 deletions x/gov/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ $ %s query gov proposal 1
return err
}

return clientCtx.PrintOutput(&res.Proposal)
return clientCtx.PrintProto(&res.Proposal)
},
}

Expand Down Expand Up @@ -167,7 +167,7 @@ $ %s query gov proposals --page=2 --limit=100
return fmt.Errorf("no proposals found")
}

return clientCtx.PrintOutput(res)
return clientCtx.PrintProto(res)
},
}

Expand Down Expand Up @@ -246,7 +246,7 @@ $ %s query gov vote 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk
}
}

return clientCtx.PrintOutput(&res.Vote)
return clientCtx.PrintProto(&res.Vote)
},
}

Expand Down Expand Up @@ -309,7 +309,7 @@ $ %[1]s query gov votes 1 --page=2 --limit=100
// TODO migrate to use JSONMarshaler (implement MarshalJSONArray
// or wrap lists of proto.Message in some other message)
clientCtx.LegacyAmino.MustUnmarshalJSON(resByTxQuery, &votes)
return clientCtx.PrintOutputLegacy(votes)
return clientCtx.PrintObjectLegacy(votes)

}

Expand All @@ -327,7 +327,7 @@ $ %[1]s query gov votes 1 --page=2 --limit=100
return err
}

return clientCtx.PrintOutput(res)
return clientCtx.PrintProto(res)

},
}
Expand Down Expand Up @@ -400,7 +400,7 @@ $ %s query gov deposit 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk
clientCtx.JSONMarshaler.MustUnmarshalJSON(resByTxQuery, &deposit)
}

return clientCtx.PrintOutput(&deposit)
return clientCtx.PrintProto(&deposit)
},
}

Expand Down Expand Up @@ -461,7 +461,7 @@ $ %s query gov deposits 1
// or wrap lists of proto.Message in some other message)
clientCtx.LegacyAmino.MustUnmarshalJSON(resByTxQuery, &dep)

return clientCtx.PrintOutputLegacy(dep)
return clientCtx.PrintObjectLegacy(dep)
}

pageReq, err := client.ReadPageRequest(cmd.Flags())
Expand All @@ -478,7 +478,7 @@ $ %s query gov deposits 1
return err
}

return clientCtx.PrintOutput(res)
return clientCtx.PrintProto(res)
},
}

Expand Down Expand Up @@ -536,7 +536,7 @@ $ %s query gov tally 1
return err
}

return clientCtx.PrintOutput(&res.Tally)
return clientCtx.PrintProto(&res.Tally)
},
}

Expand Down Expand Up @@ -599,7 +599,7 @@ $ %s query gov params
depositRes.GetDepositParams(),
)

return clientCtx.PrintOutputLegacy(params)
return clientCtx.PrintObjectLegacy(params)
},
}

Expand Down Expand Up @@ -654,7 +654,7 @@ $ %s query gov param deposit
return fmt.Errorf("argument must be one of (voting|tallying|deposit), was %s", args[0])
}

return clientCtx.PrintOutputLegacy(out)
return clientCtx.PrintObjectLegacy(out)
},
}

Expand Down Expand Up @@ -696,7 +696,7 @@ $ %s query gov proposer 1
return err
}

return clientCtx.PrintOutputLegacy(prop)
return clientCtx.PrintObjectLegacy(prop)
},
}

Expand Down
Loading

0 comments on commit fe41718

Please sign in to comment.