Documentation ¶
Overview ¶
Package maputil provide map data util functions. eg: convert, sub-value get, simple merge
Index ¶
- Constants
- func CombineToMap[K comdef.SortedType, V any](keys []K, values []V) map[K]V
- func DeepGet(mp map[string]any, path string) (val any)
- func EachAnyMap(mp any, fn func(key string, val any))
- 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 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 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 MergeSMap(src, dst map[string]string, ignoreCase bool) map[string]string
- func MergeStringMap(src, dst map[string]string, ignoreCase bool) map[string]string
- 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 ToAnyMap(mp any) map[string]any
- 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 Values(mp any) (values []any)
- type Aliases
- type Data
- func (d Data) Bool(key 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) int
- func (d Data) Int64(key string) int64
- func (d Data) IsEmtpy() 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) 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) Str(key string) string
- func (d Data) StrMap(key string) map[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) Sub(key string) Data
- func (d Data) ToStringMap() map[string]string
- func (d Data) Uint(key string) uint64
- func (d Data) Value(key string) (any, bool)
- type Map
- type MapFormatter
- type SMap
- func (m SMap) Bool(key string) bool
- func (m SMap) Default(key, defVal string) string
- func (m SMap) Get(key string) string
- func (m SMap) Has(key string) bool
- func (m SMap) HasValue(val string) bool
- func (m SMap) Int(key string) int
- func (m SMap) Int64(key string) int64
- func (m SMap) Ints(key string) []int
- func (m SMap) IsEmpty() bool
- func (m SMap) Keys() []string
- func (m SMap) Str(key string) string
- func (m SMap) String() string
- func (m SMap) Strings(key string) (ss []string)
- func (m SMap) ToKVPairs() []string
- func (m SMap) Value(key string) (string, bool)
- func (m SMap) Values() []string
Constants ¶
const ( Wildcard = "*" PathSep = "." )
some consts for separators
const ( ValSepStr = "," ValSepChar = ',' KeySepStr = "." KeySepChar = '.' )
Key, value sep char consts
Variables ¶
This section is empty.
Functions ¶
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 EachAnyMap ¶ added in v0.6.8
EachAnyMap iterates the given map and calls the given function for each item.
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 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 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 MergeStringMap ¶
MergeStringMap simple merge two string map. merge src to dst 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 ToStringMap ¶ added in v0.3.9
ToStringMap convert map[string]any to map[string]string
Types ¶
type Aliases ¶ added in v0.3.9
Aliases implemented a simple string alias map.
func (Aliases) AddAliasMap ¶ added in v0.3.9
AddAliasMap to the Aliases
func (Aliases) AddAliases ¶ added in v0.3.9
AddAliases to the Aliases
func (Aliases) ResolveAlias ¶ added in v0.3.9
ResolveAlias by given name.
type Data ¶ added in v0.3.9
Data an map data type
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) SetByKeys ¶ added in v0.5.9
SetByKeys sets a value in the map by path keys. Supports dot syntax to set deep values.
For 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.
For example:
d.SetByPath("name.first", "Mat")
func (Data) StringsByStr ¶ added in v0.5.3
StringsByStr value get by key
func (Data) ToStringMap ¶ added in v0.5.3
ToStringMap convert to 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
SMap is alias of map[string]string
func CombineToSMap ¶ added in v0.6.5
CombineToSMap combine two string-slice to SMap(map[string]string)