-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.go
132 lines (100 loc) · 2.93 KB
/
storage.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
package i18n
import (
"sync"
"golang.org/x/text/language"
)
// Storage interface
type Storage interface {
GetAll() ([]*Translation, error)
Store(*Translation) error
Delete(*Translation) error
DefaultLanguage() (language.Tag, error)
SupportedLanguages() ([]language.Tag, error)
SetDefaultLanguage(language.Tag) error
StoreSupportedLanguage(language.Tag) error
DeleteSupportedLanguage(language.Tag) error
}
type inMemoryStorage struct {
lock sync.RWMutex
translations []*Translation
defaultLang language.Tag
supportedLangs []language.Tag
}
// NewInMemoryStorage Creates a non persistent in memory translation store
func NewInMemoryStorage() Storage {
return new(inMemoryStorage)
}
func (storage *inMemoryStorage) SupportedLanguages() ([]language.Tag, error) {
storage.lock.RLock()
defer storage.lock.RUnlock()
return storage.supportedLangs, nil
}
func (storage *inMemoryStorage) DefaultLanguage() (language.Tag, error) {
storage.lock.RLock()
defer storage.lock.RUnlock()
return storage.defaultLang, nil
}
func (storage *inMemoryStorage) StoreSupportedLanguage(tag language.Tag) error {
storage.lock.Lock()
defer storage.lock.Unlock()
for _, l := range storage.supportedLangs {
if l.String() == tag.String() {
return nil
}
}
storage.supportedLangs = append(storage.supportedLangs, tag)
return nil
}
func (storage *inMemoryStorage) DeleteSupportedLanguage(tag language.Tag) error {
storage.lock.Lock()
defer storage.lock.Unlock()
for i, l := range storage.supportedLangs {
if l.String() == tag.String() {
storage.supportedLangs = append(storage.supportedLangs[:i], storage.supportedLangs[i+1:]...)
return nil
}
}
return nil
}
func (storage *inMemoryStorage) SetDefaultLanguage(tag language.Tag) error {
err := storage.StoreSupportedLanguage(tag)
if err != nil {
return err
}
storage.lock.Lock()
defer storage.lock.Unlock()
storage.defaultLang = tag
return nil
}
func (storage *inMemoryStorage) GetAll() ([]*Translation, error) {
storage.lock.RLock()
defer storage.lock.RUnlock()
return storage.translations, nil
}
func (storage *inMemoryStorage) Store(translation *Translation) error {
storage.lock.Lock()
defer storage.lock.Unlock()
for _, t := range storage.translations {
sameLang := t.Lang.String() == translation.Lang.String()
sameKey := t.Key == translation.Key
if sameLang && sameKey {
t.Value = translation.Value
return nil
}
}
storage.translations = append(storage.translations, translation)
return nil
}
func (storage *inMemoryStorage) Delete(translation *Translation) error {
storage.lock.Lock()
defer storage.lock.Unlock()
for i, t := range storage.translations {
sameLang := t.Lang.String() == translation.Lang.String()
sameKey := t.Key == translation.Key
if sameLang && sameKey {
storage.translations, storage.translations[len(storage.translations)-1] = append(storage.translations[:i], storage.translations[i+1:]...), nil
return nil
}
}
return nil
}