mapx

package
v0.11.7 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 5 Imported by: 0

README

MapX

Lightweight helpers for working with Go maps (and structs) in a type-safe way.

Features

  • Generic accessorsValueOf[T], ValueOr[T] for any type
  • Basic operationsGet, Set, Has, Count, Copy, Merge
  • Typed accessorsString, Bool, IntCoerce, Float64Coerce, Strings, AnySlice, Map
  • Default helpersStringOr, BoolOr, IntCoerceOr, Float64CoerceOr, StringsOr, AnySliceOr, MapOr, AnyOr
  • Object helpersKeys extracts field names from maps or structs
  • ConversionToGeneric converts any map to map[any]any

Quick Start

package main

import (
    "fmt"

    "github.com/kaptinlin/gozod/pkg/mapx"
)

func main() {
    cfg := map[string]any{
        "app":  "demo",
        "port": 3000,
    }

    // Generic accessors (preferred)
    app, _ := mapx.ValueOf[string](cfg, "app")   // "demo"
    port := mapx.ValueOr(cfg, "port", 80)  // 3000

    // Typed convenience wrappers
    debug := mapx.BoolOr(cfg, "debug", false)

    // Copy & merge
    clone := mapx.Copy(cfg)
    merged := mapx.Merge(cfg, map[string]any{"port": 8080})

    fmt.Println(app, port, debug, clone, merged)
}

API Cheat-Sheet

Category Functions
Generic ValueOf[T], ValueOr[T]
Basic ops Get, Set, Has, Count, Copy, Merge
Accessors String, Bool, IntCoerce, Float64Coerce, Strings, AnySlice, Map
Defaults StringOr, BoolOr, IntCoerceOr, Float64CoerceOr, StringsOr, AnySliceOr, MapOr, AnyOr
Object helpers Keys
Convert ToGeneric

Documentation

Overview

Package mapx provides type-safe utility functions for working with Go maps.

The core generic functions ValueOf and ValueOr eliminate boilerplate for typed map access. Convenience wrappers (String, Bool, etc.) are provided for common types. Numeric helpers use explicit `Coerce` suffixes when they accept compatible numeric types.

Usage:

name, ok := mapx.ValueOf[string](m, "name")
port := mapx.ValueOr(m, "port", 8080)
merged := mapx.Merge(a, b)

Index

Constants

This section is empty.

Variables

View Source
var ErrInputNotMap = errors.New("input is not a map type")

ErrInputNotMap indicates that the input is not a map type.

Functions

func AnyOr added in v0.6.0

func AnyOr(m map[string]any, key string, def any) any

AnyOr returns the value for key, or def.

func AnySlice added in v0.6.0

func AnySlice(m map[string]any, key string) ([]any, bool)

AnySlice returns the []any value for key.

func AnySliceOr added in v0.6.0

func AnySliceOr(m map[string]any, key string, def []any) []any

AnySliceOr returns the []any value for key, or def.

func Bool added in v0.6.0

func Bool(m map[string]any, key string) (bool, bool)

Bool returns the bool value for key.

func BoolOr added in v0.6.0

func BoolOr(m map[string]any, key string, def bool) bool

BoolOr returns the bool value for key, or def.

func Copy

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

Copy returns a shallow clone, or nil if m is nil.

func Count

func Count(m map[string]any) int

Count returns the number of entries.

func Float64Coerce added in v0.10.0

func Float64Coerce(m map[string]any, key string) (float64, bool)

Float64Coerce returns the float64 value for key, with numeric coercion from float32, int, int32, and int64.

func Float64CoerceOr added in v0.10.0

func Float64CoerceOr(m map[string]any, key string, def float64) float64

Float64CoerceOr returns the float64 value for key (with numeric coercion), or def.

func Get

func Get(m map[string]any, key string) (any, bool)

Get returns the value for key and whether it was found.

func Has

func Has(m map[string]any, key string) bool

Has reports whether key exists in the map.

func IntCoerce added in v0.10.0

func IntCoerce(m map[string]any, key string) (int, bool)

IntCoerce returns the int value for key, with numeric coercion from int32, int64, and float64.

func IntCoerceOr added in v0.10.0

func IntCoerceOr(m map[string]any, key string, def int) int

IntCoerceOr returns the int value for key (with numeric coercion), or def.

func Keys

func Keys(v any) []string

Keys returns the string keys of an object (map or struct). For map[any]any, only string keys are included.

func Map added in v0.6.0

func Map(m map[string]any, key string) (map[string]any, bool)

Map returns the map[string]any value for key.

func MapOr added in v0.6.0

func MapOr(m map[string]any, key string, def map[string]any) map[string]any

MapOr returns the map[string]any value for key, or def.

func Merge

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

Merge returns a new map containing all entries from both maps. Entries in b take precedence over entries in a.

func Set

func Set(m map[string]any, key string, value any)

Set assigns value to key. It is a no-op on a nil map.

func String added in v0.6.0

func String(m map[string]any, key string) (string, bool)

String returns the string value for key.

func StringOr added in v0.6.0

func StringOr(m map[string]any, key, def string) string

StringOr returns the string value for key, or def.

func Strings added in v0.6.0

func Strings(m map[string]any, key string) ([]string, bool)

Strings returns the []string value for key.

func StringsOr added in v0.6.0

func StringsOr(m map[string]any, key string, def []string) []string

StringsOr returns the []string value for key, or def.

func ToGeneric

func ToGeneric(v any) (map[any]any, error)

ToGeneric converts any map type to map[any]any. It returns ErrInputNotMap for non-map inputs.

func ValueOf added in v0.6.0

func ValueOf[T any](m map[string]any, key string) (T, bool)

ValueOf performs a type assertion on m[key]. Reading from a nil map is safe and returns the zero value.

func ValueOr added in v0.6.0

func ValueOr[T any](m map[string]any, key string, def T) T

ValueOr returns the typed value for key, or def when the key is missing or the type assertion fails.

Types

This section is empty.

Jump to

Keyboard shortcuts

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