Remove BPA from Makefile
[icn.git] / cmd / bpa-operator / vendor / k8s.io / apiextensions-apiserver / pkg / apis / apiextensions / v1beta1 / marshal.go
1 /*
2 Copyright 2017 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 v1beta1
18
19 import (
20         "errors"
21
22         "k8s.io/apimachinery/pkg/util/json"
23 )
24
25 var jsTrue = []byte("true")
26 var jsFalse = []byte("false")
27
28 func (s JSONSchemaPropsOrBool) MarshalJSON() ([]byte, error) {
29         if s.Schema != nil {
30                 return json.Marshal(s.Schema)
31         }
32
33         if s.Schema == nil && !s.Allows {
34                 return jsFalse, nil
35         }
36         return jsTrue, nil
37 }
38
39 func (s *JSONSchemaPropsOrBool) UnmarshalJSON(data []byte) error {
40         var nw JSONSchemaPropsOrBool
41         switch {
42         case len(data) == 0:
43         case data[0] == '{':
44                 var sch JSONSchemaProps
45                 if err := json.Unmarshal(data, &sch); err != nil {
46                         return err
47                 }
48                 nw.Allows = true
49                 nw.Schema = &sch
50         case len(data) == 4 && string(data) == "true":
51                 nw.Allows = true
52         case len(data) == 5 && string(data) == "false":
53                 nw.Allows = false
54         default:
55                 return errors.New("boolean or JSON schema expected")
56         }
57         *s = nw
58         return nil
59 }
60
61 func (s JSONSchemaPropsOrStringArray) MarshalJSON() ([]byte, error) {
62         if len(s.Property) > 0 {
63                 return json.Marshal(s.Property)
64         }
65         if s.Schema != nil {
66                 return json.Marshal(s.Schema)
67         }
68         return []byte("null"), nil
69 }
70
71 func (s *JSONSchemaPropsOrStringArray) UnmarshalJSON(data []byte) error {
72         var first byte
73         if len(data) > 1 {
74                 first = data[0]
75         }
76         var nw JSONSchemaPropsOrStringArray
77         if first == '{' {
78                 var sch JSONSchemaProps
79                 if err := json.Unmarshal(data, &sch); err != nil {
80                         return err
81                 }
82                 nw.Schema = &sch
83         }
84         if first == '[' {
85                 if err := json.Unmarshal(data, &nw.Property); err != nil {
86                         return err
87                 }
88         }
89         *s = nw
90         return nil
91 }
92
93 func (s JSONSchemaPropsOrArray) MarshalJSON() ([]byte, error) {
94         if len(s.JSONSchemas) > 0 {
95                 return json.Marshal(s.JSONSchemas)
96         }
97         return json.Marshal(s.Schema)
98 }
99
100 func (s *JSONSchemaPropsOrArray) UnmarshalJSON(data []byte) error {
101         var nw JSONSchemaPropsOrArray
102         var first byte
103         if len(data) > 1 {
104                 first = data[0]
105         }
106         if first == '{' {
107                 var sch JSONSchemaProps
108                 if err := json.Unmarshal(data, &sch); err != nil {
109                         return err
110                 }
111                 nw.Schema = &sch
112         }
113         if first == '[' {
114                 if err := json.Unmarshal(data, &nw.JSONSchemas); err != nil {
115                         return err
116                 }
117         }
118         *s = nw
119         return nil
120 }
121
122 func (s JSON) MarshalJSON() ([]byte, error) {
123         if len(s.Raw) > 0 {
124                 return s.Raw, nil
125         }
126         return []byte("null"), nil
127
128 }
129
130 func (s *JSON) UnmarshalJSON(data []byte) error {
131         if len(data) > 0 && string(data) != "null" {
132                 s.Raw = data
133         }
134         return nil
135 }