maputil

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package maputil provides value-conversion and deep-clone helpers used by LogLayer transports and plugins.

Two primitives:

  • ToMap normalizes any value to map[string]any. Structs are walked via reflection (respecting json tags); maps and pointers-to-structs are unwrapped; values implementing json.Marshaler at the top level fall back to a JSON roundtrip. Lossy on type, suitable for transports that want a flat shape.
  • Cloner deep-clones a value with replacement predicates applied at any depth. Preserves the runtime type; suitable for redaction and rewrite-style plugins.

Both are safe to use from any goroutine.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToMap

func ToMap(v any) map[string]any

ToMap returns v as a map[string]any. Nil and existing maps pass through; pointer values are dereferenced; structs are walked via reflection, honoring `json` tags (rename, omitempty, "-"). Top-level values with a custom MarshalJSON method, and types that don't reduce to an object (slices, primitives), fall back to a JSON roundtrip; that roundtrip returns nil if the JSON shape isn't an object.

Types

type Cloner

type Cloner struct {
	MatchKey   func(string) bool
	MatchValue func(string) bool
	Censor     any
}

Cloner deep-clones a value, replacing sensitive content with Censor. Designed for redaction-style plugins that walk arbitrary user values (maps, structs, slices, pointers) without forcing the type to change.

Canonical use case: the plugins/redact plugin. Cloner exists in this public package so third-party plugin authors can build their own redactors with the same type-preservation semantics. If you only need flat-map mutation, prefer ToMap and modify the resulting map directly; Cloner's reflection walk is only worth its cost when the caller's runtime type matters.

MatchKey is invoked with the JSON name (or struct field name when no json tag is present) for struct fields and with the literal key for string-keyed maps. MatchValue is invoked with every string value encountered, at any depth.

The caller's input is never mutated. Containers (maps, slices, structs, pointers) are freshly allocated in the returned value.

func (*Cloner) Clone

func (c *Cloner) Clone(v any) any

Clone returns a deep clone of v with sensitive content replaced by Censor. Returns v unchanged when nothing in v is reachable for matching (basic scalars without a value match, nil, channels, functions).

Safe against cyclic inputs: a self-referencing pointer chain breaks at the cycle (the second visit returns the element's zero value). Pathologically deep non-pointer nesting bottoms out at maxCloneDepth.

Jump to

Keyboard shortcuts

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