jsonmatch

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2018 License: MIT Imports: 13 Imported by: 0

README

JSONMatch

JSONMatch is a variant of JSONPath that simplifes the syntax and eliminating to the maximum extent the number of special characters required to express a path.

The result of matching a document using JSONMatch is a set of matched values in the document. The present library provides functions for mutating, setting or deleting the values in the match set.

Examples

Select the first name:

name.first

Select all keys (or array values) in the designated path:

employees.tasks.* or employee.tasks[*]

Select the first and second name (union)

name[first, second]

Select an employee from an array based on index

employees[5]

Select a subset of employees from an array according to index

employees[1,2,5,9]

Select employees 1 through 3 using the slice operation

employees[1:3]

Union of two ranges of employees

employees[1:3, 9:12]

Union of a range and a number of individual indicies

employees[1:3, 9, 12]

Select a subset of employees using a filter

employees[wage > 50000]

Select a subset of employees filtering based on the existence of the key 'bonus'.

employees[bonus?]

Recursively select any sub-document under 'some.path' in the document matching the filter:

some.path..[key=="4f5xa"]

Select number from an array where the individual numbers match the filter. (@ == this)

numbers[@ > 50]

Union of completely separate paths:

[employee[5].name, company.name, wageTiers[compensation > 10000]]

Enclose attribute names in single quotes (where needed), and literal strings in double quotes:

`employees['the name' == "John Smith"]``

Currently filters in jsonpath2 do not support boolean operations, although , is synonymous with or/||. You could do:

`employees[name == "John Smith", name == "Granny Smith"]``

There is no way to express an intersection. The following is invalid syntax, but considered for a future version:

INVALID: employees[name.first == "John" && name.last == "Smith"]

[NOTE: Apparently there is a way to do intersections with the current syntax. You can actually do this: employees[name.first == "John"][name.last == "Smith"]. It looks weird, but it works.]

Select from start of array:

array[:3]

Select through end of array:

array[3:]

Select every element in array:

