## Description Closes: #12709 Part of Sign Mode Textual (ADR 050) implementation. Renders Timestamp messages as RFC 3339 (simplified ISO 8601). --- ### 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... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [x] 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` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - [x] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] 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)
90 lines
2.4 KiB
Go
90 lines
2.4 KiB
Go
package valuerenderer
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
|
|
|
cosmos_proto "github.com/cosmos/cosmos-proto"
|
|
)
|
|
|
|
// Textual holds the configuration for dispatching
|
|
// to specific value renderers for SIGN_MODE_TEXTUAL.
|
|
type Textual struct {
|
|
scalars map[string]ValueRenderer
|
|
messages map[protoreflect.FullName]ValueRenderer
|
|
}
|
|
|
|
// NewTextual returns a new Textual which provides
|
|
// value renderers.
|
|
func NewTextual() Textual {
|
|
t := Textual{}
|
|
t.init()
|
|
return t
|
|
}
|
|
|
|
// 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 encoded as strings.
|
|
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)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
case fd.Kind() == protoreflect.MessageKind:
|
|
md := fd.Message()
|
|
fullName := md.FullName()
|
|
|
|
vr, found := r.messages[fullName]
|
|
if found {
|
|
return vr, nil
|
|
}
|
|
// TODO default message renderer
|
|
return nil, fmt.Errorf("no value renderer for message %s", fullName)
|
|
|
|
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{}
|
|
}
|
|
if r.messages == nil {
|
|
r.messages = map[protoreflect.FullName]ValueRenderer{}
|
|
r.messages["google.protobuf.Timestamp"] = NewTimestampValueRenderer()
|
|
}
|
|
}
|
|
|
|
// DefineScalar adds a value renderer to the given Cosmos scalar.
|
|
func (r *Textual) DefineScalar(scalar string, vr ValueRenderer) {
|
|
r.init()
|
|
r.scalars[scalar] = vr
|
|
}
|