github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/conversion/decode.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 conversion
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"net/url"
    23  
    24  	"github.com/ugorji/go/codec"
    25  )
    26  
    27  func (s *Scheme) DecodeToVersionedObject(data []byte) (obj interface{}, version, kind string, err error) {
    28  	version, kind, err = s.DataVersionAndKind(data)
    29  	if err != nil {
    30  		return
    31  	}
    32  	if version == "" && s.InternalVersion != "" {
    33  		return nil, "", "", fmt.Errorf("version not set in '%s'", string(data))
    34  	}
    35  	if kind == "" {
    36  		return nil, "", "", fmt.Errorf("kind not set in '%s'", string(data))
    37  	}
    38  	obj, err = s.NewObject(version, kind)
    39  	if err != nil {
    40  		return nil, "", "", err
    41  	}
    42  
    43  	if err := codec.NewDecoderBytes(data, new(codec.JsonHandle)).Decode(obj); err != nil {
    44  		return nil, "", "", err
    45  	}
    46  	return
    47  }
    48  
    49  // Decode converts a JSON string back into a pointer to an api object.
    50  // Deduces the type based upon the fields added by the MetaInsertionFactory
    51  // technique. The object will be converted, if necessary, into the
    52  // s.InternalVersion type before being returned. Decode will not decode
    53  // objects without version set unless InternalVersion is also "".
    54  func (s *Scheme) Decode(data []byte) (interface{}, error) {
    55  	return s.DecodeToVersion(data, s.InternalVersion)
    56  }
    57  
    58  // DecodeToVersion converts a JSON string back into a pointer to an api object.
    59  // Deduces the type based upon the fields added by the MetaInsertionFactory
    60  // technique. The object will be converted, if necessary, into the versioned
    61  // type before being returned. Decode will not decode objects without version
    62  // set unless version is also "".
    63  func (s *Scheme) DecodeToVersion(data []byte, version string) (interface{}, error) {
    64  	obj, sourceVersion, kind, err := s.DecodeToVersionedObject(data)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	// Version and Kind should be blank in memory.
    69  	if err := s.SetVersionAndKind("", "", obj); err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	// Convert if needed.
    74  	if version != sourceVersion {
    75  		objOut, err := s.NewObject(version, kind)
    76  		if err != nil {
    77  			return nil, err
    78  		}
    79  		flags, meta := s.generateConvertMeta(sourceVersion, version, obj)
    80  		if err := s.converter.Convert(obj, objOut, flags, meta); err != nil {
    81  			return nil, err
    82  		}
    83  		obj = objOut
    84  	}
    85  	return obj, nil
    86  }
    87  
    88  // DecodeInto parses a JSON string and stores it in obj. Returns an error
    89  // if data.Kind is set and doesn't match the type of obj. Obj should be a
    90  // pointer to an api type.
    91  // If obj's version doesn't match that in data, an attempt will be made to convert
    92  // data into obj's version.
    93  func (s *Scheme) DecodeInto(data []byte, obj interface{}) error {
    94  	return s.DecodeIntoWithSpecifiedVersionKind(data, obj, "", "")
    95  }
    96  
    97  // DecodeIntoWithSpecifiedVersionKind compares the passed in specifiedVersion and
    98  // specifiedKind with data.Version and data.Kind, defaulting data.Version and
    99  // data.Kind to the specified value if they are empty, or generating an error if
   100  // data.Version and data.Kind are not empty and differ from the specified value.
   101  // The function then implements the functionality of DecodeInto.
   102  // If specifiedVersion and specifiedKind are empty, the function degenerates to
   103  // DecodeInto.
   104  func (s *Scheme) DecodeIntoWithSpecifiedVersionKind(data []byte, obj interface{}, specifiedVersion, specifiedKind string) error {
   105  	if len(data) == 0 {
   106  		return errors.New("empty input")
   107  	}
   108  	dataVersion, dataKind, err := s.DataVersionAndKind(data)
   109  	if err != nil {
   110  		return err
   111  	}
   112  	if dataVersion == "" {
   113  		dataVersion = specifiedVersion
   114  	}
   115  	if dataKind == "" {
   116  		dataKind = specifiedKind
   117  	}
   118  	if len(specifiedVersion) > 0 && (dataVersion != specifiedVersion) {
   119  		return errors.New(fmt.Sprintf("The apiVersion in the data (%s) does not match the specified apiVersion(%s)", dataVersion, specifiedVersion))
   120  	}
   121  	if len(specifiedKind) > 0 && (dataKind != specifiedKind) {
   122  		return errors.New(fmt.Sprintf("The kind in the data (%s) does not match the specified kind(%s)", dataKind, specifiedKind))
   123  	}
   124  
   125  	objVersion, objKind, err := s.ObjectVersionAndKind(obj)
   126  	if err != nil {
   127  		return err
   128  	}
   129  	if dataKind == "" {
   130  		// Assume objects with unset Kind fields are being unmarshalled into the
   131  		// correct type.
   132  		dataKind = objKind
   133  	}
   134  	if dataVersion == "" {
   135  		// Assume objects with unset Version fields are being unmarshalled into the
   136  		// correct type.
   137  		dataVersion = objVersion
   138  	}
   139  
   140  	external, err := s.NewObject(dataVersion, dataKind)
   141  	if err != nil {
   142  		return err
   143  	}
   144  	if err := codec.NewDecoderBytes(data, new(codec.JsonHandle)).Decode(external); err != nil {
   145  		return err
   146  	}
   147  	flags, meta := s.generateConvertMeta(dataVersion, objVersion, external)
   148  	if err := s.converter.Convert(external, obj, flags, meta); err != nil {
   149  		return err
   150  	}
   151  
   152  	// Version and Kind should be blank in memory.
   153  	return s.SetVersionAndKind("", "", obj)
   154  }
   155  
   156  func (s *Scheme) DecodeParametersInto(parameters url.Values, obj interface{}) error {
   157  	if err := s.Convert(&parameters, obj); err != nil {
   158  		return err
   159  	}
   160  	// TODO: Should we do any convertion here?
   161  	return nil
   162  }