Documentation ¶
Overview ¶
Package maputil provide map data util functions. eg: convert, sub-value get, simple merge
Index ¶
- Constants
- func DeepGet(mp map[string]any, path 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 HTTPQueryString(data map[string]any) string
- func HasAllKeys(mp any, keys ...any) (ok bool, noKey any)
- func HasKey(mp, key any) (ok bool)
- 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 ToString(mp map[string]any) string
- func ToString2(mp any) string
- func ToStringMap(src map[string]any) map[string]string
- 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) 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) 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) Value(key string) (string, bool)
- func (m SMap) Values() []string
Constants ¶
const ( ValSepStr = "," ValSepChar = ',' KeySepStr = "." KeySepChar = '.' )
Key, value sep char consts
Variables ¶
This section is empty.
Functions ¶
func FlatWithFunc ¶
FlatWithFunc flat a tree-map with custom collect handle func
func Flatten ¶
Flatten convert tree map to flat key-value map.
Examples:
{"top": {"sub": "value", "sub2": "value2"} } -> {"top.sub": "value", "top.sub2": "value2" }
func FormatIndent ¶
FormatIndent format map data to string with newline and indent.
func HTTPQueryString ¶
HTTPQueryString convert map[string]any data to http query string.
func HasAllKeys ¶
HasAllKeys check of the given map.
func KeyToLower ¶
KeyToLower convert keys to lower case.
func MakeByKeys ¶
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 ¶
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 ¶
SetByKeys set sub-map value by path keys. Supports dot syntax to set deep values.
For example:
SetByKeys([]string{"name", "first"}, "Mat")
func SetByPath ¶
SetByPath set sub-map value by key path. Supports dot syntax to set deep values.
For example:
SetByPath("name.first", "Mat")
func ToStringMap ¶
ToStringMap convert map[string]any to map[string]string
Types ¶
type Aliases ¶
Aliases implemented a simple string alias map.
func (Aliases) AddAliasMap ¶
AddAliasMap to the Aliases
func (Aliases) AddAliases ¶
AddAliases to the Aliases
func (Aliases) ResolveAlias ¶
ResolveAlias by given name.
type Data ¶
Data an map data type
func (Data) GetByPath ¶
GetByPath get value from the data map by path. eg: top.sub Supports dot syntax to get deep values.
func (Data) SetByKeys ¶
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 ¶
SetByPath sets a value in the map. Supports dot syntax to set deep values.
For example:
d.SetByPath("name.first", "Mat")
func (Data) StringsByStr ¶
StringsByStr value get by key
func (Data) ToStringMap ¶
ToStringMap convert to map[string]string
type MapFormatter ¶
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) FormatTo ¶
func (f *MapFormatter) FormatTo(w io.Writer)
FormatTo to custom buffer
func (*MapFormatter) WithFn ¶
func (f *MapFormatter) WithFn(fn func(f *MapFormatter)) *MapFormatter
WithFn for config self
func (*MapFormatter) WithIndent ¶
func (f *MapFormatter) WithIndent(indent string) *MapFormatter
WithIndent string