dproto

package module
v1.0.0 Latest Latest
Warning

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

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

README

dproto

GoDoc Go Report Card codecov Build Status

Description

This library allows you to use Protobufs (Google Protocol Buffers) without needing to generate protobuf code and compile it into your project. This means you don't need to compile .proto files.

This library is designed for marshaling and unmarshalling protobufs in a dynamic way. It is designed to interface with any standard protobuf library and is tested against the official protobuf C++ and Golang bindings, in addition to the C Nanopdb library.

The intent of this library was to allow creating long running services that can interpret and interface with clients using new/unknown protobuf messages.

The basic idea is that you construct a ProtoFieldMap that contains any protobuf field number to protobuf type associations you are interested in and then you call DecodeBUffer on the protobuf payload. DecodeBuffer returns an array of FieldValues that it decoded from the payload. Each FieldValue specifies the protobuf field number and value decoded as a Golang primitive(must be inside a interface{}).

Status

Dproto supports all ProtoBuf primitive types. The following is a complete list:

  • int32, int64
  • uint32, uint64
  • sint32, sint64
  • fixed32, fixed64
  • sfixed32, sfixed64
  • float, double
  • bool, string, bytes

Name Explanation

Since we are marshalling and unmarshalling Protobuf messages in a dynamic way, the project is called dproto.

Examples

To give you a little taste of what dproto can do, check out the following example of marshalling and unmarshalling a protobuf.

Say you want to interface with some other program using the following protobuf description:

message LightStatus {
    bool  status    = 1;
    int64 intensity = 2;
}

The following example encodes a new message, prints out the byte stream, and then immediately decodes it.

// Encodes and Decodes a protobuf buffer that would interface with
// a .proto file message containing "bool status =1;" and
// "int64 intensity = 2;".
func ExampleNewProtoFieldMap() {
	// Setup the ProtoFieldMap to interface with the
	// following .proto file:
	//     message LightStatus {
	//         bool  status    = 1;
	//         int64 intensity = 2;
	//     }

	// Setup a FieldMap that holds the type-fieldnumber association
	// This is effectively the information held in a .proto file
	fm := dproto.NewProtoFieldMap()

	// Add Protobuf bool as field 1 ("bool status = 1;")
	if !fm.Add(1, descriptor.FieldDescriptorProto_TYPE_BOOL) {
		panic("Failed to add bool field 1")
	}
	// Add Protobuf int64 as field 2 ("int64 intensity = 2;")
	if !fm.Add(2, descriptor.FieldDescriptorProto_TYPE_INT64) {
		panic("Failed to add bool field 1")
	}

	// Provide some values for our "status" and "intensity"
	values := []dproto.FieldValue{
		dproto.FieldValue{
			Field: 1, // status field number
			Value: bool(true),
		},
		dproto.FieldValue{
			Field: 2, // intensity field number
			Value: int64(10),
		},
	}

	// Encode out values into the protobuf message described in fm
	bytes, err := fm.EncodeBuffer(values)
	if err != nil {
		panic("Error Encoding: " + err.Error())
	}
	fmt.Printf("ProtobufBinary: [ %# x ]\n", bytes)

	// Decode all protobuf fields
	decodedValues, err := fm.DecodeBuffer(bytes)
	if err != nil {
		panic("Error Decoding: " + err.Error())
	}
	for _, val := range decodedValues {
		fmt.Printf("%v: %v\n", val.Field, val.Value)
	}
	// Unordered output:
	// ProtobufBinary: [ 0x08 0x01 0x10 0x0a ]
	// 1: true
	// 2: 10
}

This and other examples can be found in example_test.go.

Documentation

Overview

Package dproto allows for marshalling and unmarshalling of Protobuf messages in a dynamic manor. This means you can interface with new Protobuf libraries or clients during runtime, without compiling.

Dproto currently implements the basic abstraction layer between declared Protobuf message field/types and the field/wiretypes. These associations are expected from the user before marshalling/unmarshalling. It is up to the user how to store and translate these associations to the dproto library.

Note that we say some construct is on the "wire" or has a "wiretype", when it refers the bits in a marshalled buffer. See the following link for more information on wire types: https://developers.google.com/protocol-buffers/docs/encoding

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrInvalidProtoBufType = errors.New("Invalid protobuf type")

