maps

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2026 License: MIT Imports: 5 Imported by: 105

Documentation

Overview

Package maps provides reusable functions for manipulating nested map[string]any maps are common unmarshal products from various serializers such as json, yaml etc.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy

func Copy(mp map[string]any) map[string]any

Copy returns a deep copy of a conf map.

It's important to note that all nested maps should be map[string]any and not map[any]any. Use IntfaceKeysToStrings() to convert if necessary.

func Delete

func Delete(mp map[string]any, path []string)

Delete removes the entry present at a given path, from the map. The path is the key map slice, for eg:, parent.child.key -> [parent child key]. Any empty, nested map on the path, is recursively deleted.

It's important to note that all nested maps should be map[string]any and not map[any]any. Use IntfaceKeysToStrings() to convert if necessary.

func Flatten

func Flatten(m map[string]any, keys []string, delim string) (map[string]any, map[string][]string)

Flatten takes a map[string]any and traverses it and flattens nested children into keys delimited by delim.

It's important to note that all nested maps should be map[string]any and not map[any]any. Use IntfaceKeysToStrings() to convert if necessary.

eg: `{ "parent": { "child": 123 }}` becomes `{ "parent.child": 123 }` In addition, it keeps track of and returns a map of the delimited keypaths with a slice of key parts, for eg: { "parent.child": ["parent", "child"] }. This parts list is used to remember the key path's original structure to unflatten later.

func Int64SliceToLookupMap

func Int64SliceToLookupMap(s []int64) map[int64]bool

Int64SliceToLookupMap takes a slice of int64s and returns a lookup map with the slice values as keys with true values.

func IntfaceKeysToStrings

func IntfaceKeysToStrings(mp map[string]any)

IntfaceKeysToStrings recursively converts map[any]any to map[string]any. Some parses such as YAML unmarshal return this.

func Merge

func Merge(a, b map[string]any)

Merge recursively merges map a into b (left to right), mutating and expanding map b. Note that there's no copying involved, so map b will retain references to map a.

It's important to note that all nested maps should be map[string]any and not map[any]any. Use IntfaceKeysToStrings() to convert if necessary.

func MergeStrict

func MergeStrict(a, b map[string]any) error

MergeStrict recursively merges map a into b (left to right), mutating and expanding map b. Note that there's no copying involved, so map b will retain references to map a. If an equal key in either of the maps has a different value type, it will return the first error.

It's important to note that all nested maps should be map[string]any and not map[any]any. Use IntfaceKeysToStrings() to convert if necessary.

func Search(mp map[string]any, path []string) any

Search recursively searches a map for a given path. The path is the key map slice, for eg:, parent.child.key -> [parent child key].

It's important to note that all nested maps should be map[string]any and not map[any]any. Use IntfaceKeysToStrings() to convert if necessary.

func StringSliceToLookupMap

func StringSliceToLookupMap(s []string) map[string]bool

StringSliceToLookupMap takes a slice of strings and returns a lookup map with the slice values as keys with true values.

func Unflatten

func Unflatten(m map[string]any, delim string) map[string]any

Unflatten takes a flattened key:value map (non-nested with delimited keys) and returns a nested map where the keys are split into hierarchies by the given delimiter. For instance, `parent.child.key: 1` to `{parent: {child: {key: 1}}}`

Keys are sorted by length to make merge-order deterministic so that nested keys don't end up overwriting their parent keys. eg: `parent.child` -> `parent`.

It's important to note that all nested maps should be map[string]any and not map[any]any. Use IntfaceKeysToStrings() to convert if necessary.

Types

This section is empty.

Jump to

Keyboard shortcuts

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