scalar

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 30, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package scalar provides a type-safe encoding, decoding, and size computation layer for all protobuf scalar types. It builds on top of the wire package, bridging raw wire format primitives to protobuf-typed values such as int32, sint64, double, bool, string, and bytes.

All encoding functions follow the append pattern inherited from wire: they take a byte slice, append the encoded value, and return the extended slice. All decoding functions follow the consume pattern: they read from the front of a byte slice and return the decoded value along with the number of bytes consumed. A negative bytes-consumed value signals an error propagated from the wire layer.

The Kind type enumerates all 18 protobuf field kinds (double through group) and provides methods for wire type mapping, packability queries, and string representation. The Codec type offers dynamic dispatch of scalar encoding and decoding by Kind, using uint64 as the universal value representation for numeric types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendBool

func AppendBool(b []byte, v bool) []byte

AppendBool appends the protobuf varint encoding of v to b and returns the extended slice. True is encoded as varint 1 and false as varint 0.

func AppendBytes

func AppendBytes(b []byte, v []byte) []byte

AppendBytes appends a length-delimited byte field to b and returns the extended slice. The data length is encoded as a varint prefix followed by the raw data bytes.

func AppendDouble

func AppendDouble(b []byte, v float64) []byte

AppendDouble appends the little-endian IEEE 754 encoding of v to b and returns the extended slice.

func AppendEnum

func AppendEnum(b []byte, v int32) []byte

AppendEnum appends the protobuf varint encoding of enum value v to b and returns the extended slice. Enum encoding is identical to int32 encoding.

func AppendFixed32

func AppendFixed32(b []byte, v uint32) []byte

AppendFixed32 appends the little-endian encoding of v to b and returns the extended slice.

func AppendFixed64

func AppendFixed64(b []byte, v uint64) []byte

AppendFixed64 appends the little-endian encoding of v to b and returns the extended slice.

func AppendFloat

func AppendFloat(b []byte, v float32) []byte

AppendFloat appends the little-endian IEEE 754 encoding of v to b and returns the extended slice.

func AppendInt32

func AppendInt32(b []byte, v int32) []byte

AppendInt32 appends the protobuf varint encoding of v to b and returns the extended slice. Negative values are sign-extended to 64 bits, producing a 10-byte varint per the protobuf specification.

func AppendInt64

func AppendInt64(b []byte, v int64) []byte

AppendInt64 appends the protobuf varint encoding of v to b and returns the extended slice. Negative values are encoded as their two's-complement uint64 representation, producing a 10-byte varint.

func AppendSfixed32

func AppendSfixed32(b []byte, v int32) []byte

AppendSfixed32 appends the little-endian encoding of v to b and returns the extended slice. The signed int32 is reinterpreted as uint32 for encoding.

func AppendSfixed64

func AppendSfixed64(b []byte, v int64) []byte

AppendSfixed64 appends the little-endian encoding of v to b and returns the extended slice. The signed int64 is reinterpreted as uint64 for encoding.

func AppendSint32

func AppendSint32(b []byte, v int32) []byte

AppendSint32 appends the ZigZag-then-varint encoding of v to b and returns the extended slice. ZigZag encoding maps small absolute values to small unsigned values, producing compact output for values near zero.

func AppendSint64

func AppendSint64(b []byte, v int64) []byte

AppendSint64 appends the ZigZag-then-varint encoding of v to b and returns the extended slice. ZigZag encoding maps small absolute values to small unsigned values, producing compact output for values near zero.

func AppendString

func AppendString(b []byte, v string) []byte

AppendString appends a length-delimited string field to b and returns the extended slice. The string length is encoded as a varint prefix followed by the raw string bytes.

func AppendUint32

func AppendUint32(b []byte, v uint32) []byte

AppendUint32 appends the protobuf varint encoding of v to b and returns the extended slice.

func AppendUint64

func AppendUint64(b []byte, v uint64) []byte

AppendUint64 appends the protobuf varint encoding of v to b and returns the extended slice.

func ConsumeBool

func ConsumeBool(b []byte) (bool, int)

ConsumeBool reads a protobuf varint from the front of b and returns the decoded bool value and the number of bytes consumed. Any non-zero varint value decodes as true per the protobuf specification. A negative bytesConsumed signals an error propagated from the wire layer.

func ConsumeBytes

func ConsumeBytes(b []byte) ([]byte, int)

ConsumeBytes reads a length-delimited byte field from the front of b and returns the decoded data as a sub-slice of the input and the number of bytes consumed. A negative bytesConsumed signals an error propagated from the wire layer.

func ConsumeDouble

func ConsumeDouble(b []byte) (float64, int)

ConsumeDouble reads a little-endian IEEE 754 encoded float64 from the front of b. It returns the decoded value and the number of bytes consumed (8). A negative bytesConsumed signals an error propagated from the wire layer.

func ConsumeEnum

func ConsumeEnum(b []byte) (int32, int)

ConsumeEnum reads a protobuf varint from the front of b and returns the decoded enum value as int32 and the number of bytes consumed. Enum decoding is identical to int32 decoding. A negative bytesConsumed signals an error propagated from the wire layer.

