| 1 |
/* |
| 2 |
Copyright 2021 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package spec3 |
| 18 |
|
| 19 |
import ( |
| 20 |
"encoding/json" |
| 21 |
|
| 22 |
"github.com/go-openapi/swag" |
| 23 |
"k8s.io/kube-openapi/pkg/internal" |
| 24 |
jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json" |
| 25 |
"k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/jsontext" |
| 26 |
"k8s.io/kube-openapi/pkg/validation/spec" |
| 27 |
) |
| 28 |
|
| 29 |
type Encoding struct { |
| 30 |
EncodingProps |
| 31 |
spec.VendorExtensible |
| 32 |
} |
| 33 |
|
| 34 |
// MarshalJSON is a custom marshal function that knows how to encode Encoding as JSON |
| 35 |
func (e *Encoding) MarshalJSON() ([]byte, error) { |
| 36 |
if internal.UseOptimizedJSONMarshalingV3 { |
| 37 |
return internal.DeterministicMarshal(e) |
| 38 |
} |
| 39 |
b1, err := json.Marshal(e.EncodingProps) |
| 40 |
if err != nil { |
| 41 |
return nil, err |
| 42 |
} |
| 43 |
b2, err := json.Marshal(e.VendorExtensible) |
| 44 |
if err != nil { |
| 45 |
return nil, err |
| 46 |
} |
| 47 |
return swag.ConcatJSON(b1, b2), nil |
| 48 |
} |
| 49 |
|
| 50 |
func (e *Encoding) MarshalJSONTo(enc *jsontext.Encoder) error { |
| 51 |
var x struct { |
| 52 |
EncodingProps encodingPropsOmitZero `json:",inline"` |
| 53 |
Extensions spec.Extensions `json:",inline"` |
| 54 |
} |
| 55 |
x.Extensions = internal.SanitizeExtensions(e.Extensions) |
| 56 |
x.EncodingProps = encodingPropsOmitZero(e.EncodingProps) |
| 57 |
return jsonv2.MarshalEncode(enc, x) |
| 58 |
} |
| 59 |
|
| 60 |
func (e *Encoding) UnmarshalJSON(data []byte) error { |
| 61 |
if internal.UseOptimizedJSONUnmarshalingV3 { |
| 62 |
return jsonv2.Unmarshal(data, e) |
| 63 |
} |
| 64 |
if err := json.Unmarshal(data, &e.EncodingProps); err != nil { |
| 65 |
return err |
| 66 |
} |
| 67 |
if err := json.Unmarshal(data, &e.VendorExtensible); err != nil { |
| 68 |
return err |
| 69 |
} |
| 70 |
return nil |
| 71 |
} |
| 72 |
|
| 73 |
func (e *Encoding) UnmarshalJSONFrom(dec *jsontext.Decoder) error { |
| 74 |
var x struct { |
| 75 |
Extensions spec.Extensions `json:",inline"` |
| 76 |
EncodingProps |
| 77 |
} |
| 78 |
if err := jsonv2.UnmarshalDecode(dec, &x); err != nil { |
| 79 |
return err |
| 80 |
} |
| 81 |
|
| 82 |
e.Extensions = internal.SanitizeExtensions(x.Extensions) |
| 83 |
e.EncodingProps = x.EncodingProps |
| 84 |
return nil |
| 85 |
} |
| 86 |
|
| 87 |
type EncodingProps struct { |
| 88 |
// Content Type for encoding a specific property |
| 89 |
ContentType string `json:"contentType,omitempty"` |
| 90 |
// A map allowing additional information to be provided as headers |
| 91 |
Headers map[string]*Header `json:"headers,omitempty"` |
| 92 |
// Describes how a specific property value will be serialized depending on its type |
| 93 |
Style string `json:"style,omitempty"` |
| 94 |
// When this is true, property values of type array or object generate separate parameters for each value of the array, or key-value-pair of the map. For other types of properties this property has no effect |
| 95 |
Explode bool `json:"explode,omitempty"` |
| 96 |
// AllowReserved determines whether the parameter value SHOULD allow reserved characters, as defined by RFC3986 |
| 97 |
AllowReserved bool `json:"allowReserved,omitempty"` |
| 98 |
} |
| 99 |
|
| 100 |
type encodingPropsOmitZero struct { |
| 101 |
ContentType string `json:"contentType,omitempty"` |
| 102 |
Headers map[string]*Header `json:"headers,omitempty"` |
| 103 |
Style string `json:"style,omitempty"` |
| 104 |
Explode bool `json:"explode,omitzero"` |
| 105 |
AllowReserved bool `json:"allowReserved,omitzero"` |
| 106 |
} |