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 ¶
- func ClearCache()
- func Execute(data interface{}, queryStr string) (interface{}, error)
- func ExecuteCached(data interface{}, queryStr string) (interface{}, error)
- func ExecutePath(data interface{}, path []interface{}) (interface{}, error)
- func FormatTokens(tokens []Token) string
- func PathFromQuery(queryStr string) ([]interface{}, error)
- func SimplifyNode(node Node) []interface{}
- func TokenTypeName(t TokenType) string
- type DotNode
- type IdentifierNode
- type IndexNode
- type Lexer
- type Node
- type NodeType
- type Parser
- type Query
- type RootNode
- type Token
- type TokenType
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 ExecuteCached ¶
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 ¶
FormatTokens formats a slice of tokens for debugging.
func PathFromQuery ¶
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 ¶
TokenTypeName returns the name of a token type.
Types ¶
type DotNode ¶
DotNode represents a dot notation access.
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.
type IndexNode ¶
type IndexNode struct {
Index int
}
IndexNode represents an array index access.
type Lexer ¶
type Lexer struct {
// contains filtered or unexported fields
}
Lexer tokenizes query strings.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser parses query strings into AST.
type Query ¶
type Query struct {
Root *RootNode
}
Query represents a parsed query.
func ParseQuery ¶
ParseQuery is a convenience function that parses a query string.
type RootNode ¶
type RootNode struct {
Child Node
}
RootNode represents the root of a query.
type Token ¶
Token represents a lexical 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 // TokenError represents a lexing error TokenError )