-
Notifications
You must be signed in to change notification settings - Fork 0
/
stres.go
460 lines (372 loc) · 9.88 KB
/
stres.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package stres
import (
"errors"
"io/ioutil"
"os"
"strings"
"sync"
"github.com/Vinetwigs/stres/types"
)
var fileType types.FileType
var encDec = types.EncoderDecoder{}
var quantityValues = [...]string{"zero", "one", "two", "few", "many"}
var data []byte
var string_entries map[string]string = make(map[string]string)
var string_array_entries map[string]types.StringArray = make(map[string]types.StringArray)
var plural_string_entries map[string]types.Plural = make(map[string]types.Plural)
var few_threshold = 20
const (
XML types.FileType = "xml"
YAML types.FileType = "yml"
JSON types.FileType = "json"
TOML types.FileType = "toml"
WATSON types.FileType = "watson"
MSGPACK types.FileType = "msgpack"
)
var (
ErrorEmptyStringName error = errors.New("stres: string name can't be empty")
ErrorEmptyStringArrayName error = errors.New("stres: string-array name can't be empty")
ErrorEmptyQuantityStringName error = errors.New("stres: quantity string name can't be empty")
ErrorDuplicateStringName error = errors.New("stres: string name already inserted")
ErrorDuplicateStringArrayName error = errors.New("stres: string-array name already inserted")
ErrorDuplicateQuantityStringName error = errors.New("stres: quantity string name already inserted")
ErrorQuantityStringPluralNotFound error = errors.New("stres: plural not found for the given quantity")
ErrorQuantityStringEmptyValues error = errors.New("stres: provided empty array to quantity string creationg")
)
/*
Loads values from strings file into internal dictionaries.
Needs to be invoked only one time (but before getting strings values).
Takes a FileType parameter to specify strings file format.
*/
func LoadValues(t types.FileType) error {
SetResourceType(t)
var err error
n := &types.Nesting{}
data, err = readBytes(strings.Join([]string{"./strings/strings.", string(t)}, ""))
if err != nil {
return err
}
err = encDec.Decode(data, &n)
if err != nil {
return err
}
var wg sync.WaitGroup
wg.Add(3)
// Load strings
go func() {
defer wg.Done()
for i := 0; i < len(n.Strings); i++ {
string_entries[n.Strings[i].Name] = n.Strings[i].Value
}
}()
// Load string arrays
go func() {
defer wg.Done()
for i := 0; i < len(n.StringsArray); i++ {
string_array_entries[n.StringsArray[i].Name] = *n.StringsArray[i]
}
}()
// Load quantity strings
go func() {
defer wg.Done()
for i := 0; i < len(n.Plurals); i++ {
plural_string_entries[n.Plurals[i].Name] = *n.Plurals[i]
}
}()
wg.Wait()
return nil
}
/*
Used to specify string file extension. If t is a wrong FileType, sets resource type to XML by default.
*/
func SetResourceType(t types.FileType) {
switch t {
case XML:
xmlED := &types.XMLStrategy{}
encDec.SetStrategy(xmlED)
fileType = XML
case JSON:
jsonED := &types.JSONStrategy{}
encDec.SetStrategy(jsonED)
fileType = JSON
case YAML:
yamlED := &types.YAMLStrategy{}
encDec.SetStrategy(yamlED)
fileType = YAML
case TOML:
tomlED := &types.TOMLStrategy{}
encDec.SetStrategy(tomlED)
fileType = TOML
case WATSON:
watsonED := &types.WatsonStrategy{}
encDec.SetStrategy(watsonED)
fileType = WATSON
case MSGPACK:
msgpackED := &types.MsgPackStrategy{}
encDec.SetStrategy(msgpackED)
fileType = MSGPACK
default:
xmlED := &types.XMLStrategy{}
encDec.SetStrategy(xmlED)
fileType = XML
}
}
/*
Creates strings resource file in "strings" directory, throws an error otherwise.
Takes a FileType parameter to specify strings file format.
*/
func CreateResourceFile(t types.FileType) (*os.File, error) {
fileType = t
SetResourceType(t)
os.Mkdir("strings", os.ModePerm)
file, err := os.Create("strings/strings." + string(fileType))
if err != nil {
return nil, err
}
_, err = NewString("name", "value")
if err != nil {
return nil, err
}
return file, nil
}
/*
Deletes resource file if exists, throws an error otherwise.
Uses setted resource file extension.
*/
func DeleteResourceFile() error {
err := os.Remove("strings/strings." + string(fileType))
if err != nil {
return err
}
err = os.Remove("strings")
if err != nil {
return err
}
return nil
}
/*
Adds a new string resource to resource file. Throws an error if the chosen name is already inserted or it is an empty string.
*/
func NewString(name, value string) (types.String, error) {
if strings.TrimSpace(name) == "" {
return *new(types.String), ErrorEmptyStringName
}
if isDuplicateString(name) {
return *new(types.String), ErrorDuplicateStringName
}
string_entries[name] = value
var err error
s := types.String{
Name: name,
Value: value,
}
n := &types.Nesting{}
data, err = readBytes(strings.Join([]string{"./strings/strings.", string(fileType)}, ""))
if err != nil {
return *new(types.String), err
}
err = encDec.Decode(data, &n)
if err != nil {
return *new(types.String), err
}
n.Strings = append(n.Strings, &s)
data, err = encDec.Encode(n)
if err != nil {
return *new(types.String), err
}
err = writeBytes(strings.Join([]string{"./strings/strings.", string(fileType)}, ""), data)
if err != nil {
return *new(types.String), err
}
return s, nil
}
/*
Adds a new string-array resource to resource file. Throws an error if the chosen name is already inserted or it is an empty string.
*/
func NewStringArray(name string, values []string) (types.StringArray, error) {
if strings.TrimSpace(name) == "" {
return *new(types.StringArray), ErrorEmptyStringArrayName
}
if isDuplicateStringArray(name) {
return *new(types.StringArray), ErrorDuplicateStringArrayName
}
sa := &types.StringArray{Name: name}
for i := 0; i < len(values); i++ {
item := &types.Item{
Value: values[i],
}
sa.Items = append(sa.Items, item)
}
string_array_entries[name] = *sa
var err error
n := &types.Nesting{}
data, err = readBytes("./strings/strings." + string(fileType))
if err != nil {
return *new(types.StringArray), err
}
err = encDec.Decode(data, &n)
if err != nil {
return *new(types.StringArray), err
}
n.StringsArray = append(n.StringsArray, sa)
data, err = encDec.Encode(n)
if err != nil {
return *new(types.StringArray), err
}
err = writeBytes("strings/strings."+string(fileType), data)
if err != nil {
return *new(types.StringArray), err
}
return *sa, nil
}
/*
Adds a new quantity string resource to resource file.
Throws an error if the chosen name is already inserted or it is an empty string.
The function uses only the first 5 values in the array.
The first values is assigned to "zero" quantity.
The second values is assigned to "one" quantity.
The third values is assigned to "two" quantity.
The fourth values is assigned to "few" quantity.
The fifth values is assigned to "more" quantity.
*/
func NewQuantityString(name string, values []string) (types.Plural, error) {
if strings.TrimSpace(name) == "" {
return *new(types.Plural), ErrorEmptyStringArrayName
}
if len(values) == 0 {
return *new(types.Plural), ErrorQuantityStringEmptyValues
}
if isDuplicateQuantityString(name) {
return *new(types.Plural), ErrorDuplicateQuantityStringName
}
pl := &types.Plural{Name: name}
for i := 0; i < len(values) && i < 5; i++ {
item := &types.PluralItem{
Quantity: quantityValues[i],
Value: values[i],
}
pl.Items = append(pl.Items, item)
}
plural_string_entries[name] = *pl
var err error
n := &types.Nesting{}
data, err = readBytes("./strings/strings." + string(fileType))
if err != nil {
return *new(types.Plural), err
}
err = encDec.Decode(data, &n)
if err != nil {
return *new(types.Plural), err
}
n.Plurals = append(n.Plurals, pl)
data, err = encDec.Encode(n)
if err != nil {
return *new(types.Plural), err
}
err = writeBytes("strings/strings."+string(fileType), data)
if err != nil {
return *new(types.Plural), err
}
return *pl, nil
}
/*
Sets the threshold for "few" values in quantity strings.
When getting quantity strings values, the function checks if the given count is less OR EQUAL to this value.
(default value: 20)
*/
func SetFewThreshold(value int) {
few_threshold = value
}
/*
Returns the string resource's value with the given name. If not exists, returns empty string.
*/
func GetString(name string) string {
if name == "" {
return ""
}
if val, ok := string_entries[name]; ok {
return val
}
return ""
}
/*
Returns the string-array resource's values with the given name. If not exists, returns nil.
*/
func GetArrayString(name string) []string {
if name == "" {
return nil
}
var arr []string
if _, ok := string_array_entries[name]; ok {
for i := 0; i < len(string_array_entries[name].Items); i++ {
item := string_array_entries[name].Items
el := item[i].Value
arr = append(arr, el)
}
return arr
}
return nil
}
/*
Returns the quantity string resource's corresponding string value based on the value of the given count parameter.
If the plural is not found, returns an empty string.
*/
func GetQuantityString(name string, count int) string {
if name == "" {
return ""
}
idx := -1
val, exists := plural_string_entries[name]
if !exists {
return ""
}
if count == 0 {
idx = 0
}
if count == 1 {
idx = 1
}
if count == 2 {
idx = 2
}
if count > 2 && count <= few_threshold {
idx = 3
}
if idx == -1 {
idx = 4
}
for i := 0; i < len(val.Items); i++ {
if val.Items[i].Quantity == quantityValues[idx] {
return val.Items[i].Value
}
}
return ""
}
func readBytes(path string) ([]byte, error) {
d, err := ioutil.ReadFile(path)
if err != nil {
return *new([]byte), err
}
return d, nil
}
func isDuplicateString(name string) bool {
if _, ok := string_entries[name]; ok {
return true
}
return false
}
func isDuplicateStringArray(name string) bool {
if _, ok := string_array_entries[name]; ok {
return true
}
return false
}
func isDuplicateQuantityString(name string) bool {
if _, ok := plural_string_entries[name]; ok {
return true
}
return false
}
func writeBytes(path string, data []byte) error {
return os.WriteFile(path, data, 0666)
}