Esempio n. 1
0
// FlattenMap is used to flatten out a `map[string]map[string]*`
func (resource *Resource) FlattenMap(key string) {
	keys := resource.Keys
	res := resource.Result.(map[string]interface{})
	if m, ok := res[key]; ok && util.Contains(keys, key) {
		switch m.(type) {
		case []map[string]interface{}:
			for i, hashmap := range m.([]map[string]interface{}) {
				for k, v := range hashmap {
					newKey := fmt.Sprintf("%s%d:%s", key, i, k)
					res[newKey] = v
					keys = append(keys, newKey)
				}
			}
		case map[string]interface{}:
			for k, v := range m.(map[string]interface{}) {
				newKey := fmt.Sprintf("%s:%s", key, k)
				res[newKey] = v
				keys = append(keys, newKey)
			}
		case map[string]string:
			for k, v := range m.(map[string]string) {
				newKey := fmt.Sprintf("%s:%s", key, k)
				res[newKey] = v
				keys = append(keys, newKey)
			}
		}
	}
	delete(res, key)
	resource.Keys = util.RemoveFromList(keys, key)
}
Esempio n. 2
0
// FlattenMap is used to flatten out a `map[string]map[string]*`
func (resource *Resource) FlattenMap(key string) {

	res := resource.Result.(map[string]interface{})
	if m, ok := res[key]; ok && util.Contains(resource.Keys, key) {
		switch m.(type) {
		case []map[string]interface{}:
			for i, hashmap := range m.([]map[string]interface{}) {
				for k, v := range hashmap {
					newKey := fmt.Sprintf("%s%d:%s", key, i, k)
					res[newKey] = v
					resource.Keys = append(resource.Keys, newKey)
					resource.FlattenMap(newKey)
				}
			}
		case []interface{}:
			for i, element := range m.([]interface{}) {
				newKey := fmt.Sprintf("%s%d", key, i)
				res[newKey] = element
				resource.Keys = append(resource.Keys, newKey)
				resource.FlattenMap(newKey)
			}
		case map[string]interface{}, map[interface{}]interface{}:
			mMap := toStringKeys(m)
			for k, v := range mMap {
				newKey := fmt.Sprintf("%s:%s", key, k)
				res[newKey] = v
				resource.Keys = append(resource.Keys, newKey)
				resource.FlattenMap(newKey)
			}
		case map[string]string:
			for k, v := range m.(map[string]string) {
				newKey := fmt.Sprintf("%s:%s", key, k)
				res[newKey] = v
				resource.Keys = append(resource.Keys, newKey)
			}
		default:
			return
		}
		delete(res, key)
		resource.Keys = util.RemoveFromList(resource.Keys, key)
	}
}