maputil

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

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/zhangyiming748/pretty/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 (
	ValSepStr  = ","
	ValSepChar = ','
	KeySepStr  = "."
	KeySepChar = '.'
)

Key, value sep char consts

Variables

This section is empty.

Functions

func DeepGet

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

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

func FlatWithFunc

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

FlatWithFunc flat a tree-map with custom collect handle func

func Flatten

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

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 HTTPQueryString

func HTTPQueryString(data map[string]any) string

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

func HasKey

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

HasKey check of the given map.

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

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 MergeSMap

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

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) error

SetByPath set sub-map value by key path. Supports dot syntax to set deep values.

For example:

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

func ToString

func ToString(mp map[string]any) string

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

func ToString2

func ToString2(mp any) string

ToString2 simple and quickly convert a map to string.

func ToStringMap

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

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

func Values

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

Values get all values from the given map.

Types

type Aliases

type Aliases map[string]string

Aliases implemented a simple string alias map.

func (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) bool

HasAlias in the Aliases

func (Aliases) ResolveAlias

func (as Aliases) ResolveAlias(alias string) string

ResolveAlias by given name.

type Data

type Data map[string]any

Data an map data type

func (Data) Bool

func (d Data) Bool(key string) bool

Bool value get

func (Data) Default

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

Default get value from the data map with default value

func (Data) Get

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

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) bool

Has value on the data map

func (Data) Int

func (d Data) Int(key string) int

Int value get

func (Data) Int64

func (d Data) Int64(key string) int64

Int64 value get

func (Data) IsEmtpy

func (d Data) IsEmtpy() bool

IsEmtpy if the data map

func (Data) Keys

func (d Data) Keys() []string

Keys of the data map

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) 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

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

func (d Data) Str(key string) string

Str value get by key

func (Data) StrSplit

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

StrSplit get strings by split key value

func (Data) String

func (d Data) String() string

String data to string

func (Data) StringMap

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

StringMap get map[string]string value

func (Data) Strings

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

Strings get []string value

func (Data) StringsByStr

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

StringsByStr value get by key

func (Data) Sub

func (d Data) Sub(key string) Data

Sub get sub value as new Data

func (Data) ToStringMap

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

ToStringMap convert to map[string]string

func (Data) Uint

func (d Data) Uint(key string) uint64

Uint value get

func (Data) Value

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

Value get from the data map

type Map

type Map = Data

Map alias of Data

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 NewFormatter

func NewFormatter(mp any) *MapFormatter

NewFormatter instance

func (*MapFormatter) Format

func (f *MapFormatter) Format() string

Format to string

func (*MapFormatter) FormatTo

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

FormatTo to custom buffer

func (*MapFormatter) String

func (f *MapFormatter) String() string

Format to string

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

type SMap

type SMap map[string]string

SMap is alias of map[string]string

func (SMap) Bool

func (m SMap) Bool(key string) bool

Bool value get

func (SMap) Default

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

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

func (SMap) Get

func (m SMap) Get(key string) string

Get value by key

func (SMap) Has

func (m SMap) Has(key string) bool

Has key on the data map

func (SMap) HasValue

func (m SMap) HasValue(val string) bool

HasValue on the data map

func (SMap) Int

func (m SMap) Int(key string) int

Int value get

func (SMap) Int64

func (m SMap) Int64(key string) int64

Int64 value get

func (SMap) Ints

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

Ints value to []int

func (SMap) IsEmpty

func (m SMap) IsEmpty() bool

IsEmpty of the data map

func (SMap) Keys

func (m SMap) Keys() []string

Keys of the string-map

func (SMap) Str

func (m SMap) Str(key string) string

Str value get

func (SMap) String

func (m SMap) String() string

String data to string

func (SMap) Strings

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

Strings value to []string

func (SMap) Value

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

Value get from the data map

func (SMap) Values

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