evalexpr

package module
v0.0.0-...-45bf3f5 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2020 License: Apache-2.0 Imports: 2 Imported by: 0

README

go-eval-expr

A go library to evaluate expressions backed by data

Go Eval Expr allows you to define different sources to evaluate expressions:

This example shows where you define one value from prometheus while another from memory:

// -1 means no expiration period
cachedSources := sources.NewCachedSources(-1, 10*time.Minute)

limitValue := float64(1500)

evaluator := &evalexpr.Evaluator{
  Expression: "value_from_prometheus > comparison_value",
  Sources:    cachedSources,
}

cachedSources.SetSource("value_from_prometheus", sources.NewPrometheusSource(
  "http://demo.robustperception.io:9090",
  "rate(prometheus_tsdb_head_samples_appended_total[5m])",
), 0)

cachedSources.SetSource("comparison_value", sources.NewMemorySource(
  limitValue,
), 0)

output, env, err := evaluator.Eval()
// output is true
assert.Nil(t, err)
assert.Equal(t, true, output)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EvalWithSources

func EvalWithSources(expression string, sources Sources) (interface{}, map[string]interface{}, error)

Types

type Evaluator

type Evaluator struct {
	Expression string
	Sources    Sources
}

func (*Evaluator) Eval

func (e *Evaluator) Eval() (interface{}, map[string]interface{}, error)

type Source

type Source interface {
	GetValue() (interface{}, error)
}

type Sources

type Sources interface {
	GetSource(string) (Source, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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