header.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2015 go-swagger maintainers
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package spec
  15. import (
  16. "encoding/json"
  17. "github.com/go-openapi/swag"
  18. "k8s.io/kube-openapi/pkg/internal"
  19. jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json"
  20. )
  21. const (
  22. jsonArray = "array"
  23. )
  24. // HeaderProps describes a response header
  25. type HeaderProps struct {
  26. Description string `json:"description,omitempty"`
  27. }
  28. // Header describes a header for a response of the API
  29. //
  30. // For more information: http://goo.gl/8us55a#headerObject
  31. type Header struct {
  32. CommonValidations
  33. SimpleSchema
  34. VendorExtensible
  35. HeaderProps
  36. }
  37. // MarshalJSON marshal this to JSON
  38. func (h Header) MarshalJSON() ([]byte, error) {
  39. if internal.UseOptimizedJSONMarshaling {
  40. return internal.DeterministicMarshal(h)
  41. }
  42. b1, err := json.Marshal(h.CommonValidations)
  43. if err != nil {
  44. return nil, err
  45. }
  46. b2, err := json.Marshal(h.SimpleSchema)
  47. if err != nil {
  48. return nil, err
  49. }
  50. b3, err := json.Marshal(h.HeaderProps)
  51. if err != nil {
  52. return nil, err
  53. }
  54. b4, err := json.Marshal(h.VendorExtensible)
  55. if err != nil {
  56. return nil, err
  57. }
  58. return swag.ConcatJSON(b1, b2, b3, b4), nil
  59. }
  60. func (h Header) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
  61. var x struct {
  62. CommonValidations commonValidationsOmitZero `json:",inline"`
  63. SimpleSchema simpleSchemaOmitZero `json:",inline"`
  64. Extensions
  65. HeaderProps
  66. }
  67. x.CommonValidations = commonValidationsOmitZero(h.CommonValidations)
  68. x.SimpleSchema = simpleSchemaOmitZero(h.SimpleSchema)
  69. x.Extensions = internal.SanitizeExtensions(h.Extensions)
  70. x.HeaderProps = h.HeaderProps
  71. return opts.MarshalNext(enc, x)
  72. }
  73. // UnmarshalJSON unmarshals this header from JSON
  74. func (h *Header) UnmarshalJSON(data []byte) error {
  75. if internal.UseOptimizedJSONUnmarshaling {
  76. return jsonv2.Unmarshal(data, h)
  77. }
  78. if err := json.Unmarshal(data, &h.CommonValidations); err != nil {
  79. return err
  80. }
  81. if err := json.Unmarshal(data, &h.SimpleSchema); err != nil {
  82. return err
  83. }
  84. if err := json.Unmarshal(data, &h.VendorExtensible); err != nil {
  85. return err
  86. }
  87. return json.Unmarshal(data, &h.HeaderProps)
  88. }
  89. func (h *Header) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  90. var x struct {
  91. CommonValidations
  92. SimpleSchema
  93. Extensions
  94. HeaderProps
  95. }
  96. if err := opts.UnmarshalNext(dec, &x); err != nil {
  97. return err
  98. }
  99. h.CommonValidations = x.CommonValidations
  100. h.SimpleSchema = x.SimpleSchema
  101. h.Extensions = internal.SanitizeExtensions(x.Extensions)
  102. h.HeaderProps = x.HeaderProps
  103. return nil
  104. }