tree

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FuncGrep = "grep"
	FuncTmpl = "tmpl"
	FuncSh   = "sh"
	FuncLua  = "lua"
	FuncExpr = "expr"

	FuncToInt      = "to_int"
	FuncToFloat    = "to_float"
	FuncToBool     = "to_bool"
	FuncToString   = "to_string"
	FuncToTime     = "to_time"
	FuncToDuration = "to_duration"

	FuncLeast    = "least"
	FuncGreatest = "greatest"
	FuncCoalesce = "coalesce"

	FuncIf     = "if"
	FuncIfNull = "ifnull"
	FuncNullIf = "nullif"

	FuncAbs     = "abs"
	FuncSqrt    = "sqrt"
	FuncDegrees = "degrees"
	FuncRadians = "radians"
	FuncAcos    = "acos"
	FuncAsin    = "asin"
	FuncAtan    = "atan"
	FuncCos     = "cos"
	FuncSin     = "sin"
	FuncTan     = "tan"
	FuncCot     = "cot"
	FuncLn      = "ln"
	FuncLog2    = "log2"
	FuncLog10   = "log10"
	FuncExp     = "exp"
	FuncCeil    = "ceil"
	FuncFloor   = "floor"
	FuncRound   = "round"
	FuncAtan2   = "atan2"
	FuncPow     = "pow"
	FuncE       = "e"
	FuncPi      = "pi"
	FuncRand    = "rand"

	FuncLen           = "len"
	FuncSize          = "size"
	FuncRegexpCount   = "regexp_count"
	FuncRegexpInstr   = "regexp_instr"
	FuncRegexpSubstr  = "regexp_substr"
	FuncRegexpReplace = "regexp_replace"
	FuncRegexpLike    = "regexp_like"
	FuncFormat        = "format"
	FuncLower         = "lower"
	FuncUpper         = "upper"
	FuncSha2          = "sha2"
	FuncConcatWs      = "concat_ws"
	FuncInstr         = "instr"
	FuncInstrCount    = "instr_count"
	FuncSubstr        = "substr"
	FuncSubstrIndex   = "substr_index"
	FuncReplace       = "replace"
	FuncTrim          = "trim"

	FuncStrToTime  = "strtotime"
	FuncTimeFormat = "timeformat"
	FuncYear       = "year"
	FuncMonth      = "month"
	FuncDay        = "day"
	FuncHour       = "hour"
	FuncMinute     = "minute"
	FuncSecond     = "second"
	FuncDayOfWeek  = "dayofweek"
	FuncDayOfYear  = "dayofyear"
	FuncNewTime    = "newtime"
	FuncSleep      = "sleep"
	FuncNow        = "now"

	FuncDir       = "dir"
	FuncBasename  = "basename"
	FuncExtension = "extension"
	FuncAbsPath   = "abspath"
	FuncRelPath   = "relpath"

	FuncInverse = "inverse"
	FuncEnvOr   = "envor"
	FuncEnv     = "env"
)
View Source
const (
	InputTableName    = "input"
	TableKeySeparator = "___"
	NodeValueKey      = "___value___"
)
View Source
const (
	FuncArgMaxLen = 100
)

Variables

View Source
var (
	ErrIgnore               = errors.New("Ignore")
	ErrNotImplmented        = errors.New("NotImplemented")
	ErrInvalidTree          = errors.New("InvalidTree")
	ErrInvalidKey           = errors.New("InvalidKey")
	ErrInvalidValue         = errors.New("InvalidValue")
	ErrInvalidArgument      = errors.New("InvalidArgument")
	ErrInvalidFunctionArity = errors.New("InvalidFunctionArity")
	ErrParseGenResult       = errors.New("ParseGenResult")
	ErrGenTemplate          = errors.New("GenTemplate")
)

Functions

func AsChan added in v0.0.6

func AsChan(ctx context.Context, it NIter, n Node, concurrency int, recvC chan<- *N) error

func AsUnaryArgUnaryRetNodeDataFunction

func AsUnaryArgUnaryRetNodeDataFunction(f func(*OP) (*OP, error)) func(ND) (ND, error)

func AsVariadicArgUnaryRetNodeDataFunction

func AsVariadicArgUnaryRetNodeDataFunction(f func(...*OP) (*OP, error)) func(...ND) (ND, error)

func MapNodeDataFunction

func MapNodeDataFunction(name string, f NodeDataMapper) func(*N) (*N, error)

func NodeAsEnviron

func NodeAsEnviron(n *N) []string

func NodeAsStructuredMap

func NodeAsStructuredMap(n *N) map[string]any

