| 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 |
"strings" |
| 20 |
|
| 21 |
"github.com/go-openapi/swag" |
| 22 |
"k8s.io/kube-openapi/pkg/internal" |
| 23 |
jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json" |
| 24 |
"k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/jsontext" |
| 25 |
) |
| 26 |
|
| 27 |
// Extensions vendor specific extensions |
| 28 |
type Extensions map[string]interface{} |
| 29 |
|
| 30 |
// Add adds a value to these extensions |
| 31 |
func (e Extensions) Add(key string, value interface{}) { |
| 32 |
realKey := strings.ToLower(key) |
| 33 |
e[realKey] = value |
| 34 |
} |
| 35 |
|
| 36 |
// GetString gets a string value from the extensions |
| 37 |
func (e Extensions) GetString(key string) (string, bool) { |
| 38 |
if v, ok := e[strings.ToLower(key)]; ok { |
| 39 |
str, ok := v.(string) |
| 40 |
return str, ok |
| 41 |
} |
| 42 |
return "", false |
| 43 |
} |
| 44 |
|
| 45 |
// GetBool gets a string value from the extensions |
| 46 |
func (e Extensions) GetBool(key string) (bool, bool) { |
| 47 |
if v, ok := e[strings.ToLower(key)]; ok { |
| 48 |
str, ok := v.(bool) |
| 49 |
return str, ok |
| 50 |
} |
| 51 |
return false, false |
| 52 |
} |
| 53 |
|
| 54 |
// GetStringSlice gets a string value from the extensions |
| 55 |
func (e Extensions) GetStringSlice(key string) ([]string, bool) { |
| 56 |
if v, ok := e[strings.ToLower(key)]; ok { |
| 57 |
arr, isSlice := v.([]interface{}) |
| 58 |
if !isSlice { |
| 59 |
return nil, false |
| 60 |
} |
| 61 |
var strs []string |
| 62 |
for _, iface := range arr { |
| 63 |
str, isString := iface.(string) |
| 64 |
if !isString { |
| 65 |
return nil, false |
| 66 |
} |
| 67 |
strs = append(strs, str) |
| 68 |
} |
| 69 |
return strs, ok |
| 70 |
} |
| 71 |
return nil, false |
| 72 |
} |
| 73 |
|
| 74 |
// GetObject gets the object value from the extensions. |
| 75 |
// out must be a json serializable type; the json go struct |
| 76 |
// tags of out are used to populate it. |
| 77 |
func (e Extensions) GetObject(key string, out interface{}) error { |
| 78 |
// This json serialization/deserialization could be replaced with |
| 79 |
// an approach using reflection if the optimization becomes justified. |
| 80 |
if v, ok := e[strings.ToLower(key)]; ok { |
| 81 |
b, err := json.Marshal(v) |
| 82 |
if err != nil { |
| 83 |
return err |
| 84 |
} |
| 85 |
err = json.Unmarshal(b, out) |
| 86 |
if err != nil { |
| 87 |
return err |
| 88 |
} |
| 89 |
} |
| 90 |
return nil |
| 91 |
} |
| 92 |
|
| 93 |
func (e Extensions) sanitizeWithExtra() (extra map[string]any) { |
| 94 |
for k, v := range e { |
| 95 |
if !internal.IsExtensionKey(k) { |
| 96 |
if extra == nil { |
| 97 |
extra = make(map[string]any) |
| 98 |
} |
| 99 |
extra[k] = v |
| 100 |
delete(e, k) |
| 101 |
} |
| 102 |
} |
| 103 |
return extra |
| 104 |
} |
| 105 |
|
| 106 |
// VendorExtensible composition block. |
| 107 |
type VendorExtensible struct { |
| 108 |
Extensions Extensions |
| 109 |
} |
| 110 |
|
| 111 |
// AddExtension adds an extension to this extensible object |
| 112 |
func (v *VendorExtensible) AddExtension(key string, value interface{}) { |
| 113 |
if value == nil { |
| 114 |
return |
| 115 |
} |
| 116 |
if v.Extensions == nil { |
| 117 |
v.Extensions = make(map[string]interface{}) |
| 118 |
} |
| 119 |
v.Extensions.Add(key, value) |
| 120 |
} |
| 121 |
|
| 122 |
// MarshalJSON marshals the extensions to json |
| 123 |
func (v VendorExtensible) MarshalJSON() ([]byte, error) { |
| 124 |
toser := make(map[string]interface{}) |
| 125 |
for k, v := range v.Extensions { |
| 126 |
lk := strings.ToLower(k) |
| 127 |
if strings.HasPrefix(lk, "x-") { |
| 128 |
toser[k] = v |
| 129 |
} |
| 130 |
} |
| 131 |
return json.Marshal(toser) |
| 132 |
} |
| 133 |
|
| 134 |
// UnmarshalJSON for this extensible object |
| 135 |
func (v *VendorExtensible) UnmarshalJSON(data []byte) error { |
| 136 |
var d map[string]interface{} |
| 137 |
if err := json.Unmarshal(data, &d); err != nil { |
| 138 |
return err |
| 139 |
} |
| 140 |
for k, vv := range d { |
| 141 |
lk := strings.ToLower(k) |
| 142 |
if strings.HasPrefix(lk, "x-") { |
| 143 |
if v.Extensions == nil { |
| 144 |
v.Extensions = map[string]interface{}{} |
| 145 |
} |
| 146 |
v.Extensions[k] = vv |
| 147 |
} |
| 148 |
} |
| 149 |
return nil |
| 150 |
} |
| 151 |
|
| 152 |
// InfoProps the properties for an info definition |
| 153 |
type InfoProps struct { |
| 154 |
Description string `json:"description,omitempty"` |
| 155 |
Title string `json:"title,omitempty"` |
| 156 |
TermsOfService string `json:"termsOfService,omitempty"` |
| 157 |
Contact *ContactInfo `json:"contact,omitempty"` |
| 158 |
License *License `json:"license,omitempty"` |
| 159 |
Version string `json:"version,omitempty"` |
| 160 |
} |
| 161 |
|
| 162 |
// Info object provides metadata about the API. |
| 163 |
// The metadata can be used by the clients if needed, and can be presented in the Swagger-UI for convenience. |
| 164 |
// |
| 165 |
// For more information: http://goo.gl/8us55a#infoObject |
| 166 |
type Info struct { |
| 167 |
VendorExtensible |
| 168 |
InfoProps |
| 169 |
} |
| 170 |
|
| 171 |
// MarshalJSON marshal this to JSON |
| 172 |
func (i Info) MarshalJSON() ([]byte, error) { |
| 173 |
if internal.UseOptimizedJSONMarshaling { |
| 174 |
return internal.DeterministicMarshal(i) |
| 175 |
} |
| 176 |
b1, err := json.Marshal(i.InfoProps) |
| 177 |
if err != nil { |
| 178 |
return nil, err |
| 179 |
} |
| 180 |
b2, err := json.Marshal(i.VendorExtensible) |
| 181 |
if err != nil { |
| 182 |
return nil, err |
| 183 |
} |
| 184 |
return swag.ConcatJSON(b1, b2), nil |
| 185 |
} |
| 186 |
|
| 187 |
func (i Info) MarshalJSONTo(enc *jsontext.Encoder) error { |
| 188 |
var x struct { |
| 189 |
Extensions Extensions `json:",inline"` |
| 190 |
InfoProps |
| 191 |
} |
| 192 |
x.Extensions = i.Extensions |
| 193 |
x.InfoProps = i.InfoProps |
| 194 |
return jsonv2.MarshalEncode(enc, x) |
| 195 |
} |
| 196 |
|
| 197 |
// UnmarshalJSON marshal this from JSON |
| 198 |
func (i *Info) UnmarshalJSON(data []byte) error { |
| 199 |
if internal.UseOptimizedJSONUnmarshaling { |
| 200 |
return jsonv2.Unmarshal(data, i) |
| 201 |
} |
| 202 |
|
| 203 |
if err := json.Unmarshal(data, &i.InfoProps); err != nil { |
| 204 |
return err |
| 205 |
} |
| 206 |
return json.Unmarshal(data, &i.VendorExtensible) |
| 207 |
} |
| 208 |
|
| 209 |
func (i *Info) UnmarshalJSONFrom(dec *jsontext.Decoder) error { |
| 210 |
var x struct { |
| 211 |
Extensions Extensions `json:",inline"` |
| 212 |
InfoProps |
| 213 |
} |
| 214 |
if err := jsonv2.UnmarshalDecode(dec, &x); err != nil { |
| 215 |
return err |
| 216 |
} |
| 217 |
i.Extensions = internal.SanitizeExtensions(x.Extensions) |
| 218 |
i.InfoProps = x.InfoProps |
| 219 |
return nil |
| 220 |
} |