gpb

package module
v0.0.0-...-0e1a344 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2022 License: MIT Imports: 3 Imported by: 0

README

GoDoc

GPB is a Go package inspired by tidwall/gjson, that provides a fast and simple way to get fields from a protobuf binary message.

Getting Started

Install

Install the package by simply run go get:

➜ go get -u github.com/ywx217/gpb

Performance

Benchmarks of GPB alongside golang/protobuf is in gpb_test.go, each operation gets a single int32 value from the encoded protobuf data.

These benchmarks were run on a MacBook Pro 15" Intel Core i7@2.20GHz, use make bench-compare to reproduce the results.

name description time/op allocs/op allocs/op
GoProtobufTiny-12 Tidy payload using protobuf 140ns ± 2% 52.0B ± 0% 2.00 ± 0%
GpbTiny-12 Tiny payload using gpb 42.9ns ± 8% 0.00B 0.00
GoProtobufSmall-12 Small payload using protobuf 3.82µs ± 4% 2.50kB ± 0% 89.0 ± 0%
GpbSmall-12 Small payload using gpb 77.8ns ± 4% 0.00B 0.00
Test data
Tidy payload, size=2B
GoEnum {
    foo:FOO1
}
Small payload, size=451B
GoTest {
    Kind: TIME
    RequiredField: {
      Label: "label"
      Type: "type"
    }
    RepeatedField: {
      Label: "label"
      Type: "type"
    }
    RepeatedField: {
      Label: "label"
      Type: "type"
    }
    F_Bool_required: true
    F_Int32_required: 3
    F_Int64_required: 6
    F_Fixed32_required: 32
    F_Fixed64_required: 64
    F_Uint32_required: 3232
    F_Uint64_required: 6464
    F_Float_required: 3232
    F_Double_required: 6464
    F_String_required: "string"
    F_Bytes_required: "bytes"
    F_Sint32_required: -32
    F_Sint64_required: -64
    F_Sfixed32_required: -32
    F_Sfixed64_required: -64
    F_Bool_repeated: false
    F_Bool_repeated: true
    F_Int32_repeated: 32
    F_Int32_repeated: 33
    F_Int64_repeated: 64
    F_Int64_repeated: 65
    F_Fixed32_repeated: 3232
    F_Fixed32_repeated: 3333
    F_Fixed64_repeated: 6464
    F_Fixed64_repeated: 6565
    F_Uint32_repeated: 323232
    F_Uint32_repeated: 333333
    F_Uint64_repeated: 646464
    F_Uint64_repeated: 656565
    F_Float_repeated: 32
    F_Float_repeated: 33
    F_Double_repeated: 64
    F_Double_repeated: 65
    F_String_repeated: "hello"
    F_String_repeated: "sailor"
    F_Bytes_repeated: "big"
    F_Bytes_repeated: "nose"
    F_Sint32_repeated: 32
    F_Sint32_repeated: -32
    F_Sint64_repeated: 64
    F_Sint64_repeated: -64
    F_Sfixed32_repeated: 32
    F_Sfixed32_repeated: -32
    F_Sfixed64_repeated: 64
    F_Sfixed64_repeated: -64
    F_Bool_defaulted: true
    F_Int32_defaulted: 32
    F_Int64_defaulted: 64
    F_Fixed32_defaulted: 320
    F_Fixed64_defaulted: 640
    F_Uint32_defaulted: 3200
    F_Uint64_defaulted: 6400
    F_Float_defaulted: 314159
    F_Double_defaulted: 271828
    F_String_defaulted: "hello, \"world!\"\n"
    F_Bytes_defaulted: "Bignose"
    F_Sint32_defaulted: -32
    F_Sint64_defaulted: -64
    F_Sfixed32_defaulted: -32
    F_Sfixed64_defaulted: -64
    RequiredGroup: {
      RequiredField: "required"
    }
    RepeatedGroup: {
      RequiredField: "repeated"
    }
    RepeatedGroup: {
      RequiredField: "repeated"
    }
}

Documentation

Index

Constants

View Source
const InvalidWireType protowire.Type = -1

Variables

View Source
var (
	ErrUnknownWireType  = errors.New("unknown wire type")
	ErrInvalidLength    = errors.New("invalid length")
	ErrEndGroupNotFound = errors.New("end group not found")
)

Functions

This section is empty.

Types

type Result

