Skip to content

Commit 8712824

Browse files
Elliotantonmedv
authored andcommitted
Move conf.Options out of internal package
1 parent ffa18bb commit 8712824

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

expr.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ import (
1212
"github.com/antonmedv/expr/vm"
1313
)
1414

15+
// Option for configuring config.
16+
type Option func(c *conf.Config)
17+
1518
// Eval parses, compiles and runs given input.
1619
func Eval(input string, env interface{}) (interface{}, error) {
17-
if _, ok := env.(conf.Option); ok {
20+
if _, ok := env.(Option); ok {
1821
return nil, fmt.Errorf("misused expr.Eval: second argument (env) should be passed without expr.Env")
1922
}
2023

@@ -41,7 +44,7 @@ func Eval(input string, env interface{}) (interface{}, error) {
4144
// as well as all fields of embedded structs and struct itself.
4245
// If map is passed, all items will be treated as variables.
4346
// Methods defined on this type will be available as functions.
44-
func Env(i interface{}) conf.Option {
47+
func Env(i interface{}) Option {
4548
return func(c *conf.Config) {
4649
if _, ok := i.(map[string]interface{}); ok {
4750
c.MapEnv = true
@@ -51,42 +54,42 @@ func Env(i interface{}) conf.Option {
5154
}
5255

5356
// Operator allows to override binary operator with function.
54-
func Operator(operator string, fn ...string) conf.Option {
57+
func Operator(operator string, fn ...string) Option {
5558
return func(c *conf.Config) {
5659
c.Operators[operator] = append(c.Operators[operator], fn...)
5760
}
5861
}
5962

6063
// AsBool tells the compiler to expect boolean result.
61-
func AsBool() conf.Option {
64+
func AsBool() Option {
6265
return func(c *conf.Config) {
6366
c.Expect = reflect.Bool
6467
}
6568
}
6669

6770
// AsInt64 tells the compiler to expect int64 result.
68-
func AsInt64() conf.Option {
71+
func AsInt64() Option {
6972
return func(c *conf.Config) {
7073
c.Expect = reflect.Int64
7174
}
7275
}
7376

7477
// AsFloat64 tells the compiler to expect float64 result.
75-
func AsFloat64() conf.Option {
78+
func AsFloat64() Option {
7679
return func(c *conf.Config) {
7780
c.Expect = reflect.Float64
7881
}
7982
}
8083

8184
// Optimize turns optimizations on or off.
82-
func Optimize(b bool) conf.Option {
85+
func Optimize(b bool) Option {
8386
return func(c *conf.Config) {
8487
c.Optimize = b
8588
}
8689
}
8790

8891
// Compile parses and compiles given input expression to bytecode program.
89-
func Compile(input string, ops ...conf.Option) (*vm.Program, error) {
92+
func Compile(input string, ops ...Option) (*vm.Program, error) {
9093
config := &conf.Config{
9194
Operators: make(map[string][]string),
9295
Optimize: true,
@@ -128,7 +131,7 @@ func Compile(input string, ops ...conf.Option) (*vm.Program, error) {
128131
// Parse parses input string to a program.
129132
//
130133
// Deprecated: use expr.Compile instead.
131-
func Parse(input string, ops ...conf.Option) (*vm.Program, error) {
134+
func Parse(input string, ops ...Option) (*vm.Program, error) {
132135
return Compile(input, ops...)
133136
}
134137

expr_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,17 @@ func TestExpr_fetch_from_func(t *testing.T) {
824824
assert.Contains(t, err.Error(), "cannot fetch Value from func()")
825825
}
826826

827+
func TestExpr_reference_options(t *testing.T) {
828+
options := []expr.Option{expr.Env(map[string]interface{}{})}
829+
830+
e, err := expr.Compile("'hello world'", options...)
831+
assert.NoError(t, err)
832+
833+
output, err := expr.Run(e, "'hello world'")
834+
assert.NoError(t, err)
835+
assert.Equal(t, "hello world", output)
836+
}
837+
827838
//
828839
// Mock types
829840
//

internal/conf/config.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,3 @@ func (c *Config) Check() error {
4747
}
4848
return nil
4949
}
50-
51-
// Option for configuring config.
52-
type Option func(c *Config)

0 commit comments

Comments
 (0)