Skip to content

Commit 226bdb0

Browse files
committed
Add AsKind and AsInt func
1 parent 9c67ada commit 226bdb0

File tree

6 files changed

+73
-45
lines changed

6 files changed

+73
-45
lines changed

checker/checker.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func Check(tree *parser.Tree, config *conf.Config) (t reflect.Type, err error) {
3131

3232
if v.config.Expect != reflect.Invalid {
3333
switch v.config.Expect {
34-
case reflect.Int64, reflect.Float64:
34+
case reflect.Int, reflect.Int64, reflect.Float64:
3535
if !isNumber(t) {
3636
return nil, fmt.Errorf("expected %v, but got %v", v.config.Expect, t)
3737
}

compiler/compiler.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ func Compile(tree *parser.Tree, config *conf.Config) (program *Program, err erro
3737
c.compile(tree.Node)
3838

3939
switch c.cast {
40-
case reflect.Int64:
40+
case reflect.Int:
4141
c.emit(OpCast, 0)
42-
case reflect.Float64:
42+
case reflect.Int64:
4343
c.emit(OpCast, 1)
44+
case reflect.Float64:
45+
c.emit(OpCast, 2)
4446
}
4547

4648
program = &Program{

compiler/compiler_test.go

-26
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@ package compiler_test
22

33
import (
44
"math"
5-
"reflect"
65
"testing"
76

87
"github.com/antonmedv/expr"
9-
"github.com/antonmedv/expr/compiler"
10-
"github.com/antonmedv/expr/conf"
11-
"github.com/antonmedv/expr/parser"
128
"github.com/antonmedv/expr/vm"
139
"github.com/antonmedv/expr/vm/runtime"
1410
"github.com/stretchr/testify/assert"
@@ -242,25 +238,3 @@ func TestCompile(t *testing.T) {
242238
assert.Equal(t, test.program.Disassemble(), program.Disassemble(), test.input)
243239
}
244240
}
245-
246-
func TestCompile_cast(t *testing.T) {
247-
input := `1`
248-
expected := &vm.Program{
249-
Constants: []interface{}{
250-
1,
251-
},
252-
Bytecode: []vm.Opcode{
253-
vm.OpPush,
254-
vm.OpCast,
255-
},
256-
Arguments: []int{0, 1},
257-
}
258-
259-
tree, err := parser.Parse(input)
260-
require.NoError(t, err)
261-
262-
program, err := compiler.Compile(tree, &conf.Config{Expect: reflect.Float64})
263-
require.NoError(t, err)
264-
265-
assert.Equal(t, expected.Disassemble(), program.Disassemble())
266-
}

expr.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,35 @@ func ConstExpr(fn string) Option {
7575
}
7676
}
7777

78-
// AsBool tells the compiler to expect boolean result.
78+
// AsKind tells the compiler to expect kind of the result.
79+
func AsKind(kind reflect.Kind) Option {
80+
return func(c *conf.Config) {
81+
c.Expect = kind
82+
}
83+
}
84+
85+
// AsBool tells the compiler to expect a boolean result.
7986
func AsBool() Option {
8087
return func(c *conf.Config) {
8188
c.Expect = reflect.Bool
8289
}
8390
}
8491

85-
// AsInt64 tells the compiler to expect int64 result.
92+
// AsInt tells the compiler to expect an int result.
93+
func AsInt() Option {
94+
return func(c *conf.Config) {
95+
c.Expect = reflect.Int
96+
}
97+
}
98+
99+
// AsInt64 tells the compiler to expect an int64 result.
86100
func AsInt64() Option {
87101
return func(c *conf.Config) {
88102
c.Expect = reflect.Int64
89103
}
90104
}
91105

92-
// AsFloat64 tells the compiler to expect float64 result.
106+
// AsFloat64 tells the compiler to expect a float64 result.
93107
func AsFloat64() Option {
94108
return func(c *conf.Config) {
95109
c.Expect = reflect.Float64

expr_test.go

+48-12
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,24 @@ func ExampleEnv_tagged_field_names() {
135135
// Output : Hello World
136136
}
137137

138+
func ExampleAsKind() {
139+
program, err := expr.Compile("{a: 1, b: 2}", expr.AsKind(reflect.Map))
140+
if err != nil {
141+
fmt.Printf("%v", err)
142+
return
143+
}
144+
145+
output, err := expr.Run(program, nil)
146+
if err != nil {
147+
fmt.Printf("%v", err)
148+
return
149+
}
150+
151+
fmt.Printf("%v", output)
152+
153+
// Output: map[a:1 b:2]
154+
}
155+
138156
func ExampleAsBool() {
139157
env := map[string]int{
140158
"foo": 0,
@@ -169,8 +187,8 @@ func ExampleAsBool_error() {
169187
// Output: expected bool, but got int
170188
}
171189

172-
func ExampleAsFloat64() {
173-
program, err := expr.Compile("42", expr.AsFloat64())
190+
func ExampleAsInt() {
191+
program, err := expr.Compile("42", expr.AsInt())
174192
if err != nil {
175193
fmt.Printf("%v", err)
176194
return
@@ -182,17 +200,9 @@ func ExampleAsFloat64() {
182200
return
183201
}
184202

185-
fmt.Printf("%v", output.(float64))
186-
187-
// Output: 42
188-
}
203+
fmt.Printf("%T(%v)", output, output)
189204

190-
func ExampleAsFloat64_error() {
191-
_, err := expr.Compile(`!!true`, expr.AsFloat64())
192-
193-
fmt.Printf("%v", err)
194-
195-
// Output: expected float64, but got bool
205+
// Output: int(42)
196206
}
197207

198208
func ExampleAsInt64() {
@@ -217,6 +227,32 @@ func ExampleAsInt64() {
217227
// Output: 5
218228
}
219229

230+
func ExampleAsFloat64() {
231+
program, err := expr.Compile("42", expr.AsFloat64())
232+
if err != nil {
233+
fmt.Printf("%v", err)
234+
return
235+
}
236+
237+
output, err := expr.Run(program, nil)
238+
if err != nil {
239+
fmt.Printf("%v", err)
240+
return
241+
}
242+
243+
fmt.Printf("%v", output.(float64))
244+
245+
// Output: 42
246+
}
247+
248+
func ExampleAsFloat64_error() {
249+
_, err := expr.Compile(`!!true`, expr.AsFloat64())
250+
251+
fmt.Printf("%v", err)
252+
253+
// Output: expected float64, but got bool
254+
}
255+
220256
func ExampleOperator() {
221257
code := `
222258
Now() > CreatedAt &&

vm/vm.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,10 @@ func (vm *VM) Run(program *Program, env interface{}) (out interface{}, err error
354354
t := arg
355355
switch t {
356356
case 0:
357-
vm.push(runtime.ToInt64(vm.pop()))
357+
vm.push(runtime.ToInt(vm.pop()))
358358
case 1:
359+
vm.push(runtime.ToInt64(vm.pop()))
360+
case 2:
359361
vm.push(runtime.ToFloat64(vm.pop()))
360362
}
361363

0 commit comments

Comments
 (0)