-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
classifier.go
41 lines (35 loc) · 868 Bytes
/
classifier.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
package classifier
import (
"context"
"errors"
"github.com/bitmagnet-io/bitmagnet/internal/model"
"go.uber.org/zap"
)
var (
ErrNoMatch = errors.New("no match")
)
type Classifier interface {
Classify(ctx context.Context, torrent model.Torrent) (Classification, error)
}
type SubClassifier interface {
Classifier
Key() string
Priority() int
}
type classifier struct {
subClassifiers []SubClassifier
logger *zap.SugaredLogger
}
func (c classifier) Classify(ctx context.Context, t model.Torrent) (Classification, error) {
for _, sc := range c.subClassifiers {
tc, err := sc.Classify(ctx, t)
if err == nil {
return tc, nil
}
if !errors.Is(err, ErrNoMatch) {
c.logger.Errorw("error classifying content", "classifier", sc.Key(), "torrent", t, "error", err)
return Classification{}, err
}
}
return Classification{}, ErrNoMatch
}