selectr

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2023 License: MIT Imports: 4 Imported by: 0

README

selectr GoDoc test

Select values from objects/arrays with key-path notation.

Key-path notation

Key-path notation is a simple format for describing how to traverse data structures. It's very similar to C languages, but with limitations. See the reference document for more information.

Usage

m := map[string]interface{}{
    "foo": map[string]interface{}{
        "bar": []interface{}{
            1,
            2,
            3,
        },
    },
}

sel, _ := selectr.Parse(".foo")
sel.Resolve(m) // => map[string]interface{}{"bar": []interface{}{1, 2, 3}}

sel, _ = selectr.Parse(".foo.bar")
sel.Resolve(m) // => []interface{}{1, 2, 3}

sel, _ = selectr.Parse(".foo.bar[1]")
sel.Resolve(m) // => 2

Use cases

  • Referencing a dynamic value in a JSON/YAML file:

    Consider you maintain a program that allows users to reference arbitrary values, and one strategy of resolving a value is referencing a symbol in a JSON file.

    # example.json
    {
        "accounts": [{
            "id": 123,
            "name": "main"
        }]
    }
    

    The end-user references example.json with the selectr .accounts[0].name.

    jsonBytes, _ := os.Open(fileName)
    
    var m map[string]interface{}
    json.Unmarshal(jsonBytes, &m)
    
    sel, _ := selectr.Parse(selectrString)
    val := sel.Resolve(m)
    

    The val is resolved to "main".

  • Import dynamic values from dynamic data files.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MapEntryResolver

type MapEntryResolver struct {
	Key  string
	Expr ast.Expr
}

MapEntryResolver resolves a value from a map.

func (*MapEntryResolver) Expression

func (r *MapEntryResolver) Expression() ast.Expr

Expression returns the corresponding ast.Expr.

func (*MapEntryResolver) Resolve

func (r *MapEntryResolver) Resolve(v interface{}) (interface{}, error)

Resolve resolves the value of an entry on the map.

type ResolveError

type ResolveError struct {
	Msg  string
	Code string

	// Pos is the position in the corresponding key-path of the underlying
	// expression that the resolver was dervied from.
	Pos int
}

ResolveError represents an error that occured while resolving a value for a given selector.

func (ResolveError) Error

func (err ResolveError) Error() string

Error implements (error).Error

type Resolver

type Resolver interface {
	Resolve(interface{}) (interface{}, error)
	Expression() ast.Expr
}

Resolver resolves a value from an object.

type Selector

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

Selector represents a value selection on an object.

func Parse

func Parse(s string) (*Selector, error)

Parse parses a traversal tree from the selector string and returns a new Selector instance.

func (*Selector) Resolve

func (s *Selector) Resolve(v interface{}) (interface{}, error)

Resolve resolves the value at the specified key-path, if any, from the provided object. The root object must be an indexable type such as a Map `map[string]interface{}` or a Slice `[]interface{}`.

All errors will be prefixed with the sub-key-path the error occured at.

Example usage:

    sel := Parse("test[0].foo")
    sel.Resolve(map[string]interface{}{
	       "test": []map[string]interface{}{
		       {"foo": "bar"}
        }
    })

type SliceElementResolver

type SliceElementResolver struct {
	Index int
	Expr  *ast.IndexExpr
}

SliceElementResolve resolves values from a slice.

func (*SliceElementResolver) Expression

func (r *SliceElementResolver) Expression() ast.Expr

Expression returns the corresponding ast.Expr.

func (*SliceElementResolver) Resolve

func (r *SliceElementResolver) Resolve(v interface{}) (interface{}, error)

Resolve resolves the value of the element at the index on the slice.

type TraversalTreeNode

type TraversalTreeNode struct {
	Resolver Resolver
	Parent   *TraversalTreeNode
	Child    *TraversalTreeNode
}

TraversalTreeNode represents a tree node responsible for traversing a given object.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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