ErrInvalidProtoBufType is returned when an invalid Protobuf type was specified

View Source
var ErrMalformedProtoBuf = errors.New("Malformed protobuf buffer")

ErrMalformedProtoBuf is returned when some operation has determined that that a message field/types association does not agree with a message

View Source
var ErrMessageFieldMissing = errors.New("Message field not found")

ErrMessageFieldMissing is returned when some messgae get could not find the specified field

Functions

func ParseAs

func ParseAs(value string, pbtype descriptor.FieldDescriptorProto_Type, base int) (interface{}, error)

ParseAs parses the string as the specified Protobuf type

func ParseProtobufType

func ParseProtobufType(typ string) (descriptor.FieldDescriptorProto_Type, bool)

ParseProtobufType returns the Protobuf type enumeration of the given string

Types

type FieldNum

type FieldNum uint64

FieldNum represents Protobuf's field numbers

type FieldNumInfo added in v0.1.0

type FieldNumInfo struct {
	Field FieldNum
	Index int
}

FieldNum represents Protobuf's field repeated numbers and index

type FieldValue

type FieldValue struct {
	Field FieldNum
	Value interface{}
}

FieldValue represents deserialized value with it's associated field number

type ProtoFieldMap

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

ProtoFieldMap associates field numbers with it's high-level Protobuf type.

func NewProtoFieldMap

func NewProtoFieldMap() *ProtoFieldMap

NewProtoFieldMap create a new ProtoFieldMap object.

Example

Encodes and Decodes a protobuf buffer that would interface with a .proto file message containing "bool status =1;" and "int64 intensity = 2;".

package main

import (
	"fmt"

	"github.com/czar31/dproto"
	"github.com/golang/protobuf/protoc-gen-go/descriptor"
)

func main() {
	// Setup the ProtoFieldMap to interface with the
	// following .proto file:
	//     message LightStatus {
	//         bool  status    = 1;
	//         int64 intensity = 2;
	//     }

	// Setup a FieldMap that holds the type-fieldnumber association
	// This is effectively the information held in a .proto file
	fm := dproto.NewProtoFieldMap()

	// Add Protobuf bool as field 1 ("bool status = 1;")
	if !fm.Add(1, descriptor.FieldDescriptorProto_TYPE_BOOL) {
		panic("Failed to add bool field 1")
	}
	// Add Protobuf int64 as field 2 ("int64 intensity = 2;")
	if !fm.Add(2, descriptor.FieldDescriptorProto_TYPE_INT64) {
		panic("Failed to add bool field 1")
	}

	// Provide some values for our "status" and "intensity"
	values := []dproto.FieldValue{
		{
			Field: 1, // status field number
			Value: bool(true),
		},
		{
			Field: 2, // intensity field number
			Value: int64(10),
		},
	}

	// Encode out values into the protobuf message described in fm
	bytes, err := fm.EncodeBuffer(values)
	if err != nil {
		panic("Error Encoding: " + err.Error())
	}
	fmt.Printf("ProtobufBinary: [ %# x ]\n", bytes)

	// Decode all protobuf fields
	decodedValues, err := fm.DecodeBuffer(bytes)
	if err != nil {
		panic("Error Decoding: " + err.Error())
	}
	for _, val := range decodedValues {
		fmt.Printf("%v: %v\n", val.Field, val.Value)
	}
}
Output:

ProtobufBinary: [ 0x08 0x01 0x10 0x0a ]
1: true
2: 10

func (*ProtoFieldMap) Add

Add adds a Field-Type association to a ProtoFieldMap

func (*ProtoFieldMap) DecodeBuffer

func (fm *ProtoFieldMap) DecodeBuffer(buf []byte) ([]FieldValue, error)

DecodeBuffer will unmarshal and decode all fields in the specified buffer using the current ProtoFieldMap

func (*ProtoFieldMap) DecodeMessage

func (fm *ProtoFieldMap) DecodeMessage(m *WireMessage) ([]FieldValue, error)

DecodeMessage will decode all fields in the specified message using the current ProtoFieldMap

func (*ProtoFieldMap) EncodeBuffer

