forked from nathanielc/morgoth
/
fingerprint.go
52 lines (42 loc) · 1.14 KB
/
fingerprint.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
package morgoth
import (
"errors"
"fmt"
"github.com/nathanielc/morgoth/config"
"github.com/nathanielc/morgoth/counter"
)
type Fingerprint interface {
IsMatch(other counter.Countable) bool
}
type Fingerprinter interface {
Fingerprint(window Window) Fingerprint
}
var FingerprinterRegistery *config.Registery
func init() {
FingerprinterRegistery = config.NewRegistry()
}
type FingerprinterConf struct {
config.DynamicConfiguration
}
func (self *FingerprinterConf) UnmarshalYAML(unmarshal func(interface{}) error) error {
return self.PerformUnmarshalYAML(FingerprinterRegistery, unmarshal)
}
func FingerprinterFromYAML(yaml string) (*FingerprinterConf, error) {
conf := new(FingerprinterConf)
err := config.PerformFromYAML(yaml, conf)
if err != nil {
return nil, err
}
return conf, nil
}
func (self *FingerprinterConf) GetFingerprinter() (Fingerprinter, error) {
instance, err := self.PerformGetInstance(FingerprinterRegistery)
if err != nil {
return nil, err
}
fingerprinter, ok := instance.(Fingerprinter)
if !ok {
return nil, errors.New(fmt.Sprintf("Instance %v is not of type Fingerprinter", instance))
}
return fingerprinter, nil
}