/
driver.go
107 lines (87 loc) · 1.93 KB
/
driver.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
package jsondir
import (
"encoding/json"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"regexp"
"github.com/alash3al/xyr/utils"
)
// Driver represents the main importer driver
type Driver struct {
dir string
}
// Open implements Importer#open
func (d *Driver) Open(source string) error {
info, err := os.Stat(source)
if err != nil {
return fmt.Errorf("unable to open (%s) due to: %s", source, err)
}
if !info.IsDir() {
return fmt.Errorf("the provided path (%s) isn't a directory", source)
}
d.dir = source
return nil
}
// Import implements Importer#import
func (d *Driver) Import(loaderRegexp string) (<-chan map[string]interface{}, <-chan error, <-chan bool) {
resultChan := make(chan map[string]interface{})
errChan := make(chan error)
doneChan := make(chan bool)
go (func() {
defer (func() {
doneChan <- true
close(resultChan)
close(errChan)
close(doneChan)
})()
re, err := regexp.Compile(loaderRegexp)
if err != nil {
errChan <- err
return
}
errChan <- filepath.Walk(
d.dir,
func(path string, info fs.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if !re.MatchString(path) {
return nil
}
file, err := os.Open(path)
if err != nil {
errChan <- err
return nil
}
decoder := json.NewDecoder(file)
for {
var val interface{}
if decoder.Decode(&val) == io.EOF {
break
}
switch val := val.(type) {
case map[string]interface{}:
resultChan <- val
case []interface{}:
mSlice, err := utils.InterfaceSliceToMapStringInterfaceSlice(val)
if err != nil {
errChan <- err
continue
} else {
for _, item := range mSlice {
resultChan <- item
}
}
default:
errChan <- fmt.Errorf("unsupported value (%v), we only support array of objects or just objects", val)
}
}
return nil
},
)
})()
return resultChan, errChan, doneChan
}