-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.go
108 lines (89 loc) · 2.01 KB
/
core.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
package textsbox
import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"gopkg.in/yaml.v2"
)
type TextsBox struct {
Files []string
Data []yaml.MapSlice
Cache map[string]interface{}
KeyAlias map[string]string
}
func New() *TextsBox {
return &TextsBox{
Data: make([]yaml.MapSlice, 0),
Cache: make(map[string]interface{}),
KeyAlias: make(map[string]string),
}
}
func (tb *TextsBox) AddKeyAlias(key string, aliases ...string) {
for _, alias := range aliases {
tb.KeyAlias[alias] = key
}
}
func (tb *TextsBox) LoadFile(filename string) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
if err := tb.Load(file); err != nil {
return err
}
return nil
}
func (tb *TextsBox) Load(r io.Reader) error {
fc, err := ioutil.ReadAll(r)
if err != nil {
return err
}
ms := yaml.MapSlice{}
if err := yaml.Unmarshal(fc, &ms); err != nil {
return err
}
tb.Data = append(tb.Data, ms)
return nil
}
func (tb *TextsBox) ResetCache() {
tb.Cache = make(map[string]interface{})
}
func (tb *TextsBox) Find(key, pattern string) (interface{}, error) {
alias, exists := tb.KeyAlias[key]
if exists {
key = alias
}
pattern = fmt.Sprintf("%s.%s", key, pattern)
if value, exists := tb.Cache[pattern]; exists {
return value, nil
}
patternPath := strings.Split(pattern, ".")
for _, ms := range tb.Data {
if value, err := find(patternPath, ms); err == nil {
tb.Cache[pattern] = value
return value, nil
}
}
return "", ErrNotFound{PatternPath: patternPath}
}
type ErrNotFound struct {
PatternPath []string
}
func (e ErrNotFound) Error() string {
pattern := strings.Join(e.PatternPath, ".")
return fmt.Sprintf("Pattern `%s` not found", pattern)
}
func find(patternPath []string, ms yaml.MapSlice) (interface{}, error) {
for _, item := range ms {
if item.Key.(string) == patternPath[0] {
if v, ok := item.Value.(yaml.MapSlice); ok {
return find(patternPath[1:], v)
}
return item.Value, nil
}
}
return "", ErrNotFound{PatternPath: patternPath}
}