-
Notifications
You must be signed in to change notification settings - Fork 4
/
file.go
69 lines (60 loc) · 1.72 KB
/
file.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
package pulse
import (
"cmp"
"time"
)
// GitFile represents a file within a git repository.
type GitFile struct {
Name string
Filetype string
Repository string
Path string
}
// File represents a file that has been aggregated
// for a given time period (day, week, month, year).
type File struct {
Name string `json:"name"`
Path string `json:"path"`
Filetype string `json:"filetype"`
Duration time.Duration `json:"duration"`
}
// merge takes two files, merges them, and returns the result.
func (a File) merge(b File) File {
return File{
Name: cmp.Or(a.Name, b.Name),
Path: cmp.Or(a.Path, b.Path),
Filetype: cmp.Or(a.Filetype, b.Filetype),
Duration: a.Duration + b.Duration,
}
}
// Files represents a slice of files that has been aggregated
// for a given time period (day, week, month, year).
type Files []File
// createPathFileMap takes a slice of files and produces a map, where the
// filepath is used as the key and the file itself serves as the value.
func createPathFileMap(files Files) map[string]File {
pathFileMap := make(map[string]File)
for _, f := range files {
pathFileMap[f.Path] = f
}
return pathFileMap
}
// merge takes two slices of files, merges them, and returns the result.
func (a Files) merge(b Files) Files {
aFileMap := createPathFileMap(a)
bFileMap := createPathFileMap(b)
allPaths := make(map[string]struct{})
for path := range aFileMap {
allPaths[path] = struct{}{}
}
for path := range bFileMap {
allPaths[path] = struct{}{}
}
mergedFiles := make([]File, 0, len(allPaths))
for path := range allPaths {
aFile := aFileMap[path]
bFile := bFileMap[path]
mergedFiles = append(mergedFiles, aFile.merge(bFile))
}
return mergedFiles
}