-
Notifications
You must be signed in to change notification settings - Fork 8
/
marshal.go
103 lines (84 loc) · 2.34 KB
/
marshal.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 vfilter
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/Velocidex/ordereddict"
"www.velocidex.com/golang/vfilter/marshal"
"www.velocidex.com/golang/vfilter/types"
)
type StoredQueryItem struct {
Query string `json:"query,omitempty"`
Name string `json:"name,omitempty"`
Parameters []string `json:"parameters,omitempty"`
}
func (self *_StoredQuery) Marshal(
scope types.Scope) (*types.MarshalItem, error) {
var query string
if self.parameters == nil {
query = fmt.Sprintf("LET `%v` = %s", self.name,
FormatToString(scope, self.query))
} else {
query = fmt.Sprintf("LET `%v`(%s) = %s", self.name,
strings.Join(self.parameters, ", "),
FormatToString(scope, self.query))
}
query_str, err := json.Marshal(query)
return &types.MarshalItem{
Type: "Replay",
Data: query_str,
}, err
}
func (self *StoredExpression) Marshal(
scope types.Scope) (*types.MarshalItem, error) {
var query string
if self.parameters == nil {
query = fmt.Sprintf("LET `%v` = %s", self.name,
FormatToString(scope, self.Expr))
} else {
query = fmt.Sprintf("LET `%v`(%s) = %s", self.name,
strings.Join(self.parameters, ", "),
FormatToString(scope, self.Expr))
}
query_str, err := json.Marshal(query)
return &types.MarshalItem{
Type: "Replay",
Data: query_str,
}, err
}
type ReplayUnmarshaller struct{}
func (self ReplayUnmarshaller) Unmarshal(
unmarshaller types.Unmarshaller,
scope types.Scope, item *types.MarshalItem) (interface{}, error) {
var query string
err := json.Unmarshal(item.Data, &query)
if err != nil {
return nil, err
}
vql, err := Parse(query)
if err != nil {
return nil, err
}
for _ = range vql.Eval(context.Background(), scope) {
}
return nil, nil
}
type OrdereddictUnmarshaller struct{}
func (self OrdereddictUnmarshaller) Unmarshal(
unmarshaller types.Unmarshaller,
scope types.Scope, item *types.MarshalItem) (interface{}, error) {
dict := ordereddict.NewDict()
err := json.Unmarshal(item.Data, dict)
if err != nil {
return nil, err
}
return dict, nil
}
func NewUnmarshaller(ignore_vars []string) *marshal.Unmarshaller {
unmarshaller := marshal.NewUnmarshaller()
unmarshaller.Handlers["Scope"] = ScopeUnmarshaller{ignore_vars}
unmarshaller.Handlers["Replay"] = ReplayUnmarshaller{}
unmarshaller.Handlers["OrderedDict"] = OrdereddictUnmarshaller{}
return unmarshaller
}