Add a new Default() method on Scheme

Allow defaulter functions to be registered with the scheme.
This commit is contained in:
Clayton Coleman 2016-09-24 14:56:23 -04:00
parent 85368d070b
commit 68a9983ec7
No known key found for this signature in database
GPG Key ID: 3D16906B4F1C5CB3
2 changed files with 23 additions and 2 deletions

View File

@ -431,10 +431,10 @@ func (c *Converter) SetStructFieldCopy(srcFieldType interface{}, srcFieldName st
}
// RegisterDefaultingFunc registers a value-defaulting func with the Converter.
// defaultingFunc must take one parameters: a pointer to the input type.
// defaultingFunc must take one parameter: a pointer to the input type.
//
// Example:
// c.RegisteDefaultingFunc(
// c.RegisterDefaultingFunc(
// func(in *v1.Pod) {
// // defaulting logic...
// })

View File

@ -61,6 +61,10 @@ type Scheme struct {
// resource field labels in that version to internal version.
fieldLabelConversionFuncs map[string]map[string]FieldLabelConversionFunc
// defaulterFuncs is an array of interfaces to be called with an object to provide defaulting
// the provided object must be a pointer.
defaulterFuncs map[reflect.Type]func(interface{})
// converter stores all registered conversion functions. It also has
// default coverting behavior.
converter *conversion.Converter
@ -82,6 +86,7 @@ func NewScheme() *Scheme {
unversionedKinds: map[string]reflect.Type{},
cloner: conversion.NewCloner(),
fieldLabelConversionFuncs: map[string]map[string]FieldLabelConversionFunc{},
defaulterFuncs: map[reflect.Type]func(interface{}){},
}
s.converter = conversion.NewConverter(s.nameFunc)
@ -421,6 +426,22 @@ func (s *Scheme) AddDefaultingFuncs(defaultingFuncs ...interface{}) error {
return nil
}
// AddTypeDefaultingFuncs registers a function that is passed a pointer to an
// object and can default fields on the object. These functions will be invoked
// when Default() is called. The function will never be called unless the
// defaulted object matches srcType. If this function is invoked twice with the
// same srcType, the fn passed to the later call will be used instead.
func (s *Scheme) AddTypeDefaultingFunc(srcType Object, fn func(interface{})) {
s.defaulterFuncs[reflect.TypeOf(srcType)] = fn
}
// Default sets defaults on the provided Object.
func (s *Scheme) Default(src Object) {
if fn, ok := s.defaulterFuncs[reflect.TypeOf(src)]; ok {
fn(src)
}
}
// Copy does a deep copy of an API object.
func (s *Scheme) Copy(src Object) (Object, error) {
dst, err := s.DeepCopy(src)