feat(textual): Value Renderers for numbers (#12088)
## Description closes #12613 This PR sets up the infrastructure for value renderers as discussed in ADR-050. It's only additive, in a new package, and isn't wired up to the rest of the codebase. It adds a new root go module `tx`, which has one `textual` subpackage for now. Only adds implementation for: - integers - decimals Sister PR: - [ ] https://github.com/cosmos/cosmjs/pull/1147 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
This commit is contained in:
parent
ef8049ae6b
commit
ab3febab0c
2
go.mod
2
go.mod
@ -7,7 +7,7 @@ require (
|
||||
cosmossdk.io/core v0.0.0
|
||||
cosmossdk.io/depinject v1.0.0-alpha.4
|
||||
cosmossdk.io/errors v1.0.0-beta.7
|
||||
cosmossdk.io/math v1.0.0-beta.2
|
||||
cosmossdk.io/math v1.0.0-beta.3
|
||||
github.com/99designs/keyring v1.2.1
|
||||
github.com/armon/go-metrics v0.4.0
|
||||
github.com/bgentry/speakeasy v0.1.0
|
||||
|
||||
18
tx/go.mod
Normal file
18
tx/go.mod
Normal file
@ -0,0 +1,18 @@
|
||||
module cosmossdk.io/tx
|
||||
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
cosmossdk.io/api v0.1.0-alpha8
|
||||
cosmossdk.io/math v1.0.0-beta.3
|
||||
github.com/cosmos/cosmos-proto v1.0.0-alpha7
|
||||
github.com/stretchr/testify v1.8.0
|
||||
google.golang.org/protobuf v1.28.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/gogo/protobuf v1.3.2 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
65
tx/go.sum
Normal file
65
tx/go.sum
Normal file
@ -0,0 +1,65 @@
|
||||
cosmossdk.io/api v0.1.0-alpha8 h1:Hr+8bLI4UphF+aMiDIVklrdzRm99dFaNq2inBKGDzNU=
|
||||
cosmossdk.io/api v0.1.0-alpha8/go.mod h1:gIs3NW5OSNK5wKqxF8JHnGTL82QMsXpwGeKmu2i5xFA=
|
||||
cosmossdk.io/math v1.0.0-beta.3 h1:TbZxSopz2LqjJ7aXYfn7nJSb8vNaBklW6BLpcei1qwM=
|
||||
cosmossdk.io/math v1.0.0-beta.3/go.mod h1:3LYasri3Zna4XpbrTNdKsWmD5fHHkaNAod/mNT9XdE4=
|
||||
github.com/cosmos/cosmos-proto v1.0.0-alpha7 h1:yqYUOHF2jopwZh4dVQp3xgqwftE5/2hkrwIV6vkUbO0=
|
||||
github.com/cosmos/cosmos-proto v1.0.0-alpha7/go.mod h1:dosO4pSAbJF8zWCzCoTWP7nNsjcvSUBQmniFxDg5daw=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
|
||||
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
|
||||
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
|
||||
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
|
||||
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
pgregory.net/rapid v0.4.7/go.mod h1:UYpPVyjFHzYBGHIxLFoupi8vwk6rXNzRY9OMvVxFIOU=
|
||||
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
|
||||
3
tx/textual/internal/testdata/README.md
vendored
Normal file
3
tx/textual/internal/testdata/README.md
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Testdata
|
||||
|
||||
This folder contains test vectors shared between [CosmJS](https://github.com/cosmos/cosmjs/pull/1147) and the Cosmos SDK.
|
||||
43
tx/textual/internal/testdata/decimals.json
vendored
Normal file
43
tx/textual/internal/testdata/decimals.json
vendored
Normal file
@ -0,0 +1,43 @@
|
||||
[
|
||||
["0", "0"],
|
||||
["1", "1"],
|
||||
["12", "12"],
|
||||
["123", "123"],
|
||||
["1234", "1'234"],
|
||||
["0.1", "0.1"],
|
||||
["0.01", "0.01"],
|
||||
["0.001", "0.001"],
|
||||
["0.0001", "0.0001"],
|
||||
["0.00001", "0.00001"],
|
||||
["0.000001", "0.000001"],
|
||||
["0.0000001", "0.0000001"],
|
||||
["0.00000001", "0.00000001"],
|
||||
["0.000000001", "0.000000001"],
|
||||
["0.0000000001", "0.0000000001"],
|
||||
["0.00000000001", "0.00000000001"],
|
||||
["0.000000000001", "0.000000000001"],
|
||||
["0.0000000000001", "0.0000000000001"],
|
||||
["0.00000000000001", "0.00000000000001"],
|
||||
["0.000000000000001", "0.000000000000001"],
|
||||
["0.0000000000000001", "0.0000000000000001"],
|
||||
["0.00000000000000001", "0.00000000000000001"],
|
||||
["0.000000000000000001", "0.000000000000000001"],
|
||||
["0.100000000000000000", "0.1"],
|
||||
["0.010000000000000000", "0.01"],
|
||||
["0.001000000000000000", "0.001"],
|
||||
["0.000100000000000000", "0.0001"],
|
||||
["0.000010000000000000", "0.00001"],
|
||||
["0.000001000000000000", "0.000001"],
|
||||
["0.000000100000000000", "0.0000001"],
|
||||
["0.000000010000000000", "0.00000001"],
|
||||
["0.000000001000000000", "0.000000001"],
|
||||
["0.000000000100000000", "0.0000000001"],
|
||||
["0.000000000010000000", "0.00000000001"],
|
||||
["0.000000000001000000", "0.000000000001"],
|
||||
["0.000000000000100000", "0.0000000000001"],
|
||||
["0.000000000000010000", "0.00000000000001"],
|
||||
["0.000000000000001000", "0.000000000000001"],
|
||||
["0.000000000000000100", "0.0000000000000001"],
|
||||
["0.000000000000000010", "0.00000000000000001"],
|
||||
["0.000000000000000001", "0.000000000000000001"]
|
||||
]
|
||||
19
tx/textual/internal/testdata/integers.json
vendored
Normal file
19
tx/textual/internal/testdata/integers.json
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
[
|
||||
["0", "0"],
|
||||
["1", "1"],
|
||||
["12", "12"],
|
||||
["123", "123"],
|
||||
["1234", "1'234"],
|
||||
["12345", "12'345"],
|
||||
["123456", "123'456"],
|
||||
["1234567", "1'234'567"],
|
||||
["9007199254740991", "9'007'199'254'740'991"],
|
||||
["9007199254740992", "9'007'199'254'740'992"],
|
||||
["18446744073709551615", "18'446'744'073'709'551'615"],
|
||||
["18446744073709551616", "18'446'744'073'709'551'616"],
|
||||
["340282366920938463463374607431768211455", "340'282'366'920'938'463'463'374'607'431'768'211'455"],
|
||||
["01", "1"],
|
||||
["001", "1"],
|
||||
["0001", "1"],
|
||||
["00001", "1"]
|
||||
]
|
||||
40
tx/textual/internal/testpb/1.proto
Normal file
40
tx/textual/internal/testpb/1.proto
Normal file
@ -0,0 +1,40 @@
|
||||
syntax="proto3";
|
||||
|
||||
option go_package = "cosmossdk.io/tx/textual/internal/testpb";
|
||||
|
||||
import "google/protobuf/descriptor.proto";
|
||||
import "cosmos_proto/cosmos.proto";
|
||||
import "cosmos/base/v1beta1/coin.proto";
|
||||
|
||||
enum Enumeration {
|
||||
One = 0;
|
||||
Two = 1;
|
||||
}
|
||||
|
||||
// A contains fields that are parseable by SIGN_MODE_TEXTUAL.
|
||||
message A {
|
||||
uint32 UINT32 = 1;
|
||||
uint64 UINT64 = 2;
|
||||
int32 INT32 = 3;
|
||||
int64 INT64 = 4;
|
||||
string SDKINT = 5 [(cosmos_proto.scalar) = "cosmos.Int"];
|
||||
string SDKDEC = 6 [(cosmos_proto.scalar) = "cosmos.Dec"];
|
||||
cosmos.base.v1beta1.Coin COIN = 7;
|
||||
repeated cosmos.base.v1beta1.Coin COINS = 8;
|
||||
}
|
||||
|
||||
// B contains fields that are not parseable by SIGN_MODE_TEXTUAL, some fields
|
||||
// may be moved to A at some point.
|
||||
message B {
|
||||
int32 INT32 = 1;
|
||||
sint32 SINT32 = 2;
|
||||
int64 INT64 = 3;
|
||||
sint64 SING64 = 4;
|
||||
sfixed32 SFIXED32 = 5;
|
||||
fixed32 FIXED32 = 6;
|
||||
float FLOAT = 7;
|
||||
sfixed64 SFIXED64 = 8;
|
||||
fixed64 FIXED64 = 9;
|
||||
double DOUBLE = 10;
|
||||
map<string, B> MAP = 11;
|
||||
}
|
||||
2441
tx/textual/internal/testpb/1.pulsar.go
Normal file
2441
tx/textual/internal/testpb/1.pulsar.go
Normal file
File diff suppressed because it is too large
Load Diff
2
tx/textual/internal/testpb/Makefile
Normal file
2
tx/textual/internal/testpb/Makefile
Normal file
@ -0,0 +1,2 @@
|
||||
codegen:
|
||||
@(buf generate)
|
||||
15
tx/textual/internal/testpb/buf.gen.yaml
Normal file
15
tx/textual/internal/testpb/buf.gen.yaml
Normal file
@ -0,0 +1,15 @@
|
||||
version: v1
|
||||
managed:
|
||||
enabled: true
|
||||
go_package_prefix:
|
||||
default: cosmossdk.io/tx/textual/internal/testpb
|
||||
except:
|
||||
- buf.build/googleapis/googleapis
|
||||
- buf.build/cosmos/gogo-proto
|
||||
- buf.build/cosmos/cosmos-proto
|
||||
override:
|
||||
buf.build/cosmos/cosmos-sdk: cosmossdk.io/api
|
||||
plugins:
|
||||
- name: go-pulsar
|
||||
out: .
|
||||
opt: paths=source_relative
|
||||
17
tx/textual/internal/testpb/buf.lock
Normal file
17
tx/textual/internal/testpb/buf.lock
Normal file
@ -0,0 +1,17 @@
|
||||
# Generated by buf. DO NOT EDIT.
|
||||
version: v1
|
||||
deps:
|
||||
- remote: buf.build
|
||||
owner: cosmos
|
||||
repository: cosmos-proto
|
||||
branch: main
|
||||
commit: 1935555c206d4afb9e94615dfd0fad31
|
||||
digest: b1-TNqW6xj2Pjha5Uoj9a-5uOeRo4mwswKfyqMcN3I_gZ0=
|
||||
create_time: 2021-12-02T22:04:00.31049Z
|
||||
- remote: buf.build
|
||||
owner: cosmos
|
||||
repository: gogo-proto
|
||||
branch: main
|
||||
commit: bee5511075b7499da6178d9e4aaa628b
|
||||
digest: b1-rrBIustouD-S80cVoZ_rM0qJsmei9AgbXy9GPQu6vxg=
|
||||
create_time: 2021-12-02T20:01:17.069307Z
|
||||
12
tx/textual/internal/testpb/buf.yaml
Normal file
12
tx/textual/internal/testpb/buf.yaml
Normal file
@ -0,0 +1,12 @@
|
||||
version: v1
|
||||
deps:
|
||||
- buf.build/cosmos/cosmos-proto
|
||||
- buf.build/cosmos/gogo-proto
|
||||
lint:
|
||||
use:
|
||||
- DEFAULT
|
||||
except:
|
||||
- PACKAGE_VERSION_SUFFIX
|
||||
breaking:
|
||||
ignore:
|
||||
- testpb
|
||||
56
tx/textual/valuerenderer/dec.go
Normal file
56
tx/textual/valuerenderer/dec.go
Normal file
@ -0,0 +1,56 @@
|
||||
package valuerenderer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"google.golang.org/protobuf/reflect/protoreflect"
|
||||
)
|
||||
|
||||
const thousandSeparator string = "'"
|
||||
|
||||
type decValueRenderer struct{}
|
||||
|
||||
var _ ValueRenderer = decValueRenderer{}
|
||||
|
||||
func (vr decValueRenderer) Format(_ context.Context, v protoreflect.Value, w io.Writer) error {
|
||||
formatted, err := formatDecimal(v.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = w.Write([]byte(formatted))
|
||||
return err
|
||||
}
|
||||
|
||||
func (vr decValueRenderer) Parse(_ context.Context, r io.Reader) (protoreflect.Value, error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
// formatDecimal formats a decimal into a value-rendered string. This function
|
||||
// operates with string manipulation (instead of manipulating the sdk.Dec
|
||||
// object).
|
||||
func formatDecimal(v string) (string, error) {
|
||||
parts := strings.Split(v, ".")
|
||||
intPart, err := formatInteger(parts[0])
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if len(parts) > 2 {
|
||||
return "", fmt.Errorf("invalid decimal %s", v)
|
||||
}
|
||||
|
||||
if len(parts) == 1 {
|
||||
return intPart, nil
|
||||
}
|
||||
|
||||
decPart := strings.TrimRight(parts[1], "0")
|
||||
if len(decPart) == 0 {
|
||||
return intPart, nil
|
||||
}
|
||||
|
||||
return intPart + "." + decPart, nil
|
||||
}
|
||||
48
tx/textual/valuerenderer/int.go
Normal file
48
tx/textual/valuerenderer/int.go
Normal file
@ -0,0 +1,48 @@
|
||||
package valuerenderer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"google.golang.org/protobuf/reflect/protoreflect"
|
||||
)
|
||||
|
||||
type intValueRenderer struct{}
|
||||
|
||||
var _ ValueRenderer = intValueRenderer{}
|
||||
|
||||
func (vr intValueRenderer) Format(_ context.Context, v protoreflect.Value, w io.Writer) error {
|
||||
formatted, err := formatInteger(v.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = w.Write([]byte(formatted))
|
||||
return err
|
||||
}
|
||||
|
||||
func (vr intValueRenderer) Parse(_ context.Context, r io.Reader) (protoreflect.Value, error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
// formatInteger formats an integer into a value-rendered string. This function
|
||||
// operates with string manipulation (instead of manipulating the int or sdk.Int
|
||||
// object).
|
||||
func formatInteger(v string) (string, error) {
|
||||
if v[0] == '-' {
|
||||
v = v[1:]
|
||||
}
|
||||
if len(v) > 1 {
|
||||
v = strings.TrimLeft(v, "0")
|
||||
|
||||
}
|
||||
|
||||
startOffset := 3
|
||||
for outputIndex := len(v); outputIndex > startOffset; {
|
||||
outputIndex -= 3
|
||||
v = v[:outputIndex] + thousandSeparator + v[outputIndex:]
|
||||
}
|
||||
|
||||
return v, nil
|
||||
}
|
||||
20
tx/textual/valuerenderer/types.go
Normal file
20
tx/textual/valuerenderer/types.go
Normal file
@ -0,0 +1,20 @@
|
||||
package valuerenderer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
|
||||
"google.golang.org/protobuf/reflect/protoreflect"
|
||||
)
|
||||
|
||||
// ValueRenderer defines an interface to produce formatted output for all
|
||||
// protobuf types as well as parse a string into those protobuf types.
|
||||
//
|
||||
// The notion of "value renderer" is defined in ADR-050, and that ADR provides
|
||||
// a default spec for value renderers. However, we define it as an interface
|
||||
// here, so that optionally more value renderers could be built, for example, a
|
||||
// separate one for a different language.
|
||||
type ValueRenderer interface {
|
||||
Format(context.Context, protoreflect.Value, io.Writer) error
|
||||
Parse(context.Context, io.Reader) (protoreflect.Value, error)
|
||||
}
|
||||
69
tx/textual/valuerenderer/valuerenderer.go
Normal file
69
tx/textual/valuerenderer/valuerenderer.go
Normal file
@ -0,0 +1,69 @@
|
||||
package valuerenderer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/reflect/protoreflect"
|
||||
|
||||
cosmos_proto "github.com/cosmos/cosmos-proto"
|
||||
)
|
||||
|
||||
type Textual struct {
|
||||
scalars map[string]ValueRenderer
|
||||
}
|
||||
|
||||
func NewTextual() Textual {
|
||||
return Textual{}
|
||||
}
|
||||
|
||||
// GetValueRenderer returns the value renderer for the given FieldDescriptor.
|
||||
func (r Textual) GetValueRenderer(fd protoreflect.FieldDescriptor) (ValueRenderer, error) {
|
||||
switch {
|
||||
// Scalars, such as sdk.Int and sdk.Dec.
|
||||
case fd.Kind() == protoreflect.StringKind && proto.GetExtension(fd.Options(), cosmos_proto.E_Scalar) != "":
|
||||
{
|
||||
scalar, ok := proto.GetExtension(fd.Options(), cosmos_proto.E_Scalar).(string)
|
||||
if !ok || scalar == "" {
|
||||
return nil, fmt.Errorf("got extension option %s of type %T", scalar, scalar)
|
||||
}
|
||||
|
||||
if r.scalars == nil {
|
||||
r.init()
|
||||
}
|
||||
|
||||
vr := r.scalars[scalar]
|
||||
if vr == nil {
|
||||
return nil, fmt.Errorf("got empty value renderer for scalar %s", scalar)
|
||||
}
|
||||
|
||||
return vr, nil
|
||||
}
|
||||
|
||||
// Integers
|
||||
case fd.Kind() == protoreflect.Uint32Kind ||
|
||||
fd.Kind() == protoreflect.Uint64Kind ||
|
||||
fd.Kind() == protoreflect.Int32Kind ||
|
||||
fd.Kind() == protoreflect.Int64Kind:
|
||||
{
|
||||
return intValueRenderer{}, nil
|
||||
}
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("value renderers cannot format value of type %s", fd.Kind())
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Textual) init() {
|
||||
if r.scalars == nil {
|
||||
r.scalars = map[string]ValueRenderer{}
|
||||
r.scalars["cosmos.Int"] = intValueRenderer{}
|
||||
r.scalars["cosmos.Dec"] = decValueRenderer{}
|
||||
}
|
||||
}
|
||||
|
||||
// DefineScalar adds a value renderer to the given Cosmos scalar.
|
||||
func (r *Textual) DefineScalar(scalar string, vr ValueRenderer) {
|
||||
r.init()
|
||||
r.scalars[scalar] = vr
|
||||
}
|
||||
145
tx/textual/valuerenderer/valuerenderer_test.go
Normal file
145
tx/textual/valuerenderer/valuerenderer_test.go
Normal file
@ -0,0 +1,145 @@
|
||||
package valuerenderer_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/protobuf/reflect/protoreflect"
|
||||
|
||||
"cosmossdk.io/math"
|
||||
"cosmossdk.io/tx/textual/internal/testpb"
|
||||
"cosmossdk.io/tx/textual/valuerenderer"
|
||||
)
|
||||
|
||||
func TestFormatInteger(t *testing.T) {
|
||||
type integerTest []string
|
||||
var testcases []integerTest
|
||||
raw, err := ioutil.ReadFile("../internal/testdata/integers.json")
|
||||
require.NoError(t, err)
|
||||
err = json.Unmarshal(raw, &testcases)
|
||||
require.NoError(t, err)
|
||||
|
||||
for _, tc := range testcases {
|
||||
// Parse test case strings as protobuf uint64
|
||||
i, err := strconv.ParseUint(tc[0], 10, 64)
|
||||
if err == nil {
|
||||
r, err := valueRendererOf(i)
|
||||
require.NoError(t, err)
|
||||
b := new(strings.Builder)
|
||||
err = r.Format(context.Background(), protoreflect.ValueOf(i), b)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, tc[1], b.String())
|
||||
}
|
||||
|
||||
// Parse test case strings as protobuf uint32
|
||||
i, err = strconv.ParseUint(tc[0], 10, 32)
|
||||
if err == nil {
|
||||
r, err := valueRendererOf(i)
|
||||
require.NoError(t, err)
|
||||
b := new(strings.Builder)
|
||||
err = r.Format(context.Background(), protoreflect.ValueOf(i), b)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, tc[1], b.String())
|
||||
}
|
||||
|
||||
// Parse test case strings as sdk.Ints
|
||||
sdkInt, ok := math.NewIntFromString(tc[0])
|
||||
if ok {
|
||||
r, err := valueRendererOf(sdkInt)
|
||||
require.NoError(t, err)
|
||||
b := new(strings.Builder)
|
||||
err = r.Format(context.Background(), protoreflect.ValueOf(tc[0]), b)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, tc[1], b.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatDecimal(t *testing.T) {
|
||||
type decimalTest []string
|
||||
var testcases []decimalTest
|
||||
raw, err := ioutil.ReadFile("../internal/testdata/decimals.json")
|
||||
require.NoError(t, err)
|
||||
err = json.Unmarshal(raw, &testcases)
|
||||
require.NoError(t, err)
|
||||
|
||||
for _, tc := range testcases {
|
||||
d, err := math.LegacyNewDecFromStr(tc[0])
|
||||
require.NoError(t, err)
|
||||
r, err := valueRendererOf(d)
|
||||
require.NoError(t, err)
|
||||
b := new(strings.Builder)
|
||||
err = r.Format(context.Background(), protoreflect.ValueOf(tc[0]), b)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, tc[1], b.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetADR050ValueRenderer(t *testing.T) {
|
||||
testcases := []struct {
|
||||
name string
|
||||
v interface{}
|
||||
expErr bool
|
||||
}{
|
||||
{"uint32", uint32(1), false},
|
||||
{"uint64", uint64(1), false},
|
||||
{"sdk.Int", math.NewInt(1), false},
|
||||
{"sdk.Dec", math.LegacyNewDec(1), false},
|
||||
{"float32", float32(1), true},
|
||||
{"float64", float64(1), true},
|
||||
}
|
||||
|
||||
for _, tc := range testcases {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
_, err := valueRendererOf(tc.v)
|
||||
if tc.expErr {
|
||||
require.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// valueRendererOf is like GetADR050ValueRenderer, but taking a Go type
|
||||
// as input instead of a protoreflect.FieldDescriptor.
|
||||
func valueRendererOf(v interface{}) (valuerenderer.ValueRenderer, error) {
|
||||
a, b := (&testpb.A{}).ProtoReflect().Descriptor().Fields(), (&testpb.B{}).ProtoReflect().Descriptor().Fields()
|
||||
|
||||
textual := valuerenderer.NewTextual()
|
||||
switch v := v.(type) {
|
||||
// Valid types for SIGN_MODE_TEXTUAL
|
||||
case uint32:
|
||||
return textual.GetValueRenderer(a.ByName(protoreflect.Name("UINT32")))
|
||||
case uint64:
|
||||
return textual.GetValueRenderer(a.ByName(protoreflect.Name("UINT64")))
|
||||
case int32:
|
||||
return textual.GetValueRenderer(a.ByName(protoreflect.Name("INT32")))
|
||||
case int64:
|
||||
return textual.GetValueRenderer(a.ByName(protoreflect.Name("INT64")))
|
||||
case math.Int:
|
||||
return textual.GetValueRenderer(a.ByName(protoreflect.Name("SDKINT")))
|
||||
case math.LegacyDec:
|
||||
return textual.GetValueRenderer(a.ByName(protoreflect.Name("SDKDEC")))
|
||||
|
||||
// Invalid types for SIGN_MODE_TEXTUAL
|
||||
case float32:
|
||||
return textual.GetValueRenderer(b.ByName(protoreflect.Name("FLOAT")))
|
||||
case float64:
|
||||
return textual.GetValueRenderer(b.ByName(protoreflect.Name("FLOAT")))
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("value %s of type %T not recognized", v, v)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user