path

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2023 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]any and []any 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]any{
    "name":  "John Connor",
    "email": "john@connor.mil",
    "relatives": map[string]any{
        "mom": "Sarah Connor",
        "dad": "Kyle Reese",
    },
    "enemies": []any{"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 any, name string) error

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

func Get

func Get(object any, path string) any

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

func GetBool added in v0.8.0

func GetBool(object any, path string) bool

func GetFloat added in v0.8.0

func GetFloat(object any, path string) float64

func GetFromMap

func GetFromMap(object reflect.Value, path string) (any, bool)

GetFromMap uses reflection to set a value into a map

func GetFromSlice

func GetFromSlice(object reflect.Value, path string) (any, bool)

func GetFromStruct

func GetFromStruct(object reflect.Value, path string) (any, bool)

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

func GetInt added in v0.8.0

func GetInt(object any, path string) int

func GetInt64 added in v0.8.0

func GetInt64(object any, path string) int64

func GetOK

func GetOK(object any, path string) (any, 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 GetString added in v0.8.0

func GetString(object any, path string) string

func GetWithReflection

func GetWithReflection(object reflect.Value, path string) (any, 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 any, criteria exp.Expression) bool

Match returns TRUE if the provided Getter matches the provided expression

func Set

func Set(object any, name string, value any) error

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

func SetAll

func SetAll(object any, dataset map[string]any) error

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

func SetBool added in v0.8.0

func SetBool(object any, path string, value bool) bool

func SetFloat added in v0.8.0

func SetFloat(object any, path string, value float64) bool

func SetInt added in v0.8.0

func SetInt(object any, path string, value int) bool

func SetInt64 added in v0.8.0

func SetInt64(object any, path string, value int64) bool

func SetString added in v0.8.0

func SetString(object any, path string, value string) bool

func SetToMap

func SetToMap(object reflect.Value, path string, value any) error

SetToMap uses reflection to set a value into a map

func SetToSlice

func SetToSlice(object reflect.Value, path string, value any) error

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

func SetToStruct

func SetToStruct(object reflect.Value, path string, value any) error

SetToStruct sets a map reflection value

func SetWithReflection

func SetWithReflection(object reflect.Value, path string, value any) 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 BoolGetter added in v0.8.0

type BoolGetter interface {
	GetBool(string) bool
}

type BoolGetterSetter added in v0.8.0

type BoolGetterSetter interface {
	BoolGetter
	BoolSetter
}

type BoolSetter added in v0.8.0

type BoolSetter interface {
	SetBool(string, bool) bool
}

type BytesGetter added in v0.8.0

type BytesGetter interface {
	GetBytes(string) []byte
}

type BytesGetterSetter added in v0.8.0

type BytesGetterSetter interface {
	BytesGetter
	BytesSetter
}

type BytesSetter added in v0.8.0

type BytesSetter interface {
	SetBytes(string, []byte) bool
}

type ChildGetter added in v0.8.0

type ChildGetter interface {
	GetChild(string) (any, bool)
}

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 Delta added in v0.8.0

type Delta struct {
	// contains filtered or unexported fields
}

Delta tracks changes to an object

func NewDelta added in v0.8.0

func NewDelta(object any) Delta

NewDelta returns a fully initialized Delta object

func (*Delta) Error added in v0.8.0

func (d *Delta) Error() error

Error returns derp.MultiError containing all errors that have been collected

func (*Delta) HasChanged added in v0.8.0

func (d *Delta) HasChanged() bool

HasChanged returns TRUE if any of the values have been changed

func (*Delta) SetBool added in v0.8.0

func (d *Delta) SetBool(path string, value bool)

SetBool tracks changes to a bool value and collects errors

func (*Delta) SetFloat added in v0.8.0

func (d *Delta) SetFloat(path string, value float64)

SetFloat tracks changes to a float value and collects errors

func (*Delta) SetInt added in v0.8.0

func (d *Delta) SetInt(path string, value int)

SetInt tracks changes to an int value and collects errors

func (*Delta) SetInt64 added in v0.8.0

func (d *Delta) SetInt64(path string, value int64)

SetInt64 tracks changes to an int64 value and collects errors

func (*Delta) SetString added in v0.8.0

func (d *Delta) SetString(path string, value string)

SetString tracks changes to a string value and collects errors

type FloatGetter added in v0.8.0

type FloatGetter interface {
	GetFloat(string) float64
}

type FloatGetterSetter added in v0.8.0

type FloatGetterSetter interface {
	FloatGetter
	FloatSetter
}

type FloatSetter added in v0.8.0

type FloatSetter interface {
	SetFloat(string, float64) bool
}

type Getter

type Getter interface {
	GetPath(string) (any, bool)
}

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

type Int64Getter added in v0.8.0

type Int64Getter interface {
	GetInt64(string) int64
}

type Int64GetterSetter added in v0.8.0

type Int64GetterSetter interface {
	Int64Getter
	Int64Setter
}

type Int64Setter added in v0.8.0

type Int64Setter interface {
	SetInt64(string, int64) bool
}

type IntGetter added in v0.8.0

type IntGetter interface {
	GetInt(string) int
}

type IntGetterSetter added in v0.8.0

type IntGetterSetter interface {
	IntGetter
	IntSetter
}

type IntSetter added in v0.8.0

type IntSetter interface {
	SetInt(string, int) bool
}

type Setter

type Setter interface {
	SetPath(string, any) error
}

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

type StringGetter added in v0.8.0

type StringGetter interface {
	GetString(string) string
}

type StringGetterSetter added in v0.8.0

type StringGetterSetter interface {
	StringGetter
	StringSetter
}

type StringSetter added in v0.8.0

type StringSetter interface {
	SetString(string, string) bool
}

Jump to

Keyboard shortcuts

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