-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dictionary.go
129 lines (116 loc) · 3.11 KB
/
Dictionary.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// =================================================================
//
// Copyright (C) 2019 Spatial Current, Inc. - All Rights Reserved
// Released as open source under the MIT License. See LICENSE file.
//
// =================================================================
package dfl
import (
"strings"
)
// Dictionary is a Node representing a dictionary of key value pairs.
type Dictionary struct {
Nodes map[Node]Node
}
func NewDictionary(m map[string]interface{}) *Dictionary {
nodes := map[Node]Node{}
for k, v := range m {
if d, ok := v.(map[string]interface{}); ok {
nodes[&Literal{Value: k}] = NewDictionary(d)
} else {
nodes[&Literal{Value: k}] = &Literal{Value: v}
}
}
return &Dictionary{Nodes: nodes}
}
// Len returns the length of the underlying array.
func (d Dictionary) Len() int {
return len(d.Nodes)
}
func (d Dictionary) Dfl(quotes []string, pretty bool, tabs int) string {
if len(d.Nodes) == 0 {
return "{}"
}
values := make([]string, 0)
for k, v := range d.Nodes {
values = append(values, k.Dfl(quotes, pretty, tabs+1)+": "+v.Dfl(quotes, pretty, tabs+1))
}
if pretty {
return "{" + "\n" + FormatList(values, ",", pretty, tabs+1) + "\n" + strings.Repeat(DefaultTab, tabs) + "}"
}
return "{" + FormatList(values, ",", pretty, tabs) + "}"
}
// Sql returns the SQL representation of this node as a string
func (d Dictionary) Sql(pretty bool, tabs int) string {
str := SqlQuote + SqlArrayPrefix
i := 0
for k, v := range d.Nodes {
if i > 0 {
str += ", "
}
str += k.Sql(pretty, tabs) + ":" + v.Sql(pretty, tabs)
i += 1
}
str = str + SqlArraySuffix + SqlQuote + "::json"
return str
}
func (d Dictionary) Map() map[string]interface{} {
return map[string]interface{}{
"nodes": d.Nodes,
}
}
// Compile returns a compiled version of this node.
func (d Dictionary) Compile() Node {
nodes := map[Node]Node{}
for k, v := range d.Nodes {
nodes[k.Compile()] = v.Compile()
}
return Dictionary{Nodes: nodes}
}
func (d Dictionary) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error) {
values := map[interface{}]interface{}{}
for k, v := range d.Nodes {
_, keyValue, err := k.Evaluate(vars, ctx, funcs, quotes)
if err != nil {
return vars, values, err
}
_, valueValue, err := v.Evaluate(vars, ctx, funcs, quotes)
if err != nil {
return vars, values, err
}
values[keyValue] = valueValue
}
return vars, values, nil
}
func (d Dictionary) Attributes() []string {
set := make(map[string]struct{})
for k, v := range d.Nodes {
for _, x := range k.Attributes() {
set[x] = struct{}{}
}
for _, x := range v.Attributes() {
set[x] = struct{}{}
}
}
attrs := make([]string, 0, len(set))
for x := range set {
attrs = append(attrs, x)
}
return attrs
}
func (d Dictionary) Variables() []string {
set := make(map[string]struct{})
for k, v := range d.Nodes {
for _, x := range k.Variables() {
set[x] = struct{}{}
}
for _, x := range v.Variables() {
set[x] = struct{}{}
}
}
attrs := make([]string, 0, len(set))
for x := range set {
attrs = append(attrs, x)
}
return attrs
}