embedded.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package runtime
  14. import (
  15. "errors"
  16. "k8s.io/apimachinery/pkg/conversion"
  17. "k8s.io/apimachinery/pkg/runtime/schema"
  18. )
  19. type encodable struct {
  20. E Encoder `json:"-"`
  21. obj Object
  22. versions []schema.GroupVersion
  23. }
  24. func (e encodable) GetObjectKind() schema.ObjectKind { return e.obj.GetObjectKind() }
  25. func (e encodable) DeepCopyObject() Object {
  26. var out encodable = e
  27. out.obj = e.obj.DeepCopyObject()
  28. copy(out.versions, e.versions)
  29. return out
  30. }
  31. // NewEncodable creates an object that will be encoded with the provided codec on demand.
  32. // Provided as a convenience for test cases dealing with internal objects.
  33. func NewEncodable(e Encoder, obj Object, versions ...schema.GroupVersion) Object {
  34. if _, ok := obj.(*Unknown); ok {
  35. return obj
  36. }
  37. return encodable{e, obj, versions}
  38. }
  39. func (re encodable) UnmarshalJSON(in []byte) error {
  40. return errors.New("runtime.encodable cannot be unmarshalled from JSON")
  41. }
  42. // Marshal may get called on pointers or values, so implement MarshalJSON on value.
  43. // http://stackoverflow.com/questions/21390979/custom-marshaljson-never-gets-called-in-go
  44. func (re encodable) MarshalJSON() ([]byte, error) {
  45. return Encode(re.E, re.obj)
  46. }
  47. // NewEncodableList creates an object that will be encoded with the provided codec on demand.
  48. // Provided as a convenience for test cases dealing with internal objects.
  49. func NewEncodableList(e Encoder, objects []Object, versions ...schema.GroupVersion) []Object {
  50. out := make([]Object, len(objects))
  51. for i := range objects {
  52. if _, ok := objects[i].(*Unknown); ok {
  53. out[i] = objects[i]
  54. continue
  55. }
  56. out[i] = NewEncodable(e, objects[i], versions...)
  57. }
  58. return out
  59. }
  60. func (re *Unknown) UnmarshalJSON(in []byte) error {
  61. if re == nil {
  62. return errors.New("runtime.Unknown: UnmarshalJSON on nil pointer")
  63. }
  64. re.TypeMeta = TypeMeta{}
  65. re.Raw = append(re.Raw[0:0], in...)
  66. re.ContentEncoding = ""
  67. re.ContentType = ContentTypeJSON
  68. return nil
  69. }
  70. // Marshal may get called on pointers or values, so implement MarshalJSON on value.
  71. // http://stackoverflow.com/questions/21390979/custom-marshaljson-never-gets-called-in-go
  72. func (re Unknown) MarshalJSON() ([]byte, error) {
  73. // If ContentType is unset, we assume this is JSON.
  74. if re.ContentType != "" && re.ContentType != ContentTypeJSON {
  75. return nil, errors.New("runtime.Unknown: MarshalJSON on non-json data")
  76. }
  77. if re.Raw == nil {
  78. return []byte("null"), nil
  79. }
  80. return re.Raw, nil
  81. }
  82. func Convert_runtime_Object_To_runtime_RawExtension(in *Object, out *RawExtension, s conversion.Scope) error {
  83. if in == nil {
  84. out.Raw = []byte("null")
  85. return nil
  86. }
  87. obj := *in
  88. if unk, ok := obj.(*Unknown); ok {
  89. if unk.Raw != nil {
  90. out.Raw = unk.Raw
  91. return nil
  92. }
  93. obj = out.Object
  94. }
  95. if obj == nil {
  96. out.Raw = nil
  97. return nil
  98. }
  99. out.Object = obj
  100. return nil
  101. }
  102. func Convert_runtime_RawExtension_To_runtime_Object(in *RawExtension, out *Object, s conversion.Scope) error {
  103. if in.Object != nil {
  104. *out = in.Object
  105. return nil
  106. }
  107. data := in.Raw
  108. if len(data) == 0 || (len(data) == 4 && string(data) == "null") {
  109. *out = nil
  110. return nil
  111. }
  112. *out = &Unknown{
  113. Raw: data,
  114. // TODO: Set ContentEncoding and ContentType appropriately.
  115. // Currently we set ContentTypeJSON to make tests passing.
  116. ContentType: ContentTypeJSON,
  117. }
  118. return nil
  119. }
  120. func DefaultEmbeddedConversions() []interface{} {
  121. return []interface{}{
  122. Convert_runtime_Object_To_runtime_RawExtension,
  123. Convert_runtime_RawExtension_To_runtime_Object,
  124. }
  125. }