info.go 5.5 KB

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