protoenc

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2023 License: MPL-2.0 Imports: 14 Imported by: 2

Documentation

Overview

Package protoenc provides a way to marshal and unmarshal Go structs tp protocol buffers.

Example (Protobuf)

This example defines, encodes, and decodes a Person message format equivalent to the example used in the Protocol Buffers overview.

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package main

import (
	"encoding/hex"
	"fmt"
	"reflect"

	"github.com/siderolabs/protoenc"
)

// Go-based protobuf definition for the example Person message format
//
//nolint:govet
type Person struct {
	Name  string        `protobuf:"1"`
	ID    int32         `protobuf:"2"`
	Email *string       `protobuf:"3"`
	Phone []PhoneNumber `protobuf:"4"`
}

type PhoneType uint32

const (
	MOBILE PhoneType = iota
	HOME
	WORK
)

//nolint:govet
type PhoneNumber struct {
	Number string     `protobuf:"1"`
	Type   *PhoneType `protobuf:"2"`
}

// This example defines, encodes, and decodes a Person message format
// equivalent to the example used in the Protocol Buffers overview.
//
//nolint:nosnakecase
func main() {
	// Create a Person record
	email := "alice@somewhere"
	ptype := WORK
	person := Person{
		"Alice",
		123,
		&email,
		[]PhoneNumber{
			{"111-222-3333", nil},
			{"444-555-6666", &ptype},
		},
	}

	// Encode it
	buf, err := protoenc.Marshal(&person)
	if err != nil {
		panic("Encode failed: " + err.Error())
	}

	fmt.Print(hex.Dump(buf))

	// Decode it
	person2 := Person{}
	if err = protoenc.Unmarshal(buf, &person2); err != nil {
		panic(err)
	}

	if !reflect.DeepEqual(person, person2) {
		panic("Decode failed")
	}

}
Output:

00000000  0a 05 41 6c 69 63 65 10  7b 1a 0f 61 6c 69 63 65  |..Alice.{..alice|
00000010  40 73 6f 6d 65 77 68 65  72 65 22 0e 0a 0c 31 31  |@somewhere"...11|
00000020  31 2d 32 32 32 2d 33 33  33 33 22 10 0a 0c 34 34  |1-222-3333"...44|
00000030  34 2d 35 35 35 2d 36 36  36 36 10 02              |4-555-6666..|
Example (Test1)

https://developers.google.com/protocol-buffers/docs/encoding

t := Test1{150}
buf := panicOnErr(protoenc.Marshal(&t))
fmt.Print(hex.Dump(buf))
Output:

00000000  08 96 01                                          |...|
Example (Test2)
t := Test2{B: "testing"}
buf := panicOnErr(protoenc.Marshal(&t))
fmt.Print(hex.Dump(buf))
Output:

00000000  12 07 74 65 73 74 69 6e  67                       |..testing|
Example (Test3)
t := Test3{C: Test1{150}}
buf := panicOnErr(protoenc.Marshal(&t))
fmt.Print(hex.Dump(buf))
Output:

00000000  1a 03 08 96 01                                    |.....|

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanEncoderDecoder

func CleanEncoderDecoder()

CleanEncoderDecoder cleans the map of encoders and decoders. It's not safe to it call concurrently.

func Marshal

func Marshal(structPtr interface{}) (result []byte, err error)

Marshal a Go struct into protocol buffer format. The caller must pass a pointer to the struct to encode.

func ParseTag

func ParseTag(field reflect.StructField) int

ParseTag parses the protobuf tag of the given struct field.

func RegisterEncoderDecoder

func RegisterEncoderDecoder[T any, Enc func(T) ([]byte, error), Dec func([]byte) (T, error)](enc Enc, dec Dec)

RegisterEncoderDecoder registers the given encoder and decoder for the given type. T should be struct or pointer to struct.

func Unmarshal

func Unmarshal(buf []byte, structPtr interface{}) error

Unmarshal a protobuf value into a Go value. The caller must pass a pointer to the struct to decode into.

Types

type FieldData

type FieldData struct {
	Num        protowire.Number
	FieldIndex []int
	Field      reflect.StructField
}

FieldData represents a field of a struct with proto number and field index.

func StructFields

func StructFields(typ reflect.Type) ([]FieldData, error)

StructFields returns a list of StructFields for the given struct type.

func (*FieldData) IsZero

func (fd *FieldData) IsZero() bool

IsZero returns true if FieldData is zero.

type FixedS32

type FixedS32 int32

FixedS32 fields will be encoded as fixed 32-bit signed integers.

type FixedS64

type FixedS64 int64

FixedS64 fields will be encoded as fixed 64-bit signed integers.

type FixedU32

type FixedU32 uint32

FixedU32 fields will be encoded as fixed 64-bit unsigned integers.

type FixedU64

type FixedU64 uint64

FixedU64 fields will be encoded as fixed 64-bit unsigned integers.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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