goexpr

package module
v0.0.0-...-724f3ca Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2022 License: MIT Imports: 7 Imported by: 0

README

Build Coverage Go Report

Goexpr

Goexpr provides support for evaluating expressions with parameters, arimethetic, logical, and string operations.

  • basic expression: 1 > 0
  • parameterized expression: x > 0
  • nested parameterized expression: a.b > 0
  • arithmetic expression: (x * y / 100) >= 50
  • string expression: real == "expected"
  • float64 expression: (part / total) * 100

Installation

When used with Go modules, use the following import path:

go get github.com/leonests/goexpr

Quickstart

Example 1: Simple Usage Without Parameters

import "github.com/leonests/goexpr"

expr, err := goexpr.NewExpr("1 > 0")
result, err := expr.Eval(nil)
// result is true.

Example 2: Simple Usage With Parameters

import "github.com/leonests/goexpr"

expr, err := goexpr.NewExpr(`(x * y / 100) >= 50`)
param := map[string]interface{}{ "x": 100, "y": 50}
result, err := expr.Eval(parametes)
// result is true.

Advanced

Bracket Accessor

Dot Accessor

Method

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Expr

type Expr struct {
	// contains filtered or unexported fields
}

func NewExpr

func NewExpr(expr string) (res *Expr, err error)

func (*Expr) Eval

func (expr *Expr) Eval(params map[string]interface{}) (interface{}, error)

type ExprFunc

type ExprFunc func(args ...interface{}) (interface{}, error)

ExprFunc represents a function that can be called within an expression

type LexerToken

type LexerToken struct {
	Type  TokenType
	Value interface{}
}

type TokenType

type TokenType int //enum
const (
	ILLEGAL TokenType = iota

	// literal operators
	CHAR     // 'a', '爱', ...
	STRING   // "abc"
	NUMBER   // 123, 123.456 treated as float64
	BOOL     // true, false
	VARIABLE // a1, b_2, c
	SELECTOR // a.b.c,
	ACCESSOR // .a.b

	// prefix operators
	NOT // !
	NEG // -

	// normal operators
	ADD // +
	SUB // -
	MUL // *
	QUO // /
	REM // %

	AND // &
	OR  // |
	XOR // ^
	SHL // <<
	SHR // >>

	// ternary operators
	TERNARY_IF
	TERNARY_ELSE

	// logical operators
	LAND // &&
	LOR  // ||

	// comparer operators
	EQ  // ==
	NEQ // !=
	LT  // <
	GT  // >
	LEQ // <=
	GEQ // >=

	// clause operators
	LPAREN // (
	RPAREN // )

	LBRACKET // [
	RBRACKET // ]

	FUNC // represent function

	LITERAL // represent all literal operators
	CLAUSE  // represent all clause operators
	EOF
)

func (TokenType) Priority

func (op TokenType) Priority() opPriority

func (TokenType) String

func (t TokenType) String() string

Jump to

Keyboard shortcuts

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