コード例 #1
0
ファイル: resource.go プロジェクト: nelsnelson/rack
// 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)
}
コード例 #2
0
ファイル: context.go プロジェクト: satyamkotakonda/rack
// limitFields returns only the fields the user specified in the `fields` flag. If
// the flag wasn't provided, all fields are returned.
func (ctx *Context) limitFields(resource *Resource) {
	if ctx.CLIContext.IsSet("fields") {
		fields := strings.Split(strings.ToLower(ctx.CLIContext.String("fields")), ",")
		newKeys := []string{}
		for _, key := range resource.Keys {
			if util.Contains(fields, strings.Join(strings.Split(strings.ToLower(key), " "), "-")) {
				newKeys = append(newKeys, key)
			}
		}
		resource.Keys = newKeys
	}
}
コード例 #3
0
ファイル: resource.go プロジェクト: satyamkotakonda/rack
// 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)
	}
}