-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionary.go
More file actions
147 lines (133 loc) · 3.54 KB
/
Dictionary.go
File metadata and controls
147 lines (133 loc) · 3.54 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// =================================================================
//
// 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
Items []Item
}
func NewDictionary(m map[string]interface{}) *Dictionary {
items := make([]Item, 0)
for k, v := range m {
if d, ok := v.(map[string]interface{}); ok {
items = append(items, Item{
Key: &Literal{Value: k},
Value: NewDictionary(d),
})
} else {
items = append(items, Item{
Key: &Literal{Value: k},
Value: &Literal{Value: v},
})
}
}
return &Dictionary{Items: items}
}
// Len returns the length of the underlying array.
func (d Dictionary) Len() int {
return len(d.Items)
}
func (d Dictionary) Dfl(quotes []string, pretty bool, tabs int) string {
if len(d.Items) == 0 {
return "{}"
}
values := make([]string, 0)
for _, i := range d.Items {
values = append(values, i.Key.Dfl(quotes, pretty, tabs+1)+": "+i.Value.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
for i, item := range d.Items {
if i > 0 {
str += ", "
}
str += item.Key.Sql(pretty, tabs) + ":" + item.Value.Sql(pretty, tabs)
i += 1
}
str = str + SqlArraySuffix + SqlQuote + "::json"
return str
}
func (d Dictionary) Map() map[string]interface{} {
items := []map[string]interface{}{}
for _, item := range d.Items {
items = append(items, item.Map())
}
return map[string]interface{}{
"@type": "dictionary",
"@value": items,
}
}
func (d Dictionary) MarshalMap() (interface{}, error) {
return d.Map(), nil
}
// Compile returns a compiled version of this node.
func (d Dictionary) Compile() Node {
items := make([]Item, 0)
for _, i := range d.Items {
items = append(items, Item{
Key: i.Key.Compile(),
Value: i.Value.Compile(),
})
}
return &Dictionary{Items: items}
}
func (d Dictionary) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error) {
values := map[interface{}]interface{}{}
for _, i := range d.Items {
_, keyValue, err := i.Key.Evaluate(vars, ctx, funcs, quotes)
if err != nil {
return vars, values, err
}
_, valueValue, err := i.Value.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 _, i := range d.Items {
for _, x := range i.Key.Attributes() {
set[x] = struct{}{}
}
for _, x := range i.Value.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 _, i := range d.Items {
for _, x := range i.Key.Variables() {
set[x] = struct{}{}
}
for _, x := range i.Value.Variables() {
set[x] = struct{}{}
}
}
attrs := make([]string, 0, len(set))
for x := range set {
attrs = append(attrs, x)
}
return attrs
}