conv

package
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 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 MysqlTimeFormat = "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

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

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

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

func ConvertStructToSyncMap

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

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

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

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

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

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

func JoinMap

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

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

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

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