Add documentation and tests to conversion.

This commit is contained in:
Daniel Smith
2014-08-01 14:24:12 -07:00
parent 5c0f5e85e2
commit 1cc7fce523
8 changed files with 331 additions and 145 deletions

View File

@@ -28,12 +28,12 @@ import (
// s.InternalVersion type before being returned. Decode will refuse to decode
// objects without a version, because that's probably an error.
func (s *Scheme) Decode(data []byte) (interface{}, error) {
version, kind, err := s.DataAPIVersionAndKind(data)
version, kind, err := s.DataVersionAndKind(data)
if err != nil {
return nil, err
}
if version == "" {
return nil, fmt.Errorf("API Version not set in '%s'", string(data))
return nil, fmt.Errorf("version not set in '%s'", string(data))
}
obj, err := s.NewObject(version, kind)
if err != nil {
@@ -47,8 +47,7 @@ func (s *Scheme) Decode(data []byte) (interface{}, error) {
}
// Version and Kind should be blank in memory.
blankVersionAndKind := s.MetaInsertionFactory.Create("", "")
err = s.converter.Convert(blankVersionAndKind, obj, SourceToDest|IgnoreMissingFields|AllowDifferentFieldNames)
err = s.SetVersionAndKind("", "", obj)
if err != nil {
return nil, err
}
@@ -71,14 +70,14 @@ func (s *Scheme) Decode(data []byte) (interface{}, error) {
// DecodeInto parses a YAML or JSON string and stores it in obj. Returns an error
// if data.Kind is set and doesn't match the type of obj. Obj should be a
// pointer to an api type.
// If obj's APIVersion doesn't match that in data, an attempt will be made to convert
// If obj's version doesn't match that in data, an attempt will be made to convert
// data into obj's version.
func (s *Scheme) DecodeInto(data []byte, obj interface{}) error {
dataVersion, dataKind, err := s.DataAPIVersionAndKind(data)
dataVersion, dataKind, err := s.DataVersionAndKind(data)
if err != nil {
return err
}
objVersion, objKind, err := s.ObjectAPIVersionAndKind(obj)
objVersion, objKind, err := s.ObjectVersionAndKind(obj)
if err != nil {
return err
}
@@ -120,10 +119,5 @@ func (s *Scheme) DecodeInto(data []byte, obj interface{}) error {
}
// Version and Kind should be blank in memory.
blankVersionAndKind := s.MetaInsertionFactory.Create("", "")
err = s.converter.Convert(blankVersionAndKind, obj, SourceToDest|IgnoreMissingFields|AllowDifferentFieldNames)
if err != nil {
return err
}
return nil
return s.SetVersionAndKind("", "", obj)
}