-
Notifications
You must be signed in to change notification settings - Fork 8
/
if.go
86 lines (70 loc) · 1.83 KB
/
if.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
package functions
import (
"context"
"github.com/Velocidex/ordereddict"
"www.velocidex.com/golang/vfilter/arg_parser"
"www.velocidex.com/golang/vfilter/types"
"www.velocidex.com/golang/vfilter/utils"
)
type _IfFunctionArgs struct {
Condition types.Any `vfilter:"required,field=condition"`
Then types.LazyAny `vfilter:"optional,field=then"`
Else types.LazyAny `vfilter:"optional,field=else"`
}
type _IfFunction struct{}
func (self _IfFunction) Info(scope types.Scope, type_map *types.TypeMap) *types.FunctionInfo {
return &types.FunctionInfo{
Name: "if",
Doc: "If condition is true, return the 'then' value otherwise the 'else' value.",
ArgType: type_map.AddType(scope, _IfFunctionArgs{}),
}
}
func (self _IfFunction) Call(
ctx context.Context,
scope types.Scope,
args *ordereddict.Dict) types.Any {
arg := &_IfFunctionArgs{}
err := arg_parser.ExtractArgs(scope, args, arg)
if err != nil {
scope.Log("if: %v", err)
return types.Null{}
}
if scope.Bool(arg.Condition) {
if utils.IsNil(arg.Then) {
return &types.Null{}
}
lazy_expr, ok := arg.Then.(types.LazyExpr)
if ok {
arg.Then = lazy_expr.ReduceWithScope(ctx, scope)
}
switch t := arg.Then.(type) {
case types.StoredQuery:
// If Function with subqueries should return a lazy subquery
return t
case types.LazyExpr:
exp := t.ReduceWithScope(ctx, scope)
s, ok := exp.(types.StoredQuery)
if ok {
return s
}
default:
return t
}
}
if utils.IsNil(arg.Else) {
return &types.Null{}
}
lazy_expr, ok := arg.Else.(types.LazyExpr)
if ok {
arg.Else = lazy_expr.ReduceWithScope(ctx, scope)
}
switch t := arg.Else.(type) {
case types.StoredQuery:
// If Function with subqueries should return a lazy subquery
return t
case types.LazyExpr:
return t.ReduceWithScope(ctx, scope)
default:
return t
}
}