func ConsumeFixed32

func ConsumeFixed32(b []byte) (uint32, int)

ConsumeFixed32 reads a little-endian encoded uint32 from the front of b. It returns the decoded value and the number of bytes consumed (4). A negative bytesConsumed signals an error propagated from the wire layer.

func ConsumeFixed64

func ConsumeFixed64(b []byte) (uint64, int)

ConsumeFixed64 reads a little-endian encoded uint64 from the front of b. It returns the decoded value and the number of bytes consumed (8). A negative bytesConsumed signals an error propagated from the wire layer.

func ConsumeFloat

func ConsumeFloat(b []byte) (float32, int)

ConsumeFloat reads a little-endian IEEE 754 encoded float32 from the front of b. It returns the decoded value and the number of bytes consumed (4). A negative bytesConsumed signals an error propagated from the wire layer.

func ConsumeInt32

func ConsumeInt32(b []byte) (int32, int)

ConsumeInt32 reads a protobuf varint from the front of b and returns the decoded int32 value and the number of bytes consumed. A negative bytesConsumed signals an error propagated from the wire layer.

func ConsumeInt64

func ConsumeInt64(b []byte) (int64, int)

ConsumeInt64 reads a protobuf varint from the front of b and returns the decoded int64 value and the number of bytes consumed. A negative bytesConsumed signals an error propagated from the wire layer.

func ConsumeSfixed32

func ConsumeSfixed32(b []byte) (int32, int)

ConsumeSfixed32 reads a little-endian encoded int32 from the front of b. It returns the decoded value and the number of bytes consumed (4). The uint32 wire representation is reinterpreted as int32. A negative bytesConsumed signals an error propagated from the wire layer.

func ConsumeSfixed64

func ConsumeSfixed64(b []byte) (int64, int)

ConsumeSfixed64 reads a little-endian encoded int64 from the front of b. It returns the decoded value and the number of bytes consumed (8). The uint64 wire representation is reinterpreted as int64. A negative bytesConsumed signals an error propagated from the wire layer.

func ConsumeSint32

func ConsumeSint32(b []byte) (int32, int)

ConsumeSint32 reads a ZigZag-encoded varint from the front of b, decodes it to the original signed int32 value, and returns the value and bytes consumed. A negative bytesConsumed signals an error propagated from the wire layer.

func ConsumeSint64

func ConsumeSint64(b []byte) (int64, int)

ConsumeSint64 reads a ZigZag-encoded varint from the front of b, decodes it to the original signed int64 value, and returns the value and bytes consumed. A negative bytesConsumed signals an error propagated from the wire layer.

func ConsumeString

func ConsumeString(b []byte) (string, int)

ConsumeString reads a length-delimited string field from the front of b and returns the decoded string and the number of bytes consumed. A negative bytesConsumed signals an error propagated from the wire layer.

func ConsumeUint32

func ConsumeUint32(b []byte) (uint32, int)

ConsumeUint32 reads a protobuf varint from the front of b and returns the decoded uint32 value and the number of bytes consumed. A negative bytesConsumed signals an error propagated from the wire layer.

func ConsumeUint64

func ConsumeUint64(b []byte) (uint64, int)

ConsumeUint64 reads a protobuf varint from the front of b and returns the decoded uint64 value and the number of bytes consumed. A negative bytesConsumed signals an error propagated from the wire layer.

func DefaultValue

func DefaultValue(k Kind) any

DefaultValue returns the Go zero value for the given protobuf field kind. Each kind maps to a specific Go type: float64 for double, float32 for float, int32 for int32/sint32/sfixed32/enum, int64 for int64/sint64/sfixed64, uint32 for uint32/fixed32, uint64 for uint64/fixed64, bool for bool, string for string, []byte(nil) for bytes, and nil for message/group. Invalid Kind values return nil.

func SizeBool

func SizeBool(_ bool) int

SizeBool returns the number of bytes required to encode a bool value as a protobuf varint. Both true (varint 1) and false (varint 0) require exactly one byte.

func SizeBytes

func SizeBytes(v []byte) int

SizeBytes returns the number of bytes required to encode a length-delimited byte field. The result includes both the varint-encoded length prefix and the data itself.

func SizeDouble

func SizeDouble(float64) int

SizeDouble returns the encoded size of a double value, which is always 8.

func SizeEnum

func SizeEnum(v int32) int

SizeEnum returns the number of bytes required to varint-encode enum value v. Enum sizing is identical to int32 sizing.

func SizeFixed32

func SizeFixed32(uint32) int

SizeFixed32 returns the encoded size of a fixed32 value, which is always 4.

func SizeFixed64

func SizeFixed64(uint64) int

SizeFixed64 returns the encoded size of a fixed64 value, which is always 8.

func SizeFloat

func SizeFloat(float32) int

SizeFloat returns the encoded size of a float value, which is always 4.

func SizeInt32

func SizeInt32(v int32) int

