-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
classifier.go
162 lines (140 loc) · 3.51 KB
/
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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package classifier
import (
"context"
"errors"
"fmt"
"github.com/bitmagnet-io/bitmagnet/internal/classifier/classification"
"github.com/bitmagnet-io/bitmagnet/internal/model"
"github.com/bitmagnet-io/bitmagnet/internal/protobuf"
"github.com/google/cel-go/cel"
"strings"
)
type Compiler interface {
Compile(source Source) (Runner, error)
}
type Runner interface {
Run(ctx context.Context, workflow string, t model.Torrent) (classification.Result, error)
}
type compiler struct {
options []compilerOption
dependencies dependencies
}
type compilerContext struct {
features
celEnv *cel.Env
source any
path []string
workflowNames map[string]struct{}
}
type compilerOption func(Source, *compilerContext) error
type executionContext struct {
context.Context
dependencies
workflows map[string]action
torrent model.Torrent
torrentPb *protobuf.Torrent
result classification.Result
resultPb *protobuf.Classification
}
func (c executionContext) withResult(result classification.Result) executionContext {
c.result = result
c.resultPb = protobuf.NewClassification(result)
return c
}
func (c compilerContext) child(pathPart string, source any) compilerContext {
c.source = source
newPath := make([]string, len(c.path), len(c.path)+1)
copy(newPath, c.path)
newPath = append(newPath, pathPart)
c.path = newPath
return c
}
func (c compilerContext) error(cause error) error {
if asCompilerError(cause) != nil {
return cause
}
return compilerError{c.path, cause}
}
func (c compilerContext) fatal(cause error) error {
if asFatalCompilerError(cause) != nil {
return cause
}
cErr := asCompilerError(cause)
if cErr != nil {
return fatalCompilerError{compilerError: *cErr}
}
return fatalCompilerError{compilerError{c.path, cause}}
}
func (c compiler) Compile(source Source) (Runner, error) {
ctx := &compilerContext{
source: source,
workflowNames: source.workflowNames(),
}
source, sourceErr := decode[Source](*ctx)
if sourceErr != nil {
return nil, ctx.fatal(sourceErr)
}
for _, opt := range c.options {
if err := opt(source, ctx); err != nil {
return nil, ctx.fatal(err)
}
}
workflowsCtx := ctx.child("workflows", source.Workflows)
workflows := make(map[string]action)
for name, src := range source.Workflows {
a, err := ctx.compileAction(workflowsCtx.child(name, src))
if err != nil {
return nil, ctx.fatal(err)
}
workflows[name] = a
}
return runner{
dependencies: c.dependencies,
workflows: workflows,
}, nil
}
func decodeTo[T any](ctx compilerContext, target *T) error {
decoder, decoderErr := newDecoder(target)
if decoderErr != nil {
return ctx.error(decoderErr)
}
return decoder.Decode(ctx.source)
}
func decode[T any](ctx compilerContext) (T, error) {
var target T
err := decodeTo(ctx, &target)
return target, err
}
type compilerError struct {
path []string
cause error
}
func (e compilerError) Error() string {
return fmt.Sprintf("compiler error at path '%s': %s", strings.Join(e.path, "."), e.cause)
}
func (e compilerError) Unwrap() error {
return e.cause
}
func asCompilerError(err error) *compilerError {
ue := &compilerError{}
if ok := errors.As(err, ue); ok {
return ue
}
return nil
}
type fatalCompilerError struct {
compilerError
}
func (e fatalCompilerError) Unwrap() error {
return e.compilerError
}
func asFatalCompilerError(err error) *fatalCompilerError {
ue := &fatalCompilerError{}
if ok := errors.As(err, ue); ok {
return ue
}
return nil
}
func numericPathPart(num int) string {
return fmt.Sprintf("[%d]", num)
}