path

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2020 License: Apache-2.0 Imports: 4 Imported by: 1

README

path 🏞

GoDoc Go Report Card Build Status Codecov

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 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 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) 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) Set

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

Set tries to return the value of the object at this 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