yamlpath

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: May 8, 2025 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package yamlpath was copied, refactored and trimmed from: https://github.com/goccy/go-yaml/blob/4653a1bb5c0047bb37280ac341e2f091cb44352f/path.go

The main difference with the original is that instead of returning a child node value, it returns the node itself. For example:

# Given YAML:
a:
  b:
    c: 1

# Given path:
a.b

# Original result:
c: 1 node

# New result:
b node

Features which are not used here were removed.

The second major difference is that it attempts to find the closest ast.Node parent if it can't find a direct match. For example:

# Given YAML:
a:
  b:
    c: 1

# Given path:
a.b.d

# Original result:
error

# New result:
b node

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Match

func Match(yamlPath, concrete string) bool

Match returns true if the yaml path representation matches concrete path. The path can contain special identifiers, like wildcards.

Types

type Path

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

Path represents a single YAMLPath (like).

func FromString

func FromString(s string) (*Path, error)

FromString creates a Path from string.

Only a subset of rules are supported in comparison to JSONPath or YAMLPath: $ : the root object/element .<child> : child operator [num] : indexed element of an array

If you want to use reserved characters such as `.` as a key name, enclose them in single quotation as follows ( $.foo.'bar.baz'.name ). If you want to use a single quote with reserved characters, escape it with `\` ( $.foo.'bar.baz\'s value'.name ).

func (*Path) FilterFile

func (p *Path) FilterFile(f *ast.File) (ast.Node, bool, error)

FilterFile filters the whole ast.File.

func (*Path) FilterNode

func (p *Path) FilterNode(node ast.Node) (ast.Node, bool, error)

FilterNode filters single ast.Node. If it cannot find the filtered ast.Node, it will remove the last element from the path and repeat this step until a match is found. In the end, if none of the nodes matched the selector, a root node will be returned.

func (*Path) String

func (p *Path) String() string

String returns textual Path representation.

type PathBuilder

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

PathBuilder represent builder for YAMLPath.

func (*PathBuilder) Build

func (b *PathBuilder) Build() *Path

Build returns the built Path pointer.

func (*PathBuilder) Child

func (b *PathBuilder) Child(name string) *PathBuilder

Child adds '.<name>' to the current path.

func (*PathBuilder) Copy

func (b *PathBuilder) Copy() *PathBuilder

Copy copies the PathBuilder along with its fields.

func (*PathBuilder) Index

func (b *PathBuilder) Index(idx uint) *PathBuilder

Index adds '[<idx>]' to the current path.

func (*PathBuilder) Root

func (b *PathBuilder) Root() *PathBuilder

Root adds '$' to the current path.

Jump to

Keyboard shortcuts

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