github.com/wtfutil/wtf@v0.43.0/utils/conversions.go (about)

     1  package utils
     2  
     3  import (
     4  	"strconv"
     5  )
     6  
     7  /* -------------------- Map Conversion -------------------- */
     8  
     9  // MapToStrs takes a map of interfaces and returns a map of strings
    10  func MapToStrs(aMap map[string]interface{}) map[string]string {
    11  	results := make(map[string]string, len(aMap))
    12  
    13  	for key, val := range aMap {
    14  		results[key] = val.(string)
    15  	}
    16  
    17  	return results
    18  }
    19  
    20  /* -------------------- Slice Conversion -------------------- */
    21  
    22  // IntsToUints takes a slice of ints and returns a slice of uints
    23  func IntsToUints(slice []int) []uint {
    24  	results := make([]uint, len(slice))
    25  
    26  	for i, val := range slice {
    27  		results[i] = uint(val)
    28  	}
    29  
    30  	return results
    31  }
    32  
    33  // ToInts takes a slice of interfaces and returns a slice of ints
    34  func ToInts(slice []interface{}) []int {
    35  	results := make([]int, len(slice))
    36  
    37  	for i, val := range slice {
    38  		results[i] = val.(int)
    39  	}
    40  
    41  	return results
    42  }
    43  
    44  // ToStrs takes a slice of interfaces and returns a slice of strings
    45  func ToStrs(slice []interface{}) []string {
    46  	results := make([]string, len(slice))
    47  
    48  	for i, val := range slice {
    49  		switch t := val.(type) {
    50  		case int:
    51  			results[i] = strconv.Itoa(t)
    52  		case string:
    53  			results[i] = t
    54  		}
    55  	}
    56  
    57  	return results
    58  }
    59  
    60  // ToUints takes a slice of interfaces and returns a slice of ints
    61  func ToUints(slice []interface{}) []uint {
    62  	results := make([]uint, len(slice))
    63  
    64  	for i, val := range slice {
    65  		results[i] = val.(uint)
    66  	}
    67  
    68  	return results
    69  }