...

Source file src/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1/conversion.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  
    22  	"k8s.io/apimachinery/pkg/conversion"
    23  	"k8s.io/apimachinery/pkg/util/json"
    24  
    25  	"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
    26  )
    27  
    28  func Convert_apiextensions_JSONSchemaProps_To_v1beta1_JSONSchemaProps(in *apiextensions.JSONSchemaProps, out *JSONSchemaProps, s conversion.Scope) error {
    29  	if err := autoConvert_apiextensions_JSONSchemaProps_To_v1beta1_JSONSchemaProps(in, out, s); err != nil {
    30  		return err
    31  	}
    32  	if in.Default != nil && *(in.Default) == nil {
    33  		out.Default = nil
    34  	}
    35  	if in.Example != nil && *(in.Example) == nil {
    36  		out.Example = nil
    37  	}
    38  	return nil
    39  }
    40  
    41  var nullLiteral = []byte(`null`)
    42  
    43  func Convert_apiextensions_JSON_To_v1beta1_JSON(in *apiextensions.JSON, out *JSON, s conversion.Scope) error {
    44  	raw, err := json.Marshal(*in)
    45  	if err != nil {
    46  		return err
    47  	}
    48  	if len(raw) == 0 || bytes.Equal(raw, nullLiteral) {
    49  		// match JSON#UnmarshalJSON treatment of literal nulls
    50  		out.Raw = nil
    51  	} else {
    52  		out.Raw = raw
    53  	}
    54  	return nil
    55  }
    56  
    57  func Convert_v1beta1_JSON_To_apiextensions_JSON(in *JSON, out *apiextensions.JSON, s conversion.Scope) error {
    58  	if in != nil {
    59  		var i interface{}
    60  		if len(in.Raw) > 0 && !bytes.Equal(in.Raw, nullLiteral) {
    61  			if err := json.Unmarshal(in.Raw, &i); err != nil {
    62  				return err
    63  			}
    64  		}
    65  		*out = i
    66  	} else {
    67  		out = nil
    68  	}
    69  	return nil
    70  }
    71  

View as plain text