Skip to content

Import path: gitlab.soludian.com/soludian/fountain/libs/base/map_util

map_util

go
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

some consts for separators

go
const (
    KWildcard = "*"
    KPathSep  = "."
)

Key, value sep char consts

go
const (
    KValSepStr  = ","
    KValSepChar = ','
    KKeySepStr  = "."
    KKeySepChar = '.'
)

func CombineToMap

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

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

func DeepGet

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

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

func EachAnyMap

go
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

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

FlatWithFunc flat a tree-map with custom collect handle func

func Flatten

go
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

go
func FormatIndent(mp any, indent string) string

FormatIndent format map data to string with newline and indent.

func GetByPath

go
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

go
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

go
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

go
func HTTPQueryString(data map[string]any) string

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

func HasAllKeys

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

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

func HasKey

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

HasKey check of the given map.

func HasOneKey

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

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

func KeyToLower

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

KeyToLower convert keys to lower case.

func Keys

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

Keys get all keys of the given map.

func MakeByKeys

go
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

go
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

go
func MapCopy[K comparable, V any](src map[K]V) map[K]V

MapCopy 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

go
func MapCopyKeys[K comparable, V any](src map[K]V, keys ...K) map[K]V

MapCopyKeys 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

go
func MapEqual[K, V comparable](a, b map[K]V) bool

MapEqual 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

go
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

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

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

func MergeStringMap

go
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

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

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

func SetByKeys

go
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

go
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

go
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 StringsMapToAnyMap

go
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

go
func ToAnyMap(mp any) map[string]any

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

func ToString

go
func ToString(mp map[string]any) string

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

func ToString2

go
func ToString2(mp any) string

ToString2 simple and quickly convert a map to string.

func ToStringMap

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

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

func TryAnyMap

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

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

func Values

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

Values get all values from the given map.

type Aliases

Aliases implemented a simple string alias map.

go
type Aliases map[string]string

func (Aliases) AddAlias

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

AddAlias to the Aliases

func (Aliases) AddAliasMap

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

AddAliasMap to the Aliases

func (Aliases) AddAliases

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

AddAliases to the Aliases

func (Aliases) HasAlias

go
func (as Aliases) HasAlias(alias string) bool

HasAlias in the Aliases

func (Aliases) ResolveAlias

go
func (as Aliases) ResolveAlias(alias string) string

ResolveAlias by given name.

type Data

Data an map data type

go
type Data map[string]any

func (Data) Bool

go
func (d Data) Bool(key string) bool

Bool value get

func (Data) Default

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

Default get value from the data map with default value

func (Data) Get

go
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

go
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

go
func (d Data) Has(key string) bool

Has value on the data map

func (Data) Int

go
func (d Data) Int(key string) int

Int value get

func (Data) Int64

go
func (d Data) Int64(key string) int64

Int64 value get

func (Data) IsEmtpy

go
func (d Data) IsEmtpy() bool

IsEmtpy if the data map

func (Data) Keys

go
func (d Data) Keys() []string

Keys of the data map

func (Data) Load

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

Load other data to current data map

func (Data) LoadSMap

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

LoadSMap to data

func (Data) Set

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

Set value to the data map

func (Data) SetByKeys

go
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.

For example:

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

func (Data) SetByPath

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

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

For example:

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

func (Data) Str

go
func (d Data) Str(key string) string

Str value get by key

func (Data) StrMap

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

StrMap get map[string]string value

func (Data) StrSplit

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

StrSplit get strings by split key value

func (Data) String

go
func (d Data) String() string

String data to string

func (Data) StringMap

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

StringMap get map[string]string value

func (Data) Strings

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

Strings get []string value

func (Data) StringsByStr

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

StringsByStr value get by key

func (Data) Sub

go
func (d Data) Sub(key string) Data

Sub get sub value as new Data

func (Data) ToStringMap

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

ToStringMap convert to map[string]string

func (Data) Uint

go
func (d Data) Uint(key string) uint64

Uint value get

func (Data) Value

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

Value get from the data map

type Map

Map alias of Data

go
type Map = Data

type MapFormatter

MapFormatter struct

go
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

go
func NewFormatter(mp any) *MapFormatter

NewFormatter instance

func (*MapFormatter) Format

go
func (f *MapFormatter) Format() string

Format to string

func (*MapFormatter) FormatTo

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

FormatTo to custom buffer

func (*MapFormatter) String

go
func (f *MapFormatter) String() string

Format to string

func (*MapFormatter) WithFn

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

WithFn for config self

func (*MapFormatter) WithIndent

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

WithIndent string

type SMap

SMap is alias of map[string]string

go
type SMap map[string]string

func CombineToSMap

go
func CombineToSMap(keys, values []string) SMap

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

func (SMap) Bool

go
func (m SMap) Bool(key string) bool

Bool value get

func (SMap) Default

go
func (m SMap) Default(key, defVal string) string

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

func (SMap) Get

go
func (m SMap) Get(key string) string

Get value by key

func (SMap) Has

go
func (m SMap) Has(key string) bool

Has key on the data map

func (SMap) HasValue

go
func (m SMap) HasValue(val string) bool

HasValue on the data map

func (SMap) Int

go
func (m SMap) Int(key string) int

Int value get

func (SMap) Int64

go
func (m SMap) Int64(key string) int64

Int64 value get

func (SMap) Ints

go
func (m SMap) Ints(key string) []int

Ints value to []int

func (SMap) IsEmpty

go
func (m SMap) IsEmpty() bool

IsEmpty of the data map

func (SMap) Keys

go
func (m SMap) Keys() []string

Keys of the string-map

func (SMap) Load

go
func (m SMap) Load(data map[string]string)

Load data to the map

func (SMap) Set

go
func (m SMap) Set(key string, val any)

Set value to the data map

func (SMap) Str

go
func (m SMap) Str(key string) string

Str value get

func (SMap) String

go
func (m SMap) String() string

String data to string

func (SMap) Strings

go
func (m SMap) Strings(key string) (ss []string)

Strings value to []string

func (SMap) ToKVPairs

go
func (m SMap) ToKVPairs() []string

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

func (SMap) Value

go
func (m SMap) Value(key string) (string, bool)

Value get from the data map

func (SMap) Values

go
func (m SMap) Values() []string

Values of the string-map

Generated by gomarkdoc