-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
73 lines (60 loc) · 1.64 KB
/
parser.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
package main
import (
"fmt"
"io"
"os"
"path/filepath"
jsoniter "github.com/json-iterator/go"
)
// parse provides the parsing process.
func (c *config) parse() error {
// Create slice for results.
results := make([]map[string]string, 0, len(c.jsonFolder.files))
// Read JSON files and collect data.
for index, file := range c.jsonFolder.files {
if file.IsDir() {
continue
}
// Open the given file (without defer, see end of this loop).
jsonFile, err := os.Open(filepath.Clean(fmt.Sprintf("%s/%s", c.jsonFolder.path, file.Name())))
if err != nil {
return err
}
// Read data from the given file.
b, err := io.ReadAll(jsonFile)
if err != nil {
return err
}
// Create slice for the current result.
result := make([]map[string]string, 0)
// Unmarshal JSON data to the result slice.
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(b, &result)
if err != nil {
return err
}
// Filtering and qualifying input data.
for _, r := range result {
// Skip unneeded fields and content points and append a current result to the
// main results slice.
if c.filter(r[c.contentField]) {
results = append(results, r)
}
}
// Save data to current CSV chunk.
if index > 0 && index%c.chunkSize == 0 {
// Write a new CSV file with collected (and filtered) data.
if err = c.save(results); err != nil {
return err
}
result = nil // clean current result slice
results = nil // clean main results slice
}
// Physically close the current open file (without defer) to prevent max open
// files error.
err = jsonFile.Close()
if err != nil {
return err
}
}
return nil
}