proto

package
Version: v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2022 License: MIT Imports: 14 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrWireTypeUnknown = errors.New("unknown wire type")
)

Functions

func DecodeTag

func DecodeTag(tag uint64) (FieldNumber, WireType)

DecodeTag reverses the encoding applied by EncodeTag.

func DecodeZigZag

func DecodeZigZag(v uint64) int64

DecodeZigZag reverses the encoding applied by EncodeZigZag.

func EncodeTag

func EncodeTag(f FieldNumber, t WireType) uint64

EncodeTag encodes a pair of field number and wire type into a protobuf tag.

func EncodeZigZag

func EncodeZigZag(v int64) uint64

EncodeZigZag returns v as a zig-zag encoded value.

func Marshal

func Marshal(v interface{}) ([]byte, error)

func MarshalTo

func MarshalTo(b []byte, v interface{}) (int, error)

func Parse

func Scan

func Scan(b []byte, fn func(FieldNumber, WireType, RawValue) (bool, error)) error

Scan calls fn for each protobuf field in the message b.

The iteration stops when all fields have been scanned, fn returns false, or an error is seen.

func Size

func Size(v interface{}) int

func Unmarshal

func Unmarshal(b []byte, v interface{}) error

Types

type Field

type Field struct {
	Index    int
	Number   FieldNumber
	Name     string
	Type     Type
	Repeated bool
}

type FieldNumber

type FieldNumber uint

FieldNumber represents a protobuf field number.

func (FieldNumber) Bool

func (f FieldNumber) Bool(v bool) RawMessage

func (FieldNumber) Bytes

func (f FieldNumber) Bytes(v []byte) RawMessage

func (FieldNumber) Fixed32

func (f FieldNumber) Fixed32(v uint32) RawMessage

func (FieldNumber) Fixed64

func (f FieldNumber) Fixed64(v uint64) RawMessage

func (FieldNumber) Float32

func (f FieldNumber) Float32(v float32) RawMessage

func (FieldNumber) Float64

func (f FieldNumber) Float64(v float64) RawMessage

func (FieldNumber) Int

func (f FieldNumber) Int(v int) RawMessage

func (FieldNumber) Int32

func (f FieldNumber) Int32(v int32) RawMessage

func (FieldNumber) Int64

func (f FieldNumber) Int64(v int64) RawMessage

func (FieldNumber) String

func (f FieldNumber) String(v string) RawMessage

func (FieldNumber) Uint

func (f FieldNumber) Uint(v uint) RawMessage

func (FieldNumber) Uint32

func (f FieldNumber) Uint32(v uint32) RawMessage

func (FieldNumber) Uint64

func (f FieldNumber) Uint64(v uint64) RawMessage

func (FieldNumber) Value

func (f FieldNumber) Value(v interface{}) RawMessage

Value constructs a RawMessage for field number f from v.

type Kind

type Kind int

Kind is an enumeration representing the various data types supported by the protobuf language.

const (
	Bool Kind = iota
	Int32
	Int64
	Sint32
	Sint64
	Uint32
	Uint64
	Fix32
	Fix64
	Sfix32
	Sfix64
	Float
	Double
	String
	Bytes
	Map
	Struct
)

type Message

type Message interface {
	// Size is the size of the protobuf representation (in bytes).
	Size() int

	// Marshal writes the message to the byte slice passed as argument.
	Marshal([]byte) error

	// Unmarshal reads the message from the byte slice passed as argument.
	Unmarshal([]byte) error
}

Message is an interface implemented by types that supported being encoded to and decoded from protobuf.

type MessageRewriter

type MessageRewriter []Rewriter

MessageRewriter maps field numbers to rewrite rules, satisfying the Rewriter interace to support composing rewrite rules.

func (MessageRewriter) Rewrite

func (r MessageRewriter) Rewrite(out, in []byte) ([]byte, error)

Rewrite applies the rewrite rule matching f in r, satisfies the Rewriter interface.

type RawMessage

type RawMessage []byte

RawMessage represents a raw protobuf-encoded message.

func Append

func Append(m RawMessage, f FieldNumber, t WireType, v []byte) RawMessage

func AppendFixed32

func AppendFixed32(m RawMessage, f FieldNumber, v uint32) RawMessage

func AppendFixed64

func AppendFixed64(m RawMessage, f FieldNumber, v uint64) RawMessage

func AppendVarint

func AppendVarint(m RawMessage, f FieldNumber, v uint64) RawMessage

func AppendVarlen

func AppendVarlen(m RawMessage, f FieldNumber, v []byte) RawMessage

func (RawMessage) Marshal

func (m RawMessage) Marshal(b []byte) error

Marshal satisfies the Message interface.

func (RawMessage) Rewrite

func (m RawMessage) Rewrite(out, _ []byte) ([]byte, error)

Rewrite satisfies the Rewriter interface.

func (RawMessage) Size

func (m RawMessage) Size() int

Size satisfies the Message interface.

func (*RawMessage) Unmarshal

func (m *RawMessage) Unmarshal(b []byte) error

Unmarshal satisfies the Message interface.

