Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetField ¶
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 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 ¶
func (f *FlattenMangler) Mangle(sf reflect.StructField) ([]reflect.StructField, error)
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 ¶
func (f *FlattenMangler) Unmangle(sf reflect.StructField, vs []FieldValueTuple) (reflect.Value, error)
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 ¶
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 ¶
func (*SetSliceMangler) Mangle(sf reflect.StructField) ([]reflect.StructField, error)
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 ¶
func (*SetSliceMangler) Unmangle(sf reflect.StructField, vs []FieldValueTuple) (reflect.Value, error)
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 ¶
func (*StringCastingMangler) Mangle(sf reflect.StructField) ([]reflect.StructField, error)
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 ¶
func (*StringCastingMangler) Unmangle(sf reflect.StructField, vs []FieldValueTuple) (reflect.Value, error)
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 ¶
func (*TextUnmarshalerMangler) Mangle(sf reflect.StructField) ([]reflect.StructField, error)
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 ¶
func (*TextUnmarshalerMangler) ShouldRecurse(reflect.StructField) bool
ShouldRecurse always returns true in order to walk nested structs.
func (*TextUnmarshalerMangler) Unmangle ¶
func (*TextUnmarshalerMangler) Unmangle(sf reflect.StructField, vs []FieldValueTuple) (reflect.Value, error)
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 ¶
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 ¶
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.