-
Notifications
You must be signed in to change notification settings - Fork 0
/
getset.go
289 lines (242 loc) · 6.92 KB
/
getset.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
// Copyright 2022 Teal.Finance/incorruptible contributors
// This file is part of Teal.Finance/incorruptible
// a tiny+secured cookie token licensed under the MIT License.
// SPDX-License-Identifier: MIT
//nolint:ireturn // Get(*TValues) returns the KVal interface to comply with that interface.
package incorruptible
import (
"fmt"
"strconv"
)
// Get / Set for multiple fields at the same timeout
func (tv *TValues) Get(keyValues ...KVal) ([]KVal, error) {
var err error
for i := range keyValues {
keyValues[i], err = keyValues[i].Get(tv)
if err != nil {
return nil, fmt.Errorf("%w at arg=%d", err, i)
}
}
return keyValues, nil
}
func (tv *TValues) Set(keyValues ...KVal) error {
for i := range keyValues {
if err := keyValues[i].Set(tv); err != nil {
return fmt.Errorf("%w at arg=%d", err, i)
}
}
return nil
}
// Get / Set for Uint64
func (tv TValues) Uint64(key int) (uint64, error) {
if err := tv.checkRead(key); err != nil {
return 0, err
}
return BytesToUint64(tv.Values[key])
}
func (tv *TValues) SetUint64(key int, val uint64) error {
if err := checkWrite(key); err != nil {
return err
}
b := Uint64ToBytes(val)
tv.set(key, b)
return nil
}
// Get / Set for Int64
func (tv TValues) Int64(key int) (int64, error) {
v, err := tv.Uint64(key)
return int64(v), err
}
func (tv *TValues) SetInt64(key int, val int64) error {
return tv.SetUint64(key, uint64(val))
}
// Get / Set for Bool
func (tv TValues) Bool(key int) (bool, error) {
if err := tv.checkRead(key); err != nil {
return false, err
}
b := tv.Values[key]
switch len(b) {
case 0:
return false, nil
case 1:
return true, nil
default:
return false, fmt.Errorf("got %d bytes but want only 0 or 1 byte for boolean encoding", len(b))
}
}
func (tv *TValues) SetBool(key int, val bool) error {
if err := checkWrite(key); err != nil {
return err
}
var buf []byte // false --> length=0
if val {
buf = []byte{0} // true --> length=1
}
tv.set(key, buf)
return nil
}
// Get / Set for String
func (tv TValues) String(key int) (string, error) {
if err := tv.checkRead(key); err != nil {
return "", err
}
return string(tv.Values[key]), nil
}
func (tv *TValues) SetString(key int, val string) error {
if err := checkWrite(key); err != nil {
return err
}
tv.set(key, []byte(val))
return nil
}
// Get / Set with default value in lieu of returning an error
func (tv TValues) Uint64IfAny(key int, defaultValue ...uint64) uint64 {
v, err := tv.Uint64(key)
if err != nil {
return defaultUint64(defaultValue...)
}
return v
}
func (tv TValues) Int64IfAny(key int, defaultValue ...int64) int64 {
v, err := tv.Int64(key)
if err != nil {
return defaultInt64(defaultValue...)
}
return v
}
func (tv TValues) BoolIfAny(key int, defaultValue ...bool) bool {
v, err := tv.Bool(key)
if err != nil {
return defaultBool(defaultValue...)
}
return v
}
func (tv TValues) StringIfAny(key int, defaultValue ...string) string {
v, err := tv.String(key)
if err != nil {
return defaultString(defaultValue...)
}
return v
}
type (
KUint64 struct {
Key int
Val uint64
}
KInt64 struct {
Key int
Val int64
}
KBool struct {
Key int
Val bool
}
KString struct {
Key int
Val string
}
)
type KVal interface {
Set(*TValues) error
Get(*TValues) (KVal, error)
Uint64() uint64
Int64() int64
Bool() bool
String() string
}
func Uint64(k int, v ...uint64) KUint64 { return KUint64{k, defaultUint64(v...)} }
func Int64(k int, v ...int64) KInt64 { return KInt64{k, defaultInt64(v...)} }
func Bool(k int, v ...bool) KBool { return KBool{k, defaultBool(v...)} }
func String(k int, v ...string) KString { return KString{k, defaultString(v...)} }
func (tv TValues) KUint64(k int, v ...uint64) KUint64 { return Uint64(k, v...) }
func (tv TValues) KInt64(k int, v ...int64) KInt64 { return Int64(k, v...) }
func (tv TValues) KBool(k int, v ...bool) KBool { return Bool(k, v...) }
func (tv TValues) KString(k int, v ...string) KString { return String(k, v...) }
func (kv KUint64) Set(tv *TValues) error { return tv.SetUint64(kv.Key, kv.Val) }
func (kv KInt64) Set(tv *TValues) error { return tv.SetInt64(kv.Key, kv.Val) }
func (kv KBool) Set(tv *TValues) error { return tv.SetBool(kv.Key, kv.Val) }
func (kv KString) Set(tv *TValues) error { return tv.SetString(kv.Key, kv.Val) }
func (kv KUint64) Get(tv *TValues) (KVal, error) { v, e := tv.Uint64(kv.Key); kv.Val = v; return kv, e }
func (kv KInt64) Get(tv *TValues) (KVal, error) { v, e := tv.Int64(kv.Key); kv.Val = v; return kv, e }
func (kv KBool) Get(tv *TValues) (KVal, error) { v, e := tv.Bool(kv.Key); kv.Val = v; return kv, e }
func (kv KString) Get(tv *TValues) (KVal, error) { v, e := tv.String(kv.Key); kv.Val = v; return kv, e }
func (kv KUint64) Uint64() uint64 { return kv.Val }
func (kv KInt64) Uint64() uint64 { return uint64(kv.Val) }
func (kv KBool) Uint64() uint64 { return toUint64(kv.Val) }
func (kv KString) Uint64() uint64 { v, _ := strconv.Atoi(kv.Val); return uint64(v) }
func (kv KUint64) Int64() int64 { return int64(kv.Val) }
func (kv KInt64) Int64() int64 { return kv.Val }
func (kv KBool) Int64() int64 { return int64(toUint64(kv.Val)) }
func (kv KString) Int64() int64 { v, _ := strconv.Atoi(kv.Val); return int64(v) }
func (kv KUint64) Bool() bool { return kv.Val != 0 }
func (kv KInt64) Bool() bool { return kv.Val != 0 }
func (kv KBool) Bool() bool { return kv.Val }
func (kv KString) Bool() bool { return kv.Val != "" }
func (kv KUint64) String() string { return strconv.FormatUint(kv.Val, 10) }
func (kv KInt64) String() string { return strconv.FormatInt(kv.Val, 10) }
func (kv KBool) String() string { return "" }
func (kv KString) String() string { return kv.Val }
func toUint64(v bool) uint64 {
if v {
return 1
}
return 0
}
func defaultUint64(defaultValue ...uint64) uint64 {
if len(defaultValue) == 0 {
return 0
}
return defaultValue[0]
}
func defaultInt64(defaultValue ...int64) int64 {
if len(defaultValue) == 0 {
return 0
}
return defaultValue[0]
}
func defaultBool(defaultValue ...bool) bool {
if len(defaultValue) == 0 {
return false
}
return defaultValue[0]
}
func defaultString(defaultValue ...string) string {
if len(defaultValue) == 0 {
return ""
}
return defaultValue[0]
}
func checkWrite(key int) error {
if key < 0 {
return fmt.Errorf("key=%d must not be negative", key)
}
if key > MaxValues {
return fmt.Errorf("key=%d is over max=%d storage", MaxValues, key)
}
return nil
}
func (tv TValues) checkRead(key int) error {
if key < 0 {
return fmt.Errorf("key=%d must not be negative", key)
}
if key >= len(tv.Values) {
return fmt.Errorf("key=%d out of range: max=%d", key, len(tv.Values)-1)
}
return nil
}
func (tv *TValues) set(key int, buf []byte) {
if key == len(tv.Values) {
tv.Values = append(tv.Values, buf)
return
}
if key >= cap(tv.Values) {
values := make([][]byte, MaxValues+1)
copy(values, tv.Values)
tv.Values = values
}
if key >= len(tv.Values) {
tv.Values = tv.Values[:key+1]
}
tv.Values[key] = buf
}