encoding.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. "github.com/go-openapi/swag"
  17. "k8s.io/kube-openapi/pkg/internal"
  18. jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json"
  19. "k8s.io/kube-openapi/pkg/validation/spec"
  20. )
  21. type Encoding struct {
  22. EncodingProps
  23. spec.VendorExtensible
  24. }
  25. // MarshalJSON is a custom marshal function that knows how to encode Encoding as JSON
  26. func (e *Encoding) MarshalJSON() ([]byte, error) {
  27. if internal.UseOptimizedJSONMarshalingV3 {
  28. return internal.DeterministicMarshal(e)
  29. }
  30. b1, err := json.Marshal(e.EncodingProps)
  31. if err != nil {
  32. return nil, err
  33. }
  34. b2, err := json.Marshal(e.VendorExtensible)
  35. if err != nil {
  36. return nil, err
  37. }
  38. return swag.ConcatJSON(b1, b2), nil
  39. }
  40. func (e *Encoding) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
  41. var x struct {
  42. EncodingProps encodingPropsOmitZero `json:",inline"`
  43. spec.Extensions
  44. }
  45. x.Extensions = internal.SanitizeExtensions(e.Extensions)
  46. x.EncodingProps = encodingPropsOmitZero(e.EncodingProps)
  47. return opts.MarshalNext(enc, x)
  48. }
  49. func (e *Encoding) UnmarshalJSON(data []byte) error {
  50. if internal.UseOptimizedJSONUnmarshalingV3 {
  51. return jsonv2.Unmarshal(data, e)
  52. }
  53. if err := json.Unmarshal(data, &e.EncodingProps); err != nil {
  54. return err
  55. }
  56. if err := json.Unmarshal(data, &e.VendorExtensible); err != nil {
  57. return err
  58. }
  59. return nil
  60. }
  61. func (e *Encoding) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  62. var x struct {
  63. spec.Extensions
  64. EncodingProps
  65. }
  66. if err := opts.UnmarshalNext(dec, &x); err != nil {
  67. return err
  68. }
  69. e.Extensions = internal.SanitizeExtensions(x.Extensions)
  70. e.EncodingProps = x.EncodingProps
  71. return nil
  72. }
  73. type EncodingProps struct {
  74. // Content Type for encoding a specific property
  75. ContentType string `json:"contentType,omitempty"`
  76. // A map allowing additional information to be provided as headers
  77. Headers map[string]*Header `json:"headers,omitempty"`
  78. // Describes how a specific property value will be serialized depending on its type
  79. Style string `json:"style,omitempty"`
  80. // When this is true, property values of type array or object generate separate parameters for each value of the array, or key-value-pair of the map. For other types of properties this property has no effect
  81. Explode bool `json:"explode,omitempty"`
  82. // AllowReserved determines whether the parameter value SHOULD allow reserved characters, as defined by RFC3986
  83. AllowReserved bool `json:"allowReserved,omitempty"`
  84. }
  85. type encodingPropsOmitZero struct {
  86. ContentType string `json:"contentType,omitempty"`
  87. Headers map[string]*Header `json:"headers,omitempty"`
  88. Style string `json:"style,omitempty"`
  89. Explode bool `json:"explode,omitzero"`
  90. AllowReserved bool `json:"allowReserved,omitzero"`
  91. }