-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvisitor.go
More file actions
69 lines (55 loc) · 2.47 KB
/
visitor.go
File metadata and controls
69 lines (55 loc) · 2.47 KB
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
package bexpression
import (
"context"
"github.com/collibra/go-bexpression/base"
)
type FunctionVisitorOptions struct {
EnterExpressionElementFn func(ctx context.Context, element base.VisitableElement) error
LeaveExpressionElementFn func(ctx context.Context, element base.VisitableElement)
LiteralFn func(ctx context.Context, literal interface{}) error
}
func WithEnterExpressionElementFn(enterExpressionElementFn func(ctx context.Context, element base.VisitableElement) error) func(*FunctionVisitorOptions) {
return func(options *FunctionVisitorOptions) {
options.EnterExpressionElementFn = enterExpressionElementFn
}
}
func WithLeaveExpressionElementFn(leaveExpressionElementFn func(ctx context.Context, element base.VisitableElement)) func(options *FunctionVisitorOptions) {
return func(options *FunctionVisitorOptions) {
options.LeaveExpressionElementFn = leaveExpressionElementFn
}
}
func WithLiteralFn(literalFn func(ctx context.Context, literal interface{}) error) func(*FunctionVisitorOptions) {
return func(options *FunctionVisitorOptions) {
options.LiteralFn = literalFn
}
}
var _ base.Visitor = (*FunctionVisitor)(nil)
type FunctionVisitor struct {
EnterExpressionElementFn func(ctx context.Context, element base.VisitableElement) error
LeaveExpressionElementFn func(ctx context.Context, element base.VisitableElement)
LiteralFn func(ctx context.Context, literal interface{}) error
}
func NewFunctionVisitor(opts ...func(*FunctionVisitorOptions)) *FunctionVisitor {
options := &FunctionVisitorOptions{
EnterExpressionElementFn: func(ctx context.Context, element base.VisitableElement) error { return nil },
LeaveExpressionElementFn: func(ctx context.Context, element base.VisitableElement) {},
LiteralFn: func(ctx context.Context, literal interface{}) error { return nil },
}
for _, opt := range opts {
opt(options)
}
return &FunctionVisitor{
EnterExpressionElementFn: options.EnterExpressionElementFn,
LeaveExpressionElementFn: options.LeaveExpressionElementFn,
LiteralFn: options.LiteralFn,
}
}
func (f FunctionVisitor) EnterExpressionElement(ctx context.Context, element base.VisitableElement) error {
return f.EnterExpressionElementFn(ctx, element)
}
func (f FunctionVisitor) LeaveExpressionElement(ctx context.Context, element base.VisitableElement) {
f.LeaveExpressionElementFn(ctx, element)
}
func (f FunctionVisitor) Literal(ctx context.Context, l interface{}) error {
return f.LiteralFn(ctx, l)
}