query

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package query provides a simple query language for navigating data structures.

The query language supports:

  • Field access: "field" or "object.field"
  • Array indexing: "array[0]" or "array[0].field"
  • Nested access: "user.address.street"

Example:

data := map[string]interface{}{
	"user": map[string]interface{}{
		"name": "John",
		"emails": []interface{}{
			"john@example.com",
			"john.doe@company.com",
		},
	},
}

name, _ := query.Execute(data, "user.name")         // "John"
email, _ := query.Execute(data, "user.emails[0]")  // "john@example.com"

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClearCache

func ClearCache()

ClearCache clears the query cache. This is mainly useful for testing.

func Execute

func Execute(data interface{}, queryStr string) (interface{}, error)

Execute executes a query against data and returns the result.

func ExecuteCached

func ExecuteCached(data interface{}, queryStr string) (interface{}, error)

ExecuteCached executes a query with caching. This is the recommended way to execute queries in production.

func ExecutePath

func ExecutePath(data interface{}, path []interface{}) (interface{}, error)

ExecutePath executes a path against data.

func FormatTokens

func FormatTokens(tokens []Token) string

FormatTokens formats a slice of tokens for debugging.

func PathFromQuery

func PathFromQuery(queryStr string) ([]interface{}, error)

PathFromQuery converts a query string directly to a path. This is a convenience function for simple queries.

func SimplifyNode

func SimplifyNode(node Node) []interface{}

SimplifyNode simplifies an AST node for easier execution. This converts complex nodes into a linear path.

func TokenTypeName

func TokenTypeName(t TokenType) string

TokenTypeName returns the name of a token type.

Types

type DotNode

type DotNode struct {
	Left  Node
	Right Node
}

DotNode represents a dot notation access.

func (*DotNode) String

func (n *DotNode) String() string

String returns the string representation.

func (*DotNode) Type

func (n *DotNode) Type() NodeType

Type returns the node type.

type IdentifierNode

type IdentifierNode struct {
	Name string
}

IdentifierNode represents a field name.

func (*IdentifierNode) String

func (n *IdentifierNode) String() string

String returns the string representation.

func (*IdentifierNode) Type

func (n *IdentifierNode) Type() NodeType

Type returns the node type.

type IndexNode

type IndexNode struct {
	Index int
}

IndexNode represents an array index access.

func (*IndexNode) String

func (n *IndexNode) String() string

String returns the string representation.

func (*IndexNode) Type

func (n *IndexNode) Type() NodeType

Type returns the node type.

type Lexer

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

Lexer tokenizes query strings.

func NewLexer

func NewLexer(input string) *Lexer

NewLexer creates a new lexer for the input string.

func (*Lexer) Next

func (l *Lexer) Next() Token

Next returns the next token.

func (*Lexer) Peek

func (l *Lexer) Peek() Token

Peek returns the next token without consuming it.

type Node

type Node interface {
	Type() NodeType
	String() string
}

Node represents a node in the query AST.

type NodeType

type NodeType int

NodeType represents the type of AST node.

const (
	// NodeIdentifier represents a field name or identifier
	NodeIdentifier NodeType = iota
	// NodeIndex represents an array index access
	NodeIndex
	// NodeDot represents a dot notation access
	NodeDot
	// NodeRoot represents the root of the query
	NodeRoot
	// NodeWildcard represents a wildcard array access [*]
	NodeWildcard
)

type Parser

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

Parser parses query strings into AST.

func NewParser

func NewParser(input string) *Parser

NewParser creates a new parser for the input string.

func (*Parser) Parse

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

Parse parses the input and returns a Query AST.

type Query

type Query struct {
	Root *RootNode
}

Query represents a parsed query.

func ParseQuery

func ParseQuery(input string) (*Query, error)

ParseQuery is a convenience function that parses a query string.

func (*Query) String

func (q *Query) String() string

String returns the string representation of the query.

type RootNode

type RootNode struct {
	Child Node
}

RootNode represents the root of a query.

func (*RootNode) String

func (n *RootNode) String() string

String returns the string representation.

func (*RootNode) Type

func (n *RootNode) Type() NodeType

Type returns the node type.

type Token

type Token struct {
	Type  TokenType
	Value string
	Pos   int
}

Token represents a lexical token.

func Tokenize

func Tokenize(input string) []Token

Tokenize returns all tokens from the input string. This is a convenience function for testing.

func (Token) String

func (t Token) String() string

String returns the string representation of a token.

type TokenType

type TokenType int

TokenType represents the type of a token.

const (
	// TokenEOF represents end of input
	TokenEOF TokenType = iota
	// TokenIdentifier represents a field name
	TokenIdentifier
	// TokenDot represents a dot separator
	TokenDot
	// TokenLeftBracket represents '['
	TokenLeftBracket
	// TokenRightBracket represents ']'
	TokenRightBracket
	// TokenNumber represents a numeric value
	TokenNumber
	// TokenStar represents a wildcard '*'
	TokenStar
	// TokenError represents a lexing error
	TokenError
)

type Wildcard added in v0.3.0

type Wildcard struct{}

Wildcard is a sentinel value used in path segments to represent [*].

type WildcardNode added in v0.3.0

type WildcardNode struct{}

WildcardNode represents a wildcard array access [*].

func (*WildcardNode) String added in v0.3.0

func (n *WildcardNode) String() string

String returns the string representation.

func (*WildcardNode) Type added in v0.3.0

func (n *WildcardNode) Type() NodeType

Type returns the node type.

Jump to

Keyboard shortcuts

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