pl

package module
v0.0.0-...-0453503 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2022 License: Apache-2.0 Imports: 7 Imported by: 2

README

pipeline

test Go Report Card codecov

Single line expression inspired by pipeline in text/template. A pipeline is a sequence of functions separated by |. Functions can take arguments, and the result of the previous function is passed to the last argument of the next function. The first word of a pipeline element is the name of the function, and the following words become the function's arguments. Pipeline can be nested by wrapping them with (...) in argument position.

Usage

executor := pl.NewExecutor()
executor.Funcs["sum"] = func(vs ...int) int {
	rst := 0
	for _, v := range vs {
		rst += v
	}

	return rst
}

rst, err := executor.ExecuteExpr("(sum 1 2 (sum 3 | sum (sum $.answer 5) 6) 7 (sum 8) | sum 9 10)", struct{ Answer int }{Answer: 42})
if err != nil {
	panic(err)
}

// v == 93
v, ok := rst.(int)
if !ok {
	panic("expected v to be of type int.")
} else if v != 93 {
	panic("expected v to be 93")
}

Syntax

pipeline = '(', function, { '|', function }, ')';
function = name, { { ' ' }*, argument };
name     = identifier;
argument = string | number | reference | pipeline;

identifier = letter, { letter | digit | '_' }*;
string     = '"', ? printable characters ?, '"';
number     = integer | floating_point;
reference  = '$', { reference_part }*;

integer        = [ '-' | '+' ], { digit }*;
floating_point = integer, [ '.', { digit }* ];
reference_part = '[', integer, ']' | '.', identifier;

letter = /[a-zA-Z]/;
digit  = /[0-9]/;

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("not found")

Functions

func Resolve

func Resolve(data any, ref Ref) (any, error)

Types

type Arg

type Arg struct {
	String *string  `parser:"  @String"`
	Float  *float64 `parser:"| @Float"`
	Int    *int     `parser:"| @Int"`
	Ref    Ref      `parser:"| '$' @@+"`
	Nested *Pl      `parser:"| @@"`
}

func NewArgs

func NewArgs(args ...interface{}) ([]*Arg, error)

type ConvMap

type ConvMap map[reflect.Type]map[reflect.Type](func(v reflect.Value) (any, error))

func NewConvMap

func NewConvMap() ConvMap

func (ConvMap) Convert

func (m ConvMap) Convert(out reflect.Type, in reflect.Type, v reflect.Value) (any, error)

func (ConvMap) ConvertTo

func (m ConvMap) ConvertTo(out reflect.Type, in any) (any, error)

func (ConvMap) MergeWith

func (m ConvMap) MergeWith(other ConvMap)

func (ConvMap) Set

func (m ConvMap) Set(from reflect.Type, to reflect.Type, conv func(v reflect.Value) (any, error))

type Executor

type Executor struct {
	Funcs FuncMap
	Convs ConvMap
}

func NewExecutor

func NewExecutor() *Executor

func (*Executor) Execute

func (e *Executor) Execute(pl *Pl, data any) ([]any, error)

func (*Executor) ExecuteExpr

func (e *Executor) ExecuteExpr(expr string, data any) ([]any, error)

type Fn

type Fn struct {
	Name string `parser:"@Ident"`
	Args []*Arg `parser:"@@*"`
}

func NewFn

func NewFn(name string, args ...interface{}) (*Fn, error)

type FuncMap

type FuncMap map[string]any

func NewFuncMap

func NewFuncMap() FuncMap

type Pl

type Pl struct {
	Funcs []*Fn `parser:"'(' ( @@ ( '|' @@ )* )? ')'"`
}

func NewPl

func NewPl(fs ...*Fn) *Pl

func ParseString

func ParseString(expr string) (*Pl, error)

type Ref

type Ref []RefKey

func NewRef

func NewRef(entries ...interface{}) (Ref, error)

func (Ref) String

func (r Ref) String() string

type RefKey

type RefKey struct {
	Name  *string `parser:"  (('.' @(Ident|String)) | ('[' @(Ident|String) ']'))"`
	Index *int    `parser:"| '[' @Int ']'"`
}

func (*RefKey) String

func (k *RefKey) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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