transform

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetField

func GetField(sf reflect.StructField, v reflect.Value) reflect.Value

GetField should be called after calling the flatten mangler. It uses the dialsfieldpath tag of the mangled StructFields (sf) set by the flatten mangler to get the path to the original field. It returns the concrete value of the original field and if the original field value isn't populated or any intermediate fields are nil, it will return the zero value for its concrete type.

Types

type AnonymousFlattenMangler added in v0.14.0

type AnonymousFlattenMangler struct{}

AnonymousFlattenMangler hoists the fields from the types of anonymous struct-fields into the parent type. (working around decoders/sources that are unaware of anonymous fields) Note: this mangler is unaware of TextUnmarshaler implementations (it's tricky to do right when flattening). It should be combined with the TextUnmarshalerMangler if the prefered handling is to mask the other fields in that struct with the TextUnmarshaler implementation.

func (AnonymousFlattenMangler) Mangle added in v0.14.0

Mangle is called for every field in a struct, and maps that to one or more output fields. Implementations that desire to leave fields unchanged should return the argument unchanged. (particularly useful if taking advantage of recursive evaluation)

func (AnonymousFlattenMangler) ShouldRecurse added in v0.14.0

func (a AnonymousFlattenMangler) ShouldRecurse(_ reflect.StructField) bool

ShouldRecurse is called after Mangle for each field so nested struct fields get iterated over after any transformation done by Mangle().

func (AnonymousFlattenMangler) Unmangle added in v0.14.0

Unmangle is called for every source-field->mangled-field mapping-set, with the mangled-field and its populated value set. The implementation of Unmangle should return a reflect.Value that will be used for the next mangler or final struct value) Returned reflect.Value should be convertible to the field's type.

type FieldValueTuple

type FieldValueTuple struct {
	Field reflect.StructField
	Value reflect.Value
}

FieldValueTuple ties together the StructField and the value to be converted back to the input-type

type FlattenMangler

type FlattenMangler struct {
	// contains filtered or unexported fields
}

FlattenMangler implements the Mangler interface

func DefaultFlattenMangler

func DefaultFlattenMangler() *FlattenMangler

DefaultFlattenMangler returns a FlattenMangler with preset values for tag, nameEncodeCasing, and tagEncodeCasing

func NewFlattenMangler

func NewFlattenMangler(tag string, nameEnc, tagEnc caseconversion.EncodeCasingFunc) *FlattenMangler

NewFlattenMangler is the constructor for FlattenMangler

func (*FlattenMangler) Mangle

Mangle goes through each StructField and flattens the structure

func (*FlattenMangler) ShouldRecurse

func (f *FlattenMangler) ShouldRecurse(reflect.StructField) bool

ShouldRecurse returns false because Mangle walks through nested structs and doesn't need Transform's recursion

func (*FlattenMangler) Unmangle

Unmangle goes through the struct and populates the values of the struct that come from the populated flattened struct fields

type Mangler

type Mangler interface {
	// Mangle is called for every field in a struct, and maps that to one or more output fields.
	// Implementations that desire to leave fields unchanged should return
	// the argument unchanged. (particularly useful if taking advantage of
	// recursive evaluation)
	Mangle(reflect.StructField) ([]reflect.StructField, error)
	// Unmangle is called for every source-field->mangled-field
	// mapping-set, with the mangled-field and its populated value set. The
	// implementation of Unmangle should return a reflect.Value that will
	// be used for the next mangler or final struct value)
	// Returned reflect.Value should be convertible to the field's type.
	Unmangle(reflect.StructField, []FieldValueTuple) (reflect.Value, error)
	// ShouldRecurse is called after Mangle for each field so nested struct
	// fields get iterated over after any transformation done by Mangle().
	ShouldRecurse(reflect.StructField) bool
}

Mangler implementations operate on a field-by-field basis

type ReverseTranslateError

type ReverseTranslateError struct {
	Err       error
	ErrString string
}

ReverseTranslateError represents an error in reverse translation.

func (*ReverseTranslateError) Error

func (e *ReverseTranslateError) Error() string

Error implements the Error interface.

func (*ReverseTranslateError) Unwrap

func (e *ReverseTranslateError) Unwrap() error

Unwrap returns the inner error.

type SetSliceMangler

type SetSliceMangler struct {
}

SetSliceMangler conveniently maps from sets (e.g. map[string]struct{}) to slices (e.g. []string)

func (*SetSliceMangler) Mangle

Mangle changes the type of the provided StructField from a map[T]struct{} to []T.

func (*SetSliceMangler) ShouldRecurse

func (*SetSliceMangler) ShouldRecurse(reflect.StructField) bool

ShouldRecurse always returns true in order to walk nested structs.

func (*SetSliceMangler) Unmangle

Unmangle turns []T back into a map[T]struct{}. Naturally, deduplication will occur.

type StringCastingMangler

type StringCastingMangler struct{}

StringCastingMangler mangles config struct fields into string types, then unmangles the filled-in fields back to the original types, in order to abstract away the details of type conversion from sources.

func (*StringCastingMangler) Mangle

Mangle changes the type of the provided StructField to string.

func (*StringCastingMangler) ShouldRecurse

func (*StringCastingMangler) ShouldRecurse(reflect.StructField) bool

ShouldRecurse always returns true in order to walk nested structs.

func (*StringCastingMangler) Unmangle

Unmangle casts the string value in the mangled config struct to the type in the original struct.

type TextUnmarshalerMangler

type TextUnmarshalerMangler struct{}

TextUnmarshalerMangler changes types that implement encoding.TextUnmarshaler to string and uses that interface to cast back to their original type.

func (*TextUnmarshalerMangler) Mangle

Mangle changes the type of the provided StructField to string if that StructField type implements encoding.TextUnmarshaler. Otherwise, the type is passed through unaltered.

func (*TextUnmarshalerMangler) ShouldRecurse

ShouldRecurse always returns true in order to walk nested structs.

func (*TextUnmarshalerMangler) Unmangle

Unmangle unmangles.

type Transformer

type Transformer struct {
	// contains filtered or unexported fields
}

Transformer wraps a type and an arbitrary set of Manglers.

func NewTransformer

func NewTransformer(t reflect.Type, manglers ...Mangler) *Transformer

NewTransformer constructs a Transformer instance with the specified manglers and type (the order of manglers specified here is the order they'll be evaluated in Mangle()).

func (*Transformer) ReverseTranslate

func (t *Transformer) ReverseTranslate(v reflect.Value) (reflect.Value, error)

ReverseTranslate calls each Mangler's Unmangle method in reverse order.

func (*Transformer) Translate

func (t *Transformer) Translate() (reflect.Value, error)

Translate calls `TranslateType` and returns an instance of the new type (or an error)

func (*Transformer) TranslateType

func (t *Transformer) TranslateType() (reflect.Type, error)

TranslateType calls `Mangle` on all `Manglers` in order, tracking the conversion for use in ReverseTranslate.

type UnmangleError

type UnmangleError struct {
	Err       error
	ErrString string
}

UnmangleError represents an error in unmangling.

func (*UnmangleError) Error

func (e *UnmangleError) Error() string

Error implements the Error interface.

func (*UnmangleError) Unwrap

func (e *UnmangleError) Unwrap() error

Unwrap returns in the inner error.

Jump to

Keyboard shortcuts

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