Skip to content

Commit a790918

Browse files
committed
String argument version
1 parent e98b1af commit a790918

4 files changed

Lines changed: 49 additions & 13 deletions

File tree

README.md

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,50 @@ Expression examples:
3131
3232
```Go
3333
// simple expression evaluation (error handling skipped)
34-
result, _ := xpression.Eval([]byte(`5 - 3 * (6-12)`))
34+
result, _ := xpression.EvalStr(`5-3*(6-12)`)
3535
fmt.Println(result.String())
3636
3737
// external data in expression (aka variables)
3838
foobar := 123
39+
varMapper := map[string]*int{
40+
`foobar`: &foobar,
41+
}
3942
varFunc := func(name []byte, result *xpression.Operand) error {
40-
mapper := map[string]*int{
41-
`foobar`: &foobar,
42-
}
43-
ref := mapper[string(name)]
43+
ref := varMapper[string(name)]
4444
if ref == nil {
4545
return errors.New("unknown variable")
4646
}
4747
result.SetNumber(float64(*ref))
4848
return nil
4949
}
50-
result, _ = xpression.EvalVar([]byte(`27 / foobar`), varFunc)
50+
result, _ = xpression.EvalVarStr(`27 / foobar`, varFunc)
5151
fmt.Println(result.String())
5252
```
53-
[Run in Go Playground](https://play.golang.com/p/5gqKN1DkCXp)
53+
[Run in Go Playground](https://play.golang.com/p/YaweDHqUJKa)
54+
55+
## Functions
56+
57+
```Go
58+
func Eval(expression []byte) (*Operand, error)
59+
```
60+
Eval evaluates expression and returns the result. No external variables used. See EvalVar for more.
61+
Returns a pointer to Operand or error.
62+
Use Operand's method `.String()` to get a serializable value or see Operand's `.OperandType` field to determine the result type and get the value from `.Str`, `.Number` or `.Bool` field.
63+
64+
```Go
65+
func EvalVar(expression []byte, varFunc VariableFunc) (*Operand, error)
66+
```
67+
`EvalVar` evaluates expression and returns the result. External variables can be used via `varFunc`.
68+
69+
```Go
70+
func EvalStr(expression string) (*Operand, error)
71+
```
72+
Same as `Eval` but receives a `string` instean of `[]byte`.
73+
74+
```Go
75+
func EvalVarStr(expression string, varFunc VariableFunc) (*Operand, error)
76+
```
77+
Same as `EvalVar` but receives a `string` instean of `[]byte`.
5478

5579
## xpression CLI
5680

@@ -123,6 +147,7 @@ ok github.com/Knetic/govaluate 9.810s
123147

124148
## Changelog
125149

150+
**0.9.4** (2022-11-28) -- Go 1.19 support. `EvalStr` and `EvalVarStr` helper functions added.
126151
**0.9.3** (2022-07-06) -- bugfixes: string to hex conversion `"0x123"*2`, closing bracket after a variable `(0+a)`.
127152
**0.9.2** (2022-02-23) -- Minor code refactoring. One-liner functions added (Eval, EvalVar).
128153
**0.9.1** (2022-01-02) -- Variable bounds refined.

eval.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ func Eval(expression []byte) (*Operand, error) {
99
return Evaluate(tokens, nil)
1010
}
1111

12+
// EvalStr is a wrapper for string expression.
13+
func EvalStr(expression string) (*Operand, error) {
14+
return Eval([]byte(expression))
15+
}
16+
1217
// EvalVar evaluates expression and returns the result. External variables can be used via varFunc.
1318
func EvalVar(expression []byte, varFunc VariableFunc) (*Operand, error) {
1419
tokens, err := Parse(expression)
@@ -17,3 +22,8 @@ func EvalVar(expression []byte, varFunc VariableFunc) (*Operand, error) {
1722
}
1823
return Evaluate(tokens, varFunc)
1924
}
25+
26+
// EvalStrVar evaluates expression and returns the result. External variables can be used via varFunc.
27+
func EvalVarStr(expression string, varFunc VariableFunc) (*Operand, error) {
28+
return EvalVar([]byte(expression), varFunc)
29+
}

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module github.com/bhmj/xpression
22

3-
go 1.17
3+
go 1.19
44

5-
require github.com/bhmj/readline v0.0.0-20220705162457-b109d6dec1af
5+
require github.com/bhmj/readline v0.1.0
66

77
require (
88
github.com/pkg/term v1.1.0 // indirect
9-
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009 // indirect
9+
golang.org/x/sys v0.2.0 // indirect
1010
)

go.sum

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
github.com/bhmj/readline v0.0.0-20220705162457-b109d6dec1af h1:qPvq5oXPg0lhWxqH8CyRqGMR8PhuTYZdTmbmRiWLePI=
2-
github.com/bhmj/readline v0.0.0-20220705162457-b109d6dec1af/go.mod h1:mT/+5iXLVS567ObbD2EUub7BS1hnb+o8Sz2CmVNJFf4=
1+
github.com/bhmj/readline v0.1.0 h1:j42+cSkBUlAhLVuF/zzuD6Bjvej0k7eA4vzb53lMGew=
2+
github.com/bhmj/readline v0.1.0/go.mod h1:mT/+5iXLVS567ObbD2EUub7BS1hnb+o8Sz2CmVNJFf4=
33
github.com/pkg/term v1.1.0 h1:xIAAdCMh3QIAy+5FrE8Ad8XoDhEU4ufwbaSozViP9kk=
44
github.com/pkg/term v1.1.0/go.mod h1:E25nymQcrSllhX42Ok8MRm1+hyBdHY0dCeiKZ9jpNGw=
5-
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009 h1:W0lCpv29Hv0UaM1LXb9QlBHLNP8UFfcKjblhVCWftOM=
65
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
6+
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
7+
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

0 commit comments

Comments
 (0)