Documentation ¶
Overview ¶
Package conversion provides go object versioning.
Specifically, conversion provides a way for you to define multiple versions of the same object. You may write functions which implement conversion logic, but for the fields which did not change, copying is automated. This makes it easy to modify the structures you use in memory without affecting the format you store on disk or respond to in your external API calls.
Index ¶
- Variables
- func Convert_Slice_byte_To_Slice_byte(in *[]byte, out *[]byte, s Scope) error
- func EnforcePtr(obj interface{}) (reflect.Value, error)
- type Cloner
- type ConversionFuncs
- type Converter
- func (c *Converter) AddGenericConversionFunc(fn GenericConversionFunc)
- func (c *Converter) ConversionFuncValue(inType, outType reflect.Type) (reflect.Value, bool)
- func (c *Converter) Convert(src, dest interface{}, flags FieldMatchingFlags, meta *Meta) error
- func (c *Converter) DefaultConvert(src, dest interface{}, flags FieldMatchingFlags, meta *Meta) error
- func (c *Converter) DefaultMeta(t reflect.Type) (FieldMatchingFlags, *Meta)
- func (c *Converter) HasConversionFunc(inType, outType reflect.Type) bool
- func (c *Converter) IsConversionIgnored(inType, outType reflect.Type) bool
- func (c *Converter) RegisterConversionFunc(conversionFunc interface{}) error
- func (c *Converter) RegisterDefaultingFunc(defaultingFunc interface{}) error
- func (c *Converter) RegisterGeneratedConversionFunc(conversionFunc interface{}) error
- func (c *Converter) RegisterIgnoredConversion(from, to interface{}) error
- func (c *Converter) RegisterInputDefaults(in interface{}, fn FieldMappingFunc, defaultFlags FieldMatchingFlags) error
- func (c *Converter) SetStructFieldCopy(srcFieldType interface{}, srcFieldName string, destFieldType interface{}, ...) error
- func (c *Converter) WithConversions(fns ConversionFuncs) *Converter
- type DebugLogger
- type Equalities
- type FieldMappingFunc
- type FieldMatchingFlags
- type GeneratedDeepCopyFunc
- type GenericConversionFunc
- type Meta
- type NameFunc
- type Scope
Constants ¶
This section is empty.
Variables ¶
var DefaultNameFunc = func(t reflect.Type) string { return t.Name() }
Functions ¶
func Convert_Slice_byte_To_Slice_byte ¶ added in v1.3.0
Convert_Slice_byte_To_Slice_byte prevents recursing into every byte
func EnforcePtr ¶
EnforcePtr ensures that obj is a pointer of some sort. Returns a reflect.Value of the dereferenced pointer, ensuring that it is settable/addressable. Returns an error if this is not possible.
Types ¶
type Cloner ¶ added in v0.18.0
type Cloner struct {
// contains filtered or unexported fields
}
Cloner knows how to copy one type to another.
func (*Cloner) RegisterDeepCopyFunc ¶ added in v0.18.0
RegisterGeneratedDeepCopyFunc registers a copying func with the Cloner. deepCopyFunc must take three parameters: a type input, a pointer to a type output, and a pointer to Cloner. It should return an error.
Example: c.RegisterGeneratedDeepCopyFunc(
func(in Pod, out *Pod, c *Cloner) error { // deep copy logic... return nil })
func (*Cloner) RegisterGeneratedDeepCopyFunc ¶ added in v0.18.0
func (c *Cloner) RegisterGeneratedDeepCopyFunc(fn GeneratedDeepCopyFunc) error
Similar to RegisterDeepCopyFunc, but registers deep copy function that were automatically generated.
type ConversionFuncs ¶ added in v1.2.0
type ConversionFuncs struct {
// contains filtered or unexported fields
}
func NewConversionFuncs ¶ added in v1.2.0
func NewConversionFuncs() ConversionFuncs
func (ConversionFuncs) Add ¶ added in v1.2.0
func (c ConversionFuncs) Add(fns ...interface{}) error
Add adds the provided conversion functions to the lookup table - they must have the signature `func(type1, type2, Scope) error`. Functions are added in the order passed and will override previously registered pairs.
func (ConversionFuncs) Merge ¶ added in v1.2.0
func (c ConversionFuncs) Merge(other ConversionFuncs) ConversionFuncs
Merge returns a new ConversionFuncs that contains all conversions from both other and c, with other conversions taking precedence.
type Converter ¶
type Converter struct { // If non-nil, will be called to print helpful debugging info. Quite verbose. Debug DebugLogger // contains filtered or unexported fields }
Converter knows how to convert one type to another.
func NewConverter ¶
NewConverter creates a new Converter object.
func (*Converter) AddGenericConversionFunc ¶ added in v1.3.0
func (c *Converter) AddGenericConversionFunc(fn GenericConversionFunc)
AddGenericConversionFunc adds a function that accepts the ConversionFunc call pattern (for two conversion types) to the converter. These functions are checked first during a normal conversion, but are otherwise not called. Use AddConversionFuncs when registering typed conversions.
func (*Converter) ConversionFuncValue ¶ added in v1.2.0
func (*Converter) Convert ¶
func (c *Converter) Convert(src, dest interface{}, flags FieldMatchingFlags, meta *Meta) error
Convert will translate src to dest if it knows how. Both must be pointers. If no conversion func is registered and the default copying mechanism doesn't work on this type pair, an error will be returned. Read the comments on the various FieldMatchingFlags constants to understand what the 'flags' parameter does. 'meta' is given to allow you to pass information to conversion functions, it is not used by Convert() other than storing it in the scope. Not safe for objects with cyclic references!
func (*Converter) DefaultConvert ¶ added in v0.8.0
func (c *Converter) DefaultConvert(src, dest interface{}, flags FieldMatchingFlags, meta *Meta) error
DefaultConvert will translate src to dest if it knows how. Both must be pointers. No conversion func is used. If the default copying mechanism doesn't work on this type pair, an error will be returned. Read the comments on the various FieldMatchingFlags constants to understand what the 'flags' parameter does. 'meta' is given to allow you to pass information to conversion functions, it is not used by DefaultConvert() other than storing it in the scope. Not safe for objects with cyclic references!
func (*Converter) DefaultMeta ¶ added in v1.2.0
func (c *Converter) DefaultMeta(t reflect.Type) (FieldMatchingFlags, *Meta)
DefaultMeta returns the conversion FieldMappingFunc and meta for a given type.
func (*Converter) HasConversionFunc ¶ added in v0.17.0
func (*Converter) IsConversionIgnored ¶ added in v1.2.0
IsConversionIgnored returns true if the specified objects should be dropped during conversion.
func (*Converter) RegisterConversionFunc ¶ added in v0.11.0
RegisterConversionFunc registers a conversion func with the Converter. conversionFunc must take three parameters: a pointer to the input type, a pointer to the output type, and a conversion.Scope (which should be used if recursive conversion calls are desired). It must return an error.
Example: c.RegisterConversionFunc(
func(in *Pod, out *v1.Pod, s Scope) error { // conversion logic... return nil })
func (*Converter) RegisterDefaultingFunc ¶ added in v0.11.0
RegisterDefaultingFunc registers a value-defaulting func with the Converter. defaultingFunc must take one parameters: a pointer to the input type.
Example: c.RegisteDefaultingFunc(
func(in *v1.Pod) { // defaulting logic... })
func (*Converter) RegisterGeneratedConversionFunc ¶ added in v0.17.0
Similar to RegisterConversionFunc, but registers conversion function that were automatically generated.
func (*Converter) RegisterIgnoredConversion ¶ added in v1.2.0
RegisterIgnoredConversion registers a "no-op" for conversion, where any requested conversion between from and to is ignored.
func (*Converter) RegisterInputDefaults ¶ added in v0.14.0
func (c *Converter) RegisterInputDefaults(in interface{}, fn FieldMappingFunc, defaultFlags FieldMatchingFlags) error
RegisterInputDefaults registers a field name mapping function, used when converting from maps to structs. Inputs to the conversion methods are checked for this type and a mapping applied automatically if the input matches in. A set of default flags for the input conversion may also be provided, which will be used when no explicit flags are requested.
func (*Converter) SetStructFieldCopy ¶ added in v0.6.0
func (c *Converter) SetStructFieldCopy(srcFieldType interface{}, srcFieldName string, destFieldType interface{}, destFieldName string) error
SetStructFieldCopy registers a correspondence. Whenever a struct field is encountered which has a type and name matching srcFieldType and srcFieldName, it wil be copied into the field in the destination struct matching destFieldType & Name, if such a field exists. May be called multiple times, even for the same source field & type--all applicable copies will be performed.
func (*Converter) WithConversions ¶ added in v1.2.0
func (c *Converter) WithConversions(fns ConversionFuncs) *Converter
WithConversions returns a Converter that is a copy of c but with the additional fns merged on top.
type DebugLogger ¶
type DebugLogger interface {
Logf(format string, args ...interface{})
}
DebugLogger allows you to get debugging messages if necessary.
type Equalities ¶ added in v0.9.0
type Equalities struct {
reflect.Equalities
}
The code for this type must be located in third_party, since it forks from go std lib. But for convenience, we expose the type here, too.
func EqualitiesOrDie ¶ added in v0.9.0
func EqualitiesOrDie(funcs ...interface{}) Equalities
For convenience, panics on errors
type FieldMappingFunc ¶ added in v0.14.0
type FieldMappingFunc func(key string, sourceTag, destTag reflect.StructTag) (source string, dest string)
FieldMappingFunc can convert an input field value into different values, depending on the value of the source or destination struct tags.
type FieldMatchingFlags ¶
type FieldMatchingFlags int
FieldMatchingFlags contains a list of ways in which struct fields could be copied. These constants may be | combined.
const ( // Loop through destination fields, search for matching source // field to copy it from. Source fields with no corresponding // destination field will be ignored. If SourceToDest is // specified, this flag is ignored. If neither is specified, // or no flags are passed, this flag is the default. DestFromSource FieldMatchingFlags = 0 // Loop through source fields, search for matching dest field // to copy it into. Destination fields with no corresponding // source field will be ignored. SourceToDest FieldMatchingFlags = 1 << iota // Don't treat it as an error if the corresponding source or // dest field can't be found. IgnoreMissingFields // Don't require type names to match. AllowDifferentFieldTypeNames )
func (FieldMatchingFlags) IsSet ¶
func (f FieldMatchingFlags) IsSet(flag FieldMatchingFlags) bool
IsSet returns true if the given flag or combination of flags is set.
type GeneratedDeepCopyFunc ¶ added in v1.4.0
type GeneratedDeepCopyFunc struct { Fn func(in interface{}, out interface{}, c *Cloner) error InType reflect.Type }
GeneratedDeepCopyFunc bundles an untyped generated deep-copy function of a type with a reflection type object used as a key to lookup the deep-copy function.
type GenericConversionFunc ¶ added in v1.3.0
type Meta ¶
type Meta struct { // KeyNameMapping is an optional function which may map the listed key (field name) // into a source and destination value. KeyNameMapping FieldMappingFunc // Context is an optional field that callers may use to pass info to conversion functions. Context interface{} }
Meta is supplied by Scheme, when it calls Convert.
type Scope ¶
type Scope interface { // Call Convert to convert sub-objects. Note that if you call it with your own exact // parameters, you'll run out of stack space before anything useful happens. Convert(src, dest interface{}, flags FieldMatchingFlags) error // DefaultConvert performs the default conversion, without calling a conversion func // on the current stack frame. This makes it safe to call from a conversion func. DefaultConvert(src, dest interface{}, flags FieldMatchingFlags) error // If registered, returns a function applying defaults for objects of a given type. // Used for automatically generating conversion functions. DefaultingInterface(inType reflect.Type) (interface{}, bool) // SrcTags and DestTags contain the struct tags that src and dest had, respectively. // If the enclosing object was not a struct, then these will contain no tags, of course. SrcTag() reflect.StructTag DestTag() reflect.StructTag // Flags returns the flags with which the conversion was started. Flags() FieldMatchingFlags // Meta returns any information originally passed to Convert. Meta() *Meta }
Scope is passed to conversion funcs to allow them to continue an ongoing conversion. If multiple converters exist in the system, Scope will allow you to use the correct one from a conversion function--that is, the one your conversion function was called by.
Directories ¶
Path | Synopsis |
---|---|
Package queryparams provides conversion from versioned runtime objects to URL query values
|
Package queryparams provides conversion from versioned runtime objects to URL query values |