path

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2021 License: Apache-2.0 Imports: 4 Imported by: 1

README

path 🏞

GoDoc Go Report Card Build Status Codecov Version

Resolve data from arbitrary structures

This is an experimental library for reading/writing values into arbitrary data structures, specifically the map[string]interface{} and []interface{} values returned by Go's json.Unmarshal() functions. It is inspired by the JSON-path standard, but has a very simplified syntax -- using a series of strings separated by dots.

Example Code


s := map[string]interface{}{
		"name":  "John Connor",
		"email": "john@connor.mil",
		"relatives": map[string]interface{}{
			"mom": "Sarah Connor",
			"dad": "Kyle Reese",
		},
		"enemies": []interface{}{"T-1000", "T-3000", "T-5000"},
	}

	name, err := path.Get(s, "name") // John Connor
	email, err := path.Get(s, "email") // john@connor.mil
	sarah, err := path.Get(s, "relatives.0") // t-1000

Pull Requests Welcome

Please use GitHub to make suggestions, pull requests, and enhancements. We're all in this together! 🏞

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete added in v0.3.0

func Delete(object interface{}, path string) error

func Get

func Get(object interface{}, path string) (interface{}, error)

Get tries to return the value of the object at the provided path.

func Set

func Set(object interface{}, path string, value interface{}) error

Set tries to set the value of the object at the provided path.

Types

type Deleter added in v0.3.0

type Deleter interface {
	DeletePath(Path) error
}

Deleter interface allows other objects to make it easy to trace through their property trees, and delete values from them.

type Getter

type Getter interface {
	GetPath(Path) (interface{}, error)
}

Getter interface allows other objects to make it easy to trace through their property trees, and get values from them.

type Path

type Path []string

Path is a reference to a value within another data object.

func New

func New(value string) Path

New creates a new Path object

func (Path) Delete added in v0.3.0

func (path Path) Delete(object interface{}) error

Delete tries to remove a value from ths object at this path

func (Path) Get

func (path Path) Get(object interface{}) (interface{}, error)

Get tries to return the value of the object at this path.

func (Path) HasTail added in v0.1.2

func (path Path) HasTail() bool

HasTail returns TRUE if this path has one or more items in its tail.

func (Path) Head

func (path Path) Head() string

Head returns the first token in the path.

func (Path) Index

func (path Path) Index(maximum int) (int, error)

Index is useful for vetting array indices. It attempts to convert the Head() token int an integer, and then check that the integer is within the designated array bounds (is greater than zero, and less than the maximum value provided to the function).

It returns the array index and an error

func (Path) IsEmpty

func (path Path) IsEmpty() bool

IsEmpty returns TRUE if this path does not contain any tokens

func (Path) IsTailEmpty added in v0.1.3

func (path Path) IsTailEmpty() bool

IsTailEmpty returns TRUE if this path has one or more items in its tail.

func (Path) Push added in v0.2.0

func (path Path) Push(value string) Path

Push returns a new path with a new value appended to the beginning of the path.

func (Path) Set

func (path Path) Set(object interface{}, value interface{}) error

Set tries to return the value of the object at this path.

func (Path) Split added in v0.3.0

func (path Path) Split() (string, Path)

Split returns two values, the Head and the Tail of the current path

func (Path) String

func (path Path) String() string

String implements the Stringer interface, and converts the path into a readable string

func (Path) Tail

func (path Path) Tail() Path

Tail returns a slice of all tokens *after the first token*

type Setter

type Setter interface {
	SetPath(Path, interface{}) error
}

Setter interface allows other objects to make it easy to trace through their property trees, and set values into them.

Jump to

Keyboard shortcuts

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