maputil

package
v0.6.15 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2024 License: MIT Imports: 11 Imported by: 31

README

Map Utils

maputil provide map data util functions. eg: convert, sub-value get, simple merge

  • use map[string]any as Data
  • deep get value by key path
  • deep set value by key path

Install

go get github.com/gookit/goutil/maputil

Go docs

Usage

Deep get value
mp := map[string]any {
	"top1": "val1",
	"arr1": []string{"ab", "cd"}
	"map1": map[string]any{
	    "sub1": "val2",	
    },
}

fmt.Println(maputil.DeepGet(mp, "map1.sub1")) // Output: VAL3

// get value from slice.
fmt.Println(maputil.DeepGet(mp, "arr1.1")) // Output: cd
fmt.Println(maputil.DeepGet(mp, "arr1[1]")) // Output: cd
Deep set value
mp := map[string]any {
	"top1": "val1",
	"arr1": []string{"ab"}
	"map1": map[string]any{
	    "sub1": "val2",	
    },
}

err := maputil.SetByPath(&mp, "map1.newKey", "VAL3")

fmt.Println(maputil.DeepGet(mp, "map1.newKey")) // Output: VAL3

Code Check & Testing

gofmt -w -l ./
golint ./...

Testing:

go test -v ./maputil/...

Test limit by regexp:

go test -v -run ^TestSetByKeys ./maputil/...

Documentation

Overview

Package maputil provide map data util functions. eg: convert, sub-value get, simple merge

Index

Constants

View Source
const (
	Wildcard = "*"
	PathSep  = "."
)

some consts for separators

View Source
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 DeepGet added in v0.5.8

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

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

func EachAnyMap added in v0.6.8

func EachAnyMap(mp any, fn func(key string, val any))

EachAnyMap iterates the given map and calls the given function for each item.

func FilterSMap added in v0.6.15

func FilterSMap(sm map[string]string) map[string]string

FilterSMap filter empty elem for the string map.

func FlatWithFunc added in v0.5.12

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

FlatWithFunc flat a tree-map with custom collect handle func

func Flatten added in v0.5.12

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 added in v0.5.3

func FormatIndent(mp any, indent string) string

FormatIndent 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 added in v0.6.9

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 added in v0.6.12

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 added in v0.6.1

func HTTPQueryString(data map[string]any) string

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

func HasAllKeys added in v0.5.8

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

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

func HasKey added in v0.5.7

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

HasKey check of the given map.

func HasOneKey added in v0.6.9

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]string

KeyToLower convert keys to lower case.

func Keys

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

Keys get all keys of the given map.

func MakeByKeys added in v0.5.8

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 added in v0.5.8

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 MergeMultiSMap added in v0.6.15

func MergeMultiSMap(mps ...map[string]string) map[string]string

MergeMultiSMap quick merge multi string-map data.

func MergeSMap added in v0.5.8

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

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 added in v0.5.8

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

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

func SetByKeys added in v0.5.8

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 added in v0.5.8

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 added in v0.6.6

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 added in v0.6.10

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 added in v0.6.8

func ToAnyMap(mp any) map[string]any

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

func ToString added in v0.4.5

func ToString(mp map[string]any) string

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

func ToString2 added in v0.5.3

func ToString2(mp any) string

ToString2 simple and quickly convert a map to string.

func ToStringMap added in v0.3.9

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

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

func TryAnyMap added in v0.6.9

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.

Types

type Aliases added in v0.3.9

type Aliases map[string]string

Aliases implemented a simple string alias map.

func (Aliases) AddAlias added in v0.3.9

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

AddAlias to the Aliases

func (Aliases) AddAliasMap added in v0.3.9

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

AddAliasMap to the Aliases

func (Aliases) AddAliases added in v0.3.9

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

AddAliases to the Aliases

func (Aliases) HasAlias added in v0.3.9

func (as Aliases) HasAlias(alias string) bool

HasAlias in the Aliases

func (Aliases) ResolveAlias added in v0.3.9

func (as Aliases) ResolveAlias(alias string) string

ResolveAlias by given name.

type Data added in v0.3.9

type Data map[string]any

Data an map data type

func (Data) Bool added in v0.3.13

func (d Data) Bool(key string) bool

Bool value get

func (Data) Default added in v0.3.9

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

