forked from NullHypothesis/zoossh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic.go
49 lines (39 loc) · 1.34 KB
/
generic.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
// Provides generic operations for all supported data structures.
package zoossh
import (
"fmt"
)
// Fingerprint represents a relay's fingerprint as 40 hex digits.
type Fingerprint string
// Object defines functions that should be supported by a data element, e.g., a
// router descriptor, or a router status in a consensus.
type Object interface {
String() string
GetFingerprint() Fingerprint
}
// ObjectSet defines functions that should be supported by a set of objects.
type ObjectSet interface {
Length() int
Iterate() <-chan Object
GetObject(Fingerprint) (Object, bool)
Merge(ObjectSet)
}
// ParseUnknownFile attempts to parse a file whose content we don't know. We
// try to use the right parser by looking at the file's annotation. An
// ObjectSet is returned if parsing was successful.
func ParseUnknownFile(fileName string) (ObjectSet, error) {
// First, get the file's annotation which we then use to figure out what
// parser we need.
annotation, err := GetAnnotation(fileName)
if err != nil {
return nil, err
}
// Now use the annotation to find the right parser.
if _, ok := descriptorAnnotations[*annotation]; ok {
return ParseDescriptorFile(fileName)
}
if _, ok := consensusAnnotations[*annotation]; ok {
return ParseConsensusFile(fileName)
}
return nil, fmt.Errorf("Could not find suitable parser for file %s.", fileName)
}