lesomnus / pl

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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]/;

About

License:Apache License 2.0


Languages

Language:Go 97.6%Language:Dockerfile 2.0%Language:Shell 0.4%