dictutil

package
v0.0.0-...-3ac352a Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package dictutil provides utility functions for working with nested map[string]any configuration dictionaries.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeepMerge

func DeepMerge(base, overlay map[string]any) map[string]any

DeepMerge recursively merges overlay into base. For any key present in both:

  • If both values are maps, recurse.
  • Otherwise, the overlay value replaces the base value.

Returns a new map; neither base nor overlay is modified.

func FlatKeys

func FlatKeys(data map[string]any) []string

FlatKeys returns all dot-separated leaf key paths from a nested map.

func FlatKeysWithPrefix

func FlatKeysWithPrefix(data map[string]any, prefix string) []string

FlatKeysWithPrefix returns flat keys that start with the given prefix, with the prefix stripped from each key.

func Flatten

func Flatten(data map[string]any) map[string]any

Flatten converts a nested map into a flat map with dot-separated keys. Only leaf values (non-map values) are included.

Example: {"database": {"host": "localhost"}} → {"database.host": "localhost"}

func GetNested

func GetNested(data map[string]any, keyPath string) (any, bool)

GetNested retrieves a value from a nested map using a dot-separated key path. Returns (value, true) if found, (nil, false) otherwise.

func HasNested

func HasNested(data map[string]any, keyPath string) bool

HasNested checks if a key path exists in the nested map.

func SetNested

func SetNested(data map[string]any, keyPath string, value any) error

SetNested sets a value in a nested map using a dot-separated key path. Intermediate maps are created as needed. Returns an error if an intermediate value exists but is not a map.

func ShallowMerge

func ShallowMerge(base, overlay map[string]any) map[string]any

ShallowMerge copies base then applies overlay at the top level only.

func Unflatten

func Unflatten(data map[string]any) map[string]any

Unflatten converts a flat map with dot-separated keys into a nested map.

Example: {"database.host": "localhost"} → {"database": {"host": "localhost"}}

Types

type PathError

type PathError struct {
	Path    string
	Message string
}

PathError is returned when a key path operation encounters a non-map intermediate.

func (*PathError) Error

func (e *PathError) Error() string

Error returns a human-readable description of the path error.

Jump to

Keyboard shortcuts

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