encoding

package
v1.0.0-rc.24 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: Apache-2.0 Imports: 7 Imported by: 3

Documentation

Overview

Package proto contains helper functions for Protocol Buffers (https://protobuf.dev) in addition to the ones from google.golang.org/protobuf/encoding/protowire package.

Index

Constants

View Source
const (
	FieldRequestBody               = 1
	FieldRequestMetaHeader         = 2
	FieldRequestVerificationHeader = 3
)

Common request field numbers.

Variables

This section is empty.

Functions

func CalculateRepeatedFieldsLength

func CalculateRepeatedFieldsLength(num protowire.Number, count int, lenFn RepeatedMessageLenFunc) int

CalculateRepeatedFieldsLength returns length of specified number of repeated messages with given field number.

func CalculateRequestBodyFieldLength

func CalculateRequestBodyFieldLength(ln int) int

CalculateRequestBodyFieldLength returns length of field for the request body with given length.

func CalculateRequestBodyWithMetaHeaderLength

func CalculateRequestBodyWithMetaHeaderLength(bodyLen int, metaHdrLen int) int

CalculateRequestBodyWithMetaHeaderLength returns length of request body and meta header fields with specified lengths.

func CalculateRequestLength

func CalculateRequestLength(bodyLen int, metaHdrLen int, verifHdrLen int) int

CalculateRequestLength returns length of request body, meta header and verification header fields with specified lengths.

func CalculateRequestMetaHeaderFieldLength

func CalculateRequestMetaHeaderFieldLength(ln int) int

CalculateRequestMetaHeaderFieldLength returns length of field for the request meta header with given length.

func CalculateRequestVerificationHeaderFieldLength

func CalculateRequestVerificationHeaderFieldLength(ln int) int

CalculateRequestVerificationHeaderFieldLength returns length of field for the request verification header with given length.

func EncodeRequest

func EncodeRequest[B, M Message](buf []byte, reqBody B, reqMetaHeader M) ([]byte, int)

EncodeRequest ONLY correctly encodes requests that have strictly ordered two fields (excepting verification header): field #1 is body, field #2 is meta header. Any other requests must be encoded differently. The second returned value means buffer len occupied for req.

func Marshal

func Marshal[M Message, T interface{ ProtoMessage() M }](v T) []byte

Marshal encodes v transmitted via NeoFS API protocol into a dynamically allocated buffer.

func MarshalJSON

func MarshalJSON[M proto.Message, T interface{ ProtoMessage() M }](v T) ([]byte, error)

MarshalJSON encodes v into protojson with unpopulated fields' emission.

func MarshalMessage

func MarshalMessage(m Message) []byte

MarshalMessage encodes m into a dynamically allocated buffer.

func MarshalMessageJSON

func MarshalMessageJSON(m proto.Message) ([]byte, error)

MarshalMessageJSON encodes m into protojson with unpopulated fields' emission.

func MarshalToBool

func MarshalToBool(b []byte, num protowire.Number, v bool) int

MarshalToBool encodes 'bool' protobuf field with given number and value into b and returns the number of bytes written. If the buffer is too small, MarshalToBool will panic.

func MarshalToBytes

func MarshalToBytes[T Bytes](b []byte, num protowire.Number, v T) int

MarshalToBytes encodes 'bytes' or 'string' protobuf field with given number and value into b and returns the number of bytes written. If the buffer is too small, MarshalToBytes will panic.

func MarshalToDouble

func MarshalToDouble(b []byte, num protowire.Number, v float64) int

MarshalToDouble encodes 'double' protobuf field with given number and value into b and returns the number of bytes written. If the buffer is too small, MarshalToDouble will panic.

func MarshalToEmbedded

func MarshalToEmbedded(b []byte, num protowire.Number, v Message) int

MarshalToEmbedded encodes embedded message being a protobuf field with given number and value into b and returns the number of bytes written. If the buffer is too small, MarshalToEmbedded will panic.

func MarshalToEmbeddedLength

func MarshalToEmbeddedLength(b []byte, num protowire.Number, sz int, v Message) int

MarshalToEmbeddedLength encodes embedded message being a protobuf field with given number, length and value into b and returns the number of bytes written. If the buffer is too small, MarshalToEmbeddedLength will panic.

Does not write field if message length is zero.

func MarshalToFixed32

func MarshalToFixed32(b []byte, num protowire.Number, v uint32) int

MarshalToFixed32 encodes 'fixed32' protobuf field with given number and value into b and returns the number of bytes written. If the buffer is too small, MarshalToFixed32 will panic.

func MarshalToFixed64

func MarshalToFixed64(b []byte, num protowire.Number, v uint64) int

MarshalToFixed64 encodes 'fixed64' protobuf field with given number and value into b and returns the number of bytes written. If the buffer is too small, MarshalToFixed64 will panic.

func MarshalToFloat

func MarshalToFloat(b []byte, num protowire.Number, v float32) int

MarshalToFloat encodes 'float' protobuf field with given number and value into b and returns the number of bytes written. If the buffer is too small, MarshalToFloat will panic.

func MarshalToOptionalVarint

func MarshalToOptionalVarint[T Varint](b []byte, num protowire.Number, v *T) int

MarshalToOptionalVarint encodes an optional varint protobuf field with given number and value into b and returns the number of bytes written. If the buffer is too small, MarshalToOptionalVarint will panic.

func MarshalToRepeatedBytes

func MarshalToRepeatedBytes[T Bytes](b []byte, num protowire.Number, v []T) int

MarshalToRepeatedBytes encodes 'repeated bytes' or 'repeated string' protobuf field with given number and value into b and returns the number of bytes written. If the buffer is too small, MarshalToRepeatedBytes will panic.

func MarshalToRepeatedMessages

func MarshalToRepeatedMessages[T any, M interface {
	*T
	Message
}](b []byte, num protowire.Number, v []M) int

MarshalToRepeatedMessages encodes 'repeated M' protobuf field with given number and values into b and returns the number of bytes written. If the buffer is too small, MarshalToRepeatedMessages will panic.

func MarshalToRepeatedVarint

func MarshalToRepeatedVarint[T Varint](b []byte, num protowire.Number, v []T) int

MarshalToRepeatedVarint encodes 'repeated' varint protobuf field with given number and value into b and returns the number of bytes written. If the buffer is too small, MarshalToRepeatedVarint will panic.

func MarshalToVarint

func MarshalToVarint[T Varint](b []byte, num protowire.Number, v T) int

MarshalToVarint encodes varint protobuf field with given number and value into b and returns the number of bytes written. If the buffer is too small, MarshalToVarint will panic.

func SizeBool

func SizeBool(num protowire.Number, v bool) int

SizeBool returns the encoded size of 'bool' protobuf field with given number and value.

func SizeBytes

func SizeBytes[T Bytes](num protowire.Number, v T) int

SizeBytes returns the encoded size of 'bytes' or 'string' protobuf field with given number and value.

func SizeDouble

func SizeDouble(num protowire.Number, v float64) int

SizeDouble returns the encoded size of 'double' protobuf field with given number and value.

func SizeEmbedded

func SizeEmbedded(num protowire.Number, v Message) int

SizeEmbedded returns the encoded size of embedded message being a protobuf field with given number and value.

func SizeEmbeddedLENField

func SizeEmbeddedLENField(num protowire.Number, ln int) int

SizeEmbeddedLENField returns the encoded size of embedded LEN protobuf field with given number and length. Returns zero for zero length.

func SizeFixed32

func SizeFixed32(num protowire.Number, v uint32) int

SizeFixed32 returns the encoded size of 'fixed32' protobuf field with given number and value.

func SizeFixed64

func SizeFixed64(num protowire.Number, v uint64) int

SizeFixed64 returns the encoded size of 'fixed64' protobuf field with given number and value.

func SizeFloat

func SizeFloat(num protowire.Number, v float32) int

SizeFloat returns the encoded size of 'float' protobuf field with given number and value.

func SizeOptionalVarint

func SizeOptionalVarint[T Varint](num protowire.Number, v *T) int

SizeOptionalVarint returns the encoded size of an optional varint protobuf field with given number and value.

func SizeRepeatedBytes

func SizeRepeatedBytes[T Bytes](num protowire.Number, v []T) int

SizeRepeatedBytes returns the encoded size of 'repeated bytes' or 'repeated string' protobuf field with given number and value.

func SizeRepeatedMessages

func SizeRepeatedMessages[T any, M interface {
	*T
	Message
}](num protowire.Number, v []M) int

SizeRepeatedMessages returns the encoded size of 'repeated M' protobuf field with given number and values.

func SizeRepeatedVarint

func SizeRepeatedVarint[T Varint](num protowire.Number, v []T) int

SizeRepeatedVarint returns the encoded size of 'repeated' varint protobuf field with given number and value.

func SizeVarint

func SizeVarint[T Varint](num protowire.Number, v T) int

SizeVarint returns the encoded size of varint protobuf field with given number and value.

func Unmarshal

func Unmarshal[M_ any, M interface {
	*M_
	proto.Message
	Message
}, T interface{ FromProtoMessage(M) error }](b []byte, v T) error

Unmarshal decodes v transmitted via NeoFS API protocol from b.

func UnmarshalJSON

func UnmarshalJSON[M_ any, M interface {
	*M_
	proto.Message
	Message
}, T interface{ FromProtoMessage(M) error }](b []byte, v T) error

UnmarshalJSON decodes v from protojson.

func UnmarshalJSONOptional

func UnmarshalJSONOptional[M_ any, M interface {
	*M_
	proto.Message
	Message
}, T interface{ FromProtoMessage(M) error }](b []byte, v T, dec func(T, M, bool) error) error

UnmarshalJSONOptional decodes v from protojson with ignored missing required fields.

func UnmarshalMessage

func UnmarshalMessage(b []byte, m proto.Message) error

UnmarshalMessage decodes m from b.

func UnmarshalMessageJSON

func UnmarshalMessageJSON(b []byte, m proto.Message) error

UnmarshalMessageJSON decodes m from protojson.

func UnmarshalOptional

func UnmarshalOptional[M_ any, M interface {
	*M_
	proto.Message
	Message
}, T interface{ FromProtoMessage(M) error }](b []byte, v T, dec func(T, M, bool) error) error

UnmarshalOptional decodes v from protojson with ignored missing required fields.

func WriteMessageField

func WriteMessageField(buf []byte, num protowire.Number, ln int, writeFn WriteMessageFunc) int

WriteMessageField writes LEN message with given field number into buf. Returns number of bytes written.

func WriteRepeatedFields

func WriteRepeatedFields(buf []byte, num protowire.Number, count int, lenFn RepeatedMessageLenFunc, writeFn WriteRepeatedMessageFunc) int

WriteRepeatedFields writes specified number of repeated messages with given field number into buf.

func WriteRequestBodyMessage

func WriteRequestBodyMessage(buf []byte, m Message) int

WriteRequestBodyMessage writes request body field into buf. Returns number of bytes written.

func WriteRequestBodyTagAndLength

func WriteRequestBodyTagAndLength(buf []byte, ln int) int

WriteRequestBodyTagAndLength writes request body field tag and length into buf. Returns number of bytes written.

func WriteRequestMetaHeaderMessage

func WriteRequestMetaHeaderMessage(buf []byte, m Message) int

WriteRequestMetaHeaderMessage writes request meta header field into buf. Returns number of bytes written.

func WriteRequestMetaHeaderTagAndLength

func WriteRequestMetaHeaderTagAndLength(buf []byte, metaHdrLen int) int

WriteRequestMetaHeaderTagAndLength writes request meta header field tag and length into buf. Returns number of bytes written.

func WriteRequestVerificationHeaderMessage

func WriteRequestVerificationHeaderMessage(buf []byte, m Message) int

WriteRequestVerificationHeaderMessage writes request verification header field into buf. Returns number of bytes written.

func WriteRequestVerificationHeaderTagAndLength

func WriteRequestVerificationHeaderTagAndLength(buf []byte, ln int) int

WriteRequestVerificationHeaderTagAndLength writes request verification header field tag and length into buf. Returns number of bytes written.

func WriteStablyMarshalledMessageField

func WriteStablyMarshalledMessageField(buf []byte, num protowire.Number, m Message) int

WriteStablyMarshalledMessageField writes m field with given number into buf. Returns number of bytes written.

func WriteTag

func WriteTag(buf []byte, num protowire.Number, typ protowire.Type) int

WriteTag writes tag for the field of given number and type into buf. Returns number of bytes written.

func WriteTagAndLength

func WriteTagAndLength(buf []byte, num protowire.Number, ln int) int

WriteTagAndLength writes tag for the LEN field of given number and its length into buf. Returns number of bytes written.

func WriteTagAndVarint

func WriteTagAndVarint[T Varint](buf []byte, num protowire.Number, typ protowire.Type, v T) int

WriteTagAndVarint writes tag for the field of given number and type followed by varint into buf. Returns number of bytes written.

Types

type Bytes

type Bytes interface{ ~[]byte | ~string }

Bytes is a type parameter constraint for any byte arrays.

type Message

type Message interface {
	// MarshaledSize returns size of the encoded Message in bytes.
	MarshaledSize() int
	// MarshalStable encodes the Message into b. If the buffer is too small,
	// MarshalStable will panic.
	MarshalStable(b []byte)
}

Message is provided by protobuf 'message' types used in NeoFS for so-called stable marshaling: protobuf with the order of fields in strict ascending order of their numbers.

type RepeatedMessageLenFunc

type RepeatedMessageLenFunc = func(int) int

Various function aliases.

type Varint

type Varint interface {
	~int32 | int64 | uint32 | uint64 | int // ~int32 for 'enum' fields
}

Varint is a type parameter constraint for any variable-length protobuf integers.

type WriteMessageFunc

type WriteMessageFunc = func(buf []byte) int

Various function aliases.

func WriteStablyMarshalledMessageFunc

func WriteStablyMarshalledMessageFunc(m Message) WriteMessageFunc

WriteStablyMarshalledMessageFunc returns function for m writing.

type WriteRepeatedMessageFunc

type WriteRepeatedMessageFunc = func(buf []byte, i int) int

Various function aliases.

Jump to

Keyboard shortcuts

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