expr

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2024 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExprPredicate added in v0.0.6

func ExprPredicate(code string) (engine.Predicate, error)

func FilterFlow added in v0.0.6

func FilterFlow(ctx *engine.Context) engine.Flow
Example
package main

import (
	"github.com/OutOfBedlam/tine/engine"
	_ "github.com/OutOfBedlam/tine/plugins/base"
	_ "github.com/OutOfBedlam/tine/plugins/expr"
)

func main() {
	recipe := `
	[[inlets.file]]
		data = [
			"a,100",
			"b,200",
			"c,300",
		]
		format = "csv"
		fields = ["name", "rec.value"]
		types  = ["string", "int"]
	[[flows.filter]]
		predicate = "${ rec.value } > 100"
	[[outlets.file]]
		path = "-"
		format = "json"
`
	pipe, err := engine.New(engine.WithConfig(recipe))
	if err != nil {
		panic(err)
	}
	err = pipe.Run()
	if err != nil {
		panic(err)
	}

}
Output:

{"name":"b","rec.value":200}
{"name":"c","rec.value":300}

func MapFlow added in v0.0.6

func MapFlow(ctx *engine.Context) engine.Flow
Example
package main

import (
	"github.com/OutOfBedlam/tine/engine"
	_ "github.com/OutOfBedlam/tine/plugins/base"
	_ "github.com/OutOfBedlam/tine/plugins/expr"
)

func main() {
	recipe := `
	[[inlets.file]]
		data = [
			"a,100,1.234,1724136681,true",
			"b,200,2.345,1724136682,false",
			"c,300,3.456,1724136683,true",
		]
		format = "csv"
		fields = ["name", "rec.value", "fval", "tval", "flag"]
		types  = ["string", "int", "float", "time", "bool"]
	[[flows.map]]
		code = "rec.value = ${ rec.value } * 2"
	[[flows.map]]
		code = "fval = ${ fval } * 2"
	[[flows.map]]
		code = "flag = !${ flag }"
	[[flows.map]]
		predicate = "${ name } == 'a' || ${name} == 'c'"
		code = "name = 'hello ' + ${ name }"
	[[flows.map]]
		code = '''printf("hello world %v\n", ${rec.value})'''
	[[outlets.file]]
		path = "-"
		format = "json"
`
	pipe, err := engine.New(engine.WithConfig(recipe))
	if err != nil {
		panic(err)
	}
	err = pipe.Run()
	if err != nil {
		panic(err)
	}

}
Output:

hello world 200
hello world 400
hello world 600
{"flag":false,"fval":2.468,"name":"hello a","rec.value":200,"tval":1724136681}
{"flag":true,"fval":4.69,"name":"b","rec.value":400,"tval":1724136682}
{"flag":false,"fval":6.912,"name":"hello c","rec.value":600,"tval":1724136683}

Types

type Translated added in v0.0.6

type Translated struct {
	OriginalCode   string
	Code           string
	ReferredFields []string
	ReferredVars   []string
}

func Translate added in v0.0.6

func Translate(code string) *Translated

func (*Translated) EmptyEnv added in v0.0.6

func (trans *Translated) EmptyEnv() map[string]any

func (*Translated) RecordEnv added in v0.0.6

func (trans *Translated) RecordEnv(record engine.Record) (map[string]any, error)

Jump to

Keyboard shortcuts

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