-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_loader.go
61 lines (49 loc) · 1.2 KB
/
file_loader.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
package dbuf
import (
"io"
"time"
)
// FileLoadSystem 文件加载系统接口
type FileLoadSystem interface {
// Detect the latest file and its modify time
Detect(url string) (filePath string, modify time.Time, err error)
// Read file
Read(filePath string) (io.Reader, error)
}
// FileLoader 文件加载器
type FileLoader struct {
url string
system FileLoadSystem
load LoadFunc
lastModify time.Time
}
// NewFileLoader 创建文件加载器
func NewFileLoader(system FileLoadSystem, url string, load LoadFunc) *FileLoader {
return &FileLoader{
url: url,
system: system,
load: load,
}
}
// LoadFunc load from reader
type LoadFunc func(reader io.Reader, i interface{}) error
// Load implement loader.Load
func (loader *FileLoader) Load(i interface{}) (bool, error) {
filePath, modifyTime, err := loader.system.Detect(loader.url)
if err != nil {
return false, err
}
detectNew := modifyTime.After(loader.lastModify)
if !detectNew {
return false, nil
}
reader, err := loader.system.Read(filePath)
if err != nil {
return false, err
}
if err := loader.load(reader, i); err != nil {
return false, err
}
loader.lastModify = modifyTime
return detectNew, nil
}