Documentation
¶
Index ¶
- func GetField(sf reflect.StructField, v reflect.Value) reflect.Value
- type AliasMangler
- type AnonymousFlattenMangler
- type FieldValueTuple
- type FlattenMangler
- func (f *FlattenMangler) AddPropogateTags(tags ...string)
- func (f *FlattenMangler) Mangle(sf reflect.StructField) ([]reflect.StructField, error)
- func (f *FlattenMangler) ShouldRecurse(reflect.StructField) bool
- func (f *FlattenMangler) Unmangle(sf reflect.StructField, vs []FieldValueTuple) (reflect.Value, error)
- type Mangler
- type ReverseTranslateError
- type SetSliceMangler
- type SingleTypeSubstitutionMangler
- func (s *SingleTypeSubstitutionMangler[F, T]) Mangle(sf reflect.StructField) ([]reflect.StructField, error)
- func (s *SingleTypeSubstitutionMangler[F, T]) ShouldRecurse(reflect.StructField) bool
- func (s *SingleTypeSubstitutionMangler[F, T]) Unmangle(sf reflect.StructField, fval []FieldValueTuple) (reflect.Value, error)
- type StringCastingMangler
- type TextUnmarshalerMangler
- type Transformer
- type UnmangleError
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 AliasMangler ¶ added in v0.17.0
type AliasMangler struct {
// contains filtered or unexported fields
}
AliasMangler manages aliases for dials, dialsenv, dialsflag, and dialspflag struct tags to make it possible to migrate from one name to another conveniently.
func NewAliasMangler ¶ added in v0.17.0
func NewAliasMangler(tags ...string) *AliasMangler
NewAliasMangler creates a new AliasMangler with the provided tags.
func (*AliasMangler) Mangle ¶ added in v0.17.0
func (a *AliasMangler) Mangle(sf reflect.StructField) ([]reflect.StructField, error)
Mangle implements the Mangler interface. If any alias tag is defined, the struct field will be copied with the non-aliased tag set to the alias's value.
func (AliasMangler) ShouldRecurse ¶ added in v0.17.0
func (a AliasMangler) 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 (*AliasMangler) Unmangle ¶ added in v0.17.0
func (a *AliasMangler) Unmangle(sf reflect.StructField, fvs []FieldValueTuple) (reflect.Value, error)
Unmangle implements the Mangler interface and unwinds the alias copying operation. Note that if both the source and alias are both set in the configuration, an error will be returned.
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
func (a AnonymousFlattenMangler) Mangle(sf reflect.StructField) ([]reflect.StructField, error)
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
func (a AnonymousFlattenMangler) Unmangle(sf reflect.StructField, fvs []FieldValueTuple) (reflect.Value, 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.
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) AddPropogateTags ¶ added in v1.18.1
func (f *FlattenMangler) AddPropogateTags(tags ...string)
AddPropogateTags inserts an additional set of tags to propagate down to the final leaf fields from intermediate-level struct-fields.
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 SingleTypeSubstitutionMangler ¶ added in v0.15.0
type SingleTypeSubstitutionMangler[F, T any] struct { // contains filtered or unexported fields }
SingleTypeSubstitutionMangler implements Mangler, and converts between two types that are convertible to one another Must be constructed with NewSingleTypeSubstitutionMangler to fill in the unexported reflect.Type fields. The two types must be convertible (by Go's definition), so we can directly convert one value to the other. The F type-argument is the "from" type, which is being being replaced. The T type-argument is the "to" type, which is taking its place during processing (and swapped back to F during Unmangle).
func NewSingleTypeSubstitutionMangler ¶ added in v0.15.0
func NewSingleTypeSubstitutionMangler[F, T any]() (*SingleTypeSubstitutionMangler[F, T], error)
NewSingleTypeSubstitutionMangler constructs a new SingleTypeSubstitutionMangler, filling in the unexported fields. The F type-argument is the "from" type, which is being being replaced. The T type-argument is the "to" type, which is taking its place during processing (and swapped back to F during Unmangle). If T is not convertible to F, an error is returned. (only Unmangle converts values back, so convertibility in the other direction is irrelevant)
func (*SingleTypeSubstitutionMangler[F, T]) Mangle ¶ added in v0.15.0
func (s *SingleTypeSubstitutionMangler[F, T]) Mangle(sf reflect.StructField) ([]reflect.StructField, error)
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 (*SingleTypeSubstitutionMangler[F, T]) ShouldRecurse ¶ added in v0.15.0
func (s *SingleTypeSubstitutionMangler[F, T]) 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 (*SingleTypeSubstitutionMangler[F, T]) Unmangle ¶ added in v0.15.0
func (s *SingleTypeSubstitutionMangler[F, T]) Unmangle(sf reflect.StructField, fval []FieldValueTuple) (reflect.Value, 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.
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.