header.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. b1, err := json.Marshal(h.CommonValidations)
  40. if err != nil {
  41. return nil, err
  42. }
  43. b2, err := json.Marshal(h.SimpleSchema)
  44. if err != nil {
  45. return nil, err
  46. }
  47. b3, err := json.Marshal(h.HeaderProps)
  48. if err != nil {
  49. return nil, err
  50. }
  51. b4, err := json.Marshal(h.VendorExtensible)
  52. if err != nil {
  53. return nil, err
  54. }
  55. return swag.ConcatJSON(b1, b2, b3, b4), nil
  56. }
  57. // UnmarshalJSON unmarshals this header from JSON
  58. func (h *Header) UnmarshalJSON(data []byte) error {
  59. if internal.UseOptimizedJSONUnmarshaling {
  60. return jsonv2.Unmarshal(data, h)
  61. }
  62. if err := json.Unmarshal(data, &h.CommonValidations); err != nil {
  63. return err
  64. }
  65. if err := json.Unmarshal(data, &h.SimpleSchema); err != nil {
  66. return err
  67. }
  68. if err := json.Unmarshal(data, &h.VendorExtensible); err != nil {
  69. return err
  70. }
  71. return json.Unmarshal(data, &h.HeaderProps)
  72. }
  73. func (h *Header) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  74. var x struct {
  75. CommonValidations
  76. SimpleSchema
  77. Extensions
  78. HeaderProps
  79. }
  80. if err := opts.UnmarshalNext(dec, &x); err != nil {
  81. return err
  82. }
  83. h.CommonValidations = x.CommonValidations
  84. h.SimpleSchema = x.SimpleSchema
  85. h.Extensions = x.Extensions
  86. h.HeaderProps = x.HeaderProps
  87. h.Extensions.sanitize()
  88. if len(h.Extensions) == 0 {
  89. h.Extensions = nil
  90. }
  91. return nil
  92. }