array[*] (why not just :, i don't know, ask jsonpath1)

Select the last element of an array:

array[-1]

Acknowledgements

The code was originally forked from the Kubernetes JSONPath parser. However, it has since been heavily modified.

Documentation

Overview

Package jsonmatch provides support for extracting values, mutating or deleting data using the jsonpath2 syntax.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArrayRef

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

ArrayRef is a ref to a subset of a slice

func NewArrayRef

func NewArrayRef(variable *VarRef, selection Regions) *ArrayRef

NewArrayRef creates a new ArrayRef

func (*ArrayRef) Delete

func (r *ArrayRef) Delete() error

Delete implements the Ref.Delete method

func (*ArrayRef) Depth

func (r *ArrayRef) Depth() int

Depth implements the Ref.Depth method

func (*ArrayRef) EstimateSize

func (r *ArrayRef) EstimateSize() int

EstimateSize implements the Ref.EstimateSize method

func (*ArrayRef) GetPath

func (r *ArrayRef) GetPath() string

func (*ArrayRef) IsEmpty

func (r *ArrayRef) IsEmpty() bool

IsEmpty implements the Ref.IsEmpty method

func (*ArrayRef) Merge

func (r *ArrayRef) Merge(ref Ref) (Ref, bool)

Merge implements Ref.Merge

func (*ArrayRef) Mutate

func (r *ArrayRef) Mutate(mutator MutatorFunc) error

Mutate implements the Ref.Mutate method

func (*ArrayRef) MutateRegions

func (r *ArrayRef) MutateRegions(mutator MutateRegionsFunc) error

MutateRegions lets you mutate the parts of an array as a whole. The mutator gets an array of arrays corresponding to all contiguous items selected. It returns an array of arrays where each sub array will be substituted for each corresponding region in the source. The length of each region may change as required and the length of the result will be adjusted accordingly. Also the selected indicies of the ArrayRef are updated to reflect the new positions so this method may be combined with other mutations afterwards.

func (*ArrayRef) Set

func (r *ArrayRef) Set(value interface{}) error

Set implements the Ref.Set method

func (*ArrayRef) Union

func (r *ArrayRef) Union(refs ...Ref) Ref

Union implements the Ref.Union method

func (*ArrayRef) Values

func (r *ArrayRef) Values() []interface{}

Values implements the Values method

func (*ArrayRef) Vars

func (r *ArrayRef) Vars() []*VarRef

Vars returns VarRefs for all the referenced variable

type Expression

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

Expression represents a compiled JSONpath epxression

func MustParse

func MustParse(src string) *Expression

MustParse parses a JSONPath expression, and panics on failure.

func Parse

func Parse(src string) (*Expression, error)

Parse the provided string returning its compiled representation

func (*Expression) MarshalJSON

func (n *Expression) MarshalJSON() ([]byte, error)

func (*Expression) Match

func (expr *Expression) Match(data interface{}) (*MatchSet, error)

Match runs the jsonmatch query on the data and returns a MatchSet referencing all matches

type ForEachIndexFunc

type ForEachIndexFunc func(i int) (bool, error)

ForEachIndexFunc is the type of the callback provided to ForEachIndex. The callback must return true as long as it wants the process to continue. If an error is returned, ForEachIndex returns with the error. When there are no more indicies, the process is of course terminated regardless.

type LatentMapRef

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

LatentMapRef is a reference to a keypath in a map that do not exist yet Given an object like {a: {}} and you match using the jsonmatch "a.b.c", the void map ref will refer to `a.b` as the root, (since b is a key) in a map that exists, but `c` has nowhere to go so a void map ref is formed in case the client wants to set that value. In that case the necessary maps will be created to realize the path. Every root ref in a VoidMapRef must be a MapRef. The LatentMapRef will report referring to absolutely no values, so cannot be mutated, only Set.

func NewLatentMapRef

func NewLatentMapRef(rootRef Ref, keyPath []string) *LatentMapRef

NewLatentMapRef returns a new *LatentMapRef

func (*LatentMapRef) AddKey

func (r *LatentMapRef) AddKey(key string)

AddKey adds a key to the keypath of the LatentMapRef

func (*LatentMapRef) Delete

func (r *LatentMapRef) Delete() error

Delete is a noop for LatentMapRef

func (*LatentMapRef) Depth

func (r *LatentMapRef) Depth() int

Depth returns the depth of the variable. Since a Set for this should always execute first, it pretends to be veeery deep.

func (*LatentMapRef) EstimateSize

func (r *LatentMapRef) EstimateSize() int

EstimateSize is always 0 for LatentMapRefs

func (*LatentMapRef) IsEmpty

func (r *LatentMapRef) IsEmpty() bool

IsEmpty is always true for a LatentMapRef

func (*LatentMapRef) Merge

func (r *LatentMapRef) Merge(ref Ref) (Ref, bool)

Merge only merges with itself

func (*LatentMapRef) Mutate

func (r *LatentMapRef) Mutate(mutator MutatorFunc) error

Mutate mutates values that by definition is non existant. The mutator will recieve the initial value nil

func (*LatentMapRef) Set

func (r *LatentMapRef) Set(value interface{}) error

Set sets the value of the contained variable

func (*LatentMapRef) SetWithMatchedType

func (r *LatentMapRef) SetWithMatchedType(value interface{}) error

SetWithMatchedType updates the value, but attempts to avoid changing the underlying type if the new value is one of the canonical types

func (*LatentMapRef) Union

func (r *LatentMapRef) Union(refs ...Ref) Ref

Union creates a union

func (*LatentMapRef) Values

func (r *LatentMapRef) Values() []interface{}

Values gets the value wrapped in an array for compatibility

func (*LatentMapRef) Vars

func (r *LatentMapRef) Vars() []*VarRef

Vars gets this VarRef wrapped in an array for your iteration convenience

type LiteralRef

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

LiteralRef is not a Ref at all, but just a container for a literal value

func (*LiteralRef) CanonicalValue

func (r *LiteralRef) CanonicalValue() interface{}

CanonicalValue gets the current value of the variable converted to canonical type

func (*LiteralRef) Delete

func (r *LiteralRef) Delete() error

Delete is not supported for VarRefs

func (*LiteralRef) Depth

func (r *LiteralRef) Depth() int

Depth returns the depth of the variable

func (*LiteralRef) EstimateSize

func (r *LiteralRef) EstimateSize() int

EstimateSize is always 1 for VarRefs

func (*LiteralRef) IsEmpty

func (r *LiteralRef) IsEmpty() bool

IsEmpty is never true for VarRefs

func (*LiteralRef) Merge

func (r *LiteralRef) Merge(ref Ref) (Ref, bool)

Merge only "merges" two identical VarRefs

func (*LiteralRef) Mutate

func (r *LiteralRef) Mutate(mutator MutatorFunc) error

Mutate mutates

func (*LiteralRef) Set

func (r *LiteralRef) Set(value interface{}) error

Set sets the value of the contained variable

func (*LiteralRef) SetWithMatchedType

func (r *LiteralRef) SetWithMatchedType(value interface{}) error

SetWithMatchedType updates the value, but attempts to avoid changing the underlying type if the new value is one of the canonical types

func (*LiteralRef) Union

func (r *LiteralRef) Union(refs ...Ref) Ref

Union creates a union with the VarRef

func (*LiteralRef) Value

func (r *LiteralRef) Value() interface{}

Value gets the current value of the variable in original underlying type

func (*LiteralRef) Values

func (r *LiteralRef) Values() []interface{}

Values gets the value wrapped in an array for compatibility

func (*LiteralRef) Vars

func (r *LiteralRef) Vars() []*VarRef

Vars gets this VarRef wrapped in an array for your iteration convenience

type MapRef

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

MapRef is a ref to a subset of a map type where the keys are strings

func NewMapRef

func NewMapRef(variable *VarRef, keys []string) *MapRef

NewMapRef creates a new MapRef

func (*MapRef) Delete

func (r *MapRef) Delete() error

Delete implements the Ref.Delete method

func (*MapRef) Depth

func (r *MapRef) Depth() int

Depth implements the Ref.Depth method

func (*MapRef) EstimateSize

func (r *MapRef) EstimateSize() int

EstimateSize implements the Ref.EstimateSize method

func (*MapRef) GetLatentMapRef

func (r *MapRef) GetLatentMapRef() *LatentMapRef

GetLatentMapRef generates a latent map ref for any keys that are missing or not Maps in the underlying value. If no such values, nil is returned

func (*MapRef) GetPath

func (r *MapRef) GetPath() string

func (*MapRef) IsEmpty

func (r *MapRef) IsEmpty() bool

IsEmpty implements the Ref.IsEmpty method

func (*MapRef) Merge

func (r *MapRef) Merge(ref Ref) (Ref, bool)

Merge implements Ref.Merge

func (*MapRef) Mutate

func (r *MapRef) Mutate(mutator MutatorFunc) error

Mutate implements the Ref.Mutate method

func (*MapRef) Set

func (r *MapRef) Set(value interface{}) error

Set implements the Ref.Set method

func (*MapRef) Union

func (r *MapRef) Union(refs ...Ref) Ref

Union implements the Ref.Union method

func (*MapRef) Values

func (r *MapRef) Values() []interface{}

Values implements the Ref.Values method

func (*MapRef) Vars

func (r *MapRef) Vars() []*VarRef

Vars returns VarRefs for all the referenced variable

type MatchSet

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

MatchSet represents one jsonmatch extract and provides functions for mutating or extracting the matched values. Setting selected values can be done until you have performed a mutation. You can only perform exactly one mutation.

func Match

func Match(path string, data interface{}) (*MatchSet, error)

Match compiles and excutes the jsonmatch expression on the underlying data

func (*MatchSet) Delete

func (e *MatchSet) Delete() (interface{}, error)

Delete deletes all selected values from the underlying data

func (*MatchSet) Mutate

func (e *MatchSet) Mutate(mutator MutatorFunc) (interface{}, error)

Mutate passes all selected values through the mutator and updates them in the underlying value

func (*MatchSet) MutateRegions

func (e *MatchSet) MutateRegions(mutator MutateRegionsFunc) (interface{}, error)

MutateRegions mutates the elements of each selected array as single operations. The original valuas are provided as an array of arrays, and returns an array of arrays of the same shape. Each sub-array is substituted for its corresponding original, and it is okay if the length of these sub arrays differ. The result is grown or shrunk accordingly. Main use case is to support slice-style operations like append, replace etc. If the method is used on selections that also include non-array members, an error is returned.

func (*MatchSet) Set

func (e *MatchSet) Set(value interface{}) (interface{}, error)

Set updates all selected values to the provided value

func (*MatchSet) Values

func (e *MatchSet) Values() []interface{}

Values returns an array of all the values selected by the jsonmatch

type MutateRegionsFunc

type MutateRegionsFunc func(path string, current [][]interface{}) ([][]interface{}, error)

MutateRegionsFunc is the signature for the callbacks provided to the ArrayRef.MutateAll methods that allow a selection of an array to be mutated as a whole. The selection is provided as an array of arrays (since selections might not be contiguous). Each sub-array representing a contiguous block of selected items. These may be modified directly by the mutator, or replaced with new ones.

type MutatorFunc

type MutatorFunc func(path string, value interface{}) (interface{}, error)

MutatorFunc is the signature for the callbacks provided to the Ref.Mutate methods

type ParseError

type ParseError struct {
	Message string
	Pos     int
}

ParseError describes an error in the parse and contains the position of the problem along with the message.

func (*ParseError) Error

func (e *ParseError) Error() string

type Parser

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

Parser represents a JSONpath parser

func NewParser

func NewParser(r io.Reader) *Parser

NewParser returns a new instance of Parser.

func (*Parser) Parse

func (p *Parser) Parse() (*Expression, error)

Parse executes the parser

type PathedRef

type PathedRef interface {
	GetPath() string
}

PathedRef is an interface for refs that have a definitive, resolvable paths, which is effectively Array and Map ref

type Ref

type Ref interface {
	// Values returns the value of all referenced variables in their original form
	Values() []interface{}
	// Vars returns VarRefs for all the referenced variable
	Vars() []*VarRef
	// Delete deletes the values from the referenced variables if possible (basically maps and arrays). If
	// the refs cannot be deleted (structs or primitive variables) theres an error
	Delete() error
	// Mutate changes the value of the refs by passing them through a mutator function
	Mutate(mutator MutatorFunc) error
	// Set sets the referenced values to the provided value
	Set(value interface{}) error
	// Depth returns a depth value for this ref. When applying operations to multiple refs
	// they are always performed on the refs with the highest depth first.
	Depth() int
	// Join this ref with another ref yielding what amounts to a union of the two
	// refs
	Union(ref ...Ref) Ref
	// Merge attempts to combine two refs into
	// one ref without resorting to the UnionRef. This is used to combine i.e. two
	// ArrayRefs that refer to the same underlying slice by just merging their
	// respective indicies.
	Merge(ref Ref) (Ref, bool)
	// Returns true if this Ref is not referring to any elements. I.e. after a Delete
	// operation
	IsEmpty() bool
	// Give an estimate of the number of values this Ref points to. Used for sizing
	// arrays so should err on the high side if it needs to err at all.
	EstimateSize() int
}

A Ref is a reference to a selection of values, typically in a struct, map or an array that allow operations to be performed on said values. Main use case is to represent data that have been matched using jsonmatch specs and then allow a client to perform operations on that subset.

func NewEmptyRef

func NewEmptyRef() Ref

NewEmptyRef returns an empty Ref

func NewLiteralRef

func NewLiteralRef(value interface{}) Ref

NewLiteralRef returns a new *LiteralRef

func NewUnionRef

func NewUnionRef(refs ...Ref) Ref

NewUnionRef creates a new UnionRef

type Region

type Region struct {
	Start int
	End   int
}

Region represents a region of a slice and support operations that model the behavior of a region of array indicies as an array is being manipulated

func (Region) Adjacent

func (r Region) Adjacent(other Region) bool

Adjacent returns true if the regions are exactly adjacent in either order

func (Region) Clone

func (r Region) Clone() Region

Clone returns a fresh copy of the region

func (Region) ContainsIndex

func (r Region) ContainsIndex(index int) bool

ContainsIndex is true if the index provided is covered by the region

func (Region) ContainsRegion

func (r Region) ContainsRegion(other Region) bool

ContainsRegion returns true if the reciever covers the entire other region (or more)

func (Region) Cut

func (r Region) Cut(other Region) Region

Cut returns the new position and size of this region as if the provided region was cut from the underlying array.

func (Region) Empty

func (r Region) Empty() bool

Empty is true if the region cover no items

func (Region) Equal

func (r Region) Equal(other Region) bool

Equal is true if the two regions are exactly the same

func (Region) Grow

func (r Region) Grow(count int) Region

Grow extends the region by growing it to the right

func (Region) Intersect

func (r Region) Intersect(other Region) Regions

Intersect returns the intersection of the two regions. Removing a chunk from the middle of the receiver splits the region, so since this may result in two or no regions at all the result is a region set

func (Region) IsAfter

func (r Region) IsAfter(index int) bool

IsAfter is true if the index is after the end of the region

func (Region) IsBefore

func (r Region) IsBefore(index int) bool

IsBefore is true if the index is before the start of the region

func (Region) Join

func (r Region) Join(other Region) Region

Join returns a range that cover the entire area of both regions. If the regions are not adjacent, the resulting region will also span the void between them

func (Region) Len

func (r Region) Len() int

Len returns the number of items in the region

func (Region) Overlap

func (r Region) Overlap(other Region) bool

Overlap is true if the two regions cover at least one shared element

func (Region) OverlapOrAdjacent

func (r Region) OverlapOrAdjacent(other Region) bool

OverlapOrAdjacent is true if the two regions are adjacent or overlapping

func (Region) Shift

func (r Region) Shift(diff int) Region

Shift moves the entire region by the numbre of indicies indicated

func (Region) Shrink

func (r Region) Shrink(count int) Region

Shrink region, but never smaller than nothing

type Regions

type Regions []Region

Regions model a set of Regions as in ranges of array indicies and model the movement of these ranges as the underlying array is being manipulated.

func NewRegionForEachIndex

func NewRegionForEachIndex(indicies []int) Regions

NewRegionForEachIndex takes an array of ints and convert them to a set of regions, having an individual region for each index

func NewRegionsFromIndicies

func NewRegionsFromIndicies(indicies []int) Regions

NewRegionsFromIndicies takes an array of ints and convert them to a set of regions, describing any contiguous series of indicies as single regions

func NewRegionsFromSliceSelector

func NewRegionsFromSliceSelector(selector string) (Regions, error)

NewRegionsFromSliceSelector creates a region set from a jsonmatch slice selector (only supporting literal indicies, no matching etc obviously since we don't have any data at this junction)

func (Regions) Check

func (rs Regions) Check() bool

Check validates that the regions set is fully compliant, meaning its sorted and non-overlapping.

func (Regions) CheckSorted

func (rs Regions) CheckSorted() bool

CheckSorted validates that the regions are sorted

func (Regions) Clean

func (rs Regions) Clean() Regions

Clean takes a jumble of unsorted, potentially overlapping set of regions and return a sorted set of non-overlapping regions without joining any regions, meaning we preserve any split-points and zero-length regions in the selection.

func (Regions) Compact

func (rs Regions) Compact() Regions

Compact removes any empty regions from the set

func (Regions) ContainsIndex

func (rs Regions) ContainsIndex(i int) bool

ContainsIndex checks if any of the regions in the set contains the index

func (Regions) Cut

func (rs Regions) Cut(other Region) Regions

Cut one region from the other regions. Shrinking or moving down the regions correspondingly. The number of resulting regions is always the same, regions that end up being empty will just get a lengt of 0. Call Compact to have them removed, if you need it.

func (Regions) ExtractItems

func (rs Regions) ExtractItems(source []interface{}) [][]interface{}

ExtractItems extracts the items in the regions and returns them as an array of arrays with one sub-array with the items of each region

func (Regions) ForEachIndex

func (rs Regions) ForEachIndex(callback ForEachIndexFunc) error

ForEachIndex calls the callback for each index in the region set

func (Regions) IndiciesCount

func (rs Regions) IndiciesCount() int

IndiciesCount counts the total number of indicies covered by the regions in this set

func (Regions) InsertAt

func (rs Regions) InsertAt(other Region) Regions

InsertAt returns a new region set moving regions as if something was inserted at the specfied region. If the new region is inside an existing region, this region is grown accordingly, all regions after the index is moved up by count indicies. The provided region is not actually inserted into the region set.

func (Regions) Intersect

func (rs Regions) Intersect(other Regions) Regions

func (Regions) Len

func (rs Regions) Len() int

Len implements the sortable interface

func (Regions) Less

func (rs Regions) Less(i, j int) bool

Less implements the sortable interface

func (Regions) MergeItems

func (rs Regions) MergeItems(source []interface{}, replace [][]interface{}) ([]interface{}, Regions)

MergeItems is a companion to ExtractItems that takes the arrays of the replace parameter and replaces them for each region described in rs returning the updated array and regions updated to cover the newly inserted items

func (Regions) Simplify

func (rs Regions) Simplify() Regions

Simplify removes any empty regions and joins overlapping regions

func (Regions) Sort

func (rs Regions) Sort() Regions

Sort the regions by the start index

func (Regions) Swap

func (rs Regions) Swap(i, j int)

Swap implements the sortable interface

func (Regions) ToIndicies

func (rs Regions) ToIndicies() []int

ToIndicies converts a set of regions to individual indicies

func (Regions) ToSliceSelector

func (rs Regions) ToSliceSelector() string

ToSliceSelector returns a jsonmatch slice selector for the regions on the form "2:5,7:9" that would fit in a selector as "my.array[2:5,7:9]".

func (Regions) Union

func (rs Regions) Union(otherRegions Regions) Regions

Union computes the union of two sets of Regions. Any overlapping regions are combined, while adjacent regions are kept as they are.

type Scanner

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

Scanner scans a jsonmatch expression

func NewScanner

func NewScanner(r io.Reader) *Scanner

NewScanner creates a new Scanner(!)

func (*Scanner) Scan

func (s *Scanner) Scan() (Token, string, int)

Scan gets the next token of the jsonmatch string

type Token

type Token int

Token represents a jsonmatch token

const (
	Illegal            Token = iota
	EOF                      // End of file
	Whitespace               // A contigous squence of whitespace
	ParenLeft                // (
	ParenRight               // )
	BraceLeft                // {
	BraceRight               // }
	BracketLeft              // [
	BracketRight             // ]
	Dot                      // .
	Equals                   // ==
	GT                       // >
	LT                       // <
	GTE                      // >=
	LTE                      // <=
	NEQ                      // !=
	Not                      // !
	Comma                    // ,
	Identifier               // A valid field
	DotDot                   // ..
	Dollar                   // $
	At                       // @
	Integer                  // 1234
	Float                    // 123.4
	Bool                     // true or false
	SingleQuotedString       // 'literal string'
	DoubleQuotedString       // "literal string"
	Asterisk                 // *
	QuestionMark             // ?
	Pipe                     // |
	Colon                    // :
	Exists                   // virtual operator
	Slash                    // /
)

func MatchingBrace

func MatchingBrace(token Token) Token

MatchingBrace returns the brace matching the provided token, as in '(' returns ')' and '[' returns ']'

func (Token) Literal

func (token Token) Literal() string

Literal converts a token id to a literal where applicable

func (Token) String

func (token Token) String() string

type UnionRef

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

UnionRef is the union of a collection of refs

func (*UnionRef) Delete

func (r *UnionRef) Delete() error

Delete implements the Ref.Delete method

func (*UnionRef) Depth

func (r *UnionRef) Depth() int

Depth implements the Ref.Depth methd

func (*UnionRef) EstimateSize

func (r *UnionRef) EstimateSize() int

EstimateSize implements the Ref.EstimateSize method

func (*UnionRef) IsEmpty

func (r *UnionRef) IsEmpty() bool

IsEmpty implements the Ref.IsEmpty method

func (*UnionRef) Len

func (r *UnionRef) Len() int

func (*UnionRef) Less

func (r *UnionRef) Less(i, j int) bool

func (*UnionRef) Merge

func (r *UnionRef) Merge(ref Ref) (Ref, bool)

Merge implements the Ref.Merge method

func (*UnionRef) Mutate

func (r *UnionRef) Mutate(mutator MutatorFunc) error

Mutate implements the Ref.Mutate method

func (*UnionRef) Set

func (r *UnionRef) Set(value interface{}) error

Set implements the Ref.Set method

func (*UnionRef) Swap

func (r *UnionRef) Swap(i, j int)

func (*UnionRef) Union

func (r *UnionRef) Union(refs ...Ref) Ref

Union implements the Ref.Union method

func (*UnionRef) Values

func (r *UnionRef) Values() []interface{}

Values implements the Ref.Values method

func (*UnionRef) Vars

func (r *UnionRef) Vars() []*VarRef

Vars returns VarRefs for all the referenced variable

type VarGetter

type VarGetter func() interface{}

VarGetter is the signature for VarRef getters

type VarRef

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

VarRef is a reference to a variable with the capacity to replace the contents of said variable

func NewVarRef

func NewVarRef(identity string, getter VarGetter, setter VarSetter, depth int) *VarRef

NewVarRef has a complicated signature and should not be used except in tests

func (*VarRef) CanonicalValue

func (r *VarRef) CanonicalValue() interface{}

CanonicalValue gets the current value of the variable converted to canonical type

func (*VarRef) Delete

func (r *VarRef) Delete() error

Delete is not supported for VarRefs

func (*VarRef) Depth

func (r *VarRef) Depth() int

Depth returns the depth of the variable

func (*VarRef) EstimateSize

func (r *VarRef) EstimateSize() int

EstimateSize is always 1 for VarRefs

func (*VarRef) GetPath

func (r *VarRef) GetPath() string

func (*VarRef) Index

func (r *VarRef) Index() int

Index returns the int-index of this VarRef if it is originally from a slice

func (*VarRef) IsContainer

func (r *VarRef) IsContainer() bool

IsContainer is true if this VarRef wraps a map or slice or array.

func (*VarRef) IsEmpty

func (r *VarRef) IsEmpty() bool

IsEmpty is never true for VarRefs

func (*VarRef) IsMap

func (r *VarRef) IsMap() bool

IsMap is true if the underlying value is a kind of map.

func (*VarRef) IsSlice

func (r *VarRef) IsSlice() bool

IsSlice is true if the underlying value is a kind of slice or array.

func (*VarRef) Key

func (r *VarRef) Key() string

Key returns the key of this VarRef if it was originally from a map

func (*VarRef) Merge

func (r *VarRef) Merge(ref Ref) (Ref, bool)

Merge only "merges" two identical VarRefs

func (*VarRef) Mutate

func (r *VarRef) Mutate(mutator MutatorFunc) error

Mutate mutates

func (*VarRef) Set

func (r *VarRef) Set(value interface{}) error

Set sets the value of the contained variable

func (*VarRef) SetWithMatchedType

func (r *VarRef) SetWithMatchedType(value interface{}) error

SetWithMatchedType updates the value, but attempts to avoid changing the underlying type if the new value is one of the canonical types

func (*VarRef) TypeKind

func (r *VarRef) TypeKind() reflect.Kind

TypeKind returns the kind of the type contained by the VarRef

func (*VarRef) Union

func (r *VarRef) Union(refs ...Ref) Ref

Union creates a union with the VarRef

func (*VarRef) Value

func (r *VarRef) Value() interface{}

Value gets the current value of the variable in original underlying type

func (*VarRef) Values

func (r *VarRef) Values() []interface{}

Values gets the value wrapped in an array for compatibility

func (*VarRef) Vars

func (r *VarRef) Vars() []*VarRef

Vars gets this VarRef wrapped in an array for your iteration convenience

type VarSetter

type VarSetter func(value interface{})

VarSetter is the signature for VarRef setters

Directories

Path Synopsis
This package is copied from Go library text/template.
This package is copied from Go library text/template.

Jump to

Keyboard shortcuts

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