-
Notifications
You must be signed in to change notification settings - Fork 1
/
table.go
325 lines (298 loc) · 8.19 KB
/
table.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright (c) 2017, Technomancers. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package goNTCore
import (
"github.com/technomancers/goNTCore/entryType"
"github.com/technomancers/goNTCore/util"
)
//NetworkTabler is the interface implemented by types that can read and write Network Table at a high level.
type NetworkTabler interface {
ContainsKey(key string) bool
ContainsTable(key string) bool
Delete(key string)
DeleteAll()
IsPersisted(key string) bool
GetKeys() []string
GetTable(key string) NetworkTabler
GetBoolean(key string, def bool) bool
PutBoolean(key string, val bool) bool
GetNumber(key string, def float64) float64
PutNumber(key string, val float64) bool
GetString(key string, def string) string
PutString(key string, val string) bool
GetRaw(key string, def []byte) []byte
PutRaw(key string, val []byte) bool
GetBooleanArray(key string, def []bool) []bool
PutBooleanArray(key string, val []bool) bool
GetNumberArray(key string, def []float64) []float64
PutNumberArray(key string, val []float64) bool
GetStringArray(key string, def []string) []string
PutStringArray(key string, val []string) bool
}
//DataTable implements NetworkTabler based on a backend that needs to exist on create.
type DataTable struct {
data Data
root string
}
//NewTable creates a new Network Table that is based off of the data and root passed in.
//Pass in an empty string or "/" for root table.
func NewTable(d Data, root string) *DataTable {
if root == "" {
root = "/"
}
root = util.SanatizeKey(root)
return &DataTable{
data: d,
root: root,
}
}
//ContainsKey returns true if the key exist in the table.
func (t *DataTable) ContainsKey(key string) bool {
key = t.getKey(key)
return t.data.IsKey(key)
}
//ContainsTable return true if the table exist in the table.
func (t *DataTable) ContainsTable(key string) bool {
key = t.getKey(key)
return t.data.IsTable(key)
}
//Delete deletes the given key from the table.
func (t *DataTable) Delete(key string) {
key = t.getKey(key)
t.data.DeleteEntry(key) // nolint: errcheck
}
//DeleteAll deletes all keys from the table.
func (t *DataTable) DeleteAll() {
t.data.DeleteAll(t.root) // nolint: errcheck
}
//IsPersisted returns true if the key is to be persisted.
//Returns false if the key does not exist.
func (t *DataTable) IsPersisted(key string) bool {
key = t.getKey(key)
entry, err := t.data.GetEntry(key)
if err != nil {
return false
}
return entry.Persitant
}
//GetKeys returns all the keys in the table.
func (t *DataTable) GetKeys() []string {
entries, err := t.data.GetEntries(t.root)
if err != nil {
return []string{}
}
return entries
}
//GetTable gets a table with the specified key.
//If table does not exist it creates a new one.
func (t *DataTable) GetTable(key string) NetworkTabler {
key = t.getKey(key)
return NewTable(t.data, key)
}
//GetBoolean gets the value of key as a boolean.
//If the value is not of type boolean it returns the default value passed in.
func (t *DataTable) GetBoolean(key string, def bool) bool {
key = t.getKey(key)
entry, err := t.data.GetEntry(key)
if err != nil {
return def
}
b, ok := entry.Value.(bool)
if !ok {
return def
}
return b
}
//PutBoolean puts the boolean value in the table.
//If value exist it updates it.
//If value doesn't exist it will add it.
//Returns false if key exist of a different type.
func (t *DataTable) PutBoolean(key string, val bool) bool {
entry := &Entry{
Key: key,
Type: entryType.ETypeBoolean,
Value: val,
}
if err := t.data.PutEntry(entry); err != nil {
return false
}
return true
}
//GetNumber gets the value of key as a float64.
//If the value is not of type float64 it returns the default value passed in.
func (t *DataTable) GetNumber(key string, def float64) float64 {
key = t.getKey(key)
entry, err := t.data.GetEntry(key)
if err != nil {
return def
}
d, ok := entry.Value.(float64)
if !ok {
return def
}
return d
}
//PutNumber puts the float64 value in the table.
//If value exist it updates it.
//If value doesn't exist it will add it.
//Returns false if key exist of a different type.
func (t *DataTable) PutNumber(key string, val float64) bool {
entry := &Entry{
Key: key,
Type: entryType.ETypeDouble,
Value: val,
}
if err := t.data.PutEntry(entry); err != nil {
return false
}
return true
}
//GetString gets the value of key as a string.
//If the value is not of type string it returns the default value passed in.
func (t *DataTable) GetString(key string, def string) string {
key = t.getKey(key)
entry, err := t.data.GetEntry(key)
if err != nil {
return def
}
s, ok := entry.Value.(string)
if !ok {
return def
}
return s
}
//PutString puts the string value in the table.
//If value exist it updates it.
//If value doesn't exist it will add it.
//Returns false if key exist of a different type.
func (t *DataTable) PutString(key string, val string) bool {
entry := &Entry{
Key: key,
Type: entryType.ETypeString,
Value: val,
}
if err := t.data.PutEntry(entry); err != nil {
return false
}
return true
}
//GetRaw gets the value of key as a slice of bytes.
//If the value is not of type byte slice it returns the default value passed in.
func (t *DataTable) GetRaw(key string, def []byte) []byte {
key = t.getKey(key)
entry, err := t.data.GetEntry(key)
if err != nil {
return def
}
r, ok := entry.Value.([]byte)
if !ok {
return def
}
return r
}
//PutRaw puts the taw value in the table.
//If value exist it updates it.
//If value doesn't exist it will add it.
//Returns false if key exist of a different type.
func (t *DataTable) PutRaw(key string, val []byte) bool {
entry := &Entry{
Key: key,
Type: entryType.ETypeRawData,
Value: val,
}
if err := t.data.PutEntry(entry); err != nil {
return false
}
return true
}
//GetBooleanArray gets the value of key as a slice of booleans.
//If the value is not of type boolean slice it returns the default value passed in.
func (t *DataTable) GetBooleanArray(key string, def []bool) []bool {
key = t.getKey(key)
entry, err := t.data.GetEntry(key)
if err != nil {
return def
}
ba, ok := entry.Value.([]bool)
if !ok {
return def
}
return ba
}
//PutBooleanArray puts the slice of boolean in the table.
//If value exist it updates it.
//If value doesn't exist it will add it.
//Returns false if key exist of a different type.
func (t *DataTable) PutBooleanArray(key string, val []bool) bool {
entry := &Entry{
Key: key,
Type: entryType.ETypeBooleanArray,
Value: val,
}
if err := t.data.PutEntry(entry); err != nil {
return false
}
return true
}
//GetNumberArray gets the value of key as a slice of float64s.
//If the value is not of type float64 slice it returns the default value passed in.
func (t *DataTable) GetNumberArray(key string, def []float64) []float64 {
key = t.getKey(key)
entry, err := t.data.GetEntry(key)
if err != nil {
return def
}
da, ok := entry.Value.([]float64)
if !ok {
return def
}
return da
}
//PutNumberArray puts the slice of float64 in the table.
//If value exist it updates it.
//If value doesn't exist it will add it.
//Returns false if key exist of a different type.
func (t *DataTable) PutNumberArray(key string, val []float64) bool {
entry := &Entry{
Key: key,
Type: entryType.ETypeDoubleArray,
Value: val,
}
if err := t.data.PutEntry(entry); err != nil {
return false
}
return true
}
//GetStringArray gets the value of key as a slice of strings.
//If the value is not of type string slice it returns the default value passed in.
func (t *DataTable) GetStringArray(key string, def []string) []string {
key = t.getKey(key)
entry, err := t.data.GetEntry(key)
if err != nil {
return def
}
sa, ok := entry.Value.([]string)
if !ok {
return def
}
return sa
}
//PutStringArray puts the slice of string in the table.
//If value exist it updates it.
//If value doesn't exist it will add it.
//Returns false if key exist of a different type.
func (t *DataTable) PutStringArray(key string, val []string) bool {
entry := &Entry{
Key: key,
Type: entryType.ETypeStringArray,
Value: val,
}
if err := t.data.PutEntry(entry); err != nil {
return false
}
return true
}
func (t *DataTable) getKey(key string) string {
return util.KeyJoin(t.root, key)
}