Пример #1
0
func (t *thirdPartyResourceDataCodec) populateFromObject(objIn *expapi.ThirdPartyResourceData, mapObj map[string]interface{}, data []byte) error {
	kind, ok := mapObj["kind"].(string)
	if !ok {
		return fmt.Errorf("unexpected object for kind: %#v", mapObj["kind"])
	}
	if kind != t.kind {
		return fmt.Errorf("unexpected kind: %s, expected: %s", kind, t.kind)
	}

	metadata, ok := mapObj["metadata"].(map[string]interface{})
	if !ok {
		return fmt.Errorf("unexpected object for metadata: %#v", mapObj["metadata"])
	}

	if resourceVersion, ok := metadata["resourceVersion"]; ok {
		resourceVersionStr, ok := resourceVersion.(string)
		if !ok {
			return fmt.Errorf("unexpected object for resourceVersion: %v", resourceVersion)
		}

		objIn.ResourceVersion = resourceVersionStr
	}

	name, ok := metadata["name"].(string)
	if !ok {
		return fmt.Errorf("unexpected object for name: %#v", metadata)
	}

	if labels, ok := metadata["labels"]; ok {
		labelMap, ok := labels.(map[string]interface{})
		if !ok {
			return fmt.Errorf("unexpected object for labels: %v", labelMap)
		}
		for key, value := range labelMap {
			valueStr, ok := value.(string)
			if !ok {
				return fmt.Errorf("unexpected label: %v", value)
			}
			if objIn.Labels == nil {
				objIn.Labels = map[string]string{}
			}
			objIn.Labels[key] = valueStr
		}
	}

	objIn.Name = name
	objIn.Data = data
	return nil
}
Пример #2
0
func (t *thirdPartyResourceDataCodec) populateFromObject(objIn *expapi.ThirdPartyResourceData, mapObj map[string]interface{}, data []byte) error {
	typeMeta := api.TypeMeta{}
	if err := json.Unmarshal(data, &typeMeta); err != nil {
		return err
	}
	if typeMeta.Kind != t.kind {
		return fmt.Errorf("unexpected kind: %s, expected %s", typeMeta.Kind, t.kind)
	}

	metadata, ok := mapObj["metadata"].(map[string]interface{})
	if !ok {
		return fmt.Errorf("unexpected object for metadata: %#v", mapObj["metadata"])
	}

	metadataData, err := json.Marshal(metadata)
	if err != nil {
		return err
	}

	if err := json.Unmarshal(metadataData, &objIn.ObjectMeta); err != nil {
		return err
	}

	objIn.Data = data
	return nil
}