type RawValue

type RawValue []byte

RawValue represents a single protobuf value.

RawValue instances are returned by Parse and share the backing array of the RawMessage that they were decoded from.

func (RawValue) Fixed32

func (v RawValue) Fixed32() uint32

Fixed32 decodes v as a fixed32.

The content of v will always be a valid fixed32 if v was returned by a call to Parse and the associated wire type was Fixed32. In other cases, the behavior of Fixed32 is undefined.

func (RawValue) Fixed64

func (v RawValue) Fixed64() uint64

Fixed64 decodes v as a fixed64.

The content of v will always be a valid fixed64 if v was returned by a call to Parse and the associated wire type was Fixed64. In other cases, the behavior of Fixed64 is undefined.

func (RawValue) Varint

func (v RawValue) Varint() uint64

Varint decodes v as a varint.

The content of v will always be a valid varint if v was returned by a call to Parse and the associated wire type was Varint. In other cases, the behavior of Varint is undefined.

type RewriteFunc

type RewriteFunc func([]byte, []byte) ([]byte, error)

RewriteFunc is a function type implementing the Rewriter interface.

func (RewriteFunc) Rewrite

func (r RewriteFunc) Rewrite(out, in []byte) ([]byte, error)

Rewrite satisfies the Rewriter interface.

type Rewriter

type Rewriter interface {
	// The function is expected to append the new content to the byte slice
	// passed as argument. If it wasn't able to perform the rewrite, it must
	// return a non-nil error.
	Rewrite(out, in []byte) ([]byte, error)
}

Rewriter is an interface implemented by types that support rewriting protobuf messages.

func MultiRewriter

func MultiRewriter(rewriters ...Rewriter) Rewriter

MultiRewriter constructs a Rewriter which applies all rewriters passed as arguments.

func ParseRewriteTemplate

func ParseRewriteTemplate(typ Type, jsonTemplate []byte) (Rewriter, error)

ParseRewriteTemplate constructs a Rewriter for a protobuf type using the given json template to describe the rewrite rules.

The json template contains a representation of the

type Type

type Type interface {
	// Returns a human-readable representation of the type.
	String() string

	// Returns the name of the type.
	Name() string

	// Kind returns the kind of protobuf values that are represented.
	Kind() Kind

	// When the Type represents a protobuf map, calling this method returns the
	// type of the map keys.
	//
	// If the Type is not a map type, the method panics.
	Key() Type

	// When the Type represents a protobuf map, calling this method returns the
	// type of the map values.
	//
	// If the Type is not a map type, the method panics.
	Elem() Type

	// Returns the protobuf wire type for the Type it is called on.
	WireType() WireType

	// Returns the number of fields in the protobuf message.
	//
	// If the Type does not represent a struct type, the method returns zero.
	NumField() int

	// Returns the Field at the given in Type.
	//
	// If the Type does not represent a struct type, the method panics.
	Field(int) Field

	// Returns the Field with the given name in Type.
	//
	// If the Type does not represent a struct type, or if the field does not
	// exist, the method panics.
	FieldByName(string) Field

	// Returns the Field with the given number in Type.
	//
	// If the Type does not represent a struct type, or if the field does not
	// exist, the method panics.
	FieldByNumber(FieldNumber) Field

	// For unsigned types, convert to their zig-zag form.
	//
	// The method uses the following table to perform the conversion:
	//
	//  base    | zig-zag
	//	--------+---------
	//	int32   | sint32
	//	int64   | sint64
	//	uint32  | sint32
	//	uint64  | sint64
	//	fixed32 | sfixed32
	//	fixed64 | sfixed64
	//
	// If the type cannot be converted to a zig-zag type, the method panics.
	ZigZag() Type
}

Type is an interface similar to reflect.Type. Values implementing this interface represent high level protobuf types.

Type values are safe to use concurrently from multiple goroutines.

Types are comparable value.

func TypeOf

func TypeOf(t reflect.Type) Type

TypeOf returns the protobuf type used to represent a go type.

The function uses the following table to map Go types to Protobuf:

Go      | Protobuf
--------+---------
bool    | bool
int     | int64
int32   | int32
int64   | int64
uint    | uint64
uint32  | uint32
uint64  | uint64
float32 | float
float64 | double
string  | string
[]byte  | bytes
map     | map
struct  | message

Pointer types are also supported and automatically dereferenced.

type UnmarshalFieldError

type UnmarshalFieldError struct {
	FieldNumer int
	WireType   int
	Err        error
}

func (*UnmarshalFieldError) Error

func (e *UnmarshalFieldError) Error() string

func (*UnmarshalFieldError) Unwrap

func (e *UnmarshalFieldError) Unwrap() error

type WireType

type WireType uint

The WireType enumeration represents the different protobuf wire types.

const (
	Varint  WireType = 0
	Fixed64 WireType = 1
	Varlen  WireType = 2
	Fixed32 WireType = 5
)

func (WireType) String

func (wt WireType) String() string

Directories

Path Synopsis
fixtures module

Jump to

Keyboard shortcuts

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