ql

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// PfLValue the parameter can be a lvalue in the condition
	PfLValue = 1 << 0
	// PfRValue the parameter can be a rvalue in the condition
	PfRValue = 1 << 1
	// PfNop the parameter cannot have any operation
	PfNop = 1 << 2
	// PfComparable the parameter can be compared: <, >, !=, =, >=, <=
	PfComparable = 1 << 3
	// PfInLike the IN or LIKE operations are allowed for the param
	PfInLike = 1 << 4
)
View Source
const (
	StringParamID = "__string__"
	NumberParamID = "__number__"
	ArrayParamID  = "__array__"
)

Variables

View Source
var (
	LogsCondDialect = map[string]ParamDialect[*solaris.Log, any]{
		NumberParamID: {
			Flags: PfRValue | PfComparable,
			Value: func(p *Param, log *solaris.Log) (any, error) {
				return p.Const.Value(), nil
			},
		},
		StringParamID: {
			Flags: PfRValue | PfComparable,
			Value: func(p *Param, log *solaris.Log) (any, error) {
				return p.Const.Value(), nil
			},
		},
		ArrayParamID: {
			Flags: PfRValue,
			Value: func(p *Param, log *solaris.Log) (any, error) {
				var strArr []string
				for _, elem := range p.Array {
					strArr = append(strArr, elem.Value())
				}
				return strArr, nil
			},
		},
		"id": {
			Flags: PfLValue | PfComparable | PfInLike,
			Value: func(p *Param, log *solaris.Log) (any, error) {
				return log.ID, nil
			},
		},
		"tag": {
			Flags: PfLValue | PfComparable | PfRValue | PfInLike,
			Value: func(p *Param, log *solaris.Log) (any, error) {
				if p.Function == nil {
					return "", fmt.Errorf("tag must be a function: %w", errors.ErrInvalid)
				}
				if len(p.Function.Params) != 1 {
					return "", fmt.Errorf("tag() function expects only one parameter - the name of the tag: %w", errors.ErrInvalid)
				}
				if p.Function.Params[0].ID() != StringParamID {
					return "", fmt.Errorf("tag() function expects the tag name (string) as the parameter: %w", errors.ErrInvalid)
				}
				if len(log.Tags) == 0 {
					return "", nil
				}
				return log.Tags[p.Function.Params[0].Name()], nil
			},
		},
	}
)

Functions

This section is empty.

Types

type Condition

type Condition struct {
	FirstParam  Param  `  @@`
	Op          string ` {@("<"|">"|">="|"<="|"!="|"="|"IN"|"LIKE")`
	SecondParam *Param ` @@}`
}

Condition is a unary or binary logical operation which has first mandatory param and optional operation and second param

type Const

type Const struct {
	Number float32 ` @Number`
	String string  ` | @String`
}

Const contains the constant either string or float32 value

func (Const) Value

func (c Const) Value() string

Value returns string value of the constant

type Expression

type Expression struct {
	Or []*OrCondition `@@ { "OR" @@ }`
}

Expression is an AST element which describes a series of OR conditions

func Parse

func Parse(expr string) (*Expression, error)

Parse parses the expr and in case of success returns AST

type Function

type Function struct {
	Name   string   ` @Ident `
	Params []*Param ` "(" (@@ {"," @@})? ")"`
}

Function is a functional parameter

type OrCondition

type OrCondition struct {
	And []*XCondition `@@ { "AND" @@ }`
}

OrCondition is an AST element which describes a series of AND conditions

type Param

type Param struct {
	Const      *Const    ` @@`
	Function   *Function ` | @@`
	Identifier string    ` | @Ident`
	Array      []*Const  `|"[" (@@ {"," @@})?"]"`
}

Param describes a parameter either a constant (string or number), function, identifier or an array of constants

func (Param) ID

func (p Param) ID() string

ID returns the param id by its type: - string: StringParamID - number: NumberParamID - function: the function name - identifier: the identifier name - array: ArrayParamID

func (Param) Name

func (p Param) Name() string

Name returns "value" of the constants (strings, numbers and the arrays) and names for the functions and identifiers

type ParamDialect

type ParamDialect[T, V any] struct {
	Flags int
	Value func(p *Param, obj T) (any, error)
}

type XCondition

type XCondition struct {
	Not  bool        ` [@"NOT"] `
	Cond *Condition  `( @@`
	Expr *Expression `| "(" @@ ")")`
}

XCondition is an AST element which groups either a Condition object or an Expression object

Jump to

Keyboard shortcuts

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