query

package
v0.0.2-0...-ad08894 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2018 License: MIT, MIT Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NodeFilterFn

type NodeFilterFn func(node interface{}) bool

NodeFilterFn represents a user-defined filter function, for use with Query.SetFilter().

The return value of the function must indicate if 'node' is to be included at this stage of the TOML path. Returning true will include the node, and returning false will exclude it.

NOTE: Care should be taken to write script callbacks such that they are safe to use from multiple goroutines.

Example (FilterExample)
tree, _ := toml.Load(`
      [struct_one]
      foo = "foo"
      bar = "bar"

      [struct_two]
      baz = "baz"
      gorf = "gorf"
    `)

// create a query that references a user-defined-filter
query, _ := Compile("$[?(bazOnly)]")

// define the filter, and assign it to the query
query.SetFilter("bazOnly", func(node interface{}) bool {
	if tree, ok := node.(*toml.Tree); ok {
		return tree.Has("baz")
	}
	return false // reject all other node types
})

// results contain only the 'struct_two' Tree
query.Execute(tree)
Output:

type Query

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

A Query is the representation of a compiled TOML path. A Query is safe for concurrent use by multiple goroutines.

Example (QueryExample)
config, _ := toml.Load(`
      [[book]]
      title = "The Stand"
      author = "Stephen King"
      [[book]]
      title = "For Whom the Bell Tolls"
      author = "Ernest Hemmingway"
      [[book]]
      title = "Neuromancer"
      author = "William Gibson"
    `)

// find and print all the authors in the document
query, _ := Compile("$.book.author")
authors := query.Execute(config)
for _, name := range authors.Values() {
	fmt.Println(name)
}
Output:

func Compile

func Compile(path string) (*Query, error)

Compile compiles a TOML path expression. The returned Query can be used to match elements within a Tree and its descendants. See Execute.

func (*Query) Execute

func (q *Query) Execute(tree *toml.Tree) *Result

Execute executes a query against a Tree, and returns the result of the query.

func (*Query) SetFilter

func (q *Query) SetFilter(name string, fn NodeFilterFn)

SetFilter sets a user-defined filter function. These may be used inside "?(..)" query expressions to filter TOML document elements within a query.

type Result

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

Result is the result of Executing a Query.

func CompileAndExecute

func CompileAndExecute(path string, tree *toml.Tree) (*Result, error)

CompileAndExecute is a shorthand for Compile(path) followed by Execute(tree).

func (Result) Positions

func (r Result) Positions() []toml.Position

Positions is a set of positions for values within a Result. Each index in Positions() corresponds to the entry in Value() of the same index.

func (Result) Values

func (r Result) Values() []interface{}

Values is a set of values within a Result. The order of values is not guaranteed to be in document order, and may be different each time a query is executed.

Jump to

Keyboard shortcuts

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