example.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. // Example https://swagger.io/specification/#example-object
  20. type Example struct {
  21. spec.Refable
  22. ExampleProps
  23. spec.VendorExtensible
  24. }
  25. // MarshalJSON is a custom marshal function that knows how to encode RequestBody as JSON
  26. func (e *Example) MarshalJSON() ([]byte, error) {
  27. b1, err := json.Marshal(e.Refable)
  28. if err != nil {
  29. return nil, err
  30. }
  31. b2, err := json.Marshal(e.ExampleProps)
  32. if err != nil {
  33. return nil, err
  34. }
  35. b3, err := json.Marshal(e.VendorExtensible)
  36. if err != nil {
  37. return nil, err
  38. }
  39. return swag.ConcatJSON(b1, b2, b3), nil
  40. }
  41. func (e *Example) UnmarshalJSON(data []byte) error {
  42. if err := json.Unmarshal(data, &e.Refable); err != nil {
  43. return err
  44. }
  45. if err := json.Unmarshal(data, &e.ExampleProps); err != nil {
  46. return err
  47. }
  48. if err := json.Unmarshal(data, &e.VendorExtensible); err != nil {
  49. return err
  50. }
  51. return nil
  52. }
  53. type ExampleProps struct {
  54. // Summary holds a short description of the example
  55. Summary string `json:"summary,omitempty"`
  56. // Description holds a long description of the example
  57. Description string `json:"description,omitempty"`
  58. // Embedded literal example.
  59. Value interface{} `json:"value,omitempty"`
  60. // A URL that points to the literal example. This provides the capability to reference examples that cannot easily be included in JSON or YAML documents.
  61. ExternalValue string `json:"externalValue,omitempty"`
  62. }