maputil

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2025 License: MIT Imports: 13 Imported by: 50

README

Map Utils

maputil provide map data util functions. eg: convert, sub-value get, simple merge

  • use map[string]any as Data
  • deep get value by key path
  • deep set value by key path

Install

go get github.com/gookit/goutil/maputil

Go docs

Usage

Deep get value
mp := map[string]any {
	"top1": "val1",
	"arr1": []string{"ab", "cd"}
	"map1": map[string]any{
	    "sub1": "val2",	
    },
}

fmt.Println(maputil.DeepGet(mp, "map1.sub1")) // Output: VAL3

// get value from slice.
fmt.Println(maputil.DeepGet(mp, "arr1.1")) // Output: cd
fmt.Println(maputil.DeepGet(mp, "arr1[1]")) // Output: cd
Deep set value
mp := map[string]any {
	"top1": "val1",
	"arr1": []string{"ab"}
	"map1": map[string]any{
	    "sub1": "val2",	
    },
}

err := maputil.SetByPath(&mp, "map1.newKey", "VAL3")

fmt.Println(maputil.DeepGet(mp, "map1.newKey")) // Output: VAL3

Code Check & Testing

gofmt -w -l ./
golint ./...

Testing:

go test -v ./maputil/...

Test limit by regexp:

go test -v -run ^TestSetByKeys ./maputil/...

Documentation

Overview

Package maputil provide map data util functions. eg: convert, sub-value get, simple merge

Index

Constants

View Source
const (
	Wildcard = "*"
	PathSep  = "."
)

some consts for separators

View Source
const (
	ValSepStr  = ","
	ValSepChar = ','
	KeySepStr  = "."
	KeySepChar = '.'
)

Key, value sep char consts

Variables

View Source
var (
	// ToStrMap convert map[string]any to map[string]string
	ToStrMap = ToStringMap
	// ToL2StrMap convert map[string]any to map[string]map[string]string
	ToL2StrMap = ToL2StringMap
)

alias functions

Functions

func AnyToStrMap added in v0.7.1

func AnyToStrMap(src any) map[string]string

AnyToStrMap try convert any(map[string]any, map[string]string) to map[string]string

func AppendSMap added in v0.7.2

func AppendSMap(dst, src map[string]string) map[string]string

AppendSMap append string map data to dst map.

func CombineToMap added in v0.6.9

func CombineToMap[K comdef.SortedType, V any](keys []K, values []V) map[K]V

CombineToMap combine two any slice to map[K]V. alias of arrutil.CombineToMap

func DeepGet added in v0.5.8

func DeepGet(mp map[string]any, path string) (val any)

DeepGet value by key path. eg "top" "top.sub"

func EachAnyMap added in v0.6.8

func EachAnyMap(mp any, fn func(key string, val any))

EachAnyMap iterates the given map and calls the given function for each item.

func EachTypedMap added in v0.6.16

func EachTypedMap[K comdef.SimpleType, V any](mp map[K]V, fn func(key K, val V))

EachTypedMap iterates the given map and calls the given function for each item.

func FilterSMap added in v0.6.15

func FilterSMap(sm map[string]string) map[string]string

FilterSMap filter empty elem for the string map.

func FirstKey added in v0.7.1

func FirstKey[T any](mp map[string]T) string

FirstKey returns the first key of the given map.

func FlatWithFunc added in v0.5.12

func FlatWithFunc(mp map[string]any, fn reflects.FlatFunc)

FlatWithFunc flat a tree-map with custom collect handle func

func Flatten added in v0.5.12

func Flatten(mp map[string]any) map[string]any

Flatten convert tree map to flat key-value map.

Examples:

{"top": {"sub": "value", "sub2": "value2"} }
->
{"top.sub": "value", "top.sub2": "value2" }

func FormatIndent added in v0.5.3

func FormatIndent(mp any, indent string) string

FormatIndent format map data to string with newline and indent.

func GetByPath

func GetByPath(path string, mp map[string]any) (val any, ok bool)

GetByPath get value by key path from a map(map[string]any). eg "top" "top.sub"

func GetByPathKeys added in v0.6.9

func GetByPathKeys(mp map[string]any, keys []string) (val any, ok bool)