SizeInt32 returns the number of bytes required to varint-encode v. Negative values are sign-extended to 64 bits, matching the encoding behavior of AppendInt32, and always produce a 10-byte varint.

func SizeInt64

func SizeInt64(v int64) int

SizeInt64 returns the number of bytes required to varint-encode v. Negative values are encoded as their two's-complement uint64 representation.

func SizeSfixed32

func SizeSfixed32(int32) int

SizeSfixed32 returns the encoded size of an sfixed32 value, which is always 4.

func SizeSfixed64

func SizeSfixed64(int64) int

SizeSfixed64 returns the encoded size of an sfixed64 value, which is always 8.

func SizeSint32

func SizeSint32(v int32) int

SizeSint32 returns the number of bytes required to encode v as a ZigZag-then-varint value, without performing the encoding.

func SizeSint64

func SizeSint64(v int64) int

SizeSint64 returns the number of bytes required to encode v as a ZigZag-then-varint value, without performing the encoding.

func SizeString

func SizeString(v string) int

SizeString returns the number of bytes required to encode a length-delimited string field. The result includes both the varint-encoded length prefix and the string data itself.

func SizeUint32

func SizeUint32(v uint32) int

SizeUint32 returns the number of bytes required to varint-encode v.

func SizeUint64

func SizeUint64(v uint64) int

SizeUint64 returns the number of bytes required to varint-encode v.

Types

type Codec

type Codec struct{}

Codec is a stateless struct that provides dynamic dispatch of scalar encoding, decoding, and size computation by Kind. All numeric values are represented as uint64 and the caller is responsible for type conversion. Length-delimited kinds (string, bytes, message) and group are not supported and cause a panic.

func NewCodec

func NewCodec() *Codec

NewCodec returns a pointer to a new Codec instance.

func (*Codec) AppendScalar

func (c *Codec) AppendScalar(b []byte, kind Kind, v uint64) []byte

AppendScalar appends the wire encoding of v (represented as uint64) to b for the given Kind and returns the extended slice. For zigzag kinds, v must already contain the zigzag-encoded value. Length-delimited and group kinds panic.

func (*Codec) ConsumeScalar

func (c *Codec) ConsumeScalar(kind Kind, b []byte) (uint64, int)

ConsumeScalar reads the wire encoding for the given Kind from the front of b and returns the raw uint64 value and the number of bytes consumed. For zigzag kinds, the returned value is the raw varint (caller applies zigzag decode). Length-delimited and group kinds panic.

func (*Codec) SizeScalar

func (c *Codec) SizeScalar(kind Kind, v uint64) int

SizeScalar returns the number of bytes required to encode v for the given Kind. Length-delimited and group kinds panic.

type InvalidKindError

type InvalidKindError struct {
	Method string
	K      Kind
}

InvalidKindError indicates that a Codec operation was called with a kind value outside the valid range (1-18). It carries the method name and the offending Kind for diagnostic context.

func (*InvalidKindError) Error

func (e *InvalidKindError) Error() string

Error returns a human-readable message describing the invalid kind.

type Kind

type Kind int8

Kind represents one of the 18 protobuf field kinds. It is a named type over int8 to provide type safety. The zero value is intentionally invalid.

const (
	KindDouble   Kind = iota + 1 // 1
	KindFloat                    // 2
	KindInt64                    // 3
	KindUint64                   // 4
	KindInt32                    // 5
	KindFixed64                  // 6
	KindFixed32                  // 7
	KindBool                     // 8
	KindString                   // 9
	KindBytes                    // 10
	KindUint32                   // 11
	KindEnum                     // 12
	KindSfixed32                 // 13
	KindSfixed64                 // 14
	KindSint32                   // 15
	KindSint64                   // 16
	KindMessage                  // 17
	KindGroup                    // 18
)

Kind constants for all 18 protobuf field kinds. Values start at 1 so that the zero value of Kind is invalid by design.

func (Kind) IsPackable

func (k Kind) IsPackable() bool

IsPackable reports whether fields of this kind can use packed repeated encoding. Numeric and bool kinds are packable; string, bytes, message, and group are not.

func (Kind) String

func (k Kind) String() string

String returns the lowercase protobuf name of the kind. For the 18 valid kinds (1-18) it returns names such as "double" and "sint64". For out-of-range values it returns a formatted string like "Kind(19)".

func (Kind) Valid

func (k Kind) Valid() bool

Valid reports whether k is one of the 18 defined protobuf field kinds (1 through 18 inclusive).

func (Kind) WireType

func (k Kind) WireType() wire.WireType

WireType returns the protocol buffer wire type used to encode this kind.

type UnsupportedKindError

type UnsupportedKindError struct {
	Method string
	K      Kind
}

UnsupportedKindError indicates that a Codec operation was called with a kind it does not support (e.g., length-delimited or group kinds passed to AppendScalar). It carries the method name and the offending Kind for diagnostic context.

func (*UnsupportedKindError) Error

func (e *UnsupportedKindError) Error() string

Error returns a human-readable message describing the unsupported kind.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL