path

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

README

path 🏞

GoDoc Build Status Codecov Go Report Card 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

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

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

func Get

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

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

func GetFromMap

func GetFromMap(object reflect.Value, path string) (interface{}, bool)

GetFromMap uses reflection to set a value into a map

func GetFromSlice

func GetFromSlice(object reflect.Value, path string) (interface{}, bool)

func GetFromStruct

func GetFromStruct(object reflect.Value, path string) (interface{}, bool)

GetFromStructreturns a value from a struct. The struct MUST have "path" tags that identify how each field is to be addressed.

func GetOK

func GetOK(object interface{}, path string) (interface{}, bool)

GetOK returns the value of the object at the provided path. If a value does not already exist, then the OK boolean is false.

func GetWithReflection

func GetWithReflection(object reflect.Value, path string) (interface{}, bool)

func Index

func Index(value string, 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 Match

func Match(object interface{}, criteria exp.Expression) bool

Match returns TRUE if the provided Getter matches the provided expression

func Set

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

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

func SetAll

func SetAll(object interface{}, dataset map[string]interface{}) error

SetAll adds every path from the dataset into the object. It returns an aggregate error containing all errors generated.

func SetToMap

func SetToMap(object reflect.Value, path string, value interface{}) error

SetToMap uses reflection to set a value into a map

func SetToSlice

func SetToSlice(object reflect.Value, path string, value interface{}) error

SetToSlice uses reflection to set a value into a slice/array variable.

func SetToStruct

func SetToStruct(object reflect.Value, path string, value interface{}) error

SetToStruct sets a map reflection value

func SetWithReflection

func SetWithReflection(object reflect.Value, path string, value interface{}) error

SetWithReflection uses a "path" to apply a value to a generic variable.

func Split

func Split(path string) (string, string)

Split splits the path into head and tail strings (separated by ".")

Types

type Deleter

type Deleter interface {
	DeletePath(string) 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(string) (interface{}, bool)
}

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

type Setter

type Setter interface {
	SetPath(string, 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