github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/runtime/unstructured.go (about) 1 /* 2 Copyright 2015 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 "encoding/json" 21 "fmt" 22 "net/url" 23 "reflect" 24 25 "k8s.io/kubernetes/pkg/conversion" 26 ) 27 28 // UnstructuredJSONScheme is capable of converting JSON data into the Unstructured 29 // type, which can be used for generic access to objects without a predefined scheme. 30 var UnstructuredJSONScheme ObjectDecoder = unstructuredJSONScheme{} 31 32 type unstructuredJSONScheme struct{} 33 34 // Recognizes returns true for any version or kind that is specified (internal 35 // versions are specifically excluded). 36 func (unstructuredJSONScheme) Recognizes(version, kind string) bool { 37 return len(version) > 0 && len(kind) > 0 38 } 39 40 func (s unstructuredJSONScheme) Decode(data []byte) (Object, error) { 41 unstruct := &Unstructured{} 42 if err := s.DecodeInto(data, unstruct); err != nil { 43 return nil, err 44 } 45 return unstruct, nil 46 } 47 48 func (unstructuredJSONScheme) DecodeInto(data []byte, obj Object) error { 49 unstruct, ok := obj.(*Unstructured) 50 if !ok { 51 return fmt.Errorf("the unstructured JSON scheme does not recognize %v", reflect.TypeOf(obj)) 52 } 53 54 m := make(map[string]interface{}) 55 if err := json.Unmarshal(data, &m); err != nil { 56 return err 57 } 58 if v, ok := m["kind"]; ok { 59 if s, ok := v.(string); ok { 60 unstruct.Kind = s 61 } 62 } 63 if v, ok := m["apiVersion"]; ok { 64 if s, ok := v.(string); ok { 65 unstruct.APIVersion = s 66 } 67 } 68 if len(unstruct.APIVersion) == 0 { 69 return conversion.NewMissingVersionErr(string(data)) 70 } 71 if len(unstruct.Kind) == 0 { 72 return conversion.NewMissingKindErr(string(data)) 73 } 74 unstruct.Object = m 75 return nil 76 } 77 78 func (unstructuredJSONScheme) DecodeIntoWithSpecifiedVersionKind(data []byte, obj Object, kind, version string) error { 79 return nil 80 } 81 82 func (unstructuredJSONScheme) DecodeToVersion(data []byte, version string) (Object, error) { 83 return nil, nil 84 } 85 86 func (unstructuredJSONScheme) DecodeParametersInto(paramaters url.Values, obj Object) error { 87 return nil 88 } 89 90 func (unstructuredJSONScheme) DataVersionAndKind(data []byte) (version, kind string, err error) { 91 obj := TypeMeta{} 92 if err := json.Unmarshal(data, &obj); err != nil { 93 return "", "", err 94 } 95 if len(obj.APIVersion) == 0 { 96 return "", "", conversion.NewMissingVersionErr(string(data)) 97 } 98 if len(obj.Kind) == 0 { 99 return "", "", conversion.NewMissingKindErr(string(data)) 100 } 101 return obj.APIVersion, obj.Kind, nil 102 }