conv

package
v1.3.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 3, 2021 License: MPL-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package collections provides methods to operate on collections of values (structs/maps/arrays)

Index

Examples

Constants

View Source
const MYSQL_TIME_FORMAT = "2006-01-02 15:04:05"

Variables

This section is empty.

Functions

func ConvertBool

func ConvertBool(input sql.NullBool) (output *bool)

func ConvertFloat

func ConvertFloat(input sql.NullFloat64) float64

func ConvertFloatToLongStringNumber

func ConvertFloatToLongStringNumber(input float64) string

func ConvertInt

func ConvertInt(input sql.NullInt64) int64

func ConvertMapToSyncMap added in v1.3.10

func ConvertMapToSyncMap(input map[string]interface{}) (output *sync.Map)

ConvertMapToSyncMap converts map[string]interface{} to sync.Map

func ConvertString

func ConvertString(input sql.NullString) (output string)

func ConvertStructToMap added in v1.3.10

func ConvertStructToMap(input []string) map[string]bool

ConvertStructToMap converts []string{"red", "blue", "red"} to map[string]bool{"red":true,"blue":true}

func ConvertStructToSyncMap added in v1.3.10

func ConvertStructToSyncMap(input []string) *sync.Map

ConvertStructToSyncMap converts []string{"red", "blue"} to sync.Map{"red":true,"blue":true}

func ConvertStructToSyncMapWithCallback added in v1.3.10

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 added in v1.3.10

func ConvertSyncMapToMap(input *sync.Map) (output map[string]interface{})

ConvertSyncMapToMap converts sync.Map to map[string]interface{}

func ExtractIntFromString

func ExtractIntFromString(input string, defaultVal int64) int64

func ExtractMapValues added in v1.3.10

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 FormatTimePointer(input *time.Time) interface{}

func GetMapValueOrError added in v1.3.10

func GetMapValueOrError(input map[string]string, key string) (string, error)

GetMapValueOrError returns error if map doesn't contain the provided key

func JoinMap added in v1.3.10

func JoinMap(inputMap map[string]string, sep string) (keysStr, valuesStr string)

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 added in v1.3.10

func MapToSlices(inputMap map[string]string) (keys, values []string)

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 added in v1.3.10

func MarshalSyncMap(input *sync.Map) (data []byte, err error)

MarshalSyncMap converts sync.Map to a json formatted byte string

func StringerToJson

func StringerToJson(input fmt.Stringer) []byte

func UnMarshalSyncMap added in v1.3.10

func UnMarshalSyncMap(data []byte) (syncMap *sync.Map, err error)

UnMarshalSyncMap converts json formatted byte string to sync.Map

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL