github.com/aclisp/heapster@v0.19.2-0.20160613100040-51756f899a96/Godeps/_workspace/src/k8s.io/kubernetes/pkg/runtime/embedded.go (about) 1 /* 2 Copyright 2014 The Kubernetes Authors All rights reserved. 3 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 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 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. 15 */ 16 17 package runtime 18 19 import ( 20 "errors" 21 22 "k8s.io/kubernetes/pkg/api/unversioned" 23 "k8s.io/kubernetes/pkg/conversion" 24 ) 25 26 type encodable struct { 27 E Encoder `json:"-"` 28 obj Object 29 versions []unversioned.GroupVersion 30 } 31 32 func (e encodable) GetObjectKind() unversioned.ObjectKind { return e.obj.GetObjectKind() } 33 34 // NewEncodable creates an object that will be encoded with the provided codec on demand. 35 // Provided as a convenience for test cases dealing with internal objects. 36 func NewEncodable(e Encoder, obj Object, versions ...unversioned.GroupVersion) Object { 37 if _, ok := obj.(*Unknown); ok { 38 return obj 39 } 40 return encodable{e, obj, versions} 41 } 42 43 func (re encodable) UnmarshalJSON(in []byte) error { 44 return errors.New("runtime.encodable cannot be unmarshalled from JSON") 45 } 46 47 // Marshal may get called on pointers or values, so implement MarshalJSON on value. 48 // http://stackoverflow.com/questions/21390979/custom-marshaljson-never-gets-called-in-go 49 func (re encodable) MarshalJSON() ([]byte, error) { 50 return Encode(re.E, re.obj) 51 } 52 53 // NewEncodableList creates an object that will be encoded with the provided codec on demand. 54 // Provided as a convenience for test cases dealing with internal objects. 55 func NewEncodableList(e Encoder, objects []Object, versions ...unversioned.GroupVersion) []Object { 56 out := make([]Object, len(objects)) 57 for i := range objects { 58 if _, ok := objects[i].(*Unknown); ok { 59 out[i] = objects[i] 60 continue 61 } 62 out[i] = NewEncodable(e, objects[i], versions...) 63 } 64 return out 65 } 66 67 func (re *Unknown) UnmarshalJSON(in []byte) error { 68 if re == nil { 69 return errors.New("runtime.Unknown: UnmarshalJSON on nil pointer") 70 } 71 re.TypeMeta = TypeMeta{} 72 re.Raw = append(re.Raw[0:0], in...) 73 re.ContentEncoding = "" 74 re.ContentType = ContentTypeJSON 75 return nil 76 } 77 78 // Marshal may get called on pointers or values, so implement MarshalJSON on value. 79 // http://stackoverflow.com/questions/21390979/custom-marshaljson-never-gets-called-in-go 80 func (re Unknown) MarshalJSON() ([]byte, error) { 81 // If ContentType is unset, we assume this is JSON. 82 if re.ContentType != "" && re.ContentType != ContentTypeJSON { 83 return nil, errors.New("runtime.Unknown: MarshalJSON on non-json data") 84 } 85 if re.Raw == nil { 86 return []byte("null"), nil 87 } 88 return re.Raw, nil 89 } 90 91 func Convert_runtime_Object_To_runtime_RawExtension(in *Object, out *RawExtension, s conversion.Scope) error { 92 if in == nil { 93 out.Raw = []byte("null") 94 return nil 95 } 96 obj := *in 97 if unk, ok := obj.(*Unknown); ok { 98 if unk.Raw != nil { 99 out.Raw = unk.Raw 100 return nil 101 } 102 obj = out.Object 103 } 104 if obj == nil { 105 out.Raw = nil 106 return nil 107 } 108 out.Object = obj 109 return nil 110 } 111 112 func Convert_runtime_RawExtension_To_runtime_Object(in *RawExtension, out *Object, s conversion.Scope) error { 113 if in.Object != nil { 114 *out = in.Object 115 return nil 116 } 117 data := in.Raw 118 if len(data) == 0 || (len(data) == 4 && string(data) == "null") { 119 *out = nil 120 return nil 121 } 122 *out = &Unknown{ 123 Raw: data, 124 // TODO: Set ContentEncoding and ContentType appropriately. 125 // Currently we set ContentTypeJSON to make tests passing. 126 ContentType: ContentTypeJSON, 127 } 128 return nil 129 } 130 131 func DefaultEmbeddedConversions() []interface{} { 132 return []interface{}{ 133 Convert_runtime_Object_To_runtime_RawExtension, 134 Convert_runtime_RawExtension_To_runtime_Object, 135 } 136 }