-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.go
67 lines (59 loc) · 1.56 KB
/
export.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
package export
import (
"encoding/json"
"fmt"
"os"
"service/user"
)
//Records record structure
type Records struct {
Index string `json:"index"`
Records []user.User `json:"records"`
Total int `json:"total_records"`
}
// Write interface
type Write interface {
WriteRecord(records []Records) error
WriteFile(name string, data []byte, perm os.FileMode) error
}
// WriteFile structure
type WriteFile struct{}
//WriteFile writes data to the named file, creating it if necessary.
func (w WriteFile) WriteFile(name string, data []byte, perm os.FileMode) error {
return os.WriteFile(name, data, perm)
}
//Execute collections in files, using records slice
func Execute(collections map[string][]user.User, write Write) error {
var records []Records
for i, j := range collections {
tempRecord := Records{Index: i, Records: j, Total: len(j)}
records = append(records, tempRecord)
}
err := write.WriteRecord(records)
if err != nil {
return fmt.Errorf("write record error: %v", err)
}
return nil
}
// WriteRecord Write record in file
func (w *WriteFile) WriteRecord(records []Records) error {
for _, record := range records {
file, err := encoding(record)
if err != nil {
return fmt.Errorf("marshal error: %w", err)
}
name := record.Index + ".json"
err = w.WriteFile(name, file, 0644)
if err != nil {
return fmt.Errorf("cannot write the file: %w", err)
}
}
return nil
}
func encoding(record Records) ([]byte, error) {
file, err := json.Marshal(record)
if err != nil {
return nil, fmt.Errorf("marshal error: %w", err)
}
return file, nil
}