Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / k8s.io / apimachinery / pkg / runtime / serializer / json / json.go
1 /*
2 Copyright 2014 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package json
18
19 import (
20         "encoding/json"
21         "io"
22         "strconv"
23         "unsafe"
24
25         jsoniter "github.com/json-iterator/go"
26         "github.com/modern-go/reflect2"
27         "sigs.k8s.io/yaml"
28
29         "k8s.io/apimachinery/pkg/runtime"
30         "k8s.io/apimachinery/pkg/runtime/schema"
31         "k8s.io/apimachinery/pkg/runtime/serializer/recognizer"
32         "k8s.io/apimachinery/pkg/util/framer"
33         utilyaml "k8s.io/apimachinery/pkg/util/yaml"
34 )
35
36 // NewSerializer creates a JSON serializer that handles encoding versioned objects into the proper JSON form. If typer
37 // is not nil, the object has the group, version, and kind fields set.
38 func NewSerializer(meta MetaFactory, creater runtime.ObjectCreater, typer runtime.ObjectTyper, pretty bool) *Serializer {
39         return &Serializer{
40                 meta:    meta,
41                 creater: creater,
42                 typer:   typer,
43                 yaml:    false,
44                 pretty:  pretty,
45         }
46 }
47
48 // NewYAMLSerializer creates a YAML serializer that handles encoding versioned objects into the proper YAML form. If typer
49 // is not nil, the object has the group, version, and kind fields set. This serializer supports only the subset of YAML that
50 // matches JSON, and will error if constructs are used that do not serialize to JSON.
51 func NewYAMLSerializer(meta MetaFactory, creater runtime.ObjectCreater, typer runtime.ObjectTyper) *Serializer {
52         return &Serializer{
53                 meta:    meta,
54                 creater: creater,
55                 typer:   typer,
56                 yaml:    true,
57         }
58 }
59
60 type Serializer struct {
61         meta    MetaFactory
62         creater runtime.ObjectCreater
63         typer   runtime.ObjectTyper
64         yaml    bool
65         pretty  bool
66 }
67
68 // Serializer implements Serializer
69 var _ runtime.Serializer = &Serializer{}
70 var _ recognizer.RecognizingDecoder = &Serializer{}
71
72 type customNumberExtension struct {
73         jsoniter.DummyExtension
74 }
75
76 func (cne *customNumberExtension) CreateDecoder(typ reflect2.Type) jsoniter.ValDecoder {
77         if typ.String() == "interface {}" {
78                 return customNumberDecoder{}
79         }
80         return nil
81 }
82
83 type customNumberDecoder struct {
84 }
85
86 func (customNumberDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
87         switch iter.WhatIsNext() {
88         case jsoniter.NumberValue:
89                 var number jsoniter.Number
90                 iter.ReadVal(&number)
91                 i64, err := strconv.ParseInt(string(number), 10, 64)
92                 if err == nil {
93                         *(*interface{})(ptr) = i64
94                         return
95                 }
96                 f64, err := strconv.ParseFloat(string(number), 64)
97                 if err == nil {
98                         *(*interface{})(ptr) = f64
99                         return
100                 }
101                 iter.ReportError("DecodeNumber", err.Error())
102         default:
103                 *(*interface{})(ptr) = iter.Read()
104         }
105 }
106
107 // CaseSensitiveJsonIterator returns a jsoniterator API that's configured to be
108 // case-sensitive when unmarshalling, and otherwise compatible with
109 // the encoding/json standard library.
110 func CaseSensitiveJsonIterator() jsoniter.API {
111         config := jsoniter.Config{
112                 EscapeHTML:             true,
113                 SortMapKeys:            true,
114                 ValidateJsonRawMessage: true,
115                 CaseSensitive:          true,
116         }.Froze()
117         // Force jsoniter to decode number to interface{} via int64/float64, if possible.
118         config.RegisterExtension(&customNumberExtension{})
119         return config
120 }
121
122 // Private copy of jsoniter to try to shield against possible mutations
123 // from outside. Still does not protect from package level jsoniter.Register*() functions - someone calling them
124 // in some other library will mess with every usage of the jsoniter library in the whole program.
125 // See https://github.com/json-iterator/go/issues/265
126 var caseSensitiveJsonIterator = CaseSensitiveJsonIterator()
127
128 // gvkWithDefaults returns group kind and version defaulting from provided default
129 func gvkWithDefaults(actual, defaultGVK schema.GroupVersionKind) schema.GroupVersionKind {
130         if len(actual.Kind) == 0 {
131                 actual.Kind = defaultGVK.Kind
132         }
133         if len(actual.Version) == 0 && len(actual.Group) == 0 {
134                 actual.Group = defaultGVK.Group
135                 actual.Version = defaultGVK.Version
136         }
137         if len(actual.Version) == 0 && actual.Group == defaultGVK.Group {
138                 actual.Version = defaultGVK.Version
139         }
140         return actual
141 }
142
143 // Decode attempts to convert the provided data into YAML or JSON, extract the stored schema kind, apply the provided default gvk, and then
144 // load that data into an object matching the desired schema kind or the provided into.
145 // If into is *runtime.Unknown, the raw data will be extracted and no decoding will be performed.
146 // If into is not registered with the typer, then the object will be straight decoded using normal JSON/YAML unmarshalling.
147 // If into is provided and the original data is not fully qualified with kind/version/group, the type of the into will be used to alter the returned gvk.
148 // If into is nil or data's gvk different from into's gvk, it will generate a new Object with ObjectCreater.New(gvk)
149 // On success or most errors, the method will return the calculated schema kind.
150 // The gvk calculate priority will be originalData > default gvk > into
151 func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, into runtime.Object) (runtime.Object, *schema.GroupVersionKind, error) {
152         if versioned, ok := into.(*runtime.VersionedObjects); ok {
153                 into = versioned.Last()
154                 obj, actual, err := s.Decode(originalData, gvk, into)
155                 if err != nil {
156                         return nil, actual, err
157                 }
158                 versioned.Objects = []runtime.Object{obj}
159                 return versioned, actual, nil
160         }
161
162         data := originalData
163         if s.yaml {
164                 altered, err := yaml.YAMLToJSON(data)
165                 if err != nil {
166                         return nil, nil, err
167                 }
168                 data = altered
169         }
170
171         actual, err := s.meta.Interpret(data)
172         if err != nil {
173                 return nil, nil, err
174         }
175
176         if gvk != nil {
177                 *actual = gvkWithDefaults(*actual, *gvk)
178         }
179
180         if unk, ok := into.(*runtime.Unknown); ok && unk != nil {
181                 unk.Raw = originalData
182                 unk.ContentType = runtime.ContentTypeJSON
183                 unk.GetObjectKind().SetGroupVersionKind(*actual)
184                 return unk, actual, nil
185         }
186
187         if into != nil {
188                 _, isUnstructured := into.(runtime.Unstructured)
189                 types, _, err := s.typer.ObjectKinds(into)
190                 switch {
191                 case runtime.IsNotRegisteredError(err), isUnstructured:
192                         if err := caseSensitiveJsonIterator.Unmarshal(data, into); err != nil {
193                                 return nil, actual, err
194                         }
195                         return into, actual, nil
196                 case err != nil:
197                         return nil, actual, err
198                 default:
199                         *actual = gvkWithDefaults(*actual, types[0])
200                 }
201         }
202
203         if len(actual.Kind) == 0 {
204                 return nil, actual, runtime.NewMissingKindErr(string(originalData))
205         }
206         if len(actual.Version) == 0 {
207                 return nil, actual, runtime.NewMissingVersionErr(string(originalData))
208         }
209
210         // use the target if necessary
211         obj, err := runtime.UseOrCreateObject(s.typer, s.creater, *actual, into)
212         if err != nil {
213                 return nil, actual, err
214         }
215
216         if err := caseSensitiveJsonIterator.Unmarshal(data, obj); err != nil {
217                 return nil, actual, err
218         }
219         return obj, actual, nil
220 }
221
222 // Encode serializes the provided object to the given writer.
223 func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
224         if s.yaml {
225                 json, err := caseSensitiveJsonIterator.Marshal(obj)
226                 if err != nil {
227                         return err
228                 }
229                 data, err := yaml.JSONToYAML(json)
230                 if err != nil {
231                         return err
232                 }
233                 _, err = w.Write(data)
234                 return err
235         }
236
237         if s.pretty {
238                 data, err := caseSensitiveJsonIterator.MarshalIndent(obj, "", "  ")
239                 if err != nil {
240                         return err
241                 }
242                 _, err = w.Write(data)
243                 return err
244         }
245         encoder := json.NewEncoder(w)
246         return encoder.Encode(obj)
247 }
248
249 // RecognizesData implements the RecognizingDecoder interface.
250 func (s *Serializer) RecognizesData(peek io.Reader) (ok, unknown bool, err error) {
251         if s.yaml {
252                 // we could potentially look for '---'
253                 return false, true, nil
254         }
255         _, _, ok = utilyaml.GuessJSONStream(peek, 2048)
256         return ok, false, nil
257 }
258
259 // Framer is the default JSON framing behavior, with newlines delimiting individual objects.
260 var Framer = jsonFramer{}
261
262 type jsonFramer struct{}
263
264 // NewFrameWriter implements stream framing for this serializer
265 func (jsonFramer) NewFrameWriter(w io.Writer) io.Writer {
266         // we can write JSON objects directly to the writer, because they are self-framing
267         return w
268 }
269
270 // NewFrameReader implements stream framing for this serializer
271 func (jsonFramer) NewFrameReader(r io.ReadCloser) io.ReadCloser {
272         // we need to extract the JSON chunks of data to pass to Decode()
273         return framer.NewJSONFramedReader(r)
274 }
275
276 // YAMLFramer is the default JSON framing behavior, with newlines delimiting individual objects.
277 var YAMLFramer = yamlFramer{}
278
279 type yamlFramer struct{}
280
281 // NewFrameWriter implements stream framing for this serializer
282 func (yamlFramer) NewFrameWriter(w io.Writer) io.Writer {
283         return yamlFrameWriter{w}
284 }
285
286 // NewFrameReader implements stream framing for this serializer
287 func (yamlFramer) NewFrameReader(r io.ReadCloser) io.ReadCloser {
288         // extract the YAML document chunks directly
289         return utilyaml.NewDocumentDecoder(r)
290 }
291
292 type yamlFrameWriter struct {
293         w io.Writer
294 }
295
296 // Write separates each document with the YAML document separator (`---` followed by line
297 // break). Writers must write well formed YAML documents (include a final line break).
298 func (w yamlFrameWriter) Write(data []byte) (n int, err error) {
299         if _, err := w.w.Write([]byte("---\n")); err != nil {
300                 return 0, err
301         }
302         return w.w.Write(data)
303 }