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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
package store
import (
"bytes"
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"time"
)
type Store struct {
Dir string
}
func NewStore(dir string) (*Store, error) {
// .开头的为相对路径,补全为全路径
if strings.HasPrefix(dir, ".") {
pwd, _ := os.Getwd()
dir = filepath.Join(pwd, dir)
}
store := &Store{Dir: dir}
st, err := os.Stat(dir)
if err != nil {
err = os.Mkdir(dir, 0755)
if err != nil {
return nil, err
}
} else if st != nil && !st.IsDir() {
return nil, errors.New("file already exists")
}
return store, nil
}
// 创建与结构体对应的json文件
func (s *Store) Sync(values ...interface{}) error {
for _, v := range values {
tb := parseTn(v)
if tb == "" {
return errors.New("does not find store")
}
_path := filepath.Join(s.Dir, tb)
_, err := os.Stat(_path)
if err != nil {
_ = ioutil.WriteFile(_path, []byte("[]"), 0755)
}
}
return nil
}
// 删除所有
func (s *Store) Destroy() error {
return os.RemoveAll(s.Dir)
}
func (s *Store) FindAll(v interface{}) error {
_path, err := s.before(v)
if err != nil {
return err
}
out, err := s.readAll(_path)
if err != nil {
return err
}
err = json.Unmarshal(out, &v)
return err
}
func (s *Store) FindOne(v interface{}, columns ...string) (interface{}, error) {
_path, err := s.before(v)
if err != nil {
return nil, err
}
data, err := s.readAll(_path)
if err != nil {
return nil, err
}
fields := make([]map[string]interface{}, 0)
err = json.Unmarshal(data, &fields)
if err != nil {
return nil, err
}
m := structToMap(v)
length := len(columns)
for _, item := range fields {
for i := 0; i < length; i++ {
// TODO 这里的比较需要改进
if item[columns[i]] != m[columns[i]] {
break
}
if i == length-1 {
m = item
goto OVER
}
}
}
OVER:
err = mapToStruct(m, &v)
if err != nil {
return nil, err
}
return v, nil
}
func (s *Store) Create(v interface{}) error {
_path, err := s.before(v)
if err != nil {
return err
}
data, err := s.readAll(_path)
if err != nil {
return err
}
fields := make([]map[string]interface{}, 0)
err = json.Unmarshal(data, &fields)
if err != nil {
return err
}
m := structToMap(v)
m["_id"] = randId()
fields = append(fields, m)
err = s.writeAll(_path, fields)
if err != nil {
return err
}
err = mapToStruct(m, v)
if err != nil {
return err
}
return nil
}
func (s *Store) Update(v interface{}, columns ...string) error {
_path, err := s.before(v)
if err != nil {
return err
}
data, err := s.readAll(_path)
if err != nil {
return err
}
fields := make([]map[string]interface{}, 0)
err = json.Unmarshal(data, &fields)
if err != nil {
return err
}
m := structToMap(v)
for _, v := range fields {
if v["_id"] == m["_id"] {
for _, col := range columns {
v[col] = m[col]
}
m = v
}
}
err = s.writeAll(_path, fields)
if err != nil {
return err
}
return nil
}
func (s *Store) Delete(v interface{}) error {
_path, err := s.before(v)
if err != nil {
return err
}
data, err := s.readAll(_path)
if err != nil {
return err
}
fields := make([]map[string]interface{}, 0)
err = json.Unmarshal(data, &fields)
if err != nil {
return err
}
m := structToMap(v)
length := len(fields)
for index, field := range fields {
if field["_id"] == m["_id"] {
if index == length-1 {
fields = fields[0:index]
} else {
fields = append(fields[0:index], fields[index+1:]...)
}
}
}
err = s.writeAll(_path, fields)
if err != nil {
return err
}
return nil
}
func (s *Store) Clean(v interface{}) error {
_path, err := s.before(v)
if err != nil {
return err
}
return os.Remove(_path)
}
func (s *Store) readAll(file string) ([]byte, error) {
out, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
return out, nil
}
func (s *Store) writeAll(file string, v interface{}) error {
out, err := json.MarshalIndent(v, "", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(file, out, 0755)
if err != nil {
return err
}
return nil
}
func (s *Store) before(v interface{}) (string, error) {
tb := parseTn(v)
if tb == "" {
return "", errors.New("invalid table name")
}
_path := filepath.Join(s.Dir, tb)
_, err := os.Stat(_path)
if err != nil {
return "", err
}
return _path, nil
}
func structToMap(v interface{}) map[string]interface{} {
tp := reflect.TypeOf(v).Elem()
vp := reflect.ValueOf(v).Elem()
field := make(map[string]interface{}, 0)
for i := 0; i < tp.NumField(); i++ {
field1 := tp.Field(i)
field2 := vp.Field(i)
key := field1.Tag.Get("json")
field[key] = field2.Interface()
switch field2.Kind() {
case reflect.Int:
field[key] = float64(field2.Interface().(int))
case reflect.Int8:
field[key] = float64(field2.Interface().(int8))
case reflect.Int16:
field[key] = float64(field2.Interface().(int16))
case reflect.Int32:
field[key] = float64(field2.Interface().(int32))
case reflect.Int64:
field[key] = float64(field2.Interface().(int64))
case reflect.Uint:
field[key] = float64(field2.Interface().(uint))
case reflect.Uint8:
field[key] = float64(field2.Interface().(uint8))
case reflect.Uint16:
field[key] = float64(field2.Interface().(uint16))
case reflect.Uint32:
field[key] = float64(field2.Interface().(uint32))
case reflect.Uint64:
field[key] = float64(field2.Interface().(uint64))
case reflect.Float32:
field[key] = float64(field2.Interface().(float32))
case reflect.Float64:
field[key] = field2.Interface()
default:
field[key] = field2.Interface()
}
}
return field
}
func mapToStruct(m map[string]interface{}, v interface{}) error {
out, err := json.Marshal(m)
if err != nil {
return err
}
return json.Unmarshal(out, &v)
}
func toSnake(s string) string {
out := bytes.Buffer{}
bName := []byte(s)
point := 0
for index, b := range bName {
// 非大写,不需要转化
if b < 65 || b > 90 || index-point < 2 {
out.WriteByte(b)
continue
}
// 首字符大写,直接转化为小写
if index == 0 {
out.WriteByte(b + 32)
point = index
}
// 连续三个大写,触发转化
if index-point >= 2 {
out.WriteByte(95)
out.WriteByte(b + 32)
point = index
}
}
return out.String()
}
func parseTn(v interface{}) string {
var name string
tp := reflect.TypeOf(v).Elem()
switch tp.Kind() {
case reflect.Ptr:
sp := strings.Split(tp.String(), ".")
name = sp[len(sp)-1]
case reflect.Slice:
sp := strings.Split(tp.String(), ".")
name = sp[len(sp)-1]
case reflect.Struct:
name = tp.Name()
}
name = toSnake(name)
return name + ".json"
}
func randId() string {
return fmt.Sprintf("%x", md5.Sum([]byte(time.Now().String())))
}
|