-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsplitter.go
55 lines (48 loc) · 967 Bytes
/
splitter.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
package main
import (
"os"
"path"
"time"
)
type splitter struct {
target *Target
files map[string]*os.File
dir string
day string
}
func newSplitter(target *Target, dir string) *splitter {
s := new(splitter)
s.target = target
s.dir = dir
s.files = make(map[string]*os.File)
s.day = day(time.Now())
return s
}
func (s *splitter) split(obj map[string]interface{}) *os.File {
if s.target.Split != nil && s.target.Split.By == "date" {
return s.splitByDay(obj)
}
return s.file(s.day)
}
func (s *splitter) splitByDay(obj map[string]interface{}) *os.File {
value := obj[s.target.Split.Value]
t := toTime(value, s.target.Split.Layout)
key := day(t)
return s.file(key)
}
func (s *splitter) file(key string) *os.File {
f, ok := s.files[key]
if ok {
return f
}
p := path.Join(s.dir, key+".json")
f, err := os.Create(p)
check(err)
s.files[key] = f
return f
}
func (s *splitter) close() {
for _, f := range s.files {
f.Close()
}
}