func ValidateOnlyVariadicOrAllUnaryRet

func ValidateOnlyVariadicOrAllUnaryRet(fs ...NFunction) error

Types

type ExprGenTemplate

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

func NewExprGenTemplate

func NewExprGenTemplate(expr string) *ExprGenTemplate

func (ExprGenTemplate) Generate

func (g ExprGenTemplate) Generate(_ context.Context, n *N) ([]byte, error)

type GenTemplate

type GenTemplate interface {
	// @title Generator
	// @path syntax.generator
	// @document
	// A function that generates a new node from a node is called a generator.
	// It must return a string in one of the following formats:
	//
	// - An array of JSON objects
	// - A single JSON object
	// - An "equal pair" list
	//
	// The "equal pair" format is as follows:
	//
	// “`
	// key1=value11,key2=value12,...
	// key1=value21,key2=value22,...
	// ...
	//“`
	//
	// This is equivalent to the following JSON structure:
	//
	// “`
	// [
	//   {"key1":"value11","key2":"value12",...},
	//   {"key1":"value21","key2":"value22",...},
	//   ...
	// ]
	// “`
	//
	// Each JSON object corresponds to a single node.
	// Note that nodes are not required to have the same set of keys.
	Generate(ctx context.Context, n *N) ([]byte, error)
}

type Key

type Key struct {
	Table  string
	Column string
}

func KeyFromName

func KeyFromName(s string) *Key

func KeyFromString

func KeyFromString(s string) *Key

func NewKey

func NewKey(table, column string) *Key

func (Key) Get

func (k Key) Get(n *N) (*N, bool)

func (Key) NFunction

func (k Key) NFunction() NFunction

func (Key) Name

func (k Key) Name() string

func (Key) String

func (k Key) String() string

type LuaGenTemplate

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

func NewLuaGenTemplate

func NewLuaGenTemplate(script, entrypoint string) *LuaGenTemplate

func (LuaGenTemplate) Generate

func (g LuaGenTemplate) Generate(ctx context.Context, n *N) ([]byte, error)

type N

type N = node.Node

func GenerateAndParse

func GenerateAndParse(ctx context.Context, n *N, g GenTemplate) ([]*N, error)

func ParseGenResult

func ParseGenResult(b []byte) ([]*N, error)

Parse the result of GenTemplate.Generate.

type ND

type ND = node.Data

type NDFanoutFunction

type NDFanoutFunction = iterx.FanoutFunction[ND]

type NDFunction

type NDFunction = iterx.Function[ND]

type NDIter

type NDIter = iter.Seq[ND]

type NDMapFunction

type NDMapFunction = iterx.MapFunction[ND]

type NDMultiMapFunction

type NDMultiMapFunction = iterx.MultiMapFunction[ND]

type NDReduceFunction

type NDReduceFunction = iterx.ReduceFunction[ND]

type NFanoutFunction

type NFanoutFunction = iterx.FanoutFunction[*N]

type NFunction

type NFunction = iterx.Function[*N]

func ReturnContainerValue

func ReturnContainerValue(name string, f func(ND) (ND, error)) NFunction

Create a function that converts a ValueContainer.

type NIter

type NIter = iter.Seq[*N]

func AsIter

func AsIter(ctx context.Context, it NIter, n Node) (NIter, error)

AsIter converts AST into a function and applies to the iterator.

type NMapFunction

type NMapFunction = iterx.MapFunction[*N]

type NMultiMapFunction

type NMultiMapFunction = iterx.MultiMapFunction[*N]

type NReduceFunction

type NReduceFunction = iterx.ReduceFunction[*N]

type NodeDataMapper

type NodeDataMapper = func(string, ND) (string, ND, error)

func NodeOpMapperAsData

func NodeOpMapperAsData(f NodeOpMapper) NodeDataMapper

type NodeOpMapper

type NodeOpMapper = func(string, *OP) (string, *OP, error)

func NodeDataMapperAsOP

func NodeDataMapperAsOP(f NodeDataMapper) NodeOpMapper

type OP

type OP = node.Op

type RegexpGenTemplate

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

func NewRegexpGenTemplate

func NewRegexpGenTemplate(expr, tmpl string) *RegexpGenTemplate

func (RegexpGenTemplate) Generate

func (g RegexpGenTemplate) Generate(_ context.Context, n *N) ([]byte, error)

type ShellGenTemplate

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

func NewShellGenTemplate

func NewShellGenTemplate(text string) *ShellGenTemplate

func (ShellGenTemplate) Generate

