evaluator

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2021 License: MIT Imports: 8 Imported by: 1

README

evaluator

Tag Software License test workflow Go Report Card GoDoc

A simple library for expression evaluation.

SYNOPSIS
    e, err := evaluator.New("(var1 + 0.5) * var2")
    if err != nil {
        log.Fatal(err)
    }
	ans, err := e.Eval(evaluator.Variables{"var1": 0.5, "var2": 3})
    if err != nil {
        log.Fatal(err)
    }
	fmt.Println(ans)

see godoc.org/github.com/mashiike/evaluator.

Author

Copyright (c) 2021 Mashiike.

LICENSE

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDivideByZero     = errors.New("divide by 0")
	ErrVariableNotFound = errors.New("variable not found")
)

reserved error

Functions

func IsDivideByZero added in v0.2.0

func IsDivideByZero(err error) bool

IsDivideByZero check error DivideByZero

func IsVariableNotFound added in v0.2.0

func IsVariableNotFound(err error) bool

IsVariableNotFound check error VariableNotFound

Types

type Comparator

type Comparator interface {
	// Compare performs an comparison by giving a set of variables.
	Compare(Variables) (bool, error)

	fmt.Stringer
}

Comparator is a special Evaluator whose evaluation expression is a comparison expression.

type Evaluator

type Evaluator interface {
	// Eval performs an evaluation by giving a set of variables.
	Eval(Variables) (interface{}, error)

	//Strict sets the expression evaluation to run in strict mode. The default is false.
	//For example, the behavior changes when the variable is not set.
	//  Referenced as nil if Strict is set to false.
	//  If set to true, the expression evaluation will be Error.
	Strict(bool)

	// AsComparator attempts to convert to Comparator
	AsComparator() (Comparator, bool)

	fmt.Stringer
}

Evaluator is a variable evaluator created based on one expression

func New

func New(expr string) (Evaluator, error)

New parses the expression to create an evaluator

type NumOfArgumentsMismatchError added in v0.4.0

type NumOfArgumentsMismatchError struct {
	FunctionName string
	Expected     int
	Given        int
}

NumOfArgumentsMismatchError is an error that occurs when the number of arguments of the called function is different.

func (*NumOfArgumentsMismatchError) Error added in v0.4.0

type Variables

type Variables map[string]interface{}

Variables are a group of variables given to the evaluator

Jump to

Keyboard shortcuts

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