-
Notifications
You must be signed in to change notification settings - Fork 0
/
Variable.go
58 lines (47 loc) · 1.52 KB
/
Variable.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
49
50
51
52
53
54
55
56
57
58
// =================================================================
//
// Copyright (C) 2019 Spatial Current, Inc. - All Rights Reserved
// Released as open source under the MIT License. See LICENSE file.
//
// =================================================================
package dfl
import (
"strings"
"github.com/spatialcurrent/go-dfl/pkg/dfl/syntax"
)
// Variable is a Node representing the value of a temporary variable.
// Variables start with a "#" and follow with the name or full path into the object if multiple levels deep.
// For example, #a and #a.b.c.d. You can also use a null-safe operator, e.g., #a?.b?.c?.d
type Variable struct {
Name string
}
func (v Variable) Dfl(quotes []string, pretty bool, tabs int) string {
return syntax.VariablePrefix + v.Name
}
func (v Variable) Sql(pretty bool, tabs int) string {
if pretty {
return strings.Repeat(" ", tabs) + v.Name
}
return v.Name
}
func (v Variable) Map() map[string]interface{} {
return map[string]interface{}{
"variable": syntax.VariablePrefix + v.Name,
}
}
func (v Variable) Compile() Node {
return Variable{Name: v.Name}
}
func (v Variable) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error) {
if len(v.Name) == 0 {
return vars, vars, nil
}
value, err := Extract(v.Name, vars, vars, ctx, funcs, quotes)
return vars, value, err
}
func (v Variable) Attributes() []string {
return []string{}
}
func (v Variable) Variables() []string {
return []string{v.Name}
}