| 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 | |
| 15 | package spec |
| 16 | |
| 17 | import ( |
| 18 | "encoding/json" |
| 19 | |
| 20 | "github.com/go-openapi/swag" |
| 21 | "k8s.io/kube-openapi/pkg/internal" |
| 22 | jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json" |
| 23 | ) |
| 24 | |
| 25 | const ( |
| 26 | jsonArray = "array" |
| 27 | ) |
| 28 | |
| 29 | // HeaderProps describes a response header |
| 30 | type HeaderProps struct { |
| 31 | Description string `json:"description,omitempty"` |
| 32 | } |
| 33 | |
| 34 | // Header describes a header for a response of the API |
| 35 | // |
| 36 | // For more information: http://goo.gl/8us55a#headerObject |
| 37 | type Header struct { |
| 38 | CommonValidations |
| 39 | SimpleSchema |
| 40 | VendorExtensible |
| 41 | HeaderProps |
| 42 | } |
| 43 | |
| 44 | // MarshalJSON marshal this to JSON |
| 45 | func (h Header) MarshalJSON() ([]byte, error) { |
| 46 | if internal.UseOptimizedJSONMarshaling { |
| 47 | return internal.DeterministicMarshal(h) |
| 48 | } |
| 49 | b1, err := json.Marshal(h.CommonValidations) |
| 50 | if err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | b2, err := json.Marshal(h.SimpleSchema) |
| 54 | if err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | b3, err := json.Marshal(h.HeaderProps) |
| 58 | if err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | b4, err := json.Marshal(h.VendorExtensible) |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | return swag.ConcatJSON(b1, b2, b3, b4), nil |
| 66 | } |
| 67 | |
| 68 | func (h Header) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error { |
| 69 | var x struct { |
| 70 | CommonValidations commonValidationsOmitZero `json:",inline"` |
| 71 | SimpleSchema simpleSchemaOmitZero `json:",inline"` |
| 72 | Extensions |
| 73 | HeaderProps |
| 74 | } |
| 75 | x.CommonValidations = commonValidationsOmitZero(h.CommonValidations) |
| 76 | x.SimpleSchema = simpleSchemaOmitZero(h.SimpleSchema) |
| 77 | x.Extensions = internal.SanitizeExtensions(h.Extensions) |
| 78 | x.HeaderProps = h.HeaderProps |
| 79 | return opts.MarshalNext(enc, x) |
| 80 | } |
| 81 | |
| 82 | // UnmarshalJSON unmarshals this header from JSON |
| 83 | func (h *Header) UnmarshalJSON(data []byte) error { |
| 84 | if internal.UseOptimizedJSONUnmarshaling { |
| 85 | return jsonv2.Unmarshal(data, h) |
| 86 | } |
| 87 | |
| 88 | if err := json.Unmarshal(data, &h.CommonValidations); err != nil { |
| 89 | return err |
| 90 | } |
| 91 | if err := json.Unmarshal(data, &h.SimpleSchema); err != nil { |
| 92 | return err |
| 93 | } |
| 94 | if err := json.Unmarshal(data, &h.VendorExtensible); err != nil { |
| 95 | return err |
| 96 | } |
| 97 | return json.Unmarshal(data, &h.HeaderProps) |
| 98 | } |
| 99 | |
| 100 | func (h *Header) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error { |
| 101 | var x struct { |
| 102 | CommonValidations |
| 103 | SimpleSchema |
| 104 | Extensions |
| 105 | HeaderProps |
| 106 | } |
| 107 | |
| 108 | if err := opts.UnmarshalNext(dec, &x); err != nil { |
| 109 | return err |
| 110 | } |
| 111 | |
| 112 | h.CommonValidations = x.CommonValidations |
| 113 | h.SimpleSchema = x.SimpleSchema |
| 114 | h.Extensions = internal.SanitizeExtensions(x.Extensions) |
| 115 | h.HeaderProps = x.HeaderProps |
| 116 | |
| 117 | return nil |
| 118 | } |