mappath

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2025 License: Apache-2.0 Imports: 4 Imported by: 4

README

mappath

mappath is a simple package for searching and modifying generic maps (map[string]any) or slices ([]any) using keypaths.

This functionality was originally implemented in Neptunus to navigate through event data.

Path separator is just a dot - . - that can be escaped using backslash, and a backslash also can be escaped using backslash.

It also supports negative indexes for slices that already exists. In this case, target element will be len(slice) - |index|. For example, you can get last element from this slice - [0, 2, 4, 6] - by -1 index, because it's length is 4 and 4-1=3.

Examples

rawJson := `
{
    "message": "user login",
    "metadata": {
        "user": {
            "name.full": "John Doe",
            "email": "johndoe@gmail.com",
            "roles": [ "employee", "manager" ],
            "age": 42
        }
    }
}
`

var data any
if err := json.Unmarshal([]byte(rawJson), &data); err != nil {
    return err
}

// get user first role
role, _ := mappath.Get(data, `metadata.user.name\.full`)

// get user full nale
role, _ := mappath.Get(data, "metadata.user.roles.0")

// add login to user metadata
data, _ = mappath.Put(data, "metadata.user.login", "johndoe12")

// delete user second role
data, _ = mappath.Delete(data, "metadata.user.roles.1")

It may be not so conveniently, when function returns nil data if error occured. For this cases you can use Container:

rawJson := `
{
    "message": "user login",
    "metadata": {
        "user": {
            "name": "John Doe",
            "email": "johndoe@gmail.com",
            "roles": [ "employee", "manager" ],
            "age": 42
        }
    }
}
`

c := mappath.Container{ Data: map[string]any{} }
if err := json.Unmarshal([]byte(rawJson), &c.Data); err != nil {
    return err
}

// get first user role
role, err := c.Get("metadata.user.roles.0")

// add login to user metadata
err = c.Put("metadata.user.login", "johndoe12")

// delete user second role
err = c.Delete("metadata.user.roles.1")

Container stores data and updates it only if change operations have been performed successfully.

Documentation

Index

Constants

View Source
const (
	EscapeRune    = '\\'
	PathSeparator = '.'
)

Variables

This section is empty.

Functions

func Clone

func Clone(p any) any

Clone passed map[string]any or []any.

func CurrentKey added in v1.3.0

func CurrentKey(path string) (curr string, left string)

func Delete

func Delete(p any, key string) (any, error)

Delete a value on a specified path in the provided map[string]any or []any and get the updated object.

func Get

func Get(p any, key string) (any, error)

Get a value by specified key from provided map[string]any or []any.

func Put

func Put(p any, key string, val any) (any, error)

Put a passed value on a specified path in the provided map[string]any or []any and get the updated object.

Types

type Container added in v1.1.0

type Container struct {
	Data any
}

Container stores data and updates it only if change operations have been performed successfully.

func (*Container) Clone added in v1.1.0

func (c *Container) Clone() *Container

func (*Container) Delete added in v1.1.0

func (c *Container) Delete(key string) error

func (*Container) Get added in v1.1.0

func (c *Container) Get(key string) (any, error)

func (*Container) Put added in v1.1.0

func (c *Container) Put(key string, val any) error

type InvalidPathError added in v1.1.0

type InvalidPathError struct {
	Path   string
	Reason string
}

func (*InvalidPathError) Error added in v1.1.0

func (e *InvalidPathError) Error() string

type NotFoundError added in v1.1.0

type NotFoundError struct {
	Path   string
	Reason string
}

func (*NotFoundError) Error added in v1.1.0

func (e *NotFoundError) Error() string

Jump to

Keyboard shortcuts

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