cosmos-sdk/tx/textual/valuerenderer/bytes.go
Amaury fd028db79a
feat(textual): Coin and Coins value renderers (#12729)
* wip coins

* Make coin test pass

* Remove useless file

* wip

* Fix tests

* Small tweaks

* reviews

* Add comment

* Add back go mod

* Add more coins test

* Update coins test

* Add more coins tests

* Reference todo issue

* Add metadata querier test

* add more tests

* Fix test build

* Improve comments

* Update tx/textual/internal/testdata/coin.json

Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com>

* json formatting

* add more test cases

* go mod tidy

* address review

Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com>
2022-09-14 12:32:52 +02:00

40 lines
993 B
Go

package valuerenderer
import (
"context"
"encoding/hex"
"io"
"strings"
"google.golang.org/protobuf/reflect/protoreflect"
)
// NewBytesValueRenderer returns a ValueRenderer for Protobuf bytes, which are
// encoded as capital-letter hexadecimal, without the '0x' prefix.
func NewBytesValueRenderer() ValueRenderer {
return bytesValueRenderer{}
}
type bytesValueRenderer struct{}
var _ ValueRenderer = bytesValueRenderer{}
func (vr bytesValueRenderer) Format(ctx context.Context, v protoreflect.Value, w io.Writer) error {
_, err := io.WriteString(w, strings.ToUpper(hex.EncodeToString(v.Bytes())))
return err
}
func (vr bytesValueRenderer) Parse(_ context.Context, r io.Reader) (protoreflect.Value, error) {
formatted, err := io.ReadAll(r)
if err != nil {
return protoreflect.ValueOfBytes([]byte{}), err
}
data, err := hex.DecodeString(string(formatted))
if err != nil {
return protoreflect.ValueOfBytes([]byte{}), err
}
return protoreflect.ValueOfBytes(data), nil
}