## Description Closes: #11381 --- ### 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/master/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/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/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)
102 lines
2.4 KiB
Go
102 lines
2.4 KiB
Go
package ormfield
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"io"
|
|
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
|
)
|
|
|
|
// BytesCodec encodes bytes as raw bytes. It errors if the byte array is longer
|
|
// than 255 bytes.
|
|
type BytesCodec struct{}
|
|
|
|
func (b BytesCodec) FixedBufferSize() int {
|
|
return -1
|
|
}
|
|
|
|
// ComputeBufferSize returns the bytes size of the value.
|
|
func (b BytesCodec) ComputeBufferSize(value protoreflect.Value) (int, error) {
|
|
return bytesSize(value), nil
|
|
}
|
|
|
|
func bytesSize(value protoreflect.Value) int {
|
|
return len(value.Bytes())
|
|
}
|
|
|
|
func (b BytesCodec) IsOrdered() bool {
|
|
return false
|
|
}
|
|
|
|
func (b BytesCodec) Decode(r Reader) (protoreflect.Value, error) {
|
|
bz, err := io.ReadAll(r)
|
|
return protoreflect.ValueOfBytes(bz), err
|
|
}
|
|
|
|
func (b BytesCodec) Encode(value protoreflect.Value, w io.Writer) error {
|
|
_, err := w.Write(value.Bytes())
|
|
return err
|
|
}
|
|
|
|
func (b BytesCodec) Compare(v1, v2 protoreflect.Value) int {
|
|
return bytes.Compare(v1.Bytes(), v2.Bytes())
|
|
}
|
|
|
|
// NonTerminalBytesCodec encodes bytes as raw bytes length prefixed by a single
|
|
// byte. It errors if the byte array is longer than 255 bytes.
|
|
type NonTerminalBytesCodec struct{}
|
|
|
|
func (b NonTerminalBytesCodec) FixedBufferSize() int {
|
|
return -1
|
|
}
|
|
|
|
// ComputeBufferSize returns the bytes size of the value plus the length of the
|
|
// varint length-prefix.
|
|
func (b NonTerminalBytesCodec) ComputeBufferSize(value protoreflect.Value) (int, error) {
|
|
n := bytesSize(value)
|
|
prefixLen := 1
|
|
// we use varint, if the first bit of a byte is 1 then we need to signal continuation
|
|
for n >= 0x80 {
|
|
prefixLen++
|
|
n >>= 7
|
|
}
|
|
return n + prefixLen, nil
|
|
}
|
|
|
|
func (b NonTerminalBytesCodec) IsOrdered() bool {
|
|
return false
|
|
}
|
|
|
|
func (b NonTerminalBytesCodec) Compare(v1, v2 protoreflect.Value) int {
|
|
return bytes.Compare(v1.Bytes(), v2.Bytes())
|
|
}
|
|
|
|
func (b NonTerminalBytesCodec) Decode(r Reader) (protoreflect.Value, error) {
|
|
n, err := binary.ReadUvarint(r)
|
|
if err != nil {
|
|
return protoreflect.Value{}, err
|
|
}
|
|
|
|
if n == 0 {
|
|
return protoreflect.ValueOfBytes([]byte{}), nil
|
|
}
|
|
|
|
bz := make([]byte, n)
|
|
_, err = r.Read(bz)
|
|
return protoreflect.ValueOfBytes(bz), err
|
|
}
|
|
|
|
func (b NonTerminalBytesCodec) Encode(value protoreflect.Value, w io.Writer) error {
|
|
bz := value.Bytes()
|
|
n := len(bz)
|
|
var prefix [binary.MaxVarintLen64]byte
|
|
prefixLen := binary.PutUvarint(prefix[:], uint64(n))
|
|
_, err := w.Write(prefix[:prefixLen])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = w.Write(bz)
|
|
return err
|
|
}
|