-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepare.go
55 lines (47 loc) · 1.18 KB
/
prepare.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
package kfl
import (
"github.com/rs/zerolog/log"
)
// Apply does these steps:
// - Expand the macros in given query.
// - Parse the query.
// - Precompute the values in the query.
// - Evaluate the expression.
// and returns:
// - Truthiness of the record for the given query.
// - The new record that's altered by the query.
// - Error if any step ends with an error, otherwise nil.
func Apply(b []byte, query string) (truth bool, record string, err error) {
var expr *Expression
// Prepare the query.
expr, _, err = PrepareQuery(query)
if err != nil {
log.Error().Err(err).Send()
return
}
truth, record, err = Eval(expr, string(b))
if err != nil {
log.Error().Err(err).Msg("Eval error:")
return
}
return
}
func PrepareQuery(query string) (expr *Expression, prop Propagate, err error) {
// Expand all macros in the query, if there are any.
query, err = ExpandMacros(query)
if err != nil {
log.Error().Err(err).Msg("Macro expand error:")
return
}
// Parse the query.
expr, err = Parse(query)
if err != nil {
log.Error().Err(err).Msg("Syntax error:")
return
}
prop, err = Precompute(expr)
if err != nil {
log.Error().Err(err).Msg("Precompute error:")
}
return
}