Default get value from the data map with default value

func (Data) Get added in v0.3.9

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 added in v0.5.3

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 added in v0.3.9

func (d Data) Has(key string) bool

Has value on the data map

func (Data) Int added in v0.3.9

func (d Data) Int(key string) int

Int value get

func (Data) Int64 added in v0.3.9

func (d Data) Int64(key string) int64

Int64 value get

func (Data) IsEmpty added in v0.6.15

func (d Data) IsEmpty() bool

IsEmpty if the data map

func (Data) Keys added in v0.5.8

func (d Data) Keys() []string

Keys of the data map

func (Data) Load added in v0.6.6

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

Load other data to current data map

func (Data) LoadSMap added in v0.6.8

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

LoadSMap to data

func (Data) Set added in v0.3.9

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

Set value to the data map

func (Data) SetByKeys added in v0.5.9

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 added in v0.5.8

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) Slice added in v0.6.15

func (d Data) Slice(key string) ([]any, error)

Slice get []any value from data map

func (Data) Str added in v0.3.10

func (d Data) Str(key string) string

Str value get by key

func (Data) StrMap added in v0.6.7

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

StrMap get map[string]string value

func (Data) StrSplit added in v0.5.8

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

StrSplit get strings by split key value

func (Data) String added in v0.3.9

func (d Data) String() string

String data to string

func (Data) StringMap added in v0.3.9

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

StringMap get map[string]string value

func (Data) Strings added in v0.5.3

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

Strings get []string value

func (Data) StringsByStr added in v0.5.3

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

StringsByStr value get by key

func (Data) Sub added in v0.5.8

func (d Data) Sub(key string) Data

Sub get sub value as new Data

func (Data) ToStringMap added in v0.5.3

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

ToStringMap convert to map[string]string

func (Data) Uint added in v0.6.2

func (d Data) Uint(key string) uint

Uint value get

func (Data) Uint64 added in v0.6.15

func (d Data) Uint64(key string) uint64

Uint64 value get

func (Data) Value added in v0.5.3

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

Value get from the data map

type Map added in v0.5.8

type Map = Data

Map alias of Data

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 NewFormatter added in v0.5.3

func NewFormatter(mp any) *MapFormatter

NewFormatter instance

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 map[string]string

SMap is alias of map[string]string

func CombineToSMap added in v0.6.5

func CombineToSMap(keys, values []string) SMap

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

func (SMap) Bool added in v0.3.13

func (m SMap) Bool(key string) bool

Bool value get

func (SMap) Default added in v0.5.9

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

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

func (SMap) Get added in v0.5.3

func (m SMap) Get(key string) string

Get value by key

func (SMap) Has added in v0.3.13

func (m SMap) Has(key string) bool

Has key on the data map

func (SMap) HasValue added in v0.3.13

func (m SMap) HasValue(val string) bool

HasValue on the data map

func (SMap) IfExist added in v0.6.15

func (m SMap) IfExist(key string, fn func(val string))

IfExist key, then call the fn with value.

func (SMap) IfValid added in v0.6.15

func (m SMap) IfValid(key string, fn func(val string))

IfValid value is not empty, then call the fn

func (SMap) Int added in v0.3.13

func (m SMap) Int(key string) int

Int value get

func (SMap) Int64 added in v0.3.13

func (m SMap) Int64(key string) int64

Int64 value get

func (SMap) Ints added in v0.3.13

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

Ints value to []int

func (SMap) IsEmpty added in v0.5.5

func (m SMap) IsEmpty() bool

IsEmpty of the data map

func (SMap) Keys added in v0.5.8

func (m SMap) Keys() []string

Keys of the string-map

func (SMap) Load added in v0.6.11

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

Load data to the map

func (SMap) Set added in v0.6.11

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

Set value to the data map

func (SMap) Str added in v0.3.13

func (m SMap) Str(key string) string

Str value get

func (SMap) String added in v0.3.13

func (m SMap) String() string

String data to string

func (SMap) Strings added in v0.3.13

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

Strings value to []string

func (SMap) ToKVPairs added in v0.6.3

func (m SMap) ToKVPairs() []string

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

func (SMap) Value added in v0.5.3

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

Value get from the data map

func (SMap) Values added in v0.5.8

func (m SMap) Values() []string

Values of the string-map

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL