Do not automatically decode runtime.RawExtension

Make clients opt in to decoding objects that are stored
in the generic api.List object by invoking runtime.DecodeList()
with a set of schemes. Makes it easier to handle unknown
schema objects because decoding is in the control of the code.

Add runtime.Unstructured, which is a simple in memory
representation of an external object.
This commit is contained in:
Clayton Coleman
2015-04-28 23:15:16 -04:00
parent a4316aa638
commit 12ba4e2452
20 changed files with 391 additions and 61 deletions

View File

@@ -17,6 +17,7 @@ limitations under the License.
package runtime
import (
"encoding/json"
"fmt"
"net/url"
"reflect"
@@ -164,7 +165,15 @@ func (self *Scheme) runtimeObjectToRawExtensionArray(in *[]Object, out *[]RawExt
for i := range src {
switch t := src[i].(type) {
case *Unknown:
// TODO: this should be decoupled from the scheme (since it is JSON specific)
dest[i].RawJSON = t.RawJSON
case *Unstructured:
// TODO: this should be decoupled from the scheme (since it is JSON specific)
data, err := json.Marshal(t.Object)
if err != nil {
return err
}
dest[i].RawJSON = data
default:
version := outVersion
// if the object exists
@@ -192,24 +201,17 @@ func (self *Scheme) rawExtensionToRuntimeObjectArray(in *[]RawExtension, out *[]
for i := range src {
data := src[i].RawJSON
obj, err := scheme.Decode(data)
version, kind, err := scheme.raw.DataVersionAndKind(data)
if err != nil {
if !IsNotRegisteredError(err) {
return err
}
version, kind, err := scheme.raw.DataVersionAndKind(data)
if err != nil {
return err
}
obj = &Unknown{
TypeMeta: TypeMeta{
APIVersion: version,
Kind: kind,
},
RawJSON: data,
}
return err
}
dest[i] = &Unknown{
TypeMeta: TypeMeta{
APIVersion: version,
Kind: kind,
},
RawJSON: data,
}
dest[i] = obj
}
*out = dest
return nil
@@ -275,6 +277,12 @@ func (s *Scheme) ObjectVersionAndKind(obj Object) (version, kind string, err err
return s.raw.ObjectVersionAndKind(obj)
}
// Recognizes returns true if the scheme is able to handle the provided version and kind
// of an object.
func (s *Scheme) Recognizes(version, kind string) bool {
return s.raw.Recognizes(version, kind)
}
// New returns a new API object of the given version ("" for internal
// representation) and name, or an error if it hasn't been registered.
func (s *Scheme) New(versionName, typeName string) (Object, error) {