-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array.go
114 lines (103 loc) · 2.88 KB
/
Array.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
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
104
105
106
107
108
109
110
111
112
113
114
// =================================================================
//
// Copyright (C) 2019 Spatial Current, Inc. - All Rights Reserved
// Released as open source under the MIT License. See LICENSE file.
//
// =================================================================
package dfl
import (
"reflect"
"strings"
)
// Array is a Node representing an array of values, which can be either a Literal or Attribute.
type Array struct {
Nodes []Node
}
// Len returns the length of the underlying array.
func (a Array) Len() int {
return len(a.Nodes)
}
// Dfl returns the DFL representation of this node as a string
func (a Array) Dfl(quotes []string, pretty bool, tabs int) string {
if len(a.Nodes) == 0 {
return "[]"
}
if len(a.Nodes) == 1 {
return "[" + strings.TrimSpace(a.Nodes[0].Dfl(quotes, pretty, tabs)) + "]"
}
if pretty {
return "[" + "\n" + FormatList(FormatNodes(a.Nodes, quotes, pretty, tabs), ",", pretty, tabs+1) + "\n" + strings.Repeat(DefaultTab, tabs) + "]"
}
return "[" + FormatList(FormatNodes(a.Nodes, quotes, pretty, tabs), ",", pretty, tabs) + "]"
}
// Sql returns the SQL representation of this node as a string
func (a Array) Sql(pretty bool, tabs int) string {
str := "{"
for i, x := range a.Nodes {
if i > 0 {
str += ", "
}
str += x.Sql(pretty, tabs)
}
str = str + "}"
return str
}
func (a Array) Map() map[string]interface{} {
return map[string]interface{}{
"nodes": a.Nodes,
}
}
// Compile returns a compiled version of this node.
// If all the values of an Set are literals, returns a single Literal with the corresponding array as its value.
// Otherwise returns the original node..
func (a Array) Compile() Node {
values := make([]interface{}, len(a.Nodes))
nodes := reflect.ValueOf(a.Nodes)
for i := 0; i < nodes.Len(); i++ {
n := nodes.Index(i).Interface()
switch n.(type) {
case *Literal:
values[i] = n.(*Literal).Value
default:
return a
}
}
return Literal{Value: TryConvertArray(values)}
}
func (a Array) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error) {
values := make([]interface{}, 0, len(a.Nodes))
for _, n := range a.Nodes {
vars, v, err := n.Evaluate(vars, ctx, funcs, quotes)
if err != nil {
return vars, values, err
}
values = append(values, v)
}
return vars, TryConvertArray(values), nil
}
func (a Array) Attributes() []string {
set := make(map[string]struct{})
for _, n := range a.Nodes {
for _, x := range n.Attributes() {
set[x] = struct{}{}
}
}
attrs := make([]string, 0, len(set))
for x := range set {
attrs = append(attrs, x)
}
return attrs
}
func (a Array) Variables() []string {
set := make(map[string]struct{})
for _, n := range a.Nodes {
for _, x := range n.Variables() {
set[x] = struct{}{}
}
}
attrs := make([]string, 0, len(set))
for x := range set {
attrs = append(attrs, x)
}
return attrs
}