Debian Codegraph @

pkg/validation/spec/header.go

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