Documentation ¶
Overview ¶
Package collections provides methods to operate on collections of values (structs/maps/arrays)
Index ¶
- Constants
- func ConvertBool(input sql.NullBool) (output *bool)
- func ConvertFloat(input sql.NullFloat64) float64
- func ConvertFloatToLongStringNumber(input float64) string
- func ConvertInt(input sql.NullInt64) int64
- func ConvertMapToSyncMap(input map[string]interface{}) (output *sync.Map)
- func ConvertString(input sql.NullString) (output string)
- func ConvertStructToMap(input []string) map[string]bool
- func ConvertStructToSyncMap(input []string) *sync.Map
- func ConvertStructToSyncMapWithCallback(input []string, callback func(inputItem string) (string, bool)) *sync.Map
- func ConvertSyncMapToMap(input *sync.Map) (output map[string]interface{})
- func ExtractIntFromString(input string, defaultVal int64) int64
- func ExtractMapValues(inputMap map[string]interface{}) []interface{}
- func FormatTimePointer(input *time.Time) interface{}
- func GetMapValueOrError(input map[string]string, key string) (string, error)
- func JoinMap(inputMap map[string]string, sep string) (keysStr, valuesStr string)
- func MapToSlices(inputMap map[string]string) (keys, values []string)
- func MarshalSyncMap(input *sync.Map) (data []byte, err error)
- func StringerToJSON(input fmt.Stringer) []byte
- func UnMarshalSyncMap(data []byte) (syncMap *sync.Map, err error)
Examples ¶
Constants ¶
const MysqlTimeFormat = "2006-01-02 15:04:05"
Variables ¶
This section is empty.
Functions ¶
func ConvertBool ¶
func ConvertFloat ¶
func ConvertFloat(input sql.NullFloat64) float64
func ConvertInt ¶
func ConvertMapToSyncMap ¶
ConvertMapToSyncMap converts map[string]interface{} to sync.Map
func ConvertString ¶
func ConvertString(input sql.NullString) (output string)
func ConvertStructToMap ¶
ConvertStructToMap converts []string{"red", "blue", "red"} to map[string]bool{"red":true,"blue":true}
func ConvertStructToSyncMap ¶
ConvertStructToSyncMap converts []string{"red", "blue"} to sync.Map{"red":true,"blue":true}
func ConvertStructToSyncMapWithCallback ¶
func ConvertStructToSyncMapWithCallback(input []string, callback func(inputItem string) (string, bool)) *sync.Map
* ConvertStructToSyncMapWithCallback does the same as ConvertStructToSyncMap with additional filter/mutator callback * callback result string should return a modified value, result bool if false item won't be included in the output
Example ¶
This example converts struct to sync map with additional applying of callback function to modify and filter elements
// Want to have unique map of file names without extension and without '.' and '..' // something like sync.Map{"file":true} collectedFileNames := []string{".", "..", "file.txt", "file.txt"} resultMap := ConvertStructToSyncMapWithCallback( collectedFileNames, func(inputItem string) (modifiedString string, shouldBeIncluded bool) { if inputItem == "." || inputItem == ".." { return "", false } return strings.Split(inputItem, ".")[0], true }, ) printableMap := map[string]bool{} resultMap.Range(func(key, value interface{}) bool { printableMap[key.(string)] = value.(bool) return true }) fmt.Printf("%+v", printableMap)
Output: map[file:true]
func ConvertSyncMapToMap ¶
ConvertSyncMapToMap converts sync.Map to map[string]interface{}
func ExtractIntFromString ¶
func ExtractMapValues ¶
func ExtractMapValues(inputMap map[string]interface{}) []interface{}
ExtractMapValues converts map[string]interface{}{"Bob":"Bob", "Alice":"Alice", "John":"John"} by filter values []interface{}{"Bob","Alice"} to []interface{}{"Bob", "Alice", "John"}
func FormatTimePointer ¶
func GetMapValueOrError ¶
GetMapValueOrError returns error if map doesn't contain the provided key
func JoinMap ¶
JoinMap converts map[string]string{"name":"Bob","color":"red","size","big"} to 2 strings "name,color,size" and "Bob,red,big" if "," is provided as separator
func MapToSlices ¶
MapToSlice converts map[string]string{"name":"Bob","color":"red","size","big"} to keys and values slices like []string{"name","color","size"} and []string{"Bob","red","big"}
func MarshalSyncMap ¶
MarshalSyncMap converts sync.Map to a json formatted byte string
func StringerToJSON ¶
Types ¶
This section is empty.