func (fm *ProtoFieldMap) EncodeBuffer(values []FieldValue) ([]byte, error)

EncodeBuffer will marshal and encode all fields given. The output is a raw buffer.

func (*ProtoFieldMap) EncodeMessage

func (fm *ProtoFieldMap) EncodeMessage(values []FieldValue) (*WireMessage, error)

EncodeMessage will marshal and encode all fields given. The output is a new message.

func (*ProtoFieldMap) Get

Get gets the Protobuf type associated with the field number

func (*ProtoFieldMap) Print

func (fm *ProtoFieldMap) Print()

Print shows the ProtoFieldMap to the user for debugging purposes.

func (*ProtoFieldMap) RemoveByField

func (fm *ProtoFieldMap) RemoveByField(field FieldNum) (ok bool)

RemoveByField removes the Field-Type association from a ProtoFieldMap that has the specified field number. It returns true if the association was found and removed, false otherwise

func (*ProtoFieldMap) RemoveByType

RemoveByType removes all field Field-Type association from a ProtoFieldMap that has the specified type. This will check all map entries. It returns true if an association was found and removed, false otherwise

func (*ProtoFieldMap) Reset

func (fm *ProtoFieldMap) Reset()

Reset clears the stored associations inside a ProtoFieldMap

type WireFixed32

type WireFixed32 uint64 // DecodeFixed32 gives uint64

WireFixed32 represents the Protobuf fixed32 wire type

func (WireFixed32) AsFixed32

func (v WireFixed32) AsFixed32() uint32

AsFixed32 returns the wiretype interpreted as a Protobuf fixed32

func (WireFixed32) AsFloat

func (v WireFixed32) AsFloat() float32

AsFloat returns the wiretype interpreted as a Protobuf float

func (WireFixed32) AsSfixed32

func (v WireFixed32) AsSfixed32() int32

AsSfixed32 returns the wiretype interpreted as a Protobuf sfixed32

func (*WireFixed32) FromFixed32

func (v *WireFixed32) FromFixed32(i uint32) WireFixed32

FromFixed32 sets the wiretype from a Protobuf fixed32

func (*WireFixed32) FromFloat

func (v *WireFixed32) FromFloat(i float32) WireFixed32

FromFloat sets the wiretype from a Protobuf float

func (*WireFixed32) FromSfixed32

func (v *WireFixed32) FromSfixed32(i int32) WireFixed32

FromSfixed32 sets the wiretype from a Protobuf fixed32

type WireFixed64

type WireFixed64 uint64

WireFixed64 represents the Protobuf fixed64 wire type

func (WireFixed64) AsDouble

func (v WireFixed64) AsDouble() float64

AsDouble returns the wiretype interpreted as a Protobuf double

func (WireFixed64) AsFixed64

func (v WireFixed64) AsFixed64() uint64

AsFixed64 returns the wiretype interpreted as a Protobuf fixed64

func (WireFixed64) AsSfixed64

func (v WireFixed64) AsSfixed64() int64

AsSfixed64 returns the wiretype interpreted as a Protobuf sfixed64

func (*WireFixed64) FromDouble

func (v *WireFixed64) FromDouble(i float64) WireFixed64

FromDouble sets the wiretype from a Protobuf double

func (*WireFixed64) FromFixed64

func (v *WireFixed64) FromFixed64(i uint64) WireFixed64

FromFixed64 sets the wiretype from a Protobuf fixed64

func (*WireFixed64) FromSfixed64

func (v *WireFixed64) FromSfixed64(i int64) WireFixed64

FromSfixed64 sets the wiretype from a Protobuf fixed64

type WireMessage

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

WireMessage holds the data elements of a marshalled Protobuf message. A marshalled Protobuf message is simply the concatenation of all the below key-values, where the key is the field number and the value is is converted to a wiretype.

This design is not exactly efficient for sending the fields in FieldNum order. For this reason, this implementation may be changed out in a later date. Sending fields in numerical order is recommended on the Protobuf website.

func NewWireMessage

func NewWireMessage() *WireMessage

NewWireMessage creates a new Wiremessage object.

Example
package main

import (
	"fmt"

	"github.com/czar31/dproto"
)

