Documentation ¶
Overview ¶
Package yamlpath provides an implementation of the JSONPath specification for YAML, leveraging the go-yaml library.
Index ¶
- Variables
- type Collection
- func (c Collection) Bools() ([]bool, error)
- func (c Collection) Decimals() ([]decimal.Decimal, error)
- func (c Collection) Decoder() *Decoder
- func (c Collection) Equal(other Collection) bool
- func (c Collection) Float64s() ([]float64, error)
- func (c Collection) Ints() ([]int, error)
- func (c Collection) IsEmpty() bool
- func (c Collection) IsTruthy() bool
- func (c Collection) Nodes() []*yaml.Node
- func (c Collection) Singleton() (*yaml.Node, error)
- func (c Collection) SingletonBool() (v bool, err error)
- func (c Collection) SingletonDecimal() (v decimal.Decimal, err error)
- func (c Collection) SingletonFloat64() (v float64, err error)
- func (c Collection) SingletonInt() (v int, err error)
- func (c Collection) SingletonString() (v string, err error)
- func (c Collection) Strings() ([]string, error)
- type CompileError
- type Decoder
- type KindError
- type NotSingletonError
- type TagError
- type YAMLPath
- func (yp *YAMLPath) Equal(other *YAMLPath) bool
- func (yp *YAMLPath) MarshalText() ([]byte, error)
- func (yp *YAMLPath) Match(node *yaml.Node) (Collection, error)
- func (yp *YAMLPath) MustMatch(node *yaml.Node) Collection
- func (yp *YAMLPath) String() string
- func (yp *YAMLPath) UnmarshalText(text []byte) error
Constants ¶
This section is empty.
Variables ¶
var ( // ErrSyntax is returned when an error occurs during parsing. ErrSyntax = compile.ErrSyntax // ErrCompile is returned when an error occurs during compilation. ErrCompile = compile.ErrCompile // ErrEval is returned when an error occurs during evaluation. ErrEval = errs.ErrEval // ErrNotSingleton is returned when a collection is expected to contain // only a single node, but contains more or less than one node. // // More details can be retrieved from these errors by using [errors.As] with // [NotSingletonError]. ErrNotSingleton = errs.ErrNotSingleton // ErrBadKind is returned when an error occurs during evaluation due to // a bad YAML node kind. // // More details can be retrieved from these errors by using [errors.As] with // [KindError]. ErrBadKind = errs.ErrBadKind // ErrBadTag is returned when an error occurs during evaluation due to // a bad YAML node tag. // // More details can be retrieved from these errors by using [errors.As] with // [TagError]. ErrBadTag = errs.ErrBadTag )
Functions ¶
This section is empty.
Types ¶
type Collection ¶
type Collection []*yaml.Node
Collection represents a collection of YAML nodes that can be retrieved from a YAMLPath evaluation result.
func Match ¶
func Match(path string, node *yaml.Node) (Collection, error)
Match evaluates the YAMLPath expression against the given YAML node.
func MustMatch ¶
func MustMatch(path string, node *yaml.Node) Collection
MustMatch evaluates the YAMLPath expression against the given YAML node. It panics if an error occurs.
func (Collection) Bools ¶
func (c Collection) Bools() ([]bool, error)
Bools decodes all nodes in the collection into a slice of booleans. If any of the nodes are unable to be marshaled into a boolean, an error is returned.
func (Collection) Decimals ¶
func (c Collection) Decimals() ([]decimal.Decimal, error)
Decimals decodes all nodes in the collection into a slice of decimals. If any of the nodes are unable to be marshaled into a decimal, an error is returned.
func (Collection) Decoder ¶
func (c Collection) Decoder() *Decoder
Decoder returns a new Decoder that can be used to decode the collection into target objects.
func (Collection) Equal ¶
func (c Collection) Equal(other Collection) bool
Equal compares this collection to another collection for equality.
This does a deep comparison of the YAML nodes, and introspects into whether the semantic values of the nodes are equal (e.g. YAML scalar floats may be in various notations, but represent the same value).
This comparison follows the same behavior as the `==` operator.
func (Collection) Float64s ¶
func (c Collection) Float64s() ([]float64, error)
Float64s decodes all nodes in the collection into a slice of floats. If any of the nodes are unable to be marshaled into a float, an error is returned.
func (Collection) Ints ¶
func (c Collection) Ints() ([]int, error)
Ints decodes all nodes in the collection into a slice of integers. If any of the nodes are unable to be marshaled into an integer, an error is returned.
func (Collection) IsEmpty ¶
func (c Collection) IsEmpty() bool
IsEmpty returns true if the collection is empty.
func (Collection) IsTruthy ¶
func (c Collection) IsTruthy() bool
IsTruthy evaluates whether this collection may be considered as having a truthful representation.
Truthiness of a collection is determined by the following rules:
- An empty collection is considered falsy
- A collection with multiple nodes is considered truthy
- A collection with a single node that is not a boolean is considered truthy
- A collection with a single node that is a boolean is considered truthy if the boolean value is true
func (Collection) Nodes ¶
func (c Collection) Nodes() []*yaml.Node
Nodes converts this Collection into a slice of yaml.Node objects.
func (Collection) Singleton ¶
func (c Collection) Singleton() (*yaml.Node, error)
Singleton evaluates whether the collection contains only a single node, and returns that node if it does. If it does not, an ErrNotSingleton error is raised.
func (Collection) SingletonBool ¶
func (c Collection) SingletonBool() (v bool, err error)
SingletonBool evaluates whether the collection contains only a single node, and returns the boolean value of that node if it does. If it does not, an ErrNotSingleton error is raised.
func (Collection) SingletonDecimal ¶
func (c Collection) SingletonDecimal() (v decimal.Decimal, err error)
SingletonDecimal evaluates whether the collection contains only a single node, and returns the decimal value of that node if it does. If it does not, an ErrNotSingleton error is raised.
func (Collection) SingletonFloat64 ¶
func (c Collection) SingletonFloat64() (v float64, err error)
SingletonFloat64 evaluates whether the collection contains only a single node, and returns the float value of that node if it does. If it does not, an ErrNotSingleton error is raised.
func (Collection) SingletonInt ¶
func (c Collection) SingletonInt() (v int, err error)
SingletonInt evaluates whether the collection contains only a single node, and returns the integer value of that node if it does. If it does not, an ErrNotSingleton error is raised.
func (Collection) SingletonString ¶
func (c Collection) SingletonString() (v string, err error)
SingletonString evaluates whether the collection contains only a single node, and returns the string value of that node if it does. If it does not, an ErrNotSingleton error is raised.
func (Collection) Strings ¶
func (c Collection) Strings() ([]string, error)
Strings decodes all nodes in the collection into a slice of strings. If any of the nodes are unable to be marshaled into a string, an error is returned.
type CompileError ¶
type CompileError = compile.CompileError
CompileError represents an error that occurs during compilation.
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder provides a mechanism for decoding a collection of YAML nodes into target objects.
func NewDecoder ¶
func NewDecoder(collection Collection) *Decoder
NewDecoder creates a new Decoder that can be used to decode a collection of YAML nodes into target objects.
func (*Decoder) Decode ¶
Decode decodes the next YAML node in the collection into the target object, and then advances the decoder to the next node. If the collection is exhausted, this will return an io.EOF error.
If the target object cannot be decoded, the underlying error will be returned, and the decoder will not advance to the next node.
type KindError ¶
KindError is an evaluation error returned when an expression is expecting a specific YAML node kind, but receives a node with a different kind.
This error can be tested for with errors.Is and retrieved for with errors.As. Errors of this type will also satisfy ErrBadKind and ErrEval.
type NotSingletonError ¶
type NotSingletonError = errs.NotSingletonError
NotSingletonError is an evaluation error returned when an expression is expecting a singleton value, but receives a collection with more or less than one node.
This error can be tested for with errors.Is and retrieved for with errors.As. Errors of this type will also satisfy ErrNotSingleton and ErrEval.
type TagError ¶
TagError is an evaluation error returned when an expression is expecting a specific YAML node tag, but receives a node with a different tag.
This error can be tested for with errors.Is and retrieved for with errors.As. Errors of this type will also satisfy ErrBadTag and ErrEval.
type YAMLPath ¶
type YAMLPath struct {
// contains filtered or unexported fields
}
YAMLPath is the representation of a compiled YAMLPath expression.
A YAMLPath is safe for concurrent use by multiple goroutines, except for if custom functions have been provided that are themselves unsuafe for concurrent use.
func Compile ¶
Compile parses a YAMLPath expressions and returns, if successful, a YAMLPath object that can be used to match against [yaml.Node]s.
On error, this will return a CompileError that can be used to determine the cause of the error, and the location where the error occurs.
func MustCompile ¶
MustCompile compiles a YAMLPath expression. It panics if an error occurs.
This is a convenience wrapper around Compile.
func (*YAMLPath) Equal ¶
Equal returns true if the two YAMLPath expressions are equal.
Equality is determined by evaluating whether the two paths are equal. This makes nil YAMLPath equivalent to zero value YAMLPath objects.
func (*YAMLPath) MarshalText ¶
MarshalText marshals the YAMLPath expression to text.
func (*YAMLPath) Match ¶
func (yp *YAMLPath) Match(node *yaml.Node) (Collection, error)
Match evaluates the YAMLPath expression against the given YAML node, returning all matching subnodes found. If an evaluation error occurs during the matching process, it is returned and the collection will be nil.
func (*YAMLPath) MustMatch ¶
func (yp *YAMLPath) MustMatch(node *yaml.Node) Collection
MustMatch evaluates the YAMLPath expression against the given YAML node, returning all matches. It panics if an error occurs.
func (*YAMLPath) UnmarshalText ¶
UnmarshalText unmarshals the YAMLPath expression from text.
This enables YAMLPath to be used as a field in structs that may be unmarshaled in configs like YAML or JSON.
Directories ¶
Path | Synopsis |
---|---|
internal
|
|
errs
Package errs is an internal package that provides shared errors across the go-yamlpath library.
|
Package errs is an internal package that provides shared errors across the go-yamlpath library. |
expr/exprtest
Package exprtest provides test-doubles for expr.Expr implementations.
|
Package exprtest provides test-doubles for expr.Expr implementations. |
funcs
Package funcs provides a mechanism for defining and invoking functions in a yamlpath expression.
|
Package funcs provides a mechanism for defining and invoking functions in a yamlpath expression. |
invocation
Package invocation provides a mechanism for defining and invoking functions in a yamlpath expression.
|
Package invocation provides a mechanism for defining and invoking functions in a yamlpath expression. |
invocation/arity
Package arity is a micro-package for providing the Arity type, which is used to identify whether the number of arguments being provided to an invocation is valid.
|
Package arity is a micro-package for providing the Arity type, which is used to identify whether the number of arguments being provided to an invocation is valid. |
invocation/invocationtest
Package invocationtest provides test-doubles for the invocation package
|
Package invocationtest provides test-doubles for the invocation package |
yamltest
Package yamltest provides test-doubles for yaml.Node implementations.
|
Package yamltest provides test-doubles for yaml.Node implementations. |
yamlutil
Package yamlutil provides some simple reusable utilities for working with the yaml library.
|
Package yamlutil provides some simple reusable utilities for working with the yaml library. |