Commit 2906f852 authored by Mike Danese's avatar Mike Danese

allow conversions.Scheme to expose intermidiate versioned api object

parent 5bd928fe
...@@ -22,31 +22,38 @@ import ( ...@@ -22,31 +22,38 @@ import (
"fmt" "fmt"
) )
// Decode converts a JSON string back into a pointer to an api object. func (s *Scheme) DecodeToVersionedObject(data []byte) (obj interface{}, version, kind string, err error) {
// Deduces the type based upon the fields added by the MetaInsertionFactory version, kind, err = s.DataVersionAndKind(data)
// technique. The object will be converted, if necessary, into the
// s.InternalVersion type before being returned. Decode will not decode
// objects without version set unless InternalVersion is also "".
func (s *Scheme) Decode(data []byte) (interface{}, error) {
version, kind, err := s.DataVersionAndKind(data)
if err != nil { if err != nil {
return nil, err return
} }
if version == "" && s.InternalVersion != "" { if version == "" && s.InternalVersion != "" {
return nil, fmt.Errorf("version not set in '%s'", string(data)) return nil, "", "", fmt.Errorf("version not set in '%s'", string(data))
} }
if kind == "" { if kind == "" {
return nil, fmt.Errorf("kind not set in '%s'", string(data)) return nil, "", "", fmt.Errorf("kind not set in '%s'", string(data))
} }
obj, err := s.NewObject(version, kind) obj, err = s.NewObject(version, kind)
if err != nil { if err != nil {
return nil, err return nil, "", "", err
} }
if err := json.Unmarshal(data, obj); err != nil { if err := json.Unmarshal(data, obj); err != nil {
return nil, err return nil, "", "", err
} }
return
}
// Decode converts a JSON string back into a pointer to an api object.
// Deduces the type based upon the fields added by the MetaInsertionFactory
// technique. The object will be converted, if necessary, into the
// s.InternalVersion type before being returned. Decode will not decode
// objects without version set unless InternalVersion is also "".
func (s *Scheme) Decode(data []byte) (interface{}, error) {
obj, version, kind, err := s.DecodeToVersionedObject(data)
if err != nil {
return nil, err
}
// Version and Kind should be blank in memory. // Version and Kind should be blank in memory.
if err := s.SetVersionAndKind("", "", obj); err != nil { if err := s.SetVersionAndKind("", "", obj); err != nil {
return nil, err return nil, err
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment