-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.go
More file actions
104 lines (90 loc) · 2.58 KB
/
Copy pathscript.go
File metadata and controls
104 lines (90 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package script
import (
"fmt"
)
// ========== Convenience Functions ==========
// Eval executes a script and returns the result.
// This is the simplest way to execute a script, suitable for one-time execution.
//
// Example:
//
// result, err := script.Eval("10 + 20")
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(result.Int()) // Output: 30
func Eval(source string) (Value, error) {
parser := NewParser()
defer returnParserToPool(parser)
engine := NewEngine()
ctx := NewContext()
script, err := parser.Compile(source)
if err != nil {
return Value{}, err
}
return engine.Run(ctx, script)
}
// EvalWithBindings executes a script with variable bindings.
// Suitable for scenarios requiring external data.
//
// Example:
//
// bindings := map[string]interface{}{
// "name": "Alice",
// "age": 30,
// }
// result, err := script.EvalWithBindings(`
// n := getBindValue("name")
// a := getBindValue("age")
// "Hello, " + n + "! Age: " + string(a)
// `, bindings)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(result.String()) // Output: Hello, Alice! Age: 30
func EvalWithBindings(source string, bindings map[string]interface{}) (Value, error) {
parser := NewParser()
defer returnParserToPool(parser)
engine := NewEngine()
ctx := NewContext()
// Bind all variables
for name, value := range bindings {
ctx.BindValue(name, value)
}
script, err := parser.Compile(source)
if err != nil {
return Value{}, err
}
return engine.Run(ctx, script)
}
// MustEval executes a script and panics if an error occurs.
// Suitable for executing scripts during initialization that are guaranteed not to fail.
// Similar to template.Must in the Go standard library.
//
// Example:
//
// // During initialization
// config := script.MustEval(`{"timeout": 30, "retries": 3}`)
// fmt.Println(config.Map().Pairs["timeout"].Int()) // Output: 30
func MustEval(source string) Value {
result, err := Eval(source)
if err != nil {
panic(fmt.Sprintf("script.Eval failed: %v", err))
}
return result
}
// MustEvalWithBindings executes a script with bindings and panics if an error occurs.
// Suitable for executing scripts during initialization that are guaranteed not to fail.
//
// Example:
//
// bindings := map[string]interface{}{"x": 10, "y": 20}
// result := script.MustEvalWithBindings("x + y", bindings)
// fmt.Println(result.Int()) // Output: 30
func MustEvalWithBindings(source string, bindings map[string]interface{}) Value {
result, err := EvalWithBindings(source, bindings)
if err != nil {
panic(fmt.Sprintf("script.EvalWithBindings failed: %v", err))
}
return result
}