Example #1
0
func (unstructuredJSONScheme) DecodeInto(data []byte, obj Object) error {
	unstruct, ok := obj.(*Unstructured)
	if !ok {
		return fmt.Errorf("the unstructured JSON scheme does not recognize %v", reflect.TypeOf(obj))
	}

	m := make(map[string]interface{})
	if err := json.Unmarshal(data, &m); err != nil {
		return err
	}
	if v, ok := m["kind"]; ok {
		if s, ok := v.(string); ok {
			unstruct.Kind = s
		}
	}
	if v, ok := m["apiVersion"]; ok {
		if s, ok := v.(string); ok {
			unstruct.APIVersion = s
		}
	}
	if len(unstruct.APIVersion) == 0 {
		return conversion.NewMissingVersionErr(string(data))
	}
	if len(unstruct.Kind) == 0 {
		return conversion.NewMissingKindErr(string(data))
	}
	unstruct.Object = m
	return nil
}
Example #2
0
func (unstructuredJSONScheme) DataVersionAndKind(data []byte) (version, kind string, err error) {
	obj := TypeMeta{}
	if err := json.Unmarshal(data, &obj); err != nil {
		return "", "", err
	}
	if len(obj.APIVersion) == 0 {
		return "", "", conversion.NewMissingVersionErr(string(data))
	}
	if len(obj.Kind) == 0 {
		return "", "", conversion.NewMissingKindErr(string(data))
	}
	return obj.APIVersion, obj.Kind, nil
}
Example #3
0
func (unstructuredJSONScheme) DataKind(data []byte) (unversioned.GroupVersionKind, error) {
	obj := TypeMeta{}
	if err := json.Unmarshal(data, &obj); err != nil {
		return unversioned.GroupVersionKind{}, err
	}
	if len(obj.APIVersion) == 0 {
		return unversioned.GroupVersionKind{}, conversion.NewMissingVersionErr(string(data))
	}
	if len(obj.Kind) == 0 {
		return unversioned.GroupVersionKind{}, conversion.NewMissingKindErr(string(data))
	}

	gv, err := unversioned.ParseGroupVersion(obj.APIVersion)
	if err != nil {
		return unversioned.GroupVersionKind{}, err
	}

	return gv.WithKind(obj.Kind), nil
}
Example #4
0
func NewMissingVersionErr(data string) error {
	return conversion.NewMissingVersionErr(data)
}