queryparser

package
v0.9.3 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TokenFunc      = "FUNC"
	TokenValue     = "VALUE"
	TokenFuncClose = "FUNC-CLOSE"

	// RootFunc serves as a seed function that represents the initial state
	// when there is no parent function. It can be useful for indicating
	// the top level of a query structure.
	RootFunc = "ROOT"
)

Variables

This section is empty.

Functions

func AssertSliceType

func AssertSliceType[T any](input any) ([]T, error)

AssertSliceType asserts that each element of the input slice is of type T and returns a slice of type T.

func AssertType

func AssertType[T any](input any) (T, error)

AssertType attempts to assert the input to the specified type T

func IsValue

func IsValue(arg QueryArg) bool

IsValue checks if the provided QueryArg is of type QueryArgValue.

Types

type Parser

type Parser interface {
	// Parse takes an input query and a variadic number of parameters, processes the input
	// based on these parameters, and returns the parsed result, additional extracted
	// data, and any error encountered during parsing.
	//
	// # Parameters:
	//   - ctx: A context to manage deadlines, cancellations, and other request-scoped values.
	//   - input: The input to be parsed.
	//   - params: A variadic number of additional parameters that influence the parsing process.
	//
	// # Returns:
	//   - string: The parsed query.
	//   - []any: Additional data extracted during parsing.
	//   - error: An error object providing details if the parsing fails.
	Parse(ctx context.Context, input any, params ...string) (string, []any, error)
}

Parser defines an interface for parsing input queries with additional parameters within a given context, returning a parsed query, additional extracted data, and any errors encountered during the process.

type ParserOption

type ParserOption func(*queryParser) error

func WithFunctions

func WithFunctions(funcs QueryFuncSet) ParserOption

WithFunctions sets a list of query functions for the query parser. This allows for defining the set of functions that the query parser will use.

func WithParams

func WithParams(params []string) ParserOption

WithParams sets a list of query parameters for the query parser. This allows for a specified number of additional parameters that influence the parsing process by replacing value placeholders. Placeholders in the query input (e.g., func($1, $2)) will be replaced with the corresponding values from the provided parameters.

func WithTokenizer

func WithTokenizer(tokenizer Tokenizer) ParserOption

WithTokenizer sets a custom tokenizer for the query parser. This allows for overriding the default tokenization behavior with a user-provided tokenizer

type QueryArg

type QueryArg interface {
}

QueryArg is an interface that represents an argument in a query.

type QueryArgFunc

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

QueryArgFunc represents a function argument in a query.

func (*QueryArgFunc) Value

func (qa *QueryArgFunc) Value() *QueryFunc

Value returns the underlying QueryFunc associated with the QueryArgFunc.

type QueryArgValue

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

QueryArgValue represents a value argument in a query.

func (*QueryArgValue) Value

func (qa *QueryArgValue) Value() any

Value returns the value of the QueryArgValue.

type QueryFunc

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

QueryFunc represents a function in the query. It holds the function's name, its arguments, and the result of its execution.

func Parse

func Parse(ctx context.Context, input any, options ...ParserOption) (*QueryFunc, error)

Parse process the input query with the provided options and returns a structured QueryFuncHandler representation.

func (*QueryFunc) Args

func (qf *QueryFunc) Args() []QueryArg

A slice of QueryArg that contains the arguments passed to the function. Each argument can be a value or another function.

func (*QueryFunc) Name

func (qf *QueryFunc) Name() string

The name of the function as a string.

func (*QueryFunc) Result

func (qf *QueryFunc) Result() interface{}

Holds the result of the function execution. The type of this field can vary depending on the function's output.

func (*QueryFunc) SetResult

func (qf *QueryFunc) SetResult(res interface{})

Set the result of the function execution.

type QueryFuncHandler

type QueryFuncHandler struct {
	Invoke func(qf *QueryFunc) error
	UsedBy *Set[string]
}

QueryFuncHandler represents a query function that includes an invocation method and a set of allowed functions that can call this function.

type QueryFuncSet

type QueryFuncSet map[string]QueryFuncHandler

QueryFuncSet is a map of query function names to their corresponding QueryFuncHandler.

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set is a generic set implementation using a map where the keys are of type T and the values are empty structs to save memory. It includes a mutex for thread safety.

func NewSet

func NewSet[T comparable]() *Set[T]

NewSet creates and returns a new empty Set of type T.

func (*Set[T]) Add

func (s *Set[T]) Add(values ...T) *Set[T]

Add inserts one or more values into the Set. It returns the modified Set to allow for method chaining.

func (*Set[T]) Contains

func (s *Set[T]) Contains(value T) bool

Contains checks if a value exists in the Set.

func (*Set[T]) List

func (s *Set[T]) List() []T

List returns a slice containing all elements in the Set.

func (*Set[T]) Print

func (s *Set[T]) Print() string

Print returns a string representation of the Set, with elements separated by commas. It assumes T implements fmt.Stringer.

func (*Set[T]) Remove

func (s *Set[T]) Remove(value T)

Remove deletes a value from the Set.

func (*Set[T]) Size

func (s *Set[T]) Size() int

Size returns the number of elements in the Set.

type Token

type Token struct {
	Type  string
	Value any
}

Token represents a token in the input.

type TokenSet

type TokenSet []Token

TokenSet represents a collection of tokens.

func NewTokenSet

func NewTokenSet(cap ...int) TokenSet

NewTokenSet creates and initializes a new TokenSet with an optional capacity.

func Tokenize

func Tokenize(ctx context.Context, input string) (TokenSet, error)

Tokenize breaks the input into tokens. It recognizes function names and values.

Example Input: "AND(EQ(key1, val1), OR(NOTEQ(key2, val2), NOTEQ(key3, val3)))"

This input contains:

- Function names: AND, EQ, OR, NOTEQ

- Literals: key1, key2, key3, val1, val2, val3

The function will return tokens representing these elements, allowing further processing of the query.

func (TokenSet) AddFunctionToken

func (s TokenSet) AddFunctionToken(name string, addTokens func() TokenSet) TokenSet

AddFunctionToken adds a function token with the specified name to the TokenSet. If the addTokens function is provided, it will be called to add additional tokens between the function tokens.

func (TokenSet) AddValueToken

func (s TokenSet) AddValueToken(value any) TokenSet

AddValueToken adds a value token with the specified value to the TokenSet.

func (TokenSet) Append

func (s TokenSet) Append(sets ...TokenSet) TokenSet

Append appends the tokens from multiple TokenSets to the current TokenSet.

func (TokenSet) IsEmpty

func (s TokenSet) IsEmpty() bool

IsEmpty checks if the TokenSet is empty.

func (TokenSet) Matches

func (s TokenSet) Matches(dest TokenSet) bool

Matches checks if the current TokenSet matches another TokenSet.

type Tokenizer

type Tokenizer interface {
	Tokenize(ctx context.Context, input any) (TokenSet, error)
}

Tokenizer defines the interface for tokenizing an input into a TokenSet. The Tokenize method takes a context and an input, and returns a TokenSet or an error.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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