el

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2018 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package el provides an interface and default implementation of expression language (EL) interpreter for struct tags.

Default implementation is simply based on "text/template".

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	// Name of the currently processed field.
	Name string
	// Name of the currently processed field including type.
	LongName string
	// Current value of the currently processed filed.
	Val interface{}
	// All other tags of the currently processed field.
	Tags map[string]string
	// Currently processed struct.
	Struct interface{}
	// Extra context structure.
	Extra interface{}
	// Temporary partial result evaluated on the current substruct.
	Sub interface{}
	// Function, which knows how to evaluate expression with different
	// interpreter.
	EvalExpr EvalExprFunc
}

Context is a context, passed to interpreter. It contains information about currently processed field, struct and extra.

type DefaultInterpreter

type DefaultInterpreter struct {
	// Custom functions, available for use in EL expressions.
	Funcs use.FuncMap
	// Left delimiter for templates.
	LeftDelim string
	// Right delimiter for templates.
	RightDelim string
	// Automatically enclose passed expression into delimiters before
	// interpretation (if it is not already enclosed). This allows to pass
	// simplified expressions. For example, `atoi "42"` instead of
	// `{{atoi "42"}}`.
	AutoEnclose bool
}

DefaultInterpreter is a default implementation of Interpreter, which is based on "text/template".

Besides Funcs passed to it, DefaultInterpreter creates these additional custom function for use in EL expressions:

  • set
  • eval

Function "set" with signature `func (r interface{}) interface{}` passes argument to result, but stores it internally to be used as an expression result. It's intention is to set expression result to some concrete type, other than string (which is only type "text/template" allows by design). Simply put, when you interested in interface{} result and not in it's default string representation, use "set" to store result at the end of expression.

Function "eval" with signature `func(intrpr, expr string) (interface{}, error)` can be used to evaluate given expression with given interpreter. This can be useful to evaluate expression passed as a string within context. This feature works with support of the calling code, which passes actual implementation of the "eval" function in Context.EvalExpr(). Interpreter name should be known to calling code. For example, for known implementation (structor.NewEvaluator()) interpreter name is a tag name, onto which given interpreter is mapped during creation of evaluator.

Restrictions of "text/template" package applied to custom functions.

func (*DefaultInterpreter) Execute

func (i *DefaultInterpreter) Execute(
	expression string,
	ctx *Context) (interface{}, error)

Execute implements Interpreter.Execute()

type EvalExprFunc added in v1.1.0

type EvalExprFunc func(
	interpreterName, expression string,
	context *Context) (interface{}, error)

EvalExprFunc is a type of function, which knows how to evaluate given expression using given interpreter name and context. It is used to implement special predefined custom function "eval", available within EL.

type Interpreter

type Interpreter interface {
	// Execute parses and executes expression with a given context.
	Execute(expression string, ctx *Context) (result interface{}, err error)
}

Interpreter is an interface of EL interpreter.

type InterpreterFunc added in v1.1.3

type InterpreterFunc func(string, *Context) (interface{}, error)

The InterpreterFunc type is an adapter to allow the use of ordinary functions as EL interpreter. If f is a function with the appropriate signature, InterpreterFunc(f) is a Interpreter that calls f.

func (InterpreterFunc) Execute added in v1.1.3

func (f InterpreterFunc) Execute(s string, ctx *Context) (interface{}, error)

Execute calls f(s, ctx).

Directories

Path Synopsis
Package goel provides an implementation of expression language (EL) interpreter for struct tags.
Package goel provides an implementation of expression language (EL) interpreter for struct tags.

Jump to

Keyboard shortcuts

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