Repository navigation
Expand file tree
/
Copy pathDict.go
More file actions
169 lines (145 loc) · 3.8 KB
/
Copy pathDict.go
File metadata and controls
169 lines (145 loc) · 3.8 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package haystack
import (
"bufio"
"encoding/json"
"sort"
"strings"
)
// Dict is a map of name/Val pairs.
type Dict struct {
items map[string]Val
}
// NewDict creates a new Dict object.
func NewDict(items map[string]Val) Dict {
return Dict{items: items}
}
// EmptyDict creates a new Dict object.
func EmptyDict() Dict {
return NewDict(map[string]Val{})
}
// Get returns the Val of the given name. If the name is not found, Null is returned.
func (dict Dict) Get(name string) Val {
val := dict.items[name]
if val == nil {
val = NewNull()
}
return val
}
// Names returns the key names for the given dict.
func (dict Dict) Names() []string {
names := []string{}
for name := range dict.items {
names = append(names, name)
}
return names
}
// Set sets the Val for the given name and returns a new Dict.
func (dict Dict) Set(name string, val Val) Dict {
return dict.SetAll(map[string]Val{name: val})
}
// SetAll sets all the values in the input map and returns a new Dict.
func (dict Dict) SetAll(set map[string]Val) Dict {
newDict := dict.dup()
for name, val := range set {
newDict.items[name] = val
}
return newDict
}
// dup duplicates the given Dict
func (dict Dict) dup() Dict {
newItems := map[string]Val{}
for name, val := range dict.items {
newItems[name] = val
}
return Dict{items: newItems}
}
// Size returns the number of name/Val pairs.
func (dict Dict) Size() int {
return len(dict.items)
}
// IsEmpty returns true if there is nothing in the Dict.
func (dict Dict) IsEmpty() bool {
return len(dict.items) == 0
}
// MarshalJSON represents the object in JSON object format: "{"<name1>":<val1>, "<name2>":<val2> ...}"
func (dict Dict) MarshalJSON() ([]byte, error) {
return json.Marshal(dict.items)
}
// UnmarshalJSON interprets the json object format: "{"<name1>":<val1>, "<name2>":<val2> ...}"
func (dict *Dict) UnmarshalJSON(buf []byte) error {
var jsonMap map[string]interface{}
err := json.Unmarshal(buf, &jsonMap)
if err != nil {
return err
}
newDict, newErr := dictFromJSON(jsonMap)
*dict = newDict
return newErr
}
func dictFromJSON(jsonMap map[string]interface{}) (Dict, error) {
items := map[string]Val{}
for jsonKey, jsonVal := range jsonMap {
val, err := ValFromJSON(jsonVal)
if err != nil {
return EmptyDict(), err
}
items[jsonKey] = val
}
return NewDict(items), nil
}
// MarshalHayson represents the object in JSON object format: "{"_kind":"dict", "<name1>":<val1>, "<name2>":<val2> ...}"
// The optional "_kind" is always included.
func (dict Dict) MarshalHayson() ([]byte, error) {
builder := new(strings.Builder)
builder.WriteString("{\"_kind\":\"dict\"")
names := dict.Names()
sort.Strings(names)
for _, name := range names {
builder.WriteString(",\"")
builder.WriteString(name)
builder.WriteString("\":")
val := dict.items[name]
valHayson, valErr := val.MarshalHayson()
if valErr != nil {
return []byte{}, valErr
}
builder.Write(valHayson)
}
builder.WriteString("}")
return []byte(builder.String()), nil
}
// ToZinc representes the object as: "{<name1>:<val1> <name2>:<val2> ...}" with the names in alphabetical order.
// Markers don't require a val.
func (dict Dict) ToZinc() string {
builder := new(strings.Builder)
out := bufio.NewWriter(builder)
dict.WriteZincTo(out, true)
out.Flush()
return builder.String()
}
// WriteZincTo appends the Writer with the Dict zinc representation.
func (dict Dict) WriteZincTo(buf *bufio.Writer, brackets bool) {
if brackets {
buf.WriteString("{")
}
names := dict.Names()
sort.Strings(names)
for idx, name := range names {
if idx != 0 {
buf.WriteString(" ")
}
buf.WriteString(name)
val := dict.items[name]
switch val := val.(type) {
case Grid:
val.WriteZincTo(buf, 1)
case Marker:
break
default:
buf.WriteString(":" + val.ToZinc())
}
}
if brackets {
buf.WriteString("}")
}
}