func (g ShellGenTemplate) Generate(ctx context.Context, n *N) ([]byte, error)

type StringGenTemplate

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

### envor os.Getenv with default value, like 'envor "KEY" "default_value"'.

func NewStringGenTemplate

func NewStringGenTemplate(text string) *StringGenTemplate

func (StringGenTemplate) Generate

func (g StringGenTemplate) Generate(_ context.Context, n *N) ([]byte, error)

type TreeVisitor

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

@title Syntax @path syntax @document `ndql` uses a SQL-based syntax.

## Implementation Status

- Statements: Currently, only the SELECT statement is implemented. - Clauses: FROM and WHERE clauses are available. Other clauses (e.g., GROUP BY, ORDER BY, JOIN) are not yet supported. - Operators, Functions: Some operators and functions are not yet implemented. Even if implemented, the behavior may differ from standard SQL specifications.

## Operators

- `AND` - `OR` - `XOR` - `+` (binary) - `-` (binary) - `*` - `/` - `%` - `<<` - `>>` - `<` - `<=` - `=` - `<>` - `>=` - `>` - `CASE` - `IS NULL` - `IS TRUE` - `IS FALSE` - `REGEXP` - `LIKE` - `BETWEEN` - `-` (unary) - `~`

func NewTreeVisitor

func NewTreeVisitor(ctx context.Context) *TreeVisitor

func (TreeVisitor) Visit

func (v TreeVisitor) Visit(n Node) (NFunction, error)

func (TreeVisitor) VisitBetweenExpr

func (v TreeVisitor) VisitBetweenExpr(n *BetweenExpr) (NFunction, error)

func (TreeVisitor) VisitBinaryOperationExpr

func (v TreeVisitor) VisitBinaryOperationExpr(n *BinaryOperationExpr) (NFunction, error)

func (TreeVisitor) VisitCaseExpr

func (v TreeVisitor) VisitCaseExpr(n *CaseExpr) (NFunction, error)

func (TreeVisitor) VisitColumnName

func (TreeVisitor) VisitColumnName(n *ColumnName) *Key

func (TreeVisitor) VisitColumnNameExpr

func (v TreeVisitor) VisitColumnNameExpr(n *ColumnNameExpr) (NFunction, error)

func (TreeVisitor) VisitExpr

func (v TreeVisitor) VisitExpr(n ExprNode) (NFunction, error)

func (TreeVisitor) VisitFieldList

func (v TreeVisitor) VisitFieldList(n *FieldList) (NFunction, error)

func (TreeVisitor) VisitFuncCallExpr

func (v TreeVisitor) VisitFuncCallExpr(n *FuncCallExpr) (NFunction, error)

Function calls except aggregations.

@title Functions @path syntax.functions @document - grep(pattern: String, template: String) -> []Node - tmpl(template: String) -> []Node - sh(script: String) -> []Node - lua(script: String, entrypoint: String) -> []Node - expr(expression: String) -> []Node - to_int(value) -> Int - to_float(value) -> Float - to_bool(value) -> Bool - to_string(value) -> String - to_time(value) -> Time - to_duration(value) -> Duration - least(value...) - greatest(value...) - coalesce(value...) - if(condition, then, else) - ifnull(expr1, expr2) - nullif(expr1, expr2) - abs(value: Float | Int) -> Float - sqrt(value: Float | Int) -> Float - degrees(value: Float | Int) -> Float - radians(value: Float | Int) -> Float - acos(value: Float | Int) -> Float - asin(value: Float | Int) -> Float - atan(value: Float | Int) -> Float - cos(value: Float | Int) -> Float - sin(value: Float | Int) -> Float - tan(value: Float | Int) -> Float - cot(value: Float | Int) -> Float - ln(value: Float | Int) -> Float - log2(value: Float | Int) -> Float - log10(value: Float | Int) -> Float - exp(value: Float | Int) -> Float - ceil(value: Float | Int) -> Float - floor(value: Float | Int) -> Float - round(value: Float | Int) -> Float - atan2(y: Float | Int, x: Float | Int) -> Float - pow(x: Float | Int, y: Float | Int) -> Float - e() -> Float - pi() -> Float - rand() -> Float - len(value: String) -> Int - size(value: String) -> Int - regexp_count(string: String, pattern: String) -> Int - regexp_instr(string: String, pattern: String) -> Int - regexp_substr(string: String, pattern: String) -> Int - regexp_replace(string: String, pattern: String, replacement: String) -> String - regexp_like(string: String, pattern: String) -> Bool - format(format: String, args...) -> String - lower(value: String) -> String - upper(value: String) -> String - sha2(value: String) -> String - concat_ws(separator: String, args...: []String) -> String - instr(string: String, sub: String) -> Int - instr_count(string: String, sub: String) -> Int - substr(string: String, position: Int) -> String - substr(string: String, position: Int, length: Int) -> String - replace(string: String, from: String, to: String) -> String - trim(string: String) -> String - trim(string: String, cutset: String) -> String - strtotime(string: String, format: String) -> Time - timeformat(t: Time, format: String) -> String - year(t: Time) -> int - month(t: Time) -> int - day(t: Time) -> int - hour(t: Time) -> int - minute(t: Time) -> int - second(t: Time) -> int - dayofweek(t: Time) -> int - dayofyear(t: Time) -> int - newtime(year: Int) -> Time - newtime(year: Int, month: Int) -> Time - newtime(year: Int, month: Int, day: Int) -> Time - newtime(year: Int, month: Int, day: Int, hour: Int) -> Time - newtime(year: Int, month: Int, day: Int, hour: Int, minute: Int) -> Time - newtime(year: Int, month: Int, day: Int, hour: Int, minute: Int, second: Int) -> Time - sleep(second: Int | Float | Duration) -> Int - now() -> Time - dir(path: String) -> String - basename(path: String) -> String - extension(path: String) -> String - abspath(path: String) -> String - relpath(path: String, base: String) -> String - inverse(value: Float | Int) -> Float - inverse(value: String) -> String - env(name: String) -> String - envor(name: String, default: String) -> String

