github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/runtime/serializer/yaml/yaml.go (about)

     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 yaml
    18  
    19  import (
    20  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime"
    21  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema"
    22  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/util/yaml"
    23  )
    24  
    25  // yamlSerializer converts YAML passed to the Decoder methods to JSON.
    26  type yamlSerializer struct {
    27  	// the nested serializer
    28  	runtime.Serializer
    29  }
    30  
    31  // yamlSerializer implements Serializer
    32  var _ runtime.Serializer = yamlSerializer{}
    33  
    34  // NewDecodingSerializer adds YAML decoding support to a serializer that supports JSON.
    35  func NewDecodingSerializer(jsonSerializer runtime.Serializer) runtime.Serializer {
    36  	return &yamlSerializer{jsonSerializer}
    37  }
    38  
    39  func (c yamlSerializer) Decode(data []byte, gvk *schema.GroupVersionKind, into runtime.Object) (runtime.Object, *schema.GroupVersionKind, error) {
    40  	out, err := yaml.ToJSON(data)
    41  	if err != nil {
    42  		return nil, nil, err
    43  	}
    44  	data = out
    45  	return c.Serializer.Decode(data, gvk, into)
    46  }