rawpb

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

README

rawpb

Go Reference

Raw protobuf message reader

Usage

Using the library with an example of a Prometheus remote write message.

var ts prompb.TimeSeries

r := New(
    Begin(func() error {
        // begin WriteRequest
        return nil
    }),
    End(func() error {
        // end WriteRequest
        return nil
    }),
    Message(1, New( // TimeSeries
        Begin(func() error {
            // begin TimeSeries, reset
            ts.Labels = ts.Labels[:0]
            ts.Samples = ts.Samples[:0]
            return nil
        }),
        End(func() error {
            // do something with single TimeSeries
            return nil
        }),
        Message(1, New( // Labels
            Begin(func() error {
                // append new Label
                ts.Labels = append(ts.Labels, prompb.Label{})
                return nil
            }),
            End(func() error { return nil }),
            String(1, func(v string) error { // Name
                ts.Labels[len(ts.Labels)-1].Name = v
                return nil
            }),
            String(2, func(v string) error { // Value
                ts.Labels[len(ts.Labels)-1].Value = v
                return nil
            }),
        )),
        Message(2, New( // Samples
            Begin(func() error {
                // append new Sample
                ts.Samples = append(ts.Samples, prompb.Sample{})
                return nil
            }),
            End(func() error { return nil }),
            Double(1, func(v float64) error { // Value
                ts.Samples[len(ts.Samples)-1].Value = v
                return nil
            }),
            Int64(2, func(v int64) error { // Timestamp
                ts.Samples[len(ts.Samples)-1].Timestamp = v
                return nil
            }),
        )),
    )),
)

r.Parse(raw)

Write

rawpb.Write(out, func(w *Writer) error {
    w.String(12, "test string")
    w.Float(16, 123.456)

    // submessage
    w.Message(17, func(w *Writer) error {
        w.String(1, "sub message")
        w.Enum(2, int32(test.EnumType_ENUM_TYPE_VALUE2))

        w.Message(3, func(w *Writer) error {
            w.Uint32(28, 42)
            return nil
        })
        return nil
    })
})
> go test -bench=. -benchmem
BenchmarkGogoUnmarshalWriteRequest-8   	     711	   1875505 ns/op	 3815839 B/op	   35980 allocs/op
BenchmarkRawpbParseWriteRequest-8      	    2396	    480921 ns/op	       0 B/op	       0 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrorInvalidMessage = errors.New("invalid message")
View Source
var ErrorTruncated = errors.New("message truncated")
View Source
var ErrorWrongWireType = errors.New("wrong wire type")

Functions

func Write added in v1.1.0

func Write(out io.Writer, cb func(w *Writer) error) error

Write proto message

Types

type Allocator

type Allocator interface {
	// Alloc returns a byte slice of at least n bytes
	Alloc(n int) []byte
}

Allocator manages byte buffer allocations for protocol buffer parsing

type HeapAllocator

type HeapAllocator struct {
}

HeapAllocator uses Go's built-in memory allocation

func (*HeapAllocator) Alloc

func (a *HeapAllocator) Alloc(n int) []byte

type LinearAllocator

type LinearAllocator struct {
	// contains filtered or unexported fields
}

LinearAllocator uses a single growing buffer for allocations

func NewLinearAllocator

func NewLinearAllocator() *LinearAllocator

NewLinearAllocator creates a linear allocator with initial capacity

func (*LinearAllocator) Alloc

func (a *LinearAllocator) Alloc(n int) []byte

func (*LinearAllocator) Grow

func (a *LinearAllocator) Grow(size int)

Grow pre-allocates buffer space for expected allocations

func (*LinearAllocator) Reset

func (a *LinearAllocator) Reset()

Reset recycles the allocation buffer for reuse

type Option

type Option func(*RawPB)

Option configures RawPB parser behavior

func Begin

func Begin(f func() error) Option

Begin sets a callback function to execute before parsing starts

func Bool

func Bool(num int, f func(bool) error) Option

Bool registers a callback for boolean values decoded from varint-encoded integers

func Bytes

func Bytes(num int, f func([]byte) error) Option

Bytes registers a callback for length-delimited (bytes/string) fields

func Double

func Double(num int, f func(float64) error) Option

Double registers a callback for double-precision floating point numbers using fixed64 encoding

func End

func End(f func() error) Option

End sets a callback function to execute after parsing completes

func Enum

func Enum(num int, f func(int32) error) Option

Enum registers a callback for protocol buffer enum values encoded as varints

func Fixed32

func Fixed32(num int, f func(uint32) error) Option

Fixed32 registers a callback for 32-bit fixed-size fields

func Fixed64

func Fixed64(num int, f func(uint64) error) Option

Fixed64 registers a callback for 64-bit fixed-size fields

func Float

func Float(num int, f func(float32) error) Option

Float registers a callback for single-precision floating point numbers using fixed32 encoding

func Int32

func Int32(num int, f func(int32) error) Option

Int32 registers a callback for signed 32-bit integers using varint encoding

func Int64

func Int64(num int, f func(int64) error) Option

Int64 registers a callback for signed 64-bit integers using varint encoding

func Message

func Message(num int, n *RawPB) Option

Message registers a nested message parser for length-delimited fields

func Name

func Name(name string) Option

Name sets the parser name for error reporting

func Sfixed32

