github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/runtime/codec.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors All rights reserved.
     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 runtime
    18  
    19  import (
    20  	"io"
    21  
    22  	"k8s.io/kubernetes/pkg/util/yaml"
    23  )
    24  
    25  // CodecFor returns a Codec that invokes Encode with the provided version.
    26  func CodecFor(codec ObjectCodec, version string) Codec {
    27  	return &codecWrapper{codec, version}
    28  }
    29  
    30  // yamlCodec converts YAML passed to the Decoder methods to JSON.
    31  type yamlCodec struct {
    32  	// a Codec for JSON
    33  	Codec
    34  }
    35  
    36  // yamlCodec implements Codec
    37  var _ Codec = yamlCodec{}
    38  
    39  // YAMLDecoder adds YAML decoding support to a codec that supports JSON.
    40  func YAMLDecoder(codec Codec) Codec {
    41  	return &yamlCodec{codec}
    42  }
    43  
    44  func (c yamlCodec) Decode(data []byte) (Object, error) {
    45  	out, err := yaml.ToJSON(data)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	data = out
    50  	return c.Codec.Decode(data)
    51  }
    52  
    53  func (c yamlCodec) DecodeInto(data []byte, obj Object) error {
    54  	out, err := yaml.ToJSON(data)
    55  	if err != nil {
    56  		return err
    57  	}
    58  	data = out
    59  	return c.Codec.DecodeInto(data, obj)
    60  }
    61  
    62  // EncodeOrDie is a version of Encode which will panic instead of returning an error. For tests.
    63  func EncodeOrDie(codec Codec, obj Object) string {
    64  	bytes, err := codec.Encode(obj)
    65  	if err != nil {
    66  		panic(err)
    67  	}
    68  	return string(bytes)
    69  }
    70  
    71  // codecWrapper implements encoding to an alternative
    72  // default version for a scheme.
    73  type codecWrapper struct {
    74  	ObjectCodec
    75  	version string
    76  }
    77  
    78  // Encode implements Codec
    79  func (c *codecWrapper) Encode(obj Object) ([]byte, error) {
    80  	return c.EncodeToVersion(obj, c.version)
    81  }
    82  
    83  func (c *codecWrapper) EncodeToStream(obj Object, stream io.Writer) error {
    84  	return c.EncodeToVersionStream(obj, c.version, stream)
    85  }
    86  
    87  // TODO: Make this behaviour default when we move everyone away from
    88  // the unversioned types.
    89  //
    90  // func (c *codecWrapper) Decode(data []byte) (Object, error) {
    91  // 	return c.DecodeToVersion(data, c.version)
    92  // }