transforms

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

README

Protocol Buffer Transforms

Documentation

Index

Constants

View Source
const (
	OptionTypeKeepEmpty = OptionType(1) + iota
	OptionTypeMaxDepth
	OptionTypeMaxDepthForName
	OptionTypeDefaultScalarFunc
	OptionTypeMapFunc
	OptionTypeMessageFunc
	OptionTypeRepeatedFunc
	OptionTypeKeepOrder
	OptionTypeAddOverride
	OptionTypeAddNameOverride
	OptionTypeAddScalarFunc
)

TODO(hvl): OptionTypeMaxDepthForType

Variables

This section is empty.

Functions

func IsDefaultScalar

func IsDefaultScalar(v *protoreflect.Value) bool

Types

type KeyValue

type KeyValue struct {
	Key   string
	Value interface{}
}

type MapFunc

type MapFunc func(protoreflect.FieldDescriptor, map[interface{}]interface{}) interface{}

type MessageFunc

type MessageFunc func(protoreflect.FieldDescriptor, []KeyValue) interface{}

type Option

type Option interface {
	// Type returns the Option's type.
	Type() OptionType

	// Apply applies the Option to the Walker.
	Apply(w *walker)
}

func OptionAddNameOverride

func OptionAddNameOverride(name string, v interface{}) Option

OptionAddNameOverride defines a special (post-)processing for the given field. Except for scalar fields, processing occurs after the normal conversion. For scalar fields, it is applied instead of normal conversion.

The name is a chain of field names, separated by dots. Repeated fields and their subfields can be referenced (i.e. 'addresses.street'), but there is no support for indexing. The same goes for map fields.

An override on a repeated field will be applied to each element. There is no support for post-processing the converted list as a whole. Such behaviour can be achieved by overriding the containing message.

You can effectively use multiple instances of this option as long as their name differs - otherwise, earlier ones will be overwritten by later ones.

func OptionAddScalarFunc

func OptionAddScalarFunc(k protoreflect.Kind, v ScalarFunc) Option

OptionAddScalarFunc adds a conversion function for a scalar kind. If no conversion function has been specified for a certain kind, the default function specified via SetDefaultFunc will be applied.

You can effectively use multiple instances of this option as long as their protoreflect.Kind differs - otherwise, earlier ones will be overwritten by later ones.

func OptionAddTypeOverride

func OptionAddTypeOverride(type_ string, v MessageFunc) Option

OptionAddTypeOverride defines a special treatment for the given message type. The type name is expected to be the full name, i.e. 'acme.products.Anvil'.

You can effectively use multiple instances of this option as long as their type name differs - otherwise, earlier ones will be overwritten by later ones.

func OptionDefaultScalarFunc

func OptionDefaultScalarFunc(fn ScalarFunc) Option

OptionDefaultScalarFunc sets the default scalar conversion function. When omitted, the default function is an identity function.

func OptionKeepEmpty

func OptionKeepEmpty(v bool) Option

func OptionKeepOrder

func OptionKeepOrder(v bool) Option

OptionKeepOrder will cause the Walker to return a list of KeyValue structs if set to true, rather than an unordered map. This list will be ordered according to the fields' protocol buffer numbers.

func OptionMapFunc

func OptionMapFunc(fn MapFunc) Option

OptionMapFunc sets the map type conversion function. The default is an identity function.

func OptionMaxDepth

func OptionMaxDepth(v int) Option

OptionMaxDepth sets a maximum message recursion depth. This mitigates the impact of infinite recursion in recursive messages (like protobuf.Struct). Only traversed Message fields add to the recursion depth.

A depth of zero will return the root level fields. If depth is set to less than zero, the default recursion depth (99) will be used.

func OptionMaxDepthForName

func OptionMaxDepthForName(name string, v int) Option

OptionMaxDepthForName sets a maximum message recursion depth starting from the given field. This mitigates the impact of infinite recursion in recursive messages (like protobuf.Struct). Only traversed Message fields add to the recursion depth.

A depth of zero will return the field. If depth is set to less than zero, the default recursion depth (99) will be used.

func OptionMessageFunc

func OptionMessageFunc(fn MessageFunc) Option

OptionMessageFunc sets the message type conversion function. The default is an identity function. The top-level message will not be treated by this function; it has no FieldDescriptor.

func OptionRepeatedFunc

func OptionRepeatedFunc(fn RepeatedFunc) Option

OptionRepeatedFunc sets the repeated field conversion function. The default is an identity function.

type OptionType

type OptionType int

type OverrideFunc

type OverrideFunc func(protoreflect.FieldDescriptor, interface{}) interface{}

func FromMapFunc

func FromMapFunc(f MapFunc) OverrideFunc

func FromMessageFunc

func FromMessageFunc(f MessageFunc) OverrideFunc

func FromScalarFunc

func FromScalarFunc(f ScalarFunc) OverrideFunc

FromScalarFunc creates an OverrideFunc from a Scalar func.

type RepeatedFunc

type RepeatedFunc func(protoreflect.FieldDescriptor, []interface{}) interface{}

type ScalarFunc

type ScalarFunc func(protoreflect.FieldDescriptor, *protoreflect.Value) interface{}

type Walker

type Walker interface {
	// Apply will apply this Walker to a Message.
	Apply(m proto.Message) interface{}

	// ApplyDesc will apply this Walker to a message Descriptor.
	ApplyDesc(d protoreflect.MessageDescriptor) interface{}
}

A Walker walks over a Protocol Buffers message or message descriptor. Without additional configuration, it will return it as a map.

func NewWalker

func NewWalker(options ...Option) Walker

NewWalker spawns a new Walker. The provided options will be processed in sequence and later options may overwrite earlier ones.

Jump to

Keyboard shortcuts

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