type Result struct {
	// WireType the fields wire type enumerate
	//   type==0, used for: int32, int64, uint32, uint64, sint32, sint64, bool, enum
	//   type==1, used for: fixed64, sfixed64, double
	//   type==5, used for: fixed32, sfixed32, float
	//   type==2, used for: string, bytes, embedded messages, packed repeated fields
	WireType protowire.Type

	Varint uint64
	Raw    []byte // message without length header
}

func GetAll

func GetAll(pb []byte, pbNumbers ...protowire.Number) []Result

GetAll gets all the values by the given field numbers. `pbNumbers` indicates the path to retrieve the desired field.

func GetOne

func GetOne(pb []byte, pbNumbers ...protowire.Number) Result

GetOne gets the first value by the given field numbers. `pbNumbers` indicates the path to retrieve the desired field. **Attention**: when the field is repeated in proto2, the first item is returned.

In proto3 or proto2 packed mode, the first packed group is returned, and
the UnpackVarint / UnpackFixed32 / UnpackFixed64 should be called to break
a single length-delimited frame into multiple Results.

func (Result) Bool

func (r Result) Bool() bool

func (Result) Bytes

func (r Result) Bytes() []byte

Bytes parses the result as a byte slice. When the wire type is not BytesType, nil will be returned.

func (Result) Exist

func (r Result) Exist() bool

Exist checks if it is a valid result.

func (Result) Fixed32

func (r Result) Fixed32() uint32

func (Result) Fixed64

func (r Result) Fixed64() uint64

Fixed64 parses the 64-bit raw bytes into uint64. When the wire type is not Fixed64Type, it returns 0.

func (Result) Float32

func (r Result) Float32() float32

func (Result) Float64

func (r Result) Float64() float64

Float64 parses the 64-bit raw bytes into float64. When the wire type is not Fixed64Type, it returns 0.

func (Result) GetAll

func (r Result) GetAll(pbNumbers ...protowire.Number) []Result

GetAll unlike GetOne, GetAll returns all the values by the given field numbers.

func (Result) GetIter

func (r Result) GetIter(resultSink func(Result) bool, pbNumbers ...protowire.Number) (err error)

GetIter like GetAll, gets all the values until the resultSink returns false. Both GetOne and GetAll are implemented by this function.

func (Result) GetOne

func (r Result) GetOne(pbNumbers ...protowire.Number) (result Result)

GetOne gets the first field by the given field numbers. `pbNumbers` indicates the path to retrieve the desired field. There is no heap-memory allocation in this function.

**Attention**: when the field is repeated in proto2, the first item is returned.

In proto3 or proto2 packed mode, the first packed group is returned, and
the UnpackVarint / UnpackFixed32 / UnpackFixed64 should be called to break
a single length-delimited frame into multiple Results.

func (Result) Int32

func (r Result) Int32() int32

func (Result) Int64

func (r Result) Int64() int64

func (Result) IterFields

func (r Result) IterFields(pbNumber protowire.Number, resultSink func(r Result) bool) (int, error)

IterFields read through the binary data stored in r.Raw field-by-field, skipping all the fields not interested in.

func (Result) SFixed32

func (r Result) SFixed32() int32

func (Result) SFixed64

func (r Result) SFixed64() int64

SFixed64 parses the 64-bit raw bytes into int64. When the wire type is not Fixed64Type, it returns 0.

func (Result) Sint32

func (r Result) Sint32() int32

func (Result) Sint64

func (r Result) Sint64() int64

func (Result) String

func (r Result) String() string

String parses the result as a string. When the wire type is not BytesType, empty string will be returned.

func (Result) Uint32

func (r Result) Uint32() uint32

func (Result) Uint64

func (r Result) Uint64() uint64

func (Result) Unpack

func (r Result) Unpack(itemType protowire.Type) []Result

Unpack make packed scalars into []Result. In proto2, repeated fields of primitive numeric types can be declared as packed. In proto3, the fields are packed by default.

func (Result) UnpackFixed32

func (r Result) UnpackFixed32() []Result

UnpackFixed32 unpacks the length-delimited fixed32-encoded binary data into separate results.

func (Result) UnpackFixed64

func (r Result) UnpackFixed64() []Result

UnpackFixed64 unpacks the length-delimited fixed64-encoded binary data into separate results.

func (Result) UnpackVarint

func (r Result) UnpackVarint() []Result

UnpackVarint unpacks the length-delimited varint-encoded binary data into separate results.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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