GetByPathKeys get value by path keys from a map(map[string]any). eg "top" "top.sub"

Example:

mp := map[string]any{
	"top": map[string]any{
		"sub": "value",
	},
}
val, ok := GetByPathKeys(mp, []string{"top", "sub"}) // return "value", true

func GetFromAny added in v0.6.12

func GetFromAny(path string, data any) (val any, ok bool)

GetFromAny get value by key path from any(map,slice) data. eg "top" "top.sub"

func HTTPQueryString added in v0.6.1

func HTTPQueryString(data map[string]any) string

HTTPQueryString convert map[string]any data to http query string.

func HasAllKeys added in v0.5.8

func HasAllKeys(mp any, keys ...any) (ok bool, noKey any)

HasAllKeys check of the given map. return the first not exist key

func HasKey added in v0.5.7

func HasKey(mp, key any) (ok bool)

HasKey check of the given map.

func HasOneKey added in v0.6.9

func HasOneKey(mp any, keys ...any) (ok bool, key any)

HasOneKey check of the given map. return the first exist key

func KeyToLower

func KeyToLower(src map[string]string) map[string]string

KeyToLower convert keys to lower case.

func Keys

func Keys(mp any) (keys []string)

Keys get all keys of the given map.

func MakeByKeys added in v0.5.8

func MakeByKeys(keys []string, val any) (mp map[string]any)

MakeByKeys build new value by key names

Example:

// case 1:
[]string{"site", "info"}
->
map[string]any {
	site: {info: val}
}

// case 2, last key is slice:
[]string{"site", "tags[1]"}
->
map[string]any {
	site: {tags: [val]}
}

func MakeByPath added in v0.5.8

func MakeByPath(path string, val any) (mp map[string]any)

MakeByPath build new value by key names

Example:

"site.info"
->
map[string]any {
	site: {info: val}
}

// case 2, last key is slice:
"site.tags[1]"
->
map[string]any {
	site: {tags: [val]}
}

func Merge1level added in v0.6.16

func Merge1level(mps ...map[string]any) map[string]any

Merge1level merge multi any map[string]any data. only merge one level data.

func MergeL2StrMap added in v0.7.1

func MergeL2StrMap(mps ...map[string]map[string]string) map[string]map[string]string

MergeL2StrMap merge multi level2 string-map data. The back map covers the front.

func MergeMultiSMap added in v0.6.15

func MergeMultiSMap(mps ...map[string]string) map[string]string

MergeMultiSMap quick merge multi string-map data.

func MergeSMap added in v0.5.8

func MergeSMap(src, dst map[string]string, ignoreCase bool) map[string]string

MergeSMap simple merge two string map. merge src to dst map

func MergeStrMap added in v0.7.1

func MergeStrMap(src, dst map[string]string) map[string]string

MergeStrMap simple merge two string map. merge src to dst map

func MergeStringMap

func MergeStringMap(src, dst map[string]string, ignoreCase bool) map[string]string

MergeStringMap simple merge two string map. merge src to dst map

func QuietGet added in v0.5.8

func QuietGet(mp map[string]any, path string) (val any)

QuietGet value by key path. eg "top" "top.sub"

func SetByKeys added in v0.5.8

func SetByKeys(mp *map[string]any, keys []string, val any) (err error)

SetByKeys set sub-map value by path keys. Supports dot syntax to set deep values.

For example:

SetByKeys([]string{"name", "first"}, "Mat")

func SetByPath added in v0.5.8

func SetByPath(mp *map[string]any, path string, val any) error

SetByPath set sub-map value by key path. Supports dot syntax to set deep values.

For example:

SetByPath("name.first", "Mat")

func SimpleMerge added in v0.6.6

func SimpleMerge(src, dst map[string]any) map[string]any

SimpleMerge simple merge two data map by string key. will merge the src to dst map

func SliceToMap added in v0.7.1

func SliceToMap(kvPairs ...any) map[string]any

SliceToMap convert any k-v pairs slice to map[string]any

func SliceToSMap added in v0.7.1

func SliceToSMap(kvPairs ...string) map[string]string

SliceToSMap convert string k-v pairs slice to map[string]string

  • eg: []string{k1,v1,k2,v2} -> map[string]string{k1:v1, k2:v2}

