2017-07-04 17:12:40 +02:00
|
|
|
package expression
|
2017-06-30 20:51:21 +02:00
|
|
|
|
2017-07-01 16:36:47 +02:00
|
|
|
type expressionWrapper struct {
|
2017-06-30 20:51:21 +02:00
|
|
|
fn func(ctx Context) (interface{}, error)
|
|
|
|
}
|
|
|
|
|
2017-07-01 16:36:47 +02:00
|
|
|
func (w expressionWrapper) Evaluate(ctx Context) (interface{}, error) {
|
2017-06-30 20:51:21 +02:00
|
|
|
return w.fn(ctx)
|
|
|
|
}
|
|
|
|
|
2017-07-01 16:36:47 +02:00
|
|
|
// Constant creates an expression that returns a constant value.
|
2017-06-30 20:51:21 +02:00
|
|
|
func Constant(k interface{}) Expression {
|
2017-07-01 16:36:47 +02:00
|
|
|
return expressionWrapper{
|
2017-06-30 20:51:21 +02:00
|
|
|
func(_ Context) (interface{}, error) {
|
|
|
|
return k, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-01 16:36:47 +02:00
|
|
|
// Not creates an expression that returns ! of the wrapped expression.
|
|
|
|
func Not(e Expression) Expression {
|
|
|
|
return expressionWrapper{
|
2017-06-30 20:51:21 +02:00
|
|
|
func(ctx Context) (interface{}, error) {
|
|
|
|
value, err := e.Evaluate(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return (value == nil || value == false), nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|