-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
56 lines (47 loc) · 1.11 KB
/
registry.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
package ecg
import (
"sort"
"strings"
)
type FileFormatFactory func() FileFormat
var (
fileFormats []FileFormatFactory
sorted = false
)
func Register(fileFormat FileFormatFactory) {
fileFormats = append(fileFormats, fileFormat)
}
func FileFormats() []FileFormat {
ffs := make([]FileFormat, len(fileFormats))
var af *BasicSurveyor
for i, fff := range fileFormats {
ffs[i] = fff()
if afg, ok := ffs[i].(BasicSurveyorGetter); ok {
af = afg.BasicSurveyor()
}
}
if af != nil {
for _, ff := range ffs {
if afs, ok := ff.(BasicSurveyorSetter); ok {
afs.SetBasicSurveyor(af)
}
}
}
if !sorted {
sort.Sort(FileFormatsSorter(ffs))
sorted = true
}
return ffs
}
type FileFormatsSorter []FileFormat
func (l FileFormatsSorter) Len() int {
return len(l)
}
func (l FileFormatsSorter) Less(i, j int) bool {
// I actually don't care about the order right now, just needs to be consistent and All Files first.
// TODO add priority perhaps dynamic based on confidence?
return strings.Compare(l[i].Name(), l[j].Name()) < 0
}
func (l FileFormatsSorter) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}