/
annotation.go
59 lines (48 loc) · 1.01 KB
/
annotation.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
package typast
import (
"errors"
"github.com/typical-go/typical-go/pkg/typgo"
)
type (
Annotation struct {
Filter Filter
Processor Processor
}
Processor interface {
Process(*typgo.Context, Directives) error
}
NewProcessor func(*typgo.Context, Directives) error
)
//
// Annotation
//
var _ Processor = (*Annotation)(nil)
var _ Annotator = (*Annotation)(nil)
func (a *Annotation) Process(c *typgo.Context, directives Directives) error {
if a.Processor == nil {
return errors.New("mising annotation processor")
}
var filtered []*Directive
for _, dir := range directives {
if a.isAllowed(dir) {
filtered = append(filtered, dir)
}
}
return a.Processor.Process(c, filtered)
}
func (a *Annotation) Annotate() Processor {
return a
}
func (a *Annotation) isAllowed(d *Directive) bool {
if a.Filter == nil {
return true
}
return a.Filter.IsAllowed(d)
}
//
// NewProcessor
//
var _ Processor = (NewProcessor)(nil)
func (p NewProcessor) Process(c *typgo.Context, d Directives) error {
return p(c, d)
}