server.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. Copyright 2021 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package spec3
  14. import (
  15. "encoding/json"
  16. "k8s.io/kube-openapi/pkg/validation/spec"
  17. "github.com/go-openapi/swag"
  18. )
  19. type Server struct {
  20. ServerProps
  21. spec.VendorExtensible
  22. }
  23. type ServerProps struct {
  24. // Description is a short description of the target documentation. CommonMark syntax MAY be used for rich text representation.
  25. Description string `json:"description,omitempty"`
  26. // URL is the URL for the target documentation.
  27. URL string `json:"url"`
  28. // Variables contains a map between a variable name and its value. The value is used for substitution in the server's URL templeate
  29. Variables map[string]*ServerVariable `json:"variables,omitempty"`
  30. }
  31. // MarshalJSON is a custom marshal function that knows how to encode Responses as JSON
  32. func (s *Server) MarshalJSON() ([]byte, error) {
  33. b1, err := json.Marshal(s.ServerProps)
  34. if err != nil {
  35. return nil, err
  36. }
  37. b2, err := json.Marshal(s.VendorExtensible)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return swag.ConcatJSON(b1, b2), nil
  42. }
  43. func (s *Server) UnmarshalJSON(data []byte) error {
  44. if err := json.Unmarshal(data, &s.ServerProps); err != nil {
  45. return err
  46. }
  47. if err := json.Unmarshal(data, &s.VendorExtensible); err != nil {
  48. return err
  49. }
  50. return nil
  51. }
  52. type ServerVariable struct {
  53. ServerVariableProps
  54. spec.VendorExtensible
  55. }
  56. type ServerVariableProps struct {
  57. // Enum is an enumeration of string values to be used if the substitution options are from a limited set
  58. Enum []string `json:"enum,omitempty"`
  59. // Default is the default value to use for substitution, which SHALL be sent if an alternate value is not supplied
  60. Default string `json:"default"`
  61. // Description is a description for the server variable
  62. Description string `json:"description,omitempty"`
  63. }
  64. // MarshalJSON is a custom marshal function that knows how to encode Responses as JSON
  65. func (s *ServerVariable) MarshalJSON() ([]byte, error) {
  66. b1, err := json.Marshal(s.ServerVariableProps)
  67. if err != nil {
  68. return nil, err
  69. }
  70. b2, err := json.Marshal(s.VendorExtensible)
  71. if err != nil {
  72. return nil, err
  73. }
  74. return swag.ConcatJSON(b1, b2), nil
  75. }
  76. func (s *ServerVariable) UnmarshalJSON(data []byte) error {
  77. if err := json.Unmarshal(data, &s.ServerVariableProps); err != nil {
  78. return err
  79. }
  80. if err := json.Unmarshal(data, &s.VendorExtensible); err != nil {
  81. return err
  82. }
  83. return nil
  84. }