func main() {
	m := dproto.NewWireMessage()

	// Add the following bool and int64
	m.EncodeBool(1, true)
	m.EncodeInt64(2, 10)

	bytes, err := m.Marshal()
	if err != nil {
		panic("Error Marshaling: " + err.Error())
	}
	fmt.Printf("ProtobufBinary: [ %# x ]\n", bytes)
}
Output:

ProtobufBinary: [ 0x08 0x01 0x10 0x0a ]

func Unmarshal

func Unmarshal(buf []byte) (*WireMessage, error)

Unmarshal will unmarshal a byte array into a WireMessage

func (*WireMessage) AddBytes

func (m *WireMessage) AddBytes(field FieldNum, buf []byte)

AddBytes adds a byte buffer wiretype to the wire message m

func (*WireMessage) AddFixed32

func (m *WireMessage) AddFixed32(field FieldNum, value WireFixed32)

AddFixed32 adds a WireFixed32 wiretype to the wire message m

func (*WireMessage) AddFixed64

func (m *WireMessage) AddFixed64(field FieldNum, value WireFixed64)

AddFixed64 adds a WireFixed64 wiretype to the wire message m

func (*WireMessage) AddVarint

func (m *WireMessage) AddVarint(field FieldNum, value WireVarint)

AddVarint adds a WireVarint wiretype to the wire message m

func (*WireMessage) DecodeAs

func (m *WireMessage) DecodeAs(field FieldNum, index int, pbtype descriptor.FieldDescriptorProto_Type) (val interface{}, err error)

DecodeAs fetches the field from m and decodes it as the specified Protobuf type

func (*WireMessage) DecodeBool

func (m *WireMessage) DecodeBool(field FieldNum, index int) (bool, bool)

DecodeBool fetches the field from m and decodes it as a Protobuf bool

func (*WireMessage) DecodeBytes

func (m *WireMessage) DecodeBytes(field FieldNum, index int) ([]byte, bool)

DecodeBytes fetches the field from m and decodes it as a Protobuf bytes type

func (*WireMessage) DecodeDouble

func (m *WireMessage) DecodeDouble(field FieldNum, index int) (float64, bool)

DecodeDouble fetches the field and decodes it as a Protobuf double

func (*WireMessage) DecodeFixed32

func (m *WireMessage) DecodeFixed32(field FieldNum, index int) (uint32, bool)

DecodeFixed32 fetches the field from m and decodes it as a Protobuf fixed32

func (*WireMessage) DecodeFixed64

func (m *WireMessage) DecodeFixed64(field FieldNum, index int) (uint64, bool)

DecodeFixed64 fetches the field from m and decodes it as a Protobuf fixed64

func (*WireMessage) DecodeFloat

func (m *WireMessage) DecodeFloat(field FieldNum, index int) (float32, bool)

DecodeFloat fetches the field from m and decodes it as a Protobuf float

func (*WireMessage) DecodeInt32

func (m *WireMessage) DecodeInt32(field FieldNum, index int) (int32, bool)

DecodeInt32 fetches the wiretype field and decodes it as a Protobuf int32

func (*WireMessage) DecodeInt64

func (m *WireMessage) DecodeInt64(field FieldNum, index int) (int64, bool)

DecodeInt64 fetches the field from m and decodes it as a Protobuf int64

func (*WireMessage) DecodeMessage

func (m *WireMessage) DecodeMessage(field FieldNum, index int) (*WireMessage, error)

DecodeMessage fetches the field from m and decodes it as an embedded message

func (*WireMessage) DecodeSfixed32

func (m *WireMessage) DecodeSfixed32(field FieldNum, index int) (int32, bool)

DecodeSfixed32 fetches the field from m and decodes it as a Protobuf sfixed32

func (*WireMessage) DecodeSfixed64

func (m *WireMessage) DecodeSfixed64(field FieldNum, index int) (int64, bool)

DecodeSfixed64 fetches the field from m and decodes it as a Protobuf sfixed64

func (*WireMessage) DecodeSint32

func (m *WireMessage) DecodeSint32(field FieldNum, index int) (int32, bool)

DecodeSint32 fetches the field from m and decodes it as a Protobuf sint32

func (*WireMessage) DecodeSint64