func SliceToTypeMap added in v0.7.1

func SliceToTypeMap[T any](valFunc func(any) T, kvPairs ...any) map[string]T

SliceToTypeMap convert k-v pairs slice to map[string]T

func StringsMapToAnyMap added in v0.6.10

func StringsMapToAnyMap(ssMp map[string][]string) map[string]any

StringsMapToAnyMap convert map[string][]string to map[string]any

Example:
{"k1": []string{"v1", "v2"}, "k2": []string{"v3"}}
=>
{"k": []string{"v1", "v2"}, "k2": "v3"}

mp := StringsMapToAnyMap(httpReq.Header)

func ToAnyMap added in v0.6.8

func ToAnyMap(mp any) map[string]any

ToAnyMap convert map[TYPE1]TYPE2 to map[string]any

func ToL2StringMap added in v0.7.1

func ToL2StringMap(groupsMap map[string]any) map[string]map[string]string

ToL2StringMap convert map[string]any to map[string]map[string]string

func ToString added in v0.4.5

func ToString(mp map[string]any) string

ToString simple and quickly convert map[string]any to string.

func ToString2 added in v0.5.3

func ToString2(mp any) string

ToString2 simple and quickly convert a map to string.

func ToStringMap added in v0.3.9

func ToStringMap(src map[string]any) map[string]string

ToStringMap simple convert map[string]any to map[string]string

func TryAnyMap added in v0.6.9

func TryAnyMap(mp any) (map[string]any, error)

TryAnyMap convert map[TYPE1]TYPE2 to map[string]any

func TypedKeys added in v0.6.16

func TypedKeys[K comdef.SimpleType, V any](mp map[K]V) (keys []K)

TypedKeys get all keys of the given typed map.

func TypedValues added in v0.6.16

func TypedValues[K comdef.SimpleType, V any](mp map[K]V) (values []V)

TypedValues get all values from the given typed map.

func Values

func Values(mp any) (values []any)

Values get all values from the given map.

Types

type Aliases added in v0.3.9

type Aliases map[string]string

Aliases implemented a simple string alias map.

  • key: alias, value: real name

func (Aliases) AddAlias added in v0.3.9

func (as Aliases) AddAlias(alias, real string)

AddAlias to the Aliases map

func (Aliases) AddAliasMap added in v0.3.9

func (as Aliases) AddAliasMap(alias2real map[string]string)

AddAliasMap to the Aliases map

func (Aliases) AddAliases added in v0.3.9

func (as Aliases) AddAliases(real string, aliases []string)

AddAliases to the Aliases map

func (Aliases) AliasesNames added in v0.7.2

func (as Aliases) AliasesNames() []string

AliasesNames returns all sorted alias names.

func (Aliases) GroupAliases added in v0.7.2

func (as Aliases) GroupAliases() map[string][]string

GroupAliases groups aliases by real name.

returns: {real name -> []aliases, ...}

func (Aliases) HasAlias added in v0.3.9

func (as Aliases) HasAlias(alias string) bool

HasAlias in the Aliases map

func (Aliases) ResolveAlias added in v0.3.9

func (as Aliases) ResolveAlias(alias string) string

ResolveAlias by given name.

type Data added in v0.3.9

type Data map[string]any

Data alias of map[string]any

func (Data) AnyMap added in v0.7.1

func (d Data) AnyMap(key string) map[string]any

AnyMap get sub value as map[string]any

func (Data) Bool added in v0.3.13

func (d Data) Bool(key string) bool

Bool value get

func (Data) BoolOne added in v0.7.1

func (d Data) BoolOne(keys ...string) bool

BoolOne value gets from multi keys, return first value

func (Data) Default added in v0.3.9

func (d Data) Default(key string, def any) any

Default get value from the data map with default value

func (Data) Get added in v0.3.9

func (d Data) Get(key string) any

Get value from the data map. Supports dot syntax to get deep values. eg: top.sub

func (Data) GetByPath added in v0.5.3

func (d Data) GetByPath(path string) (any, bool)

GetByPath get value from the data map by path. eg: top.sub Supports dot syntax to get deep values.

func (Data) Has added in v0.3.9

