...
1
16
17 package spec3
18
19 import (
20 "encoding/json"
21
22 "github.com/go-openapi/swag"
23 "k8s.io/kube-openapi/pkg/internal"
24 jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json"
25
26 "k8s.io/kube-openapi/pkg/validation/spec"
27 )
28
29
30
31 type Example struct {
32 spec.Refable
33 ExampleProps
34 spec.VendorExtensible
35 }
36
37
38 func (e *Example) MarshalJSON() ([]byte, error) {
39 if internal.UseOptimizedJSONMarshalingV3 {
40 return internal.DeterministicMarshal(e)
41 }
42 b1, err := json.Marshal(e.Refable)
43 if err != nil {
44 return nil, err
45 }
46 b2, err := json.Marshal(e.ExampleProps)
47 if err != nil {
48 return nil, err
49 }
50 b3, err := json.Marshal(e.VendorExtensible)
51 if err != nil {
52 return nil, err
53 }
54 return swag.ConcatJSON(b1, b2, b3), nil
55 }
56 func (e *Example) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
57 var x struct {
58 Ref string `json:"$ref,omitempty"`
59 ExampleProps `json:",inline"`
60 spec.Extensions
61 }
62 x.Ref = e.Refable.Ref.String()
63 x.Extensions = internal.SanitizeExtensions(e.Extensions)
64 x.ExampleProps = e.ExampleProps
65 return opts.MarshalNext(enc, x)
66 }
67
68 func (e *Example) UnmarshalJSON(data []byte) error {
69 if internal.UseOptimizedJSONUnmarshalingV3 {
70 return jsonv2.Unmarshal(data, e)
71 }
72 if err := json.Unmarshal(data, &e.Refable); err != nil {
73 return err
74 }
75 if err := json.Unmarshal(data, &e.ExampleProps); err != nil {
76 return err
77 }
78 if err := json.Unmarshal(data, &e.VendorExtensible); err != nil {
79 return err
80 }
81 return nil
82 }
83
84 func (e *Example) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
85 var x struct {
86 spec.Extensions
87 ExampleProps
88 }
89 if err := opts.UnmarshalNext(dec, &x); err != nil {
90 return err
91 }
92 if err := internal.JSONRefFromMap(&e.Ref.Ref, x.Extensions); err != nil {
93 return err
94 }
95 e.Extensions = internal.SanitizeExtensions(x.Extensions)
96 e.ExampleProps = x.ExampleProps
97
98 return nil
99 }
100
101 type ExampleProps struct {
102
103 Summary string `json:"summary,omitempty"`
104
105 Description string `json:"description,omitempty"`
106
107 Value interface{} `json:"value,omitempty"`
108
109 ExternalValue string `json:"externalValue,omitempty"`
110 }
111
View as plain text