mapsutil

package
v0.0.0-...-f340d17 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package mapsutil provides small map-copy helpers used across internal packages.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloneOrEmpty

func CloneOrEmpty[V any](values map[string]V) map[string]V

CloneOrEmpty returns a copy of string values, or an initialized empty map for nil input.

Use when the result must always be a non-nil map, for example when the copy is serialized as a JSON object (an empty map renders as `{}` while nil renders as `null`) or when callers may write into it later.

Example

CloneOrEmpty always yields a non-nil map, so nil input still serializes as a JSON object instead of null.

package main

import (
	"encoding/json"
	"fmt"
	"os"

	"github.com/omarluq/librecode/internal/mapsutil"
)

// mustPrintln writes example output to stdout for godoc, panicking on write
// errors so example bodies stay focused on the behavior under demonstration.
func mustPrintln(values ...any) {
	if _, err := fmt.Fprintln(os.Stdout, values...); err != nil {
		panic(err)
	}
}

func main() {
	serialized, err := json.Marshal(mapsutil.CloneOrEmpty(map[string]int(nil)))
	if err != nil {
		mustPrintln("error:", err)

		return
	}

	mustPrintln(string(serialized))
}
Output:
{}

func CloneOrNil

func CloneOrNil[V any](values map[string]V) map[string]V

CloneOrNil returns a copy of string values, or nil for nil or empty input.

Use when nil and empty mean the same thing and nil is preferred, for example before storing the copy in a struct field with `omitempty` JSON tags so empty inputs stay out of serialized output.

Example

CloneOrNil collapses nil and empty input to nil, keeping `omitempty` fields out of serialized output.

package main

import (
	"fmt"
	"os"

	"github.com/omarluq/librecode/internal/mapsutil"
)

// mustPrintln writes example output to stdout for godoc, panicking on write
// errors so example bodies stay focused on the behavior under demonstration.
func mustPrintln(values ...any) {
	if _, err := fmt.Fprintln(os.Stdout, values...); err != nil {
		panic(err)
	}
}

func main() {
	empty := map[string]string{}

	mustPrintln(mapsutil.CloneOrNil(map[string]string(nil)) == nil)
	mustPrintln(mapsutil.CloneOrNil(empty) == nil)
	mustPrintln(mapsutil.CloneOrNil(map[string]string{"k": "v"}))
}
Output:
true
true
map[k:v]

func ClonePreserveNil

func ClonePreserveNil[V any](values map[string]V) map[string]V

ClonePreserveNil returns a copy of string values, or nil for nil input.

Use when the copy must distinguish "unset" (nil) from "set but empty", for example when copying optional request metadata whose absence carries meaning or when merging only if the source was configured.

Example

ClonePreserveNil keeps the nil/empty distinction when copying optional maps.

package main

import (
	"fmt"
	"os"

	"github.com/omarluq/librecode/internal/mapsutil"
)

// mustPrintln writes example output to stdout for godoc, panicking on write
// errors so example bodies stay focused on the behavior under demonstration.
func mustPrintln(values ...any) {
	if _, err := fmt.Fprintln(os.Stdout, values...); err != nil {
		panic(err)
	}
}

func main() {
	var unset map[string]string

	empty := map[string]string{}

	mustPrintln(mapsutil.ClonePreserveNil(unset) == nil)
	mustPrintln(mapsutil.ClonePreserveNil(empty) == nil)
}
Output:
true
false

func IntMapToAnyMap

func IntMapToAnyMap(values map[string]int) map[string]any

IntMapToAnyMap copies integer map values into a JSON-friendly any map.

Types

This section is empty.

Jump to

Keyboard shortcuts

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