protoutil

package
v0.2.13 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package protoutil provides utility functions for working with protobuf Any messages. It includes methods for packing and unpacking protobuf messages into and from Any types, as well as creating new instances of strongly-typed messages from Any values.

Package protoutil provides helper functions for working with flexible configuration structures in Protobuf, such as google.protobuf.Struct and google.protobuf.Any. It simplifies the process of converting these generic types into strongly-typed Go structs derived from Protobuf messages.

Package protoutil provides helper functions for working with flexible configuration structures in Protobuf, such as google.protobuf.Struct and google.protobuf.Any. It simplifies the process of converting these generic types into strongly-typed Go structs derived from Protobuf messages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewFromAny

func NewFromAny[T any, PT ProtoMessagePtr[T]](any *anypb.Any) (PT, error)

NewFromAny creates a new instance of a strongly-typed protobuf message and unmarshals the content of an anypb.Any into it. This is the counterpart to NewTypedConfig for the Any type.

func NewFromAnyMap

func NewFromAnyMap[T any, PT ProtoMessagePtr[T]](m map[string]*anypb.Any, key string) (PT, error)

NewFromAnyMap retrieves a specific configuration by key from a map of Any messages, creates a new instance of the target type, and unmarshals the data into it.

Usage:

authzCfg, err := customize.NewFromAnyMap[authzv1.AuthzConfig](myMap, "authz")

func NewFromStruct

func NewFromStruct[T any, PT ProtoMessagePtr[T]](s *structpb.Struct) (PT, error)

NewFromStruct creates a new instance of a strongly-typed protobuf message and unmarshals the generic *structpb.Struct into it.

The generic type parameter 'T' must be the struct type of the protobuf message, and its pointer type (*T) must implement proto.Message.

This function is useful when you want to directly receive the configured object as a return value, rather than passing a destination pointer.

Usage:

authzCfg, err := customize.NewFromStruct[authzv1.AuthzConfig](middleware.Customize)
if err != nil {
    // Handle error
}
// authzCfg is now a new instance populated with data from the config file.

func NewFromStructMap

func NewFromStructMap[T any, PT ProtoMessagePtr[T]](m map[string]*structpb.Struct, key string) (PT, error)

NewFromStructMap retrieves a specific configuration by key from a map of structs, creates a new instance of the target type, and unmarshals the data into it.

Usage:

authzCfg, err := customize.NewFromStructMap[authzv1.AuthzConfig](myMap, "authz")

func Pack

func Pack(msg proto.Message) (*anypb.Any, error)

Pack packs the provided protobuf message into an anypb.Any. It returns an error if the packing fails. This is a convenient wrapper around anypb.New().

Usage:

anyValue, err := customize.Pack(&myCfg)

func UnmarshalFromMap

func UnmarshalFromMap[T proto.Message](m map[string]*structpb.Struct, key string, dest T) error

UnmarshalFromMap retrieves a specific configuration by key from a map of structs and unmarshals it into the destination protobuf message.

Usage:

var authzCfg authzv1.AuthzConfig
err := customize.UnmarshalFromMap(myMap, "authz", &authzCfg)

func UnmarshalTo

func UnmarshalTo(s *structpb.Struct, dest proto.Message) error

UnmarshalTo unmarshals a generic *structpb.Struct into a specific strongly-typed protobuf message.

It leverages the intermediate JSON representation of google.protobuf.Struct to provide the flexibility of a "normal" config file structure while allowing for strong typing within the application code.

Usage:

var authzCfg authzv1.AuthzConfig
if err := customize.UnmarshalTo(middleware.Customize, &authzCfg); err != nil {
    // Handle error
}
// authzCfg is now populated with data from the config file.

func UnpackFromMap

func UnpackFromMap(m map[string]*anypb.Any, key string, dest proto.Message) error

UnpackFromMap retrieves a specific configuration by key from a map of Any messages and unmarshals it into the destination protobuf message.

Usage:

var authzCfg authzv1.AuthzConfig
err := customize.UnpackFromMap(myMap, "authz", &authzCfg)

func UnpackTo

func UnpackTo(any *anypb.Any, dest proto.Message) error

UnpackTo unpacks the configuration from an anypb.Any into the destination message 'dest'. It returns an error if the types do not match or unpacking fails.

Types

type ProtoMessagePtr

type ProtoMessagePtr[T any] interface {
	*T
	proto.Message
}

ProtoMessagePtr is a generic constraint for a type that is a pointer to a struct T and also implements the proto.Message interface. This allows for creating generic functions that can work with any pointer to a protobuf message struct.

Jump to

Keyboard shortcuts

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