func (d Data) Has(key string) bool

Has value on the data map

func (Data) Int added in v0.3.9

func (d Data) Int(key string) int

Int value get

func (Data) Int64 added in v0.3.9

func (d Data) Int64(key string) int64

Int64 value get

func (Data) IsEmpty added in v0.6.15

func (d Data) IsEmpty() bool

IsEmpty if the data map

func (Data) Keys added in v0.5.8

func (d Data) Keys() []string

Keys of the data map

func (Data) Load added in v0.6.6

func (d Data) Load(sub map[string]any)

Load other data to current data map

func (Data) LoadSMap added in v0.6.8

func (d Data) LoadSMap(smp map[string]string)

LoadSMap to data

func (Data) One added in v0.7.1

func (d Data) One(keys ...string) any

One get value from the data by multi paths. will return first founded value

func (Data) Set added in v0.3.9

func (d Data) Set(key string, val any)

Set value to the data map

func (Data) SetByKeys added in v0.5.9

func (d Data) SetByKeys(keys []string, value any) error

SetByKeys sets a value in the map by path keys. Supports dot syntax to set deep values.

Example:

d.SetByKeys([]string{"name", "first"}, "Mat")

func (Data) SetByPath added in v0.5.8

func (d Data) SetByPath(path string, value any) error

SetByPath sets a value in the map. Supports dot syntax to set deep values.

Example:

d.SetByPath("name.first", "Mat")

func (Data) Slice added in v0.6.15

func (d Data) Slice(key string) ([]any, error)

Slice get []any value from data map

func (Data) Str added in v0.3.10

func (d Data) Str(key string) string

Str value gets by key

func (Data) StrMap added in v0.6.7

func (d Data) StrMap(key string) map[string]string

StrMap get map[string]string value

func (Data) StrOne added in v0.7.1

func (d Data) StrOne(keys ...string) string

StrOne value gets by multi keys, will return first value

func (Data) StrSplit added in v0.5.8

func (d Data) StrSplit(key, sep string) []string

StrSplit get strings by split string value

func (Data) String added in v0.3.9

func (d Data) String() string

String data to string

func (Data) StringMap added in v0.3.9

func (d Data) StringMap(key string) map[string]string

StringMap get map[string]string value

func (Data) Strings added in v0.5.3

func (d Data) Strings(key string) []string

Strings get []string value by key

func (Data) StringsByStr added in v0.5.3

func (d Data) StringsByStr(key string) []string

StringsByStr value gets by key, will split string value by ","

func (Data) StringsOne added in v0.7.1

func (d Data) StringsOne(keys ...string) []string

StringsOne get []string value by multi keys, return first founded value

func (Data) Sub added in v0.5.8

func (d Data) Sub(key string) Data

Sub get sub value(map[string]any) as new Data

func (Data) ToStringMap added in v0.5.3

func (d Data) ToStringMap() map[string]string

ToStringMap convert to map[string]string

func (Data) TryOne added in v0.7.1

func (d Data) TryOne(keys ...string) (any, bool)

TryOne get value from the data by multi paths. will return first founded value

func (Data) Uint added in v0.6.2

func (d Data) Uint(key string) uint

Uint value get

func (Data) Uint64 added in v0.6.15

func (d Data) Uint64(key string) uint64

Uint64 value get

func (Data) Value added in v0.5.3

func (d Data) Value(key string) (any, bool)

Value get from the data map

type L2StrMap added in v0.7.1

type L2StrMap map[string]map[string]string

L2StrMap is alias of map[string]map[string]string

func (L2StrMap) Exists added in v0.7.1

func (m L2StrMap) Exists(key string) bool

Exists check key path exists. eg: "top.sub"

func (L2StrMap) Get added in v0.7.1

func (m L2StrMap) Get(key string) string

Get value by key path. eg: "top.sub"

func (L2StrMap) Load added in v0.7.1

func (m L2StrMap) Load(mp map[string]map[string]string)

Load data, merge new data to old

func (L2StrMap) StrMap added in v0.7.1

func (m L2StrMap) StrMap(top string) StrMap

StrMap get by top key. eg: "top"

func (L2StrMap) Value added in v0.7.1

func (m L2StrMap) Value(key string) (val string, ok bool)

