Documentation
¶
Overview ¶
Package customize 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 ¶
- func NewFromAny[T any, PT ProtoMessagePtr[T]](any *anypb.Any) (PT, error)
- func NewFromAnyMap[T any, PT ProtoMessagePtr[T]](m map[string]*anypb.Any, key string) (PT, error)
- func NewFromStruct[T any, PT ProtoMessagePtr[T]](s *structpb.Struct) (PT, error)
- func NewFromStructMap[T any, PT ProtoMessagePtr[T]](m map[string]*structpb.Struct, key string) (PT, error)
- func Pack(msg proto.Message) (*anypb.Any, error)
- func UnmarshalFromMap[T proto.Message](m map[string]*structpb.Struct, key string, dest T) error
- func UnmarshalTo(s *structpb.Struct, dest proto.Message) error
- func UnpackFromMap(m map[string]*anypb.Any, key string, dest proto.Message) error
- func UnpackTo(any *anypb.Any, dest proto.Message) error
- type ProtoMessagePtr
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewFromAny ¶ added in v0.2.8
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 ¶ added in v0.2.8
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 ¶ added in v0.2.8
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 ¶ added in v0.2.8
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 ¶ added in v0.2.8
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 ¶ added in v0.2.8
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 ¶ added in v0.2.8
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 ¶ added in v0.2.8
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)
Types ¶
type ProtoMessagePtr ¶ added in v0.2.8
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.