func (TreeVisitor) VisitIsNullExpr

func (v TreeVisitor) VisitIsNullExpr(n *IsNullExpr) (NFunction, error)

IS NULL, IS NOT NULL

func (TreeVisitor) VisitIsTruthExpr

func (v TreeVisitor) VisitIsTruthExpr(n *IsTruthExpr) (NFunction, error)

IS TRUE, IS FALSE

func (TreeVisitor) VisitJoin

func (v TreeVisitor) VisitJoin(n *Join) (NFunction, error)

func (TreeVisitor) VisitParenthesesExpr

func (v TreeVisitor) VisitParenthesesExpr(n *ParenthesesExpr) (NFunction, error)

(EXPR)

func (TreeVisitor) VisitParrernLikeExpr

func (v TreeVisitor) VisitParrernLikeExpr(n *PatternLikeOrIlikeExpr) (NFunction, error)

LIKE

func (TreeVisitor) VisitPatternInExpr

func (v TreeVisitor) VisitPatternInExpr(n *PatternInExpr) (NFunction, error)

BETWEEN ... AND ...

func (TreeVisitor) VisitPatternRegexpExpr

func (v TreeVisitor) VisitPatternRegexpExpr(n *PatternRegexpExpr) (NFunction, error)

REGEXP

func (TreeVisitor) VisitSelectField

func (v TreeVisitor) VisitSelectField(n *SelectField) (NFunction, error)

func (TreeVisitor) VisitSelectStmt

func (v TreeVisitor) VisitSelectStmt(n *SelectStmt) (NFunction, error)

func (TreeVisitor) VisitTableRefsClause

func (v TreeVisitor) VisitTableRefsClause(n *TableRefsClause) (NFunction, error)

func (TreeVisitor) VisitTableSource

func (v TreeVisitor) VisitTableSource(n *TableSource) (NFunction, error)

func (TreeVisitor) VisitUnaryOperationExpr

func (v TreeVisitor) VisitUnaryOperationExpr(n *UnaryOperationExpr) (NFunction, error)

func (TreeVisitor) VisitValueExpr

func (v TreeVisitor) VisitValueExpr(n ValueExpr) (NFunction, error)

func (TreeVisitor) VisitValueExprDriver

func (v TreeVisitor) VisitValueExprDriver(n *driver.ValueExpr) (ND, error)

func (TreeVisitor) VisitWhere

func (v TreeVisitor) VisitWhere(n ExprNode) (NFunction, error)

type ValueContainer

type ValueContainer struct {
	*N
}

A single value as a Node.

func AsValueContainer

func AsValueContainer(n *N) *ValueContainer

func (*ValueContainer) GetContainerValue

func (c *ValueContainer) GetContainerValue() (ND, bool)

func (*ValueContainer) GetFirstValue

func (c *ValueContainer) GetFirstValue() (string, ND, bool)

GetFirstValue unwraps the value. Returns the key, value and exists value or not.

func (*ValueContainer) SetContainerValue

func (c *ValueContainer) SetContainerValue(v ND)

Jump to

Keyboard shortcuts

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