mapx

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2025 License: MIT Imports: 5 Imported by: 0

README

MapX

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

Features

  • Basic operationsGet, Set, Has, Count, Copy, Merge
  • Typed accessorsGetString, GetBool, GetInt, GetFloat64, GetStrings, GetAnySlice, GetMap
  • Default helpersGetXXXDefault (string, bool, int, float64, slice, map, any)
  • Object helpersKeys extracts field names from maps or structs via reflection
  • 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,
    }

    // Read values safely
    app, _ := mapx.GetString(cfg, "app")   // "demo"
    port := mapx.GetIntDefault(cfg, "port", 80)

    // Write values
    mapx.Set(cfg, "debug", true)

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

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

API Cheat-Sheet

Category Functions
Basic ops Get, Set, Has, Count, Copy, Merge
Accessors GetString, GetBool, GetInt, GetFloat64, GetStrings, GetAnySlice, GetMap
Defaults GetStringDefault, GetBoolDefault, GetIntDefault, GetFloat64Default, GetStringsDefault, GetAnySliceDefault, GetMapDefault, GetAnyDefault
Object helpers Keys
Convert ToGeneric

Documentation

Index

Constants

This section is empty.

Variables

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

ErrInputNotMap indicates that the provided input is not of map type.

Functions

func Copy

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

Copy creates a shallow copy of the properties map

func Count

func Count(props map[string]any) int

Count returns the number of properties in the map

func Get

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

Get safely gets a property from the properties map

func GetAnyDefault

func GetAnyDefault(props map[string]any, key string, defaultValue any) any

GetAnyDefault returns an any property or default value

func GetAnySlice

func GetAnySlice(props map[string]any, key string) ([]any, bool)

GetAnySlice safely gets a []any property from the properties map

func GetAnySliceDefault

func GetAnySliceDefault(props map[string]any, key string, defaultValue []any) []any

GetAnySliceDefault returns a []any property or default value

func GetBool

func GetBool(props map[string]any, key string) (bool, bool)

GetBool safely gets a bool property from the properties map

func GetBoolDefault

func GetBoolDefault(props map[string]any, key string, defaultValue bool) bool

GetBoolDefault returns a bool property or default value

func GetFloat64

func GetFloat64(props map[string]any, key string) (float64, bool)

GetFloat64 safely gets a float64 property from the properties map

func GetFloat64Default

func GetFloat64Default(props map[string]any, key string, defaultValue float64) float64

GetFloat64Default returns a float64 property or default value

func GetInt

func GetInt(props map[string]any, key string) (int, bool)

GetInt safely gets an int property from the properties map

func GetIntDefault

func GetIntDefault(props map[string]any, key string, defaultValue int) int

GetIntDefault returns an int property or default value

func GetMap

func GetMap(props map[string]any, key string) (map[string]any, bool)

GetMap safely gets a map[string]any property from the properties map

func GetMapDefault

func GetMapDefault(props map[string]any, key string, defaultValue map[string]any) map[string]any

GetMapDefault returns a map[string]any property or default value

func GetString

func GetString(props map[string]any, key string) (string, bool)

GetString safely gets a string property from the properties map

func GetStringDefault

func GetStringDefault(props map[string]any, key, defaultValue string) string

GetStringDefault returns a string property or default value

func GetStrings

func GetStrings(props map[string]any, key string) ([]string, bool)

GetStrings safely gets a []string property from the properties map

func GetStringsDefault

func GetStringsDefault(props map[string]any, key string, defaultValue []string) []string

GetStringsDefault returns a []string property or default value

func Has

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

Has checks if a property exists in the properties map

func Keys

func Keys(input any) []string

Keys returns the keys of an object (struct or map)

func Merge

func Merge(props1, props2 map[string]any) map[string]any

Merge merges two properties maps, with the second taking precedence

func Set

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

Set safely sets a property in the properties map

func ToGeneric

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

ToGeneric converts any map to map[any]any

Types

This section is empty.

Jump to

Keyboard shortcuts

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