2 Copyright 2014 The Kubernetes Authors.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
22 "k8s.io/apimachinery/pkg/conversion"
23 "k8s.io/apimachinery/pkg/runtime/schema"
26 type encodable struct {
29 versions []schema.GroupVersion
32 func (e encodable) GetObjectKind() schema.ObjectKind { return e.obj.GetObjectKind() }
33 func (e encodable) DeepCopyObject() Object {
35 out.obj = e.obj.DeepCopyObject()
36 copy(out.versions, e.versions)
40 // NewEncodable creates an object that will be encoded with the provided codec on demand.
41 // Provided as a convenience for test cases dealing with internal objects.
42 func NewEncodable(e Encoder, obj Object, versions ...schema.GroupVersion) Object {
43 if _, ok := obj.(*Unknown); ok {
46 return encodable{e, obj, versions}
49 func (e encodable) UnmarshalJSON(in []byte) error {
50 return errors.New("runtime.encodable cannot be unmarshalled from JSON")
53 // Marshal may get called on pointers or values, so implement MarshalJSON on value.
54 // http://stackoverflow.com/questions/21390979/custom-marshaljson-never-gets-called-in-go
55 func (e encodable) MarshalJSON() ([]byte, error) {
56 return Encode(e.E, e.obj)
59 // NewEncodableList creates an object that will be encoded with the provided codec on demand.
60 // Provided as a convenience for test cases dealing with internal objects.
61 func NewEncodableList(e Encoder, objects []Object, versions ...schema.GroupVersion) []Object {
62 out := make([]Object, len(objects))
63 for i := range objects {
64 if _, ok := objects[i].(*Unknown); ok {
68 out[i] = NewEncodable(e, objects[i], versions...)
73 func (e *Unknown) UnmarshalJSON(in []byte) error {
75 return errors.New("runtime.Unknown: UnmarshalJSON on nil pointer")
77 e.TypeMeta = TypeMeta{}
78 e.Raw = append(e.Raw[0:0], in...)
79 e.ContentEncoding = ""
80 e.ContentType = ContentTypeJSON
84 // Marshal may get called on pointers or values, so implement MarshalJSON on value.
85 // http://stackoverflow.com/questions/21390979/custom-marshaljson-never-gets-called-in-go
86 func (e Unknown) MarshalJSON() ([]byte, error) {
87 // If ContentType is unset, we assume this is JSON.
88 if e.ContentType != "" && e.ContentType != ContentTypeJSON {
89 return nil, errors.New("runtime.Unknown: MarshalJSON on non-json data")
92 return []byte("null"), nil
97 func Convert_runtime_Object_To_runtime_RawExtension(in *Object, out *RawExtension, s conversion.Scope) error {
99 out.Raw = []byte("null")
103 if unk, ok := obj.(*Unknown); ok {
118 func Convert_runtime_RawExtension_To_runtime_Object(in *RawExtension, out *Object, s conversion.Scope) error {
119 if in.Object != nil {
124 if len(data) == 0 || (len(data) == 4 && string(data) == "null") {
130 // TODO: Set ContentEncoding and ContentType appropriately.
131 // Currently we set ContentTypeJSON to make tests passing.
132 ContentType: ContentTypeJSON,
137 func DefaultEmbeddedConversions() []interface{} {
138 return []interface{}{
139 Convert_runtime_Object_To_runtime_RawExtension,
140 Convert_runtime_RawExtension_To_runtime_Object,