Value get by key path. eg: "top.sub"

type Map added in v0.5.8

type Map = Data

Map alias of Data

type MapFormatter added in v0.5.3

type MapFormatter struct {
	comdef.BaseFormatter
	// Prefix string for each element
	Prefix string
	// Indent string for each element
	Indent string
	// ClosePrefix string for last "}"
	ClosePrefix string
}

MapFormatter struct

func NewFormatter added in v0.5.3

func NewFormatter(mp any) *MapFormatter

NewFormatter instance

func (*MapFormatter) Format added in v0.5.3

func (f *MapFormatter) Format() string

Format to string

func (*MapFormatter) FormatTo added in v0.5.3

func (f *MapFormatter) FormatTo(w io.Writer)

FormatTo to custom buffer

func (*MapFormatter) String added in v0.5.3

func (f *MapFormatter) String() string

Format to string

func (*MapFormatter) WithFn added in v0.5.3

func (f *MapFormatter) WithFn(fn func(f *MapFormatter)) *MapFormatter

WithFn for config self

func (*MapFormatter) WithIndent added in v0.5.3

func (f *MapFormatter) WithIndent(indent string) *MapFormatter

WithIndent string

type SM added in v0.7.2

type SM = StrMap

SM is alias of map[string]string

type SMap added in v0.3.13

type SMap = StrMap

SMap and StrMap is alias of map[string]string

func CombineToSMap added in v0.6.5

func CombineToSMap(keys, values []string) SMap

CombineToSMap combine two string-slices to SMap(map[string]string)

func (SMap) StrOne added in v0.7.1

func (m SMap) StrOne(keys ...string) string

StrOne get first founded value by keys

type StrMap added in v0.7.1

type StrMap map[string]string

func (StrMap) Bool added in v0.7.1

func (m StrMap) Bool(key string) bool

Bool value get

func (StrMap) Default added in v0.7.1

func (m StrMap) Default(key, defVal string) string

Default get value by key. if not found, return defVal

func (StrMap) Get added in v0.7.1

func (m StrMap) Get(key string) string

Get value by key

func (StrMap) Has added in v0.7.1

func (m StrMap) Has(key string) bool

Has key on the data map

func (StrMap) HasValue added in v0.7.1

func (m StrMap) HasValue(val string) bool

HasValue on the data map

func (StrMap) IfExist added in v0.7.1

func (m StrMap) IfExist(key string, fn func(val string))

IfExist key, then call the fn with value.

func (StrMap) IfValid added in v0.7.1

func (m StrMap) IfValid(key string, fn func(val string))

IfValid value is not empty, then call the fn

func (StrMap) Int added in v0.7.1

func (m StrMap) Int(key string) int

Int value get

func (StrMap) Int64 added in v0.7.1

func (m StrMap) Int64(key string) int64

Int64 value get

func (StrMap) Ints added in v0.7.1

func (m StrMap) Ints(key string) []int

Ints value to []int

func (StrMap) IsEmpty added in v0.7.1

func (m StrMap) IsEmpty() bool

IsEmpty of the data map

func (StrMap) Keys added in v0.7.1

func (m StrMap) Keys() []string

Keys of the string-map

func (StrMap) Load added in v0.7.1

func (m StrMap) Load(data map[string]string)

Load data to the map

func (StrMap) Set added in v0.7.1

func (m StrMap) Set(key string, val any)

Set value to the data map

func (StrMap) Str added in v0.7.1

func (m StrMap) Str(key string) string

Str value get

func (StrMap) String added in v0.7.1

func (m StrMap) String() string

String data to string

func (StrMap) Strings added in v0.7.1

func (m StrMap) Strings(key string) (ss []string)

Strings value to []string

func (StrMap) ToKVPairs added in v0.7.1

func (m StrMap) ToKVPairs() []string

ToKVPairs slice convert. eg: {k1:v1,k2:v2} => {k1,v1,k2,v2}

func (StrMap) Value added in v0.7.1

func (m StrMap) Value(key string) (string, bool)

Value get from the data map

func (StrMap) Values added in v0.7.1

func (m StrMap) Values() []string

Values of the string-map

Jump to

Keyboard shortcuts

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