Skip to content

Commit

Permalink
feat(textual): Add Enum value renderer (cosmos#13853)
Browse files Browse the repository at this point in the history
* feat(textual): Add Enum value renderer

* parse

* Update ADR

* Fix test

* Update tx/textual/internal/testpb/1.proto

Co-authored-by: Likhita Polavarapu <78951027+likhita-809@users.noreply.github.com>

Co-authored-by: Likhita Polavarapu <78951027+likhita-809@users.noreply.github.com>
  • Loading branch information
amaury1093 and likhita-809 committed Nov 23, 2022
1 parent b585d17 commit fe5b0ff
Show file tree
Hide file tree
Showing 11 changed files with 1,013 additions and 102 deletions.
12 changes: 3 additions & 9 deletions docs/architecture/adr-050-sign-mode-textual-annex1.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,17 @@ Vote object
> Vote: cosmos1abc...def
> Options: 2 WeightedVoteOptions
> Options (1/2): WeightedVoteOption object
>> Option: Yes
>> Option: VOTE_OPTION_YES
>> Weight: 0.7
> Options (2/2): WeightedVoteOption object
>> Option: No
>> Option: VOTE_OPTION_NO
>> Weight: 0.3
> End of Options
```

### Enums

- String case convention: snake case to sentence case
- Allow optional annotation for textual name (TBD)
- Algorithm:
- convert enum name (`VoteOption`) to snake_case (`VOTE_OPTION`)
- truncate that prefix + `_` from the enum name if it exists (`VOTE_OPTION_` gets stripped from `VOTE_OPTION_YES` -> `YES`)
- convert rest to sentence case: `YES` -> `Yes`
- in summary: `VOTE_OPTION_YES` -> `Yes`
- Show the enum variant name as string.

#### Examples

Expand Down
4 changes: 2 additions & 2 deletions docs/architecture/adr-050-sign-mode-textual.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ Tip: 200 ibc/CDC4587874B85BEA4FCEC3CEA5A1195139799A1FEE711A07D972537E18FDA39D
*This transaction has 1 other signer:
*Signer (1/2):
*Public Key: iQ...==
*Sign mode: Direct Aux
*Sign mode: SIGN_MODE_DIRECT_AUX
*Sequence: 42
*End of other signers
*Hash of raw bytes: <hex_string>
Expand Down Expand Up @@ -549,7 +549,7 @@ Fee: 0.002 atom
*This transaction has 1 other signer:
*Signer (2/2):
*Public Key: iR...==
*Sign mode: Direct
*Sign mode: SIGN_MODE_DIRECT
*Sequence: 42
*End of other signers
*Hash of raw bytes: <hex_string>
Expand Down
1 change: 1 addition & 0 deletions tx/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
cosmossdk.io/core v0.3.2
cosmossdk.io/math v1.0.0-beta.3
github.com/cosmos/cosmos-proto v1.0.0-alpha8
github.com/google/go-cmp v0.5.9
github.com/stretchr/testify v1.8.1
google.golang.org/protobuf v1.28.1
)
Expand Down
1 change: 1 addition & 0 deletions tx/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
10 changes: 10 additions & 0 deletions tx/textual/internal/testdata/enum.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{ "proto": { "ee": 0 }, "text": "One" },
{ "proto": { "ee": 1 }, "text": "Two" },
{ "proto": { "ee": 127 }, "text": "EXTERNAL_ENUM_THREE" },
{ "proto": { "ie": 0 }, "text": "Baz.Four" },
{ "proto": { "ie": 1 }, "text": "Baz.Five" },
{ "proto": { "option": 0 }, "text": "BALLOT_OPTION_UNSPECIFIED" },
{ "proto": { "option": 1 }, "text": "BALLOT_OPTION_YES" },
{ "proto": { "option": 4 }, "text": "BALLOT_OPTION_NO_WITH_VETO" }
]
33 changes: 28 additions & 5 deletions tx/textual/internal/testpb/1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ import "google/protobuf/timestamp.proto";
import "cosmos_proto/cosmos.proto";
import "cosmos/base/v1beta1/coin.proto";

enum Enumeration {
One = 0;
Two = 1;
}

// A is used for testing value renderers.
message A {
// Fields that are parseable by SIGN_MODE_TEXTUAL.
Expand All @@ -27,6 +22,7 @@ message A {
bytes BYTES = 9;
google.protobuf.Timestamp TIMESTAMP = 10;
google.protobuf.Duration DURATION = 11;
ExternalEnum ENUM = 12;

// Fields that are not handled by SIGN_MODE_TEXTUAL.
sint32 SINT32 = 101;
Expand Down Expand Up @@ -55,3 +51,30 @@ message Bar {
string bar_id = 1;
bytes data = 2;
}

enum ExternalEnum {
One = 0;
Two = 1;
EXTERNAL_ENUM_THREE = 127;
}

// Baz is a sample message type used for testing enum rendering.
message Baz {
enum Internal_Enum {
Four = 0;
Five = 1;
}

ExternalEnum ee = 1;
Internal_Enum ie = 2;
BallotOption option = 3;

}

enum BallotOption {
BALLOT_OPTION_UNSPECIFIED = 0;
BALLOT_OPTION_YES = 1;
BALLOT_OPTION_ABSTAIN = 2;
BALLOT_OPTION_NO = 3;
BALLOT_OPTION_NO_WITH_VETO = 4;
}
Loading

0 comments on commit fe5b0ff

Please sign in to comment.