cosmos-sdk/tx/textual/valuerenderer/valuerenderer.go
Joe Abbey 3343c57a2c
feat(textual): bytes value renderer (#12734)
## Description

Closes: #12711

Implements bytes value renderer for SIGN_MODE_TEXTUAL

---

### 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)
2022-07-27 15:55:42 +00:00

72 lines
1.7 KiB
Go

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
}
case fd.Kind() == protoreflect.BytesKind:
return bytesValueRenderer{}, 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
}