func (m *WireMessage) DecodeSint64(field FieldNum, index int) (int64, bool)

DecodeSint64 fetches the field from m and decodes it as a Protobuf sint64

func (*WireMessage) DecodeString

func (m *WireMessage) DecodeString(field FieldNum, index int) (string, bool)

DecodeString fetches the field from m and decodes it as a Protobuf string

func (*WireMessage) DecodeUint32

func (m *WireMessage) DecodeUint32(field FieldNum, index int) (uint32, bool)

DecodeUint32 fetches the field from m and decodes it as a Protobuf uint32

func (*WireMessage) DecodeUint64

func (m *WireMessage) DecodeUint64(field FieldNum, index int) (uint64, bool)

DecodeUint64 fetches the field from m and decodes it as a Protobuf uint64

func (*WireMessage) EncodeAs

func (m *WireMessage) EncodeAs(field FieldNum, value interface{}, pbtype descriptor.FieldDescriptorProto_Type) error

EncodeAs adds value to the WireMessage encoded as the specified Protobuf type

Errors will ensue if the generic type is not compatible with the specified Protobuf type.

func (*WireMessage) EncodeBool

func (m *WireMessage) EncodeBool(field FieldNum, value bool)

EncodeBool adds value to the WireMessage encoded as a Protobuf bool

func (*WireMessage) EncodeBytes

func (m *WireMessage) EncodeBytes(field FieldNum, value []byte)

EncodeBytes adds value to the WireMessage encoded as a Protobuf bytes type

func (*WireMessage) EncodeDouble

func (m *WireMessage) EncodeDouble(field FieldNum, value float64)

EncodeDouble fetches the field and decodes it as a Protobuf double

func (*WireMessage) EncodeFixed32

func (m *WireMessage) EncodeFixed32(field FieldNum, value uint32)

EncodeFixed32 adds value to the WireMessage encoded as a Protobuf fixed32

func (*WireMessage) EncodeFixed64

func (m *WireMessage) EncodeFixed64(field FieldNum, value uint64)

EncodeFixed64 adds value to the WireMessage encoded as a Protobuf fixed64

func (*WireMessage) EncodeFloat

func (m *WireMessage) EncodeFloat(field FieldNum, value float32)

EncodeFloat adds value to the WireMessage encoded as a Protobuf float

func (*WireMessage) EncodeInt32

func (m *WireMessage) EncodeInt32(field FieldNum, value int32)

EncodeInt32 adds value to the WireMessage encoded as a Protobuf int32

func (*WireMessage) EncodeInt64

func (m *WireMessage) EncodeInt64(field FieldNum, value int64)

EncodeInt64 adds value to the WireMessage encoded as a Protobuf int64

func (*WireMessage) EncodeMessage

func (m *WireMessage) EncodeMessage(field FieldNum, value *WireMessage) error

EncodeMessage adds value to the WireMessage encoded as an embedded message

func (*WireMessage) EncodeSfixed32

func (m *WireMessage) EncodeSfixed32(field FieldNum, value int32)

EncodeSfixed32 adds value to the WireMessage encoded as a Protobuf sfixed32

func (*WireMessage) EncodeSfixed64

func (m *WireMessage) EncodeSfixed64(field FieldNum, value int64)

EncodeSfixed64 adds value to the WireMessage encoded as a Protobuf sfixed64

func (*WireMessage) EncodeSint32

func (m *WireMessage) EncodeSint32(field FieldNum, value int32)

EncodeSint32 adds value to the WireMessage encoded as a Protobuf sint32

func (*WireMessage) EncodeSint64

func (m *WireMessage) EncodeSint64(field FieldNum, value int64)

EncodeSint64 adds value to the WireMessage encoded as a Protobuf sint64

func (*WireMessage) EncodeString

func (m *WireMessage) EncodeString(field FieldNum, value string)

EncodeString adds value to the WireMessage encoded as a Protobuf string

func (*WireMessage) EncodeUint32

func (m *WireMessage) EncodeUint32(field FieldNum, value uint32)

EncodeUint32 adds value to the WireMessage encoded as a Protobuf uint32

func (*WireMessage) EncodeUint64

func (m *WireMessage) EncodeUint64(field FieldNum, value uint64)

