Exemple #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
}
Exemple #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
}