File tree Expand file tree Collapse file tree 1 file changed +63
-1
lines changed Expand file tree Collapse file tree 1 file changed +63
-1
lines changed Original file line number Diff line number Diff line change @@ -121,7 +121,7 @@ func ExampleEval_matches() {
121
121
// | ............^
122
122
}
123
123
124
- func ExampleRun () {
124
+ func ExampleCompile () {
125
125
env := map [string ]interface {}{
126
126
"foo" : 1 ,
127
127
"bar" : 99 ,
@@ -188,6 +188,68 @@ func ExampleEnv() {
188
188
// Output: true
189
189
}
190
190
191
+ func ExampleAsBool () {
192
+ env := map [string ]int {
193
+ "foo" : 0 ,
194
+ }
195
+
196
+ program , err := expr .Compile ("foo >= 0" , expr .Env (env ), expr .AsBool ())
197
+ if err != nil {
198
+ fmt .Printf ("%v" , err )
199
+ return
200
+ }
201
+
202
+ output , err := expr .Run (program , env )
203
+ if err != nil {
204
+ fmt .Printf ("%v" , err )
205
+ return
206
+ }
207
+
208
+ fmt .Printf ("%v" , output .(bool ))
209
+
210
+ // Output: true
211
+ }
212
+
213
+ func ExampleAsFloat64 () {
214
+ program , err := expr .Compile ("42" , expr .AsFloat64 ())
215
+ if err != nil {
216
+ fmt .Printf ("%v" , err )
217
+ return
218
+ }
219
+
220
+ output , err := expr .Run (program , nil )
221
+ if err != nil {
222
+ fmt .Printf ("%v" , err )
223
+ return
224
+ }
225
+
226
+ fmt .Printf ("%v" , output .(float64 ))
227
+
228
+ // Output: 42
229
+ }
230
+
231
+ func ExampleAsInt64 () {
232
+ env := map [string ]float64 {
233
+ "foo" : 3 ,
234
+ }
235
+
236
+ program , err := expr .Compile ("foo + 2" , expr .Env (env ), expr .AsInt64 ())
237
+ if err != nil {
238
+ fmt .Printf ("%v" , err )
239
+ return
240
+ }
241
+
242
+ output , err := expr .Run (program , env )
243
+ if err != nil {
244
+ fmt .Printf ("%v" , err )
245
+ return
246
+ }
247
+
248
+ fmt .Printf ("%v" , output .(int64 ))
249
+
250
+ // Output: 5
251
+ }
252
+
191
253
func ExampleOperator () {
192
254
type Place struct {
193
255
Code string
You can’t perform that action at this time.
0 commit comments