EncodeUint64 adds value to the WireMessage encoded as a Protobuf uint64

func (*WireMessage) GetBytes

func (m *WireMessage) GetBytes(field FieldNum, index int) ([]byte, bool)

GetBytes fetches a byte array wire field from m

func (*WireMessage) GetField

func (m *WireMessage) GetField(field FieldNum, index int) (interface{}, bool)

GetField fetches the raw wire field from m and returns it as the proper wire type

func (*WireMessage) GetFieldCount

func (m *WireMessage) GetFieldCount() int

GetFieldCount gets the number of fields in the WireMessage

func (*WireMessage) GetFieldNumInfos added in v0.1.0

func (m *WireMessage) GetFieldNumInfos() []FieldNumInfo

GetFieldNumInfos gets all field numbers contained in the WireMessage

func (*WireMessage) GetFixed32

func (m *WireMessage) GetFixed32(field FieldNum, index int) (WireFixed32, bool)

GetFixed32 fetches a fixed32 wire field from m

func (*WireMessage) GetFixed64

func (m *WireMessage) GetFixed64(field FieldNum, index int) (WireFixed64, bool)

GetFixed64 fetches a fixed64 wire field from m

func (*WireMessage) GetVarint

func (m *WireMessage) GetVarint(field FieldNum, index int) (WireVarint, bool)

GetVarint fetches a varint wire field from m

func (*WireMessage) Marshal

func (m *WireMessage) Marshal() ([]byte, error)

Marshal generates the byte stream for a given WireMessage

Example
package main

import (
	"fmt"

	"github.com/czar31/dproto"
)

func main() {
	m := dproto.NewWireMessage()

	// Add the following bool and int64
	m.EncodeBool(1, true)
	m.EncodeInt64(2, 10)

	bytes, err := m.Marshal()
	if err != nil {
		panic("Error Marshaling: " + err.Error())
	}
	fmt.Printf("ProtobufBinary: [ %# x ]\n", bytes)
}
Output:

ProtobufBinary: [ 0x08 0x01 0x10 0x0a ]

func (*WireMessage) Remove

func (m *WireMessage) Remove(field FieldNum)

Remove removes the wiretype field previously added

func (*WireMessage) Reset

func (m *WireMessage) Reset()

Reset clears the WireMessage m

func (*WireMessage) Unmarshal

func (m *WireMessage) Unmarshal(buf []byte) error

Unmarshal sorts a ProtoBuf message into it's constituent parts to be such that it's field can be accessed in constant time

This implementation has been adapted from the proto.Buffer.DebugPrint()

Example
package main

import (
	"fmt"

	"github.com/czar31/dproto"
)

func main() {
	m := dproto.NewWireMessage()

	// Add the following bool and int64
	m.EncodeBool(1, true)
	m.EncodeInt64(2, 10)

	bytes, err := m.Marshal()
	if err != nil {
		panic("Error Marshaling: " + err.Error())
	}
	fmt.Printf("ProtobufBinary: [ %# x ]\n", bytes)

	// Unmarshal the already marshalled bytes
	m, err = dproto.Unmarshal(bytes)
	if err != nil {
		panic("Error Unmarshaling: " + err.Error())
	}

	if bVal, ok := m.DecodeBool(1, 0); ok {
		fmt.Println(bVal)
	} else {
		fmt.Println("No bool field 1")
	}

	if iVal, ok := m.DecodeInt64(2, 0); ok {
		fmt.Println(iVal)
	} else {
		fmt.Println("No int64 field 2")
	}
}
Output:

ProtobufBinary: [ 0x08 0x01 0x10 0x0a ]
true
10
Example (Abstract)

This example shows how to abstractly Marshal and Unmarshal protobuf messages. It should be noted that the ProtoFieldMap class already implements this behavior for you.

package main

import (
	"fmt"

	"github.com/czar31/dproto"
	"github.com/golang/protobuf/protoc-gen-go/descriptor"
)

