info.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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) sanitize() {
  83. for k := range e {
  84. if !isExtensionKey(k) {
  85. delete(e, k)
  86. }
  87. }
  88. }
  89. func (e Extensions) sanitizeWithExtra() (extra map[string]any) {
  90. for k, v := range e {
  91. if !isExtensionKey(k) {
  92. if extra == nil {
  93. extra = make(map[string]any)
  94. }
  95. extra[k] = v
  96. delete(e, k)
  97. }
  98. }
  99. return extra
  100. }
  101. func isExtensionKey(k string) bool {
  102. return len(k) > 1 && (k[0] == 'x' || k[0] == 'X') && k[1] == '-'
  103. }
  104. // VendorExtensible composition block.
  105. type VendorExtensible struct {
  106. Extensions Extensions
  107. }
  108. // AddExtension adds an extension to this extensible object
  109. func (v *VendorExtensible) AddExtension(key string, value interface{}) {
  110. if value == nil {
  111. return
  112. }
  113. if v.Extensions == nil {
  114. v.Extensions = make(map[string]interface{})
  115. }
  116. v.Extensions.Add(key, value)
  117. }
  118. // MarshalJSON marshals the extensions to json
  119. func (v VendorExtensible) MarshalJSON() ([]byte, error) {
  120. toser := make(map[string]interface{})
  121. for k, v := range v.Extensions {
  122. lk := strings.ToLower(k)
  123. if strings.HasPrefix(lk, "x-") {
  124. toser[k] = v
  125. }
  126. }
  127. return json.Marshal(toser)
  128. }
  129. // UnmarshalJSON for this extensible object
  130. func (v *VendorExtensible) UnmarshalJSON(data []byte) error {
  131. var d map[string]interface{}
  132. if err := json.Unmarshal(data, &d); err != nil {
  133. return err
  134. }
  135. for k, vv := range d {
  136. lk := strings.ToLower(k)
  137. if strings.HasPrefix(lk, "x-") {
  138. if v.Extensions == nil {
  139. v.Extensions = map[string]interface{}{}
  140. }
  141. v.Extensions[k] = vv
  142. }
  143. }
  144. return nil
  145. }
  146. // InfoProps the properties for an info definition
  147. type InfoProps struct {
  148. Description string `json:"description,omitempty"`
  149. Title string `json:"title,omitempty"`
  150. TermsOfService string `json:"termsOfService,omitempty"`
  151. Contact *ContactInfo `json:"contact,omitempty"`
  152. License *License `json:"license,omitempty"`
  153. Version string `json:"version,omitempty"`
  154. }
  155. // Info object provides metadata about the API.
  156. // The metadata can be used by the clients if needed, and can be presented in the Swagger-UI for convenience.
  157. //
  158. // For more information: http://goo.gl/8us55a#infoObject
  159. type Info struct {
  160. VendorExtensible
  161. InfoProps
  162. }
  163. // MarshalJSON marshal this to JSON
  164. func (i Info) MarshalJSON() ([]byte, error) {
  165. b1, err := json.Marshal(i.InfoProps)
  166. if err != nil {
  167. return nil, err
  168. }
  169. b2, err := json.Marshal(i.VendorExtensible)
  170. if err != nil {
  171. return nil, err
  172. }
  173. return swag.ConcatJSON(b1, b2), nil
  174. }
  175. // UnmarshalJSON marshal this from JSON
  176. func (i *Info) UnmarshalJSON(data []byte) error {
  177. if internal.UseOptimizedJSONUnmarshaling {
  178. return jsonv2.Unmarshal(data, i)
  179. }
  180. if err := json.Unmarshal(data, &i.InfoProps); err != nil {
  181. return err
  182. }
  183. return json.Unmarshal(data, &i.VendorExtensible)
  184. }
  185. func (i *Info) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  186. var x struct {
  187. Extensions
  188. InfoProps
  189. }
  190. if err := opts.UnmarshalNext(dec, &x); err != nil {
  191. return err
  192. }
  193. x.Extensions.sanitize()
  194. if len(x.Extensions) == 0 {
  195. x.Extensions = nil
  196. }
  197. i.VendorExtensible.Extensions = x.Extensions
  198. i.InfoProps = x.InfoProps
  199. return nil
  200. }