github.com/superfly/nomad@v0.10.5-fly/jobspec/utils.go (about)

     1  package jobspec
     2  
     3  // flattenMapSlice flattens any occurrences of []map[string]interface{} into
     4  // map[string]interface{}.
     5  func flattenMapSlice(m map[string]interface{}) map[string]interface{} {
     6  	newM := make(map[string]interface{}, len(m))
     7  
     8  	for k, v := range m {
     9  		var newV interface{}
    10  
    11  		switch mapV := v.(type) {
    12  		case []map[string]interface{}:
    13  			// Recurse into each map and flatten values
    14  			newMap := map[string]interface{}{}
    15  			for _, innerM := range mapV {
    16  				for innerK, innerV := range flattenMapSlice(innerM) {
    17  					newMap[innerK] = innerV
    18  				}
    19  			}
    20  			newV = newMap
    21  
    22  		case map[string]interface{}:
    23  			// Recursively flatten maps
    24  			newV = flattenMapSlice(mapV)
    25  
    26  		default:
    27  			newV = v
    28  		}
    29  
    30  		newM[k] = newV
    31  	}
    32  
    33  	return newM
    34  }