forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope.go
48 lines (39 loc) · 1.11 KB
/
scope.go
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
package tick
import (
"fmt"
"strings"
)
type DynamicMethod func(self interface{}, args ...interface{}) (interface{}, error)
// Contains a set of variables references and their values.
type Scope struct {
variables map[string]interface{}
dynamicMethods map[string]DynamicMethod
}
//Initialize a new Scope object.
func NewScope() *Scope {
return &Scope{
variables: make(map[string]interface{}),
dynamicMethods: make(map[string]DynamicMethod),
}
}
// Set defines a name -> value pairing in the scope.
func (s *Scope) Set(name string, value interface{}) {
s.variables[name] = value
}
// Get returns the value of 'name'.
func (s *Scope) Get(name string) (interface{}, error) {
if v, ok := s.variables[name]; ok {
return v, nil
}
var possible []string
for k := range s.variables {
possible = append(possible, k)
}
return nil, fmt.Errorf("name %q is undefined. Names in scope: %s", name, strings.Join(possible, ","))
}
func (s *Scope) SetDynamicMethod(name string, m DynamicMethod) {
s.dynamicMethods[name] = m
}
func (s *Scope) DynamicMethod(name string) DynamicMethod {
return s.dynamicMethods[name]
}