github.com/argoproj/argo-cd/v2@v2.10.9/applicationset/utils/map.go (about)

     1  package utils
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  func ConvertToMapStringString(mapStringInterface map[string]interface{}) map[string]string {
     8  	mapStringString := make(map[string]string, len(mapStringInterface))
     9  
    10  	for key, value := range mapStringInterface {
    11  		strKey := fmt.Sprintf("%v", key)
    12  		strValue := fmt.Sprintf("%v", value)
    13  
    14  		mapStringString[strKey] = strValue
    15  	}
    16  	return mapStringString
    17  }
    18  
    19  func ConvertToMapStringInterface(mapStringString map[string]string) map[string]interface{} {
    20  	mapStringInterface := make(map[string]interface{}, len(mapStringString))
    21  
    22  	for key, value := range mapStringString {
    23  		mapStringInterface[key] = value
    24  	}
    25  	return mapStringInterface
    26  }
    27  
    28  func CombineStringMaps(aSI map[string]interface{}, bSI map[string]interface{}) (map[string]string, error) {
    29  
    30  	a := ConvertToMapStringString(aSI)
    31  	b := ConvertToMapStringString(bSI)
    32  
    33  	res := map[string]string{}
    34  
    35  	for k, v := range a {
    36  		res[k] = v
    37  	}
    38  
    39  	for k, v := range b {
    40  		current, present := res[k]
    41  		if present && current != v {
    42  			return nil, fmt.Errorf("found duplicate key %s with different value, a: %s ,b: %s", k, current, v)
    43  		}
    44  		res[k] = v
    45  	}
    46  
    47  	return res, nil
    48  }
    49  
    50  // CombineStringMapsAllowDuplicates merges two maps. Where there are duplicates, take the latter map's value.
    51  func CombineStringMapsAllowDuplicates(aSI map[string]interface{}, bSI map[string]interface{}) (map[string]string, error) {
    52  
    53  	a := ConvertToMapStringString(aSI)
    54  	b := ConvertToMapStringString(bSI)
    55  
    56  	res := map[string]string{}
    57  
    58  	for k, v := range a {
    59  		res[k] = v
    60  	}
    61  
    62  	for k, v := range b {
    63  		res[k] = v
    64  	}
    65  
    66  	return res, nil
    67  }