Skip to content
Snippets Groups Projects
Select Git revision
  • c03e68f3ad252d7f1687b5b7151471636367dcd2
  • main default protected
  • v1.302.0
  • v1.301.0
  • v1.300.0
  • v1.299.0
  • v1.298.0
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
22 results

map_utils.go

  • map_utils.go 1.57 KiB
    package map_utils
    
    import (
    	"fmt"
    	"strconv"
    	"strings"
    )
    
    // MapStringInterfaceToMapStringString converts a generic value typed map to a map with string values
    func MapStringInterfaceToMapStringString(inputMap map[string]interface{}) map[string]string {
    	query := make(map[string]string)
    	for mapKey, mapVal := range inputMap {
    		// Check if mapVal is a slice or a single value
    		switch mapValTyped := mapVal.(type) {
    		case []interface{}:
    			// Slice - convert each element individually
    			var mapValString []string
    
    			// Loop through each element in the slice and check the type
    			for _, sliceElem := range mapValTyped {
    				switch sliceElemTyped := sliceElem.(type) {
    				case string:
    					// Enclose strings in escaped quotations
    					mapValString = append(mapValString, fmt.Sprintf("\"%v\"", sliceElemTyped))
    				case float64:
    					// Use FormatFloat for least amount of precision.
    					mapValString = append(mapValString, strconv.FormatFloat(sliceElemTyped, 'f', -1, 64))
    				default:
    					// Convert to string
    					mapValString = append(mapValString, fmt.Sprintf("%v", sliceElemTyped))
    				}
    			}
    			// Join as a comma seperated array
    			query[mapKey] = "[" + strings.Join(mapValString, ",") + "]"
    		default:
    			// Single value - convert to string
    			query[mapKey] = fmt.Sprintf("%v", mapVal)
    		}
    	}
    
    	return query
    }
    
    // MergeMaps If there are similar properties in the maps, the last one will be used as the value
    func MergeMaps(maps ...map[string]string) map[string]string {
    	ret := map[string]string{}
    	for _, mapV := range maps {
    		for k, v := range mapV {
    			ret[k] = v
    		}
    	}
    	return ret
    }