| 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 Server struct { |
| 30 |
ServerProps |
| 31 |
spec.VendorExtensible |
| 32 |
} |
| 33 |
|
| 34 |
type ServerProps struct { |
| 35 |
// Description is a short description of the target documentation. CommonMark syntax MAY be used for rich text representation. |
| 36 |
Description string `json:"description,omitempty"` |
| 37 |
// URL is the URL for the target documentation. |
| 38 |
URL string `json:"url"` |
| 39 |
// Variables contains a map between a variable name and its value. The value is used for substitution in the server's URL templeate |
| 40 |
Variables map[string]*ServerVariable `json:"variables,omitempty"` |
| 41 |
} |
| 42 |
|
| 43 |
// MarshalJSON is a custom marshal function that knows how to encode Responses as JSON |
| 44 |
func (s *Server) MarshalJSON() ([]byte, error) { |
| 45 |
if internal.UseOptimizedJSONMarshalingV3 { |
| 46 |
return internal.DeterministicMarshal(s) |
| 47 |
} |
| 48 |
b1, err := json.Marshal(s.ServerProps) |
| 49 |
if err != nil { |
| 50 |
return nil, err |
| 51 |
} |
| 52 |
b2, err := json.Marshal(s.VendorExtensible) |
| 53 |
if err != nil { |
| 54 |
return nil, err |
| 55 |
} |
| 56 |
return swag.ConcatJSON(b1, b2), nil |
| 57 |
} |
| 58 |
|
| 59 |
func (s *Server) MarshalJSONTo(enc *jsontext.Encoder) error { |
| 60 |
var x struct { |
| 61 |
ServerProps `json:",inline"` |
| 62 |
Extensions spec.Extensions `json:",inline"` |
| 63 |
} |
| 64 |
x.Extensions = internal.SanitizeExtensions(s.Extensions) |
| 65 |
x.ServerProps = s.ServerProps |
| 66 |
return jsonv2.MarshalEncode(enc, x) |
| 67 |
} |
| 68 |
|
| 69 |
func (s *Server) UnmarshalJSON(data []byte) error { |
| 70 |
if internal.UseOptimizedJSONUnmarshalingV3 { |
| 71 |
return jsonv2.Unmarshal(data, s) |
| 72 |
} |
| 73 |
|
| 74 |
if err := json.Unmarshal(data, &s.ServerProps); err != nil { |
| 75 |
return err |
| 76 |
} |
| 77 |
if err := json.Unmarshal(data, &s.VendorExtensible); err != nil { |
| 78 |
return err |
| 79 |
} |
| 80 |
return nil |
| 81 |
} |
| 82 |
|
| 83 |
func (s *Server) UnmarshalJSONFrom(dec *jsontext.Decoder) error { |
| 84 |
var x struct { |
| 85 |
Extensions spec.Extensions `json:",inline"` |
| 86 |
ServerProps |
| 87 |
} |
| 88 |
if err := jsonv2.UnmarshalDecode(dec, &x); err != nil { |
| 89 |
return err |
| 90 |
} |
| 91 |
s.Extensions = internal.SanitizeExtensions(x.Extensions) |
| 92 |
s.ServerProps = x.ServerProps |
| 93 |
|
| 94 |
return nil |
| 95 |
} |
| 96 |
|
| 97 |
type ServerVariable struct { |
| 98 |
ServerVariableProps |
| 99 |
spec.VendorExtensible |
| 100 |
} |
| 101 |
|
| 102 |
type ServerVariableProps struct { |
| 103 |
// Enum is an enumeration of string values to be used if the substitution options are from a limited set |
| 104 |
Enum []string `json:"enum,omitempty"` |
| 105 |
// Default is the default value to use for substitution, which SHALL be sent if an alternate value is not supplied |
| 106 |
Default string `json:"default"` |
| 107 |
// Description is a description for the server variable |
| 108 |
Description string `json:"description,omitempty"` |
| 109 |
} |
| 110 |
|
| 111 |
// MarshalJSON is a custom marshal function that knows how to encode Responses as JSON |
| 112 |
func (s *ServerVariable) MarshalJSON() ([]byte, error) { |
| 113 |
if internal.UseOptimizedJSONMarshalingV3 { |
| 114 |
return internal.DeterministicMarshal(s) |
| 115 |
} |
| 116 |
b1, err := json.Marshal(s.ServerVariableProps) |
| 117 |
if err != nil { |
| 118 |
return nil, err |
| 119 |
} |
| 120 |
b2, err := json.Marshal(s.VendorExtensible) |
| 121 |
if err != nil { |
| 122 |
return nil, err |
| 123 |
} |
| 124 |
return swag.ConcatJSON(b1, b2), nil |
| 125 |
} |
| 126 |
|
| 127 |
func (s *ServerVariable) MarshalJSONTo(enc *jsontext.Encoder) error { |
| 128 |
var x struct { |
| 129 |
ServerVariableProps `json:",inline"` |
| 130 |
Extensions spec.Extensions `json:",inline"` |
| 131 |
} |
| 132 |
x.Extensions = internal.SanitizeExtensions(s.Extensions) |
| 133 |
x.ServerVariableProps = s.ServerVariableProps |
| 134 |
return jsonv2.MarshalEncode(enc, x) |
| 135 |
} |
| 136 |
|
| 137 |
func (s *ServerVariable) UnmarshalJSON(data []byte) error { |
| 138 |
if internal.UseOptimizedJSONUnmarshalingV3 { |
| 139 |
return jsonv2.Unmarshal(data, s) |
| 140 |
} |
| 141 |
if err := json.Unmarshal(data, &s.ServerVariableProps); err != nil { |
| 142 |
return err |
| 143 |
} |
| 144 |
if err := json.Unmarshal(data, &s.VendorExtensible); err != nil { |
| 145 |
return err |
| 146 |
} |
| 147 |
return nil |
| 148 |
} |
| 149 |
|
| 150 |
func (s *ServerVariable) UnmarshalJSONFrom(dec *jsontext.Decoder) error { |
| 151 |
var x struct { |
| 152 |
Extensions spec.Extensions `json:",inline"` |
| 153 |
ServerVariableProps |
| 154 |
} |
| 155 |
if err := jsonv2.UnmarshalDecode(dec, &x); err != nil { |
| 156 |
return err |
| 157 |
} |
| 158 |
s.Extensions = internal.SanitizeExtensions(x.Extensions) |
| 159 |
s.ServerVariableProps = x.ServerVariableProps |
| 160 |
|
| 161 |
return nil |
| 162 |
} |