diff options
Diffstat (limited to 'vendor/github.com/hashicorp/hil/ast/scope.go')
-rw-r--r-- | vendor/github.com/hashicorp/hil/ast/scope.go | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/hil/ast/scope.go b/vendor/github.com/hashicorp/hil/ast/scope.go new file mode 100644 index 0000000..7a975d9 --- /dev/null +++ b/vendor/github.com/hashicorp/hil/ast/scope.go | |||
@@ -0,0 +1,90 @@ | |||
1 | package ast | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | "reflect" | ||
6 | ) | ||
7 | |||
8 | // Scope is the interface used to look up variables and functions while | ||
9 | // evaluating. How these functions/variables are defined are up to the caller. | ||
10 | type Scope interface { | ||
11 | LookupFunc(string) (Function, bool) | ||
12 | LookupVar(string) (Variable, bool) | ||
13 | } | ||
14 | |||
15 | // Variable is a variable value for execution given as input to the engine. | ||
16 | // It records the value of a variables along with their type. | ||
17 | type Variable struct { | ||
18 | Value interface{} | ||
19 | Type Type | ||
20 | } | ||
21 | |||
22 | // NewVariable creates a new Variable for the given value. This will | ||
23 | // attempt to infer the correct type. If it can't, an error will be returned. | ||
24 | func NewVariable(v interface{}) (result Variable, err error) { | ||
25 | switch v := reflect.ValueOf(v); v.Kind() { | ||
26 | case reflect.String: | ||
27 | result.Type = TypeString | ||
28 | default: | ||
29 | err = fmt.Errorf("Unknown type: %s", v.Kind()) | ||
30 | } | ||
31 | |||
32 | result.Value = v | ||
33 | return | ||
34 | } | ||
35 | |||
36 | // String implements Stringer on Variable, displaying the type and value | ||
37 | // of the Variable. | ||
38 | func (v Variable) String() string { | ||
39 | return fmt.Sprintf("{Variable (%s): %+v}", v.Type, v.Value) | ||
40 | } | ||
41 | |||
42 | // Function defines a function that can be executed by the engine. | ||
43 | // The type checker will validate that the proper types will be called | ||
44 | // to the callback. | ||
45 | type Function struct { | ||
46 | // ArgTypes is the list of types in argument order. These are the | ||
47 | // required arguments. | ||
48 | // | ||
49 | // ReturnType is the type of the returned value. The Callback MUST | ||
50 | // return this type. | ||
51 | ArgTypes []Type | ||
52 | ReturnType Type | ||
53 | |||
54 | // Variadic, if true, says that this function is variadic, meaning | ||
55 | // it takes a variable number of arguments. In this case, the | ||
56 | // VariadicType must be set. | ||
57 | Variadic bool | ||
58 | VariadicType Type | ||
59 | |||
60 | // Callback is the function called for a function. The argument | ||
61 | // types are guaranteed to match the spec above by the type checker. | ||
62 | // The length of the args is strictly == len(ArgTypes) unless Varidiac | ||
63 | // is true, in which case its >= len(ArgTypes). | ||
64 | Callback func([]interface{}) (interface{}, error) | ||
65 | } | ||
66 | |||
67 | // BasicScope is a simple scope that looks up variables and functions | ||
68 | // using a map. | ||
69 | type BasicScope struct { | ||
70 | FuncMap map[string]Function | ||
71 | VarMap map[string]Variable | ||
72 | } | ||
73 | |||
74 | func (s *BasicScope) LookupFunc(n string) (Function, bool) { | ||
75 | if s == nil { | ||
76 | return Function{}, false | ||
77 | } | ||
78 | |||
79 | v, ok := s.FuncMap[n] | ||
80 | return v, ok | ||
81 | } | ||
82 | |||
83 | func (s *BasicScope) LookupVar(n string) (Variable, bool) { | ||
84 | if s == nil { | ||
85 | return Variable{}, false | ||
86 | } | ||
87 | |||
88 | v, ok := s.VarMap[n] | ||
89 | return v, ok | ||
90 | } | ||