Deep-copy functions autogeneration.

This commit is contained in:
Wojciech Tyczynski
2015-05-13 15:52:53 +02:00
parent 11b058cc7d
commit b2280db724
14 changed files with 1049 additions and 13 deletions

View File

@@ -40,6 +40,10 @@ type Scheme struct {
// default coverting behavior.
converter *Converter
// cloner stores all registered copy functions. It also has default
// deep copy behavior.
cloner *Cloner
// Indent will cause the JSON output from Encode to be indented, iff it is true.
Indent bool
@@ -60,6 +64,7 @@ func NewScheme() *Scheme {
typeToVersion: map[reflect.Type]string{},
typeToKind: map[reflect.Type][]string{},
converter: NewConverter(),
cloner: NewCloner(),
InternalVersion: "",
MetaFactory: DefaultMetaFactory,
}
@@ -212,6 +217,29 @@ func (s *Scheme) AddGeneratedConversionFuncs(conversionFuncs ...interface{}) err
return nil
}
// AddDeepCopyFuncs adds functions to the list of deep copy functions.
// Note that to copy sub-objects, you can use the conversion.Cloner object that
// will be passed to your deep-copy function.
func (s *Scheme) AddDeepCopyFuncs(deepCopyFuncs ...interface{}) error {
for _, f := range deepCopyFuncs {
if err := s.cloner.RegisterDeepCopyFunc(f); err != nil {
return err
}
}
return nil
}
// Similar to AddDeepCopyFuncs, but registers deep copy functions that were
// automatically generated.
func (s *Scheme) AddGeneratedDeepCopyFuncs(deepCopyFuncs ...interface{}) error {
for _, f := range deepCopyFuncs {
if err := s.cloner.RegisterGeneratedDeepCopyFunc(f); err != nil {
return err
}
}
return nil
}
// AddStructFieldConversion allows you to specify a mechanical copy for a moved
// or renamed struct field without writing an entire conversion function. See
// the comment in Converter.SetStructFieldCopy for parameter details.
@@ -262,6 +290,11 @@ func (s *Scheme) RegisterInputDefaults(in interface{}, fn FieldMappingFunc, defa
return s.converter.RegisterInputDefaults(in, fn, defaultFlags)
}
// Performs a deep copy of the given object.
func (s *Scheme) DeepCopy(in interface{}) (interface{}, error) {
return s.cloner.DeepCopy(in)
}
// Convert will attempt to convert in into out. Both must be pointers. For easy
// testing of conversion functions. Returns an error if the conversion isn't
// possible. You can call this with types that haven't been registered (for example,