...

Source file src/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1/marshal.go

Documentation: k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1

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

View as plain text