Documentation
¶
Overview ¶
Package maputil provide map data util functions. eg: convert, sub-value get, simple merge
Index ¶
- Constants
- Variables
- func AnyToStrMap(src any) map[string]string
- func AppendSMap(dst, src map[string]string) map[string]string
- func Clone[M ~map[K]V, K comparable, V any](items M) M
- func CloneAnyMap(items map[string]any) map[string]any
- func CloneStringMap(items map[string]string) map[string]string
- func CombineToMap[K comdef.SortedType, V any](keys []K, values []V) map[K]V
- func Copy[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2)
- func DeepGet(mp map[string]any, path string) (val any)
- func DeleteFunc[M ~map[K]V, K comparable, V any](m M, del func(K, V) bool)
- func EachAnyMap(mp any, fn func(key string, val any))
- func EachTypedMap[K comdef.SimpleType, V any](mp map[K]V, fn func(key K, val V))
- func Equal[M1 ~map[K]V, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool
- func EqualFunc[M1 ~map[K]V1, M2 ~map[K]V2, K comparable, V1, V2 any](m1 M1, m2 M2, eq func(V1, V2) bool) bool
- func Filter[K comparable, V any](mp map[K]V, fn func(K, V) bool) map[K]V
- func FilterSMap(sm map[string]string) map[string]string
- func FirstKey[T any](mp map[string]T) string
- func FlatWithFunc(mp map[string]any, fn reflects.FlatFunc)
- func Flatten(mp map[string]any) map[string]any
- func FormatIndent(mp any, indent string) string
- func GetByPath(path string, mp map[string]any) (val any, ok bool)
- func GetByPathKeys(mp map[string]any, keys []string) (val any, ok bool)
- func GetFromAny(path string, data any) (val any, ok bool)
- func HTTPQueryString(data map[string]any) string
- func HasAllKeys(mp any, keys ...any) (ok bool, noKey any)
- func HasKey(mp, key any) (ok bool)
- func HasOneKey(mp any, keys ...any) (ok bool, key any)
- func Invert[K comparable, V comparable](mp map[K]V) map[V]K
- func KeyToLower(src map[string]string) map[string]string
- func Keys(mp any) (keys []string)
- func MakeByKeys(keys []string, val any) (mp map[string]any)
- func MakeByPath(path string, val any) (mp map[string]any)
- func MapValues[K comparable, V any, R any](mp map[K]V, fn func(K, V) R) map[K]R
- func Merge1level(mps ...map[string]any) map[string]any
- func MergeFunc[K comparable, V any](dst, src map[K]V, fn func(dstVal, srcVal V) V)
- func MergeL2StrMap(mps ...map[string]map[string]string) map[string]map[string]string
- func MergeMultiSMap(mps ...map[string]string) map[string]string
- func MergeSMap(src, dst map[string]string, ignoreCase bool) map[string]string
- func MergeStrMap(src, dst map[string]string) map[string]string
- func MergeStringMap(src, dst map[string]string, ignoreCase bool) map[string]string
- func Omit[K comparable, V any](mp map[K]V, keys ...K) map[K]V
- func Pick[K comparable, V any](mp map[K]V, keys ...K) map[K]V
- func QuietGet(mp map[string]any, path string) (val any)
- func SetByKeys(mp *map[string]any, keys []string, val any) (err error)
- func SetByPath(mp *map[string]any, path string, val any) error
- func SimpleMerge(src, dst map[string]any) map[string]any
- func SliceToMap(kvPairs ...any) map[string]any
- func SliceToSMap(kvPairs ...string) map[string]string
- func SliceToTypeMap[T any](valFunc func(any) T, kvPairs ...any) map[string]T
- func StrMapToText(m map[string]string) string
- func StringsMapToAnyMap(ssMp map[string][]string) map[string]any
- func ToAnyMap(mp any) map[string]any
- func ToL2StringMap(groupsMap map[string]any) map[string]map[string]string
- func ToString(mp map[string]any) string
- func ToString2(mp any) string
- func ToStringMap(src map[string]any) map[string]string
- func TryAnyMap(mp any) (map[string]any, error)
- func TypedKeys[K comdef.SimpleType, V any](mp map[K]V) (keys []K)
- func TypedValues[K comdef.SimpleType, V any](mp map[K]V) (values []V)
- func Values(mp any) (values []any)
- type Aliases
- func (as Aliases) AddAlias(alias, real string)
- func (as Aliases) AddAliasMap(alias2real map[string]string)
- func (as Aliases) AddAliases(real string, aliases []string)
- func (as Aliases) AliasesNames() []string
- func (as Aliases) GroupAliases() map[string][]string
- func (as Aliases) HasAlias(alias string) bool
- func (as Aliases) ResolveAlias(alias string) string
- type Data
- func (d Data) AnyMap(key string) map[string]any
- func (d Data) Bool(key string) bool
- func (d Data) BoolOne(keys ...string) bool
- func (d Data) Default(key string, def any) any
- func (d Data) Get(key string) any
- func (d Data) GetByPath(path string) (any, bool)
- func (d Data) Has(key string) bool
- func (d Data) Int(key string, defVal ...int) int
- func (d Data) Int64(key string, defVal ...int64) int64
- func (d Data) IsEmpty() bool
- func (d Data) Keys() []string
- func (d Data) Load(sub map[string]any)
- func (d Data) LoadSMap(smp map[string]string)
- func (d Data) One(keys ...string) any
- func (d Data) Set(key string, val any)
- func (d Data) SetByKeys(keys []string, value any) error
- func (d Data) SetByPath(path string, value any) error
- func (d Data) Slice(key string) ([]any, error)
- func (d Data) Str(key string, defVal ...string) string
- func (d Data) StrMap(key string) map[string]string
- func (d Data) StrOne(keys ...string) string
- func (d Data) StrSplit(key, sep string) []string
- func (d Data) String() string
- func (d Data) StringMap(key string) map[string]string
- func (d Data) Strings(key string) []string
- func (d Data) StringsByStr(key string) []string
- func (d Data) StringsOne(keys ...string) []string
- func (d Data) Sub(key string) Data
- func (d Data) ToStringMap() map[string]string
- func (d Data) TryOne(keys ...string) (any, bool)
- func (d Data) Uint(key string, defVal ...uint) uint
- func (d Data) Uint16(key string, defVal ...uint16) uint16
- func (d Data) Uint64(key string, defVal ...uint64) uint64
- func (d Data) Value(key string) (any, bool)
- type L2StrMap
- type Map
- type MapFormatter
- type SM
- type SMap
- type StrMap
- func (m StrMap) Bool(key string) bool
- func (m StrMap) Default(key, defVal string) string
- func (m StrMap) Get(key string) string
- func (m StrMap) Has(key string) bool
- func (m StrMap) HasValue(val string) bool
- func (m StrMap) IfExist(key string, fn func(val string))
- func (m StrMap) IfValid(key string, fn func(val string))
- func (m StrMap) Int(key string) int
- func (m StrMap) Int64(key string) int64
- func (m StrMap) Ints(key string) []int
- func (m StrMap) IsEmpty() bool
- func (m StrMap) Keys() []string
- func (m StrMap) Load(data map[string]string)
- func (m StrMap) Set(key string, val any)
- func (m StrMap) Str(key string) string
- func (m StrMap) String() string
- func (m StrMap) Strings(key string) (ss []string)
- func (m StrMap) ToKVPairs() []string
- func (m StrMap) Value(key string) (string, bool)
- func (m StrMap) Values() []string
Constants ¶
const ( Wildcard = "*" PathSep = "." )
some consts for separators
const ( ValSepStr = "," ValSepChar = ',' KeySepStr = "." KeySepChar = '.' )
Key, value sep char consts
Variables ¶
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
AnyToStrMap try convert any(map[string]any, map[string]string) to map[string]string
func AppendSMap ¶ added in v0.7.2
AppendSMap append string map data to dst map.
func Clone ¶ added in v0.7.6
func Clone[M ~map[K]V, K comparable, V any](items M) M
Clone copy and return a new map. A nil or empty map gives back nil.
func CloneAnyMap ¶ added in v0.7.6
CloneAnyMap copy and return a new any map.
func CloneStringMap ¶ added in v0.7.6
CloneStringMap copy and return a new string map. A nil or empty map gives back nil.
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 Copy ¶ added in v0.7.4
func Copy[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2)
Copy copies all key/value pairs in src adding them to dst. When a key in src is already present in dst, the value in dst will be overwritten by the value associated with the key in src.
func DeleteFunc ¶ added in v0.7.4
func DeleteFunc[M ~map[K]V, K comparable, V any](m M, del func(K, V) bool)
DeleteFunc deletes any key/value pairs from m for which del returns true.
func EachAnyMap ¶ added in v0.6.8
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 Equal ¶ added in v0.9.0
func Equal[M1 ~map[K]V, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool
Equal reports whether the two maps have the same key/value pairs.
func EqualFunc ¶ added in v0.9.0
func EqualFunc[M1 ~map[K]V1, M2 ~map[K]V2, K comparable, V1, V2 any](m1 M1, m2 M2, eq func(V1, V2) bool) bool
EqualFunc is like Equal, but compares the values with the given function.
func Filter ¶ added in v0.9.0
func Filter[K comparable, V any](mp map[K]V, fn func(K, V) bool) map[K]V
Filter returns a new map with the key/value pairs for which fn returns true.
func FilterSMap ¶ added in v0.6.15
FilterSMap filter empty elem for the string map.
func FlatWithFunc ¶ added in v0.5.12
FlatWithFunc flat a tree-map with custom collect handle func
func Flatten ¶ added in v0.5.12
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
FormatIndent format map data to string with newline and indent.
func GetByPathKeys ¶ added in v0.6.9
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
GetFromAny get value by key path from any(map,slice) data. eg "top" "top.sub"
func HTTPQueryString ¶ added in v0.6.1
HTTPQueryString convert map[string]any data to http query string.
func HasAllKeys ¶ added in v0.5.8
HasAllKeys check of the given map. return the first not exist key
func Invert ¶ added in v0.9.0
func Invert[K comparable, V comparable](mp map[K]V) map[V]K
Invert returns a new map with the keys and values of the given map swapped.
When a value is not unique the last key of the iteration wins, so it is mostly useful for a map with unique values.
func KeyToLower ¶
KeyToLower convert keys to lower case.
func MakeByKeys ¶ added in v0.5.8
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
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 MapValues ¶ added in v0.9.0
func MapValues[K comparable, V any, R any](mp map[K]V, fn func(K, V) R) map[K]R
MapValues returns a new map with the values mapped by fn, the keys are kept.
func Merge1level ¶ added in v0.6.16
Merge1level merge multi any map[string]any data. only merge one level data.
func MergeFunc ¶ added in v0.9.0
func MergeFunc[K comparable, V any](dst, src map[K]V, fn func(dstVal, srcVal V) V)
MergeFunc copies the key/value pairs of src into dst, and uses fn to resolve a key that is present in both maps. The first argument of fn is the value of dst.
It works like maps.Copy for the keys that only exist in one of the maps.
func MergeL2StrMap ¶ added in v0.7.1
MergeL2StrMap merge multi level2 string-map data. The back map covers the front.
func MergeMultiSMap ¶ added in v0.6.15
MergeMultiSMap quick merge multi string-map data.
func MergeStrMap ¶ added in v0.7.1
MergeStrMap simple merge two string map. merge src to dst map
func MergeStringMap ¶
MergeStringMap simple merge two string map. merge src to dst map
func Omit ¶ added in v0.9.0
func Omit[K comparable, V any](mp map[K]V, keys ...K) map[K]V
Omit returns a new map without the given keys.
func Pick ¶ added in v0.9.0
func Pick[K comparable, V any](mp map[K]V, keys ...K) map[K]V
Pick returns a new map with only the given keys that exist in the map.
func SetByKeys ¶ added in v0.5.8
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
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
SimpleMerge simple merge two data map by string key. will merge the src to dst map
func SliceToMap ¶ added in v0.7.1
SliceToMap convert any k-v pairs slice to map[string]any
func SliceToSMap ¶ added in v0.7.1
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
SliceToTypeMap convert k-v pairs slice to map[string]T
func StrMapToText ¶ added in v0.7.4
StrMapToText 将 map[string]string 转换为多行 key=value 格式文本
func StringsMapToAnyMap ¶ added in v0.6.10
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 ToL2StringMap ¶ added in v0.7.1
ToL2StringMap convert map[string]any to map[string]map[string]string
func ToStringMap ¶ added in v0.3.9
ToStringMap simple convert map[string]any to map[string]string
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.
The key order is not fixed, like a range over the 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 of the given typed map.
The value order is not fixed, like a range over the map.
Types ¶
type Aliases ¶ added in v0.3.9
Aliases implemented a simple string alias map.
- key: alias, value: real name
func (Aliases) AddAliasMap ¶ added in v0.3.9
AddAliasMap to the Aliases map
func (Aliases) AddAliases ¶ added in v0.3.9
AddAliases to the Aliases map
func (Aliases) AliasesNames ¶ added in v0.7.2
AliasesNames returns all sorted alias names.
func (Aliases) GroupAliases ¶ added in v0.7.2
GroupAliases groups aliases by real name.
returns: {real name -> []aliases, ...}
func (Aliases) ResolveAlias ¶ added in v0.3.9
ResolveAlias by given name.
type Data ¶ added in v0.3.9
Data alias of map[string]any
func (Data) Get ¶ added in v0.3.9
Get value from the data map. Supports dot syntax to get deep values. eg: top.sub
func (Data) GetByPath ¶ added in v0.5.3
GetByPath get value from the data map by path. eg: top.sub Supports dot syntax to get deep values.
func (Data) One ¶ added in v0.7.1
One get value from the data by multi paths. will return first founded value
func (Data) SetByKeys ¶ added in v0.5.9
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
SetByPath sets a value in the map. Supports dot syntax to set deep values.
Example:
d.SetByPath("name.first", "Mat")
func (Data) StringsByStr ¶ added in v0.5.3
StringsByStr value gets by key, will split string value by ","
func (Data) StringsOne ¶ added in v0.7.1
StringsOne get []string value by multi keys, return first founded value
func (Data) ToStringMap ¶ added in v0.5.3
ToStringMap convert to map[string]string
func (Data) TryOne ¶ added in v0.7.1
TryOne get value from the data by multi paths. will return first founded value
type L2StrMap ¶ added in v0.7.1
L2StrMap is alias of map[string]map[string]string
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 (*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 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
CombineToSMap combine two string-slices to SMap(map[string]string)
type StrMap ¶ added in v0.7.1
func (StrMap) ToKVPairs ¶ added in v0.7.1
ToKVPairs slice convert. eg: {k1:v1,k2:v2} => {k1,v1,k2,v2}