forked from ixre/gof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
245 lines (220 loc) · 4.73 KB
/
registry.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
package gof
import (
"errors"
"fmt"
"github.com/pelletier/go-toml"
"os"
"path/filepath"
"strings"
)
// registry
type Registry struct {
path string
delimer string
data map[string]*RegistryTree
}
type RegistryTree struct {
key string
filePath string
isRoot bool
tree *toml.Tree
glob *Registry
}
// create new registry manager
func NewRegistry(path string, delimer string) (*Registry, error) {
if delimer == "" {
delimer = "."
}
return (&Registry{
path: path,
delimer: delimer,
}).init()
}
// load from config files
func (r *Registry) init() (*Registry, error) {
r.data = map[string]*RegistryTree{}
return r.dirInit()
}
func (r *Registry) dirInit() (*Registry, error) {
d, err := os.Open(r.path)
if err != nil {
return r, err
}
files, err := d.Readdir(-1)
if err != nil {
return r, err
}
for _, f := range files {
if err = r.load(r.path+"/"+f.Name(), f); err != nil {
return r, errors.New(f.Name() + ":" + err.Error())
}
}
return r, nil
}
func LoadToml(path string) (*toml.Tree, error) {
return toml.LoadFile(path)
}
func (r *Registry) load(path string, info os.FileInfo) error {
if info.IsDir() {
return nil
}
if name := info.Name(); strings.HasSuffix(name, ".conf") {
t, err := toml.LoadFile(path)
if err == nil {
key := name[:len(name)-5]
tree := &RegistryTree{
isRoot: true,
key: key,
filePath: path,
tree: t,
glob: r,
}
r.data[key] = tree
}
return err
}
return nil
}
// get file key and config key
func (r *Registry) split(key string) []string {
arr := strings.Split(key, r.delimer)
if len(arr) <= 1 {
panic("registry key not contain delimer '" + r.delimer + "'")
}
return arr
}
// get registry pair
func (r *Registry) Get(key string) interface{} {
i := strings.Index(key, r.delimer)
return r.Use(key[:i]).Get(key[i+1:])
}
func (r *Registry) Set(key string, v interface{}) error {
i := strings.Index(key, r.delimer)
rt := r.Use(key[:i])
err := rt.Set(key[i+1:], v)
if err == nil {
err = rt.Flush()
}
return err
}
func (r *Registry) Use(key string) *RegistryTree {
t, b := r.data[key]
if !b {
t = &RegistryTree{
isRoot: true,
key: key,
filePath: r.path + key + ".conf",
tree: nil,
glob: r,
}
}
return t
}
// get string
func (r *Registry) GetString(key string) string {
defer getRecover(key)
return r.Get(key).(string)
}
func getRecover(key string) {
if err := recover(); err != nil {
panic(fmt.Sprintf("key=%s; err=%s", key, err))
}
}
func (r *Registry) createNode(arr []string, value interface{}) (*toml.Tree, error) {
key := arr[0]
tree, err := toml.TreeFromMap(map[string]interface{}{})
if len(arr) == 1 {
tree.Set(key, value)
} else {
tree2, _ := toml.TreeFromMap(map[string]interface{}{
arr[1]: value,
})
tree.Set(key, tree2)
}
return tree, err
}
func (r *RegistryTree) Exists() bool {
return r.tree != nil
}
// get registry pair
func (r *RegistryTree) Get(prop string) interface{} {
if r.tree != nil {
arr := r.desSplit(prop)
if len(arr) == 1 {
return r.tree.Get(arr[0])
}
tree, result := r.tree.Get(arr[0]).(*toml.Tree)
if !result || tree == nil {
return nil
}
return tree.Get(arr[1])
}
return nil
}
// is contains prop
func (r *RegistryTree) Contains(prop string) bool {
return r.Get(prop) != nil
}
func (r *RegistryTree) GetString(prop string) string {
defer getRecover(prop)
return r.Get(prop).(string)
}
func (r *RegistryTree) GetInt(prop string) int {
defer getRecover(prop)
return r.Get(prop).(int)
}
func (r *RegistryTree) GetFloat(prop string) float64 {
defer getRecover(prop)
return r.Get(prop).(float64)
}
func (r *RegistryTree) GetBool(prop string) bool {
defer getRecover(prop)
return r.Get(prop).(bool)
}
// get file key and config key
func (r *RegistryTree) desSplit(key string) []string {
i := strings.LastIndex(key, r.glob.delimer)
if i > 0 {
return []string{key[:i], key[i+1:]}
}
return []string{key}
}
// set registry pair
func (r *RegistryTree) Set(prop string, value interface{}) error {
arr := r.desSplit(prop)
if r.tree == nil {
tree, err := r.glob.createNode(arr, value)
if err != nil {
return err
}
r.tree = tree
r.glob.data[r.key] = r
return nil
}
if len(arr) == 1 {
r.tree.Set(arr[0], value)
} else {
tree := r.tree.Get(arr[0]).(*toml.Tree)
if tree == nil {
return errors.New("no such node " + arr[0])
}
tree.Set(arr[1], value)
}
return nil
}
func (r *RegistryTree) Flush() error {
dir := filepath.Dir(r.filePath)
_, err := os.Stat(dir)
if os.IsNotExist(err) {
err = os.MkdirAll(dir, os.ModePerm)
}
if err == nil {
var fi *os.File
fi, err = os.OpenFile(r.filePath, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err == nil {
_, err = r.tree.WriteTo(fi)
fi.Close()
}
}
return err
}