rawpb

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

README

rawpb

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)
> 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

This section is empty.

Types

type Option

type Option func(*RawPB)

func Begin

func Begin(f func() error) Option

func Bool

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

func Bytes

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

func Double

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

func End

func End(f func() error) Option

func Enum

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

func Fixed32

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

func Fixed64

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

func Float

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

func Int32

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

func Int64

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

func Message

func Message(num int, n *RawPB) Option

func Sfixed32

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

func Sfixed64

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

func Sint32

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

func Sint64

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

func String

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

func Uint32

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

func Uint64

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

func UnknownBytes

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

func UnknownFixed32

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

func UnknownFixed64

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

func UnknownVarint

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

func Varint

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

type RawPB

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

func New

func New(opts ...Option) *RawPB

func (*RawPB) Parse

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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