-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper_condition.go
49 lines (39 loc) · 910 Bytes
/
wrapper_condition.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
package ogimpl
import (
"context"
"github.com/symphony09/ograph"
"github.com/symphony09/ograph/ogcore"
)
var ConditionWrapperFactory = func() ogcore.Node {
return &ConditionWrapper{}
}
type ConditionWrapper struct {
ograph.BaseWrapper
Switch string
Condition func(ctx context.Context, state ogcore.State) bool
}
func (wrapper *ConditionWrapper) Run(ctx context.Context, state ogcore.State) error {
if wrapper.Switch != "" {
if wrapper.Switch == "On" {
return wrapper.Node.Run(ctx, state)
} else {
return nil
}
} else if wrapper.Condition != nil {
if ok := wrapper.Condition(ctx, state); ok {
return wrapper.Node.Run(ctx, state)
} else {
return nil
}
}
return nil
}
func NewConditionWrapperFactory(
cond func(ctx context.Context, state ogcore.State) bool,
) func() ogcore.Node {
return func() ogcore.Node {
return &ConditionWrapper{
Condition: cond,
}
}
}