embedded.go 3.8 KB

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