-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
262 lines (239 loc) · 6.17 KB
/
store.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
// Created by Yakka (https://theyakka.com)
//
// Copyright (c) 2020 Yakka LLC.
// All rights reserved.
// See the LICENSE file for licensing details and requirements.
package ystore
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"github.com/BurntSushi/toml"
"gopkg.in/yaml.v2"
)
// Store is a giant data map that is constructed from one or more data files
// that are stored within a directory and series of sub-directories
type Store struct {
// data is the primary storage for all the parsed data/config files
data map[string]interface{}
// PrefixDirectories adds a prefix to the data map for any directories that are
// not the top-level directory. For example: given the file
// <datadir>/categories/somecat.toml, the contents of the toml file will live in
// the map under the prefix (key) "categories"
PrefixDirectories bool
// Exclude contains patterns that we should be excluding when walking the data
// directory
Exclude []string
}
//
func NewStore() *Store {
return &Store{
data: map[string]interface{}{},
PrefixDirectories: true,
}
}
func NewStoreWithData(data interface{}) *Store {
if mapData, ok := data.(map[string]interface{}); ok {
return NewStoreFromMap(mapData)
}
store := NewStore()
if mapData, ok := data.(map[interface{}]interface{}); ok {
for k, v := range mapData {
strKey, ok := k.(string)
if !ok {
continue
}
store.Set(strKey, v)
}
}
return store
}
//
func NewStoreFromMap(data map[string]interface{}) *Store {
return &Store{
data: data,
PrefixDirectories: true,
}
}
func NewStoreFromMapWithSubs(data map[string]interface{}) *Store {
store := NewStore()
for k, v := range data {
switch v.(type) {
case map[string]interface{}:
store.Set(k, NewStoreFromMap(v.(map[string]interface{})))
default:
store.Set(k, v)
}
}
return store
}
func (ds *Store) ReadFile(filePath string) error {
// clear the data map
ds.data = map[string]interface{}{}
// check to see if the directory exists
if _, statErr := os.Stat(filePath); statErr != nil {
return statErr
}
// read the data / config files within the directory
dataMap, dataReadErr := ds.readFile(filePath)
if dataReadErr != nil {
return dataReadErr
}
ds.data = dataMap
return nil
}
func (ds *Store) ReadFiles(filePaths ...string) error {
// clear the data map
ds.data = map[string]interface{}{}
// check to see if the directory exists
fullData := map[string]interface{}{}
for _, filePath := range filePaths {
if _, statErr := os.Stat(filePath); statErr != nil {
return statErr
}
// read the data / config files within the directory
fileData, dataReadErr := ds.readFile(filePath)
if dataReadErr != nil {
return dataReadErr
}
MergeMaps(fileData, fullData, nil)
}
ds.data = fullData
return nil
}
// ReadDir will parse all data files within the directory and all sub-directories
func (ds *Store) ReadDir(path string) error {
// clear the data map
ds.data = map[string]interface{}{}
// check to see if the directory exists
statInfo, statErr := os.Stat(path)
if statErr != nil {
return statErr
}
// check to see if the defined directory is actually a directory
if !statInfo.IsDir() {
return errors.New("you must specify a directory")
}
// read the data / config files within the directory
dataMap, dataReadErr := ds.readAllFiles(path)
if dataReadErr != nil {
return dataReadErr
}
ds.data = dataMap
return nil
}
func (ds *Store) readAllFiles(dirPath string) (map[string]interface{}, error) {
var fullData = map[string]interface{}{}
walkErr := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
// skip directories. the walk function will flatten any sub-directories
return nil
}
fileData, fileErr := ds.readFile(path)
if fileErr != nil {
return fileErr
}
fileDir := filepath.Dir(path)
if fileDir != path && ds.PrefixDirectories {
// add the prefix to the file data map because it is in a sub-directory
mapPrefix := BaseDir(path)
fileData = map[string]interface{}{
mapPrefix: fileData,
}
}
// merge the main data map and the file data map
MergeMaps(fileData, fullData, nil)
return nil
})
if walkErr != nil {
return nil, walkErr
}
return fullData, nil
}
func (ds *Store) readFile(filePath string) (map[string]interface{}, error) {
data, dataErr := ioutil.ReadFile(filePath)
if dataErr != nil {
return nil, dataErr
}
var fileMap map[string]interface{}
switch filepath.Ext(filePath) {
case ".toml":
tomlErr := toml.Unmarshal(data, &fileMap)
if tomlErr != nil {
return nil, tomlErr
}
return fileMap, nil
case ".yaml":
fallthrough
case ".yml":
yamlErr := yaml.Unmarshal(data, &fileMap)
if yamlErr != nil {
return nil, yamlErr
}
return fileMap, nil
case ".json":
jsonErr := json.Unmarshal(data, &fileMap)
if jsonErr != nil {
return nil, jsonErr
}
return fileMap, nil
}
return nil, errors.New(fmt.Sprintf("file type (%s) is unsupported", filepath.Ext(filePath)))
}
func (ds *Store) AllValues() map[string]interface{} {
return ds.data
}
func (ds *Store) StoreFromMap(key string) *Store {
value := ds.GetMap(key)
if value == nil {
return nil
}
return NewStoreFromMap(value)
}
func (ds *Store) StoreFromMapOrEmpty(key string) *Store {
value := ds.GetMap(key)
if value == nil {
return NewStore()
}
return NewStoreFromMap(value)
}
func (ds *Store) StoreMatching(pattern string) *Store {
matchRegex, compileErr := regexp.Compile(pattern)
if compileErr != nil {
return nil
}
matches := map[string]interface{}{}
for key, val := range ds.data {
if matchRegex.MatchString(key) {
matches[key] = val
}
}
return NewStoreFromMap(matches)
}
func (ds *Store) Len() int {
return len(ds.data)
}
func (ds *Store) MergeWith(stores ...*Store) *Store {
return MergeStores(append(stores, ds)...)
}
func MergeStores(stores ...*Store) *Store {
finalMap := map[string]interface{}{}
for _, store := range stores {
MergeMaps(store.AllValues(), finalMap, nil)
}
return NewStoreFromMap(finalMap)
}
func (ds *Store) ToJSONString() string {
jsonString, jsonErr := json.Marshal(ds.data)
if jsonErr != nil {
return ""
}
return string(jsonString)
}