Skip to content

Commit

Permalink
Merge pull request #505 from CosmWasm/rename-vote
Browse files Browse the repository at this point in the history
Rename `VoteMsg::Vote` to `VoteMsg::Option`
  • Loading branch information
chipshort authored Jan 22, 2024
2 parents c264d2d + aa67bd1 commit 1136ed6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
3 changes: 2 additions & 1 deletion docs/MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ where the old name was deprecated.
| `CanonicalizeAddress` | `CanonicalizeAddressFunc` | Follow [best practice for naming function types][ft] |
| `GoAPI.HumanAddress` | `GoAPI.HumanizeAddress` | Perfer verbs for converters |
| `GoAPI.CanonicalAddress` | `GoAPI.CanonicalizeAddress` | Perfer verbs for converters |
| `CosmosMsg.Stargate` | `Any` | The message has nothing to do with Stargate |
| `CosmosMsg.Stargate` | `CosmosMsg.Any` | The message has nothing to do with Stargate |
| `StargateMsg` | `AnyMsg` | The message has nothing to do with Stargate |
| `QueryResponse` | `QueryResult` | Brings consistency with the naming of the other results |
| `VoteMsg.Vote` | `VoteMsg.Option` | Brings consistency with Cosmos SDK naming |

[ft]: https://stackoverflow.com/a/60073310
35 changes: 31 additions & 4 deletions types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,38 @@ type voteOption int

type VoteMsg struct {
ProposalId uint64 `json:"proposal_id"`
// Vote is the vote option.
// Option is the vote option.
//
// This should be called "option" for consistency with Cosmos SDK. Sorry for that.
// See <https://github.com/CosmWasm/cosmwasm/issues/1571>.
Vote voteOption `json:"vote"`
// This used to be called "vote", but was changed for consistency with Cosmos SDK.
// The old name is still supported for backwards compatibility.
Option voteOption `json:"option"`
}

func (m *VoteMsg) UnmarshalJSON(data []byte) error {
// We need a custom unmarshaler to parse both the "stargate" and "any" variants
type InternalVoteMsg struct {
ProposalId uint64 `json:"proposal_id"`
Option *voteOption `json:"option"`
Vote *voteOption `json:"vote"` // old version
}
var tmp InternalVoteMsg
err := json.Unmarshal(data, &tmp)
if err != nil {
return err
}

if tmp.Option != nil && tmp.Vote != nil {
return fmt.Errorf("invalid VoteMsg: both 'option' and 'vote' fields are set")
} else if tmp.Option == nil && tmp.Vote != nil {
// Use "Option" for both variants
tmp.Option = tmp.Vote
}

*m = VoteMsg{
ProposalId: tmp.ProposalId,
Option: *tmp.Option,
}
return nil
}

type VoteWeightedMsg struct {
Expand Down
13 changes: 10 additions & 3 deletions types/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,24 @@ func TestAnyMsgSerialization(t *testing.T) {
}

func TestGovMsgVoteSerialization(t *testing.T) {
document := []byte(`{"vote":{"proposal_id":4,"vote":"no_with_veto"}}`)
oldDocument := []byte(`{"vote":{"proposal_id":4,"vote":"no_with_veto"}}`)

var msg GovMsg
err := json.Unmarshal(document, &msg)
err := json.Unmarshal(oldDocument, &msg)
require.NoError(t, err)

require.Nil(t, msg.VoteWeighted)
require.NotNil(t, msg.Vote)

require.Equal(t, uint64(4), msg.Vote.ProposalId)
require.Equal(t, NoWithVeto, msg.Vote.Vote)
require.Equal(t, NoWithVeto, msg.Vote.Option)

newDocument := []byte(`{"vote":{"proposal_id":4,"option":"no_with_veto"}}`)

var msg2 GovMsg
err2 := json.Unmarshal(newDocument, &msg2)
require.NoError(t, err2)
require.Equal(t, msg, msg2)
}

func TestGovMsgVoteWeightedSerialization(t *testing.T) {
Expand Down

0 comments on commit 1136ed6

Please sign in to comment.