any

package
Version: v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2020 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrEmptyMessage = errors.New("message is nil")

ErrEmptyMessage when message is nil

Functions

func GetMessageName

func GetMessageName(m []byte) (string, error)

GetMessageName returns the message name from the wrapped proto message

Example
package main

import (
	"fmt"

	"github.com/vimeda/pletter/any"
	"github.com/vimeda/pletter/pb"
)

func main() {
	// Create your proto message
	ac := pb.Example{
		ID: "1231231312",
	}

	// call the PackAndMarshal function to wrap your message and already proto.Marshal it
	raw, err := any.PackAndMarshal(&ac)
	if err != nil {
		fmt.Printf("an error occurred while packing and marshalling the message: %s", err)
	}

	// call the Unpack function that will unwrap your message from the envelop
	name, err := any.GetMessageName(raw)
	if err != nil {
		fmt.Printf("an error occurred while getting the message name: %s", err)
	}

	fmt.Printf("message name: %s", name) // should print pb.Example
}
Output:

func Pack

func Pack(m proto.Message) (pb.Envelope, error)

Pack packs a proto message into an envelope message

Example
package main

import (
	"fmt"

	"github.com/vimeda/pletter/any"
	"github.com/vimeda/pletter/pb"
)

func main() {
	// Create your proto message
	ac := pb.Example{
		ID: "1231231312",
	}

	// call the Pack function to wrap your message. This returns an envelop type
	_, err := any.Pack(&ac)
	if err != nil {
		fmt.Printf("an error occurred while packing the message: %s", err)
	}
}
Output:

func PackAndMarshal

func PackAndMarshal(m proto.Message) ([]byte, error)

PackAndMarshal packs a proto message into an envelope message and marshal it

Example
package main

import (
	"fmt"

	"github.com/vimeda/pletter/any"
	"github.com/vimeda/pletter/pb"
)

func main() {
	// Create your proto message
	ac := pb.Example{
		ID: "1231231312",
	}

	// call the PackAndMarshal function to wrap your message and already proto.Marshal it
	_, err := any.PackAndMarshal(&ac)
	if err != nil {
		fmt.Printf("an error occurred while packing and marshalling the message: %s", err)
	}
}
Output:

func Unpack

func Unpack(m []byte, t proto.Message) error

Unpack unpacks a slice of bytes into a proto message. The slice of bytes should represent an enveloped proto message

Example
package main

import (
	"fmt"

	"github.com/vimeda/pletter/any"
	"github.com/vimeda/pletter/pb"
)

func main() {
	// Create your proto message
	ac := pb.Example{
		ID: "1231231312",
	}

	// call the PackAndMarshal function to wrap your message and already proto.Marshal it
	raw, err := any.PackAndMarshal(&ac)
	if err != nil {
		fmt.Printf("an error occurred while packing and marshalling the message: %s", err)
	}

	// declare your expected type
	var expectedExample pb.Example

	// call the Unpack function that will unwrap your message from the envelop
	if err := any.Unpack(raw, &expectedExample); err != nil {
		fmt.Printf("an error occurred while unpacking the message: %s", err)
	}
}
Output:

Types

This section is empty.

Source Files

Jump to

Keyboard shortcuts

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