github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/circonus/interface.go (about)

     1  package circonus
     2  
     3  import "log"
     4  
     5  type interfaceList []interface{}
     6  type interfaceMap map[string]interface{}
     7  
     8  // newInterfaceMap returns a helper type that has methods for common operations
     9  // for accessing data.
    10  func newInterfaceMap(l interface{}) interfaceMap {
    11  	return interfaceMap(l.(map[string]interface{}))
    12  }
    13  
    14  // CollectList returns []string of values that matched the key attrName.
    15  // interfaceList most likely came from a schema.TypeSet.
    16  func (l interfaceList) CollectList(attrName schemaAttr) []string {
    17  	stringList := make([]string, 0, len(l))
    18  
    19  	for _, mapRaw := range l {
    20  		mapAttrs := mapRaw.(map[string]interface{})
    21  
    22  		if v, ok := mapAttrs[string(attrName)]; ok {
    23  			stringList = append(stringList, v.(string))
    24  		}
    25  	}
    26  
    27  	return stringList
    28  }
    29  
    30  // List returns a list of values in a Set as a string slice
    31  func (l interfaceList) List() []string {
    32  	stringList := make([]string, 0, len(l))
    33  	for _, e := range l {
    34  		switch e.(type) {
    35  		case string:
    36  			stringList = append(stringList, e.(string))
    37  		case []interface{}:
    38  			for _, v := range e.([]interface{}) {
    39  				stringList = append(stringList, v.(string))
    40  			}
    41  		default:
    42  			log.Printf("[ERROR] PROVIDER BUG: unable to convert %#v to list", e)
    43  			return nil
    44  		}
    45  	}
    46  	return stringList
    47  }
    48  
    49  // CollectList returns []string of values that matched the key attrName.
    50  // interfaceMap most likely came from a schema.TypeSet.
    51  func (m interfaceMap) CollectList(attrName schemaAttr) []string {
    52  	stringList := make([]string, 0, len(m))
    53  
    54  	for _, mapRaw := range m {
    55  		mapAttrs := mapRaw.(map[string]interface{})
    56  
    57  		if v, ok := mapAttrs[string(attrName)]; ok {
    58  			stringList = append(stringList, v.(string))
    59  		}
    60  	}
    61  
    62  	return stringList
    63  }
    64  
    65  // CollectMap returns map[string]string of values that matched the key attrName.
    66  // interfaceMap most likely came from a schema.TypeSet.
    67  func (m interfaceMap) CollectMap(attrName schemaAttr) map[string]string {
    68  	var mergedMap map[string]string
    69  
    70  	if attrRaw, ok := m[string(attrName)]; ok {
    71  		attrMap := attrRaw.(map[string]interface{})
    72  		mergedMap = make(map[string]string, len(m))
    73  		for k, v := range attrMap {
    74  			mergedMap[k] = v.(string)
    75  		}
    76  	}
    77  
    78  	if len(mergedMap) == 0 {
    79  		return nil
    80  	}
    81  
    82  	return mergedMap
    83  }