-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.go
More file actions
65 lines (51 loc) · 1.17 KB
/
init.go
File metadata and controls
65 lines (51 loc) · 1.17 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
package language_wizard
import (
"sync"
"sync/atomic"
)
// // // // // // // // // // // //
type LanguageWizardObj struct {
currentLanguage string
words map[string]string
mx sync.RWMutex
changedCh chan struct{}
closed atomic.Bool
log func(string)
}
func New(isoLanguage string, words map[string]string) (*LanguageWizardObj, error) {
if err := validateLangAndWords(isoLanguage, words); err != nil {
return nil, err
}
obj := new(LanguageWizardObj)
obj.currentLanguage = isoLanguage
obj.words = cloneWords(words)
obj.changedCh = make(chan struct{})
obj.log = func(s string) {}
return obj, nil
}
func validateLangAndWords(isoLanguage string, words map[string]string) error {
if isoLanguage == "" {
return ErrNilIsoLang
}
if len(words) == 0 {
return ErrNilWords
}
return nil
}
func cloneWords(src map[string]string) map[string]string {
dst := make(map[string]string, len(src))
for k, v := range src {
dst[k] = v
}
return dst
}
func (obj *LanguageWizardObj) Close() {
obj.mx.Lock()
defer obj.mx.Unlock()
if obj.closed.Load() {
return
}
obj.closed.Store(true)
close(obj.changedCh)
obj.words = make(map[string]string)
}