Genericize MetaInsertionFactory into a simpler interface

The common path code for MIF goes through a conversion cycle - it
can also be done through reflection. This simplifies the Create/Update
methods into Interpret (return version) and Update (through reflection).

In addition it uses only one MetaFactory implementation across all of
our packages which reduces a bit of duplication.
This commit is contained in:
Clayton Coleman
2014-10-09 18:32:46 -04:00
parent ebba1aed7d
commit d488e238dd
5 changed files with 391 additions and 244 deletions

View File

@@ -145,7 +145,7 @@ func (self *Scheme) rawExtensionToEmbeddedObject(in *RawExtension, out *Embedded
func NewScheme() *Scheme {
s := &Scheme{conversion.NewScheme()}
s.raw.InternalVersion = ""
s.raw.MetaInsertionFactory = metaInsertion{}
s.raw.MetaFactory = conversion.SimpleMetaFactory{BaseFields: []string{"TypeMeta"}, VersionField: "APIVersion", KindField: "Kind"}
s.raw.AddConversionFuncs(
s.embeddedObjectToRawExtension,
s.rawExtensionToEmbeddedObject,
@@ -339,28 +339,3 @@ func (s *Scheme) CopyOrDie(obj Object) Object {
}
return newObj
}
// metaInsertion implements conversion.MetaInsertionFactory, which lets the conversion
// package figure out how to encode our object's types and versions. These fields are
// located in our TypeMeta.
type metaInsertion struct {
TypeMeta struct {
APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"`
Kind string `json:"kind,omitempty" yaml:"kind,omitempty"`
} `json:",inline" yaml:",inline"`
}
// Create returns a new metaInsertion with the version and kind fields set.
func (metaInsertion) Create(version, kind string) interface{} {
m := metaInsertion{}
m.TypeMeta.APIVersion = version
m.TypeMeta.Kind = kind
return &m
}
// Interpret returns the version and kind information from in, which must be
// a metaInsertion pointer object.
func (metaInsertion) Interpret(in interface{}) (version, kind string) {
m := in.(*metaInsertion)
return m.TypeMeta.APIVersion, m.TypeMeta.Kind
}