nested

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 28, 2020 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var InvalidIndexErr = errors.New("invalid index")

InvalidIndexErr is returned when a given index is out of bounds.

View Source
var UnsupportedTypeErr = errors.New("unsupported type")

UnsupportedTypeErr is returned when an unsupported type is encountered.

Functions

func ComposeValue

func ComposeValue(val interface{}, path Path) map[string]interface{}

ComposeValue returns a map containing the structure of `path`, with `val` as value.

The value is always transformed into a slice unless it is already one. For example, the call

ComposeValue(42, "foo.bar")

yields the following result:

map[string]interface{}{
    "foo": map[string]interface{}{
        "bar": []int{42},
    },
}

func GetValue

func GetValue(obj interface{}, p string, o string) (map[string]interface{}, bool, error)

GetValue attempts to retrieve the value in the given string encoded path.

Types

type Field

type Field struct {
	// Name is the field name.
	Name string
	// Index is the integer representation of the field name in the case it can be converted to an
	// integer value.
	Index *int
}

Field is the representation of a term in a field path.

Assuming that 'status.dbCredentials' is a path for a value in a nested object, 'status' and 'dbCredentials' are represented as Field values.

Field contains two members: 'Name' and 'Index'. 'Name' is a string like 'status' or 'dbCredentials', and 'Index' is the optional integer representation of the value, if it can be transformed to a valid positive integer.

func NewField

func NewField(name string) Field

NewField creates a new field with the given name.

type Path

type Path []Field

Path represents a field path.

func NewPath

func NewPath(s string) Path

NewPath creates a new path with the given string in the format 'a.b.c'.

func NewPathWithParts

func NewPathWithParts(parts []string) Path

NewPathWithParts constructs a Path from given parts.

func (Path) AdjustedPath

func (p Path) AdjustedPath() Path

AdjustedPath adjusts the current path depending on the head element.

In the case the head of a path ('a' in the 'a.b.c' path) exists and is different than '*', returns itself otherwise returns the path tail ('b.c' in the example).

func (Path) BasePath

func (p Path) BasePath() Path

BasePath returns the receiver's base path.

For example, returns 'a.b' from the 'a.b.c' path.

func (Path) Clean

func (p Path) Clean() Path

Clean creates a new Path without '*' or integer values.

For example, returns 'a.b.c' for 'a.b.*.c' or 'a.b.1.c'.

func (Path) Decompose

func (p Path) Decompose() (Path, Field)

Decompose returns the receiver's base path and the last field.

func (Path) HasTail

func (p Path) HasTail() bool

HasTail asserts whether path has a tail.

func (Path) Head

func (p Path) Head() (Field, bool)

Head returns the path head if one exists.

func (Path) LastField

func (p Path) LastField() (Field, bool)

LastField returns the last field from the receiver.

func (Path) Tail

func (p Path) Tail() Path

Tail returns the path tail if present.

Returns 'b.c' in the path 'a.b.c'.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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