mappath

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2024 License: Apache-2.0 Imports: 4 Imported by: 4

README

mappath

mappath is a simple package for searchig and modifying generic maps or slices using keypaths.

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

Examples

rawJson := `
{
    "message": "user login",
    "metadata": {
        "user": {
            "name": "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 first user role
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")

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidPath = errors.New("invalid path")
	ErrNoSuchField = errors.New("no such field on the provided path")
)

Functions

func Clone

func Clone(p any) any

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

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

This section is empty.

Jump to

Keyboard shortcuts

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