func main() {

	types := []descriptor.FieldDescriptorProto_Type{
		descriptor.FieldDescriptorProto_TYPE_BOOL,
		descriptor.FieldDescriptorProto_TYPE_INT64,
	}
	values := []interface{}{
		bool(true),
		int64(10),
	}

	m := dproto.NewWireMessage()

	// Add the following bool and int64
	for index, value := range values {
		err := m.EncodeAs(dproto.FieldNum(index+1), value, types[index])
		if err != nil {
			panic(err)
		}
	}

	// Marshal the message
	bytes, err := m.Marshal()
	if err != nil {
		panic("Error Marshaling: " + err.Error())
	}
	fmt.Printf("ProtobufBinary: [ %# x ]\n", bytes)

	// Unmarshal the already marshalled bytes
	m, err = dproto.Unmarshal(bytes)
	if err != nil {
		panic("Error Unmarshaling: " + err.Error())
	}

	// Decode each field and print
	for index, typ := range types {
		val, err := m.DecodeAs(dproto.FieldNum(index+1), 0, typ)
		if err != nil {
			fmt.Println(err)
		} else {
			fmt.Println(val)
		}
	}
}
Output:

ProtobufBinary: [ 0x08 0x01 0x10 0x0a ]
true
10

type WireType

type WireType uint8 // only uses 3 bits on wire

WireType represents Protobuf's 3 bit wiretype identifier used in a marshalled packet.

type WireVarint

type WireVarint uint64

WireVarint represents the Protobuf varint wire type

func (WireVarint) AsBool

func (v WireVarint) AsBool() bool

AsBool returns the wiretype interpreted as a Protobuf bool

func (WireVarint) AsEnum

func (v WireVarint) AsEnum() uint64

AsEnum returns the wiretype interpreted as a Protobuf enum (just a uint)

func (WireVarint) AsInt32

func (v WireVarint) AsInt32() int32

AsInt32 returns the wiretype interpreted as a Protobuf int32

func (WireVarint) AsInt64

func (v WireVarint) AsInt64() int64

AsInt64 returns the wiretype interpreted as a Protobuf int64

func (WireVarint) AsSint32

func (v WireVarint) AsSint32() int32

AsSint32 returns the wiretype interpreted as a Protobuf sint32

func (WireVarint) AsSint64

func (v WireVarint) AsSint64() int64

AsSint64 returns the wiretype interpreted as a Protobuf sint64

func (WireVarint) AsTag

func (v WireVarint) AsTag() (FieldNum, WireType)

AsTag returns the field number and field type encoded in the varint as a tag

This is a low level method for parsing the raw protobuf buffer

func (WireVarint) AsUint32

func (v WireVarint) AsUint32() uint32

AsUint32 returns the wiretype interpreted as a Protobuf uint32

func (WireVarint) AsUint64

func (v WireVarint) AsUint64() uint64

AsUint64 returns the wiretype interpreted as a Protobuf uint64

func (*WireVarint) FromBool

func (v *WireVarint) FromBool(i bool) WireVarint

FromBool sets the wiretype from a Protobuf bool

func (*WireVarint) FromEnum

func (v *WireVarint) FromEnum(i uint64) WireVarint

FromEnum sets the wiretype from a Protobuf enum

func (*WireVarint) FromInt32

func (v *WireVarint) FromInt32(i int32) WireVarint

FromInt32 sets the wiretype from a Protobuf int32

func (*WireVarint) FromInt64

func (v *WireVarint) FromInt64(i int64) WireVarint

FromInt64 sets the wiretype from a Protobuf int64

func (*WireVarint) FromSint32

func (v *WireVarint) FromSint32(i int32) WireVarint

FromSint32 sets the wiretype from a Protobuf sint32

func (*WireVarint) FromSint64

func (v *WireVarint) FromSint64(i int64) WireVarint

FromSint64 sets the wiretype from a Protobuf sint64

func (*WireVarint) FromTag

func (v *WireVarint) FromTag(field FieldNum, wire WireType)

FromTag combines the field number and wire type to form the message field tag

This is a low level method for parsing the raw protobuf buffer

func (*WireVarint) FromUint32

func (v *WireVarint) FromUint32(i uint32) WireVarint

FromUint32 sets the wiretype from a Protobuf uint32

func (*WireVarint) FromUint64

func (v *WireVarint) FromUint64(i uint64) WireVarint

FromUint64 sets the wiretype from a Protobuf uint64

Jump to

Keyboard shortcuts

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