Import path:
gitlab.soludian.com/soludian/fountain/libs/base/map_util
map_util
import "gitlab.soludian.com/soludian/fountain/libs/base/map_util"Package map_util provide map data util functions. eg: convert, sub-value get, simple merge
Index
- Constants
- func CombineToMap[K com_def.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 reflect_util.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 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 MapCopy[K comparable, V any](src map[K]V) map[K]V
- func MapCopyKeys[K comparable, V any](src map[K]V, keys ...K) map[K]V
- func MapEqual[K, V comparable](a, b map[K]V) bool
- func MapMerge[K comparable, V any](src map[K]V, dst map[K]V)
- 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 StringsMapToAnyMap(ssMp map[string][]string) 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 CombineToSMap(keys, values []string) 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) Load(data map[string]string)
- func (m SMap) Set(key string, val any)
- 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 (
KWildcard = "*"
KPathSep = "."
)const (
KValSepStr = ","
KValSepChar = ','
KKeySepStr = "."
KKeySepChar = '.'
)func CombineToMap
func CombineToMap[K com_def.SortedType, V any](keys []K, values []V) map[K]VCombineToMap combine two any slice to map[K]V. alias of arr_util.CombineToMap
func DeepGet
func DeepGet(mp map[string]any, path string) (val any)DeepGet value by key path. eg "top" "top.sub"
func EachAnyMap
func EachAnyMap(mp any, fn func(key string, val any))EachAnyMap iterates the given map and calls the given function for each item.
func FlatWithFunc
func FlatWithFunc(mp map[string]any, fn reflect_util.FlatFunc)FlatWithFunc flat a tree-map with custom collect handle func
func Flatten
func Flatten(mp map[string]any) map[string]anyFlatten convert tree map to flat key-value map.
Examples:
{"top": {"sub": "value", "sub2": "value2"} }
->
{"top.sub": "value", "top.sub2": "value2" }func FormatIndent
func FormatIndent(mp any, indent string) stringFormatIndent 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
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", truefunc GetFromAny
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
func HTTPQueryString(data map[string]any) stringHTTPQueryString convert map[string]any data to http query string.
func HasAllKeys
func HasAllKeys(mp any, keys ...any) (ok bool, noKey any)HasAllKeys check of the given map. return the first not exist key
func HasKey
func HasKey(mp, key any) (ok bool)HasKey check of the given map.
func HasOneKey
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]stringKeyToLower convert keys to lower case.
func Keys
func Keys(mp any) (keys []string)Keys get all keys of the given map.
func MakeByKeys
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
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 MapCopy
func MapCopy[K comparable, V any](src map[K]V) map[K]VMapCopy copies all the entries of src into a new map. Nil is returned when src is nil. Note that if V is a pointer or reference type, it is only shallow copied.
func MapCopyKeys
func MapCopyKeys[K comparable, V any](src map[K]V, keys ...K) map[K]VMapCopyKeys copies the entries or src, identified by keys into a new map. Nil is returned when src is nil.
If no keys are provided and src is not nil, an empty non-nil map is returned.
Note that if V is a pointer or reference type, it is only shallow copied.
func MapEqual
func MapEqual[K, V comparable](a, b map[K]V) boolMapEqual check if two maps have exactly the same content. If both maps are nil, they are considered equal. When a nil map is compared to an empty map, they are not considered equal.
func MapMerge
func MapMerge[K comparable, V any](src map[K]V, dst map[K]V)MapMerge copies all entries from src to dst. Any pre-existing keys in dst are overwritten.
func MergeSMap
func MergeSMap(src, dst map[string]string, ignoreCase bool) map[string]stringMergeSMap simple merge two string map. merge src to dst map
func MergeStringMap
func MergeStringMap(src, dst map[string]string, ignoreCase bool) map[string]stringMergeStringMap simple merge two string map. merge src to dst map
func QuietGet
func QuietGet(mp map[string]any, path string) (val any)QuietGet value by key path. eg "top" "top.sub"
func SetByKeys
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
func SetByPath(mp *map[string]any, path string, val any) errorSetByPath set sub-map value by key path. Supports dot syntax to set deep values.
For example:
SetByPath("name.first", "Mat")func SimpleMerge
func SimpleMerge(src, dst map[string]any) map[string]anySimpleMerge simple merge two data map by string key. will merge the src to dst map
func StringsMapToAnyMap
func StringsMapToAnyMap(ssMp map[string][]string) map[string]anyStringsMapToAnyMap 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
func ToAnyMap(mp any) map[string]anyToAnyMap convert map[TYPE1]TYPE2 to map[string]any
func ToString
func ToString(mp map[string]any) stringToString simple and quickly convert map[string]any to string.
func ToString2
func ToString2(mp any) stringToString2 simple and quickly convert a map to string.
func ToStringMap
func ToStringMap(src map[string]any) map[string]stringToStringMap convert map[string]any to map[string]string
func TryAnyMap
func TryAnyMap(mp any) (map[string]any, error)TryAnyMap convert map[TYPE1]TYPE2 to map[string]any
func Values
func Values(mp any) (values []any)Values get all values from the given map.
type Aliases
Aliases implemented a simple string alias map.
type Aliases map[string]stringfunc (Aliases) AddAlias
func (as Aliases) AddAlias(real, alias string)AddAlias to the Aliases
func (Aliases) AddAliasMap
func (as Aliases) AddAliasMap(alias2real map[string]string)AddAliasMap to the Aliases
func (Aliases) AddAliases
func (as Aliases) AddAliases(real string, aliases []string)AddAliases to the Aliases
func (Aliases) HasAlias
func (as Aliases) HasAlias(alias string) boolHasAlias in the Aliases
func (Aliases) ResolveAlias
func (as Aliases) ResolveAlias(alias string) stringResolveAlias by given name.
type Data
Data an map data type
type Data map[string]anyfunc (Data) Bool
func (d Data) Bool(key string) boolBool value get
func (Data) Default
func (d Data) Default(key string, def any) anyDefault get value from the data map with default value
func (Data) Get
func (d Data) Get(key string) anyGet value from the data map. Supports dot syntax to get deep values. eg: top.sub
func (Data) GetByPath
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
func (d Data) Has(key string) boolHas value on the data map
func (Data) Int
func (d Data) Int(key string) intInt value get
func (Data) Int64
func (d Data) Int64(key string) int64Int64 value get
func (Data) IsEmtpy
func (d Data) IsEmtpy() boolIsEmtpy if the data map
func (Data) Keys
func (d Data) Keys() []stringKeys of the data map
func (Data) Load
func (d Data) Load(sub map[string]any)Load other data to current data map
func (Data) LoadSMap
func (d Data) LoadSMap(smp map[string]string)LoadSMap to data
func (Data) Set
func (d Data) Set(key string, val any)Set value to the data map
func (Data) SetByKeys
func (d Data) SetByKeys(keys []string, value any) errorSetByKeys 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
func (d Data) SetByPath(path string, value any) errorSetByPath sets a value in the map. Supports dot syntax to set deep values.
For example:
d.SetByPath("name.first", "Mat")func (Data) Str
func (d Data) Str(key string) stringStr value get by key
func (Data) StrMap
func (d Data) StrMap(key string) map[string]stringStrMap get map[string]string value
func (Data) StrSplit
func (d Data) StrSplit(key, sep string) []stringStrSplit get strings by split key value
func (Data) String
func (d Data) String() stringString data to string
func (Data) StringMap
func (d Data) StringMap(key string) map[string]stringStringMap get map[string]string value
func (Data) Strings
func (d Data) Strings(key string) []stringStrings get []string value
func (Data) StringsByStr
func (d Data) StringsByStr(key string) []stringStringsByStr value get by key
func (Data) Sub
func (d Data) Sub(key string) DataSub get sub value as new Data
func (Data) ToStringMap
func (d Data) ToStringMap() map[string]stringToStringMap convert to map[string]string
func (Data) Uint
func (d Data) Uint(key string) uint64Uint value get
func (Data) Value
func (d Data) Value(key string) (any, bool)Value get from the data map
type Map
Map alias of Data
type Map = Datatype MapFormatter
MapFormatter struct
type MapFormatter struct {
com_def.BaseFormatter
// Prefix string for each element
Prefix string
// Indent string for each element
Indent string
// ClosePrefix string for last "}"
ClosePrefix string
}func NewFormatter
func NewFormatter(mp any) *MapFormatterNewFormatter instance
func (*MapFormatter) Format
func (f *MapFormatter) Format() stringFormat to string
func (*MapFormatter) FormatTo
func (f *MapFormatter) FormatTo(w io.Writer)FormatTo to custom buffer
func (*MapFormatter) String
func (f *MapFormatter) String() stringFormat to string
func (*MapFormatter) WithFn
func (f *MapFormatter) WithFn(fn func(f *MapFormatter)) *MapFormatterWithFn for config self
func (*MapFormatter) WithIndent
func (f *MapFormatter) WithIndent(indent string) *MapFormatterWithIndent string
type SMap
SMap is alias of map[string]string
type SMap map[string]stringfunc CombineToSMap
func CombineToSMap(keys, values []string) SMapCombineToSMap combine two string-slice to SMap(map[string]string)
func (SMap) Bool
func (m SMap) Bool(key string) boolBool value get
func (SMap) Default
func (m SMap) Default(key, defVal string) stringDefault get value by key. if not found, return defVal
func (SMap) Get
func (m SMap) Get(key string) stringGet value by key
func (SMap) Has
func (m SMap) Has(key string) boolHas key on the data map
func (SMap) HasValue
func (m SMap) HasValue(val string) boolHasValue on the data map
func (SMap) Int
func (m SMap) Int(key string) intInt value get
func (SMap) Int64
func (m SMap) Int64(key string) int64Int64 value get
func (SMap) Ints
func (m SMap) Ints(key string) []intInts value to []int
func (SMap) IsEmpty
func (m SMap) IsEmpty() boolIsEmpty of the data map
func (SMap) Keys
func (m SMap) Keys() []stringKeys of the string-map
func (SMap) Load
func (m SMap) Load(data map[string]string)Load data to the map
func (SMap) Set
func (m SMap) Set(key string, val any)Set value to the data map
func (SMap) Str
func (m SMap) Str(key string) stringStr value get
func (SMap) String
func (m SMap) String() stringString data to string
func (SMap) Strings
func (m SMap) Strings(key string) (ss []string)Strings value to []string
func (SMap) ToKVPairs
func (m SMap) ToKVPairs() []stringToKVPairs slice convert. eg: {k1:v1,k2:v2} => {k1,v1,k2,v2}
func (SMap) Value
func (m SMap) Value(key string) (string, bool)Value get from the data map
func (SMap) Values
func (m SMap) Values() []stringValues of the string-map
Generated by gomarkdoc