func Sfixed32(num int, f func(int32) error) Option

Sfixed32 registers a callback for signed 32-bit integers using fixed32 encoding

func Sfixed64

func Sfixed64(num int, f func(int64) error) Option

Sfixed64 registers a callback for signed 64-bit integers using fixed64 encoding

func Sint32

func Sint32(num int, f func(int32) error) Option

Sint32 registers a callback for zigzag-encoded signed 32-bit integers

func Sint64

func Sint64(num int, f func(int64) error) Option

Sint64 registers a callback for zigzag-encoded signed 64-bit integers

func String

func String(num int, f func(string) error) Option

String registers a callback for string values from length-delimited fields. Uses unsafe conversion from byte slice to avoid allocation.

func Uint32

func Uint32(num int, f func(uint32) error) Option

Uint32 registers a callback for unsigned 32-bit integers using varint encoding

func Uint64

func Uint64(num int, f func(uint64) error) Option

Uint64 registers a callback for unsigned 64-bit integers using varint encoding

func UnknownBytes

func UnknownBytes(f func(num int, v []byte) error) Option

UnknownBytes handles unregistered length-delimited fields

func UnknownFixed32

func UnknownFixed32(f func(num int, v uint32) error) Option

UnknownFixed32 handles unregistered fixed32 fields

func UnknownFixed64

func UnknownFixed64(f func(num int, v uint64) error) Option

UnknownFixed64 handles unregistered fixed64 fields

func UnknownVarint

func UnknownVarint(f func(num int, v uint64) error) Option

UnknownVarint handles unregistered varint fields

func Varint

func Varint(num int, f func(uint64) error) Option

Varint registers a callback for varint-encoded fields

type RawPB

type RawPB struct {
	// contains filtered or unexported fields
}

RawPB implements a low-level protocol buffer parser without code generation

func New

func New(opts ...Option) *RawPB

New creates a new RawPB parser with optional configuration

func (*RawPB) Parse

func (pb *RawPB) Parse(body []byte) error

Parse decodes protocol buffer data directly from a byte slice

func (*RawPB) Read

func (pb *RawPB) Read(stream Reader, allocator Allocator) error

Read parses protocol buffer data from a stream with memory management via Allocator

type Reader

type Reader interface {
	io.Reader
	io.ByteScanner
}

Reader combines io.Reader with io.ByteScanner for reading protocol buffer data

type Writer added in v1.1.0

type Writer struct {
	// contains filtered or unexported fields
}

Writer implements a low-level protocol buffer writer without code generation

func NewWriter added in v1.1.0

func NewWriter(w io.Writer) *Writer

NewWriter creates a new Writer instance

func (*Writer) Bool added in v1.1.0

func (w *Writer) Bool(num int, v bool)

Bool writes a boolean field

func (*Writer) Bytes added in v1.1.0

func (w *Writer) Bytes(num int, v []byte)

Bytes writes a length-delimited byte slice field

func (*Writer) Double added in v1.1.0

func (w *Writer) Double(num int, v float64)

Double writes a double-precision floating-point field

func (*Writer) Enum added in v1.1.0

func (w *Writer) Enum(num int, v int32)

Enum writes a protocol buffer enum field

func (*Writer) Err added in v1.1.0

func (w *Writer) Err() error

Err ...

func (*Writer) Fixed32 added in v1.1.0

func (w *Writer) Fixed32(num int, v uint32)

Fixed32 writes a 32-bit fixed-size field

func (*Writer) Fixed64 added in v1.1.0

func (w *Writer) Fixed64(num int, v uint64)

Fixed64 writes a 64-bit fixed-size field

func (*Writer) Float added in v1.1.0

func (w *Writer) Float(num int, v float32)

Float writes a single-precision floating-point field

func (*Writer) Int32 added in v1.1.0

func (w *Writer) Int32(num int, v int32)

Int32 writes a signed 32-bit integer field

func (*Writer) Int64 added in v1.1.0

func (w *Writer) Int64(num int, v int64)

Int64 writes a signed 64-bit integer field

func (*Writer) Message added in v1.1.0

func (w *Writer) Message(num int, cb func(w *Writer) error)

Message writes a protocol buffer submessage using a callback function

func (*Writer) Sfixed32 added in v1.1.0

func (w *Writer) Sfixed32(num int, v int32)

Sfixed32 writes a signed 32-bit fixed-size field

func (*Writer) Sfixed64 added in v1.1.0

func (w *Writer) Sfixed64(num int, v int64)

Sfixed64 writes a signed 64-bit fixed-size field

func (*Writer) Sint32 added in v1.1.0

func (w *Writer) Sint32(num int, v int32)

Sint32 writes a signed 32-bit integer field using zigzag encoding

func (*Writer) Sint64 added in v1.1.0

func (w *Writer) Sint64(num int, v int64)

Sint64 writes a signed 64-bit integer field using zigzag encoding

func (*Writer) String added in v1.1.0

func (w *Writer) String(num int, v string)

String writes a string field

func (*Writer) Uint32 added in v1.1.0

func (w *Writer) Uint32(num int, v uint32)

Uint32 writes an unsigned 32-bit integer field

func (*Writer) Uint64 added in v1.1.0

func (w *Writer) Uint64(num int, v uint64)

Uint64 writes an unsigned 64-bit integer field

Jump to

Keyboard shortcuts

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