-
Notifications
You must be signed in to change notification settings - Fork 3
/
union.go
103 lines (85 loc) · 2.29 KB
/
union.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
package vtypes
import (
"context"
"fmt"
"io"
"github.com/Velocidex/ordereddict"
"www.velocidex.com/golang/vfilter"
)
type Union struct {
Selector *vfilter.Lambda
choice_names *ordereddict.Dict
Choices map[string]Parser
profile *Profile
}
func (self *Union) New(profile *Profile, options *ordereddict.Dict) (Parser, error) {
if options == nil {
return nil, fmt.Errorf("Union parser requires options")
}
expression, pres := options.GetString("selector")
if !pres {
return nil, fmt.Errorf("Union parser requires a lambda selector")
}
selector, err := vfilter.ParseLambda(expression)
if err != nil {
return nil, fmt.Errorf("Union parser selector expression '%v': %w",
expression, err)
}
choices, pres := options.Get("choices")
if !pres {
choices = ordereddict.NewDict()
}
choices_dict, ok := choices.(*ordereddict.Dict)
if !ok {
return nil, fmt.Errorf("Union parser requires choices to be a mapping between values and strings")
}
result := &Union{
Selector: selector,
// Map the value to the name of the type
choice_names: choices_dict,
// Map the value to the actual parser
Choices: make(map[string]Parser),
profile: profile,
}
return result, nil
}
func (self *Union) Parse(
scope vfilter.Scope, reader io.ReaderAt, offset int64) interface{} {
var value interface{}
if self.Selector != nil {
this_obj, pres := scope.Resolve("this")
if pres {
value = self.Selector.Reduce(
context.Background(), scope, []vfilter.Any{this_obj})
}
}
if IsNil(value) {
return &vfilter.Null{}
}
value_str := fmt.Sprintf("%v", value)
parser, pres := self.Choices[value_str]
if pres {
return parser.Parse(scope, reader, offset)
}
// Resolve the parser from the profile now.
parser_name, pres := self.choice_names.GetString(value_str)
if !pres {
// Try the default
parser_name, pres = self.choice_names.GetString("default")
if !pres {
// Can not find the type - return null
return vfilter.Null{}
}
parser_name = "default"
}
// Resolve the parser from the profile
parser, err := self.profile.GetParser(parser_name, ordereddict.NewDict())
if err != nil {
scope.Log("ERROR:binary_parser: Union: %v", err)
return vfilter.Null{}
}
if value_str != "default" {
self.Choices[value_str] = parser
}
return parser.Parse(scope, reader, offset)
}