-
Notifications
You must be signed in to change notification settings - Fork 20
/
searcher.go
78 lines (61 loc) · 2.11 KB
/
searcher.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
package fields
//go:generate go run ../gen/generate.go fields ./generated_signed_txn_map.go ../Filter_tags.md
import (
"fmt"
sdk "github.com/algorand/go-algorand-sdk/v2/types"
"github.com/algorand/conduit/conduit/plugins/processors/filterprocessor/expression"
)
// Searcher searches the struct with an expression
type Searcher struct {
Exp expression.Expression
Tag string
SearchInner bool
}
// This function is ONLY to be used by the filter.field function.
// The reason being is that without validation of the tag (which is provided by
// MakeFieldSearcher) then this can panic
func (f Searcher) search(input *sdk.SignedTxnWithAD) (bool, error) {
val, err := LookupFieldByTag(f.Tag, input)
if err != nil {
return false, err
}
b, err := f.Exp.Match(val)
if err != nil {
return false, err
}
if b {
return true, err
} else if f.SearchInner {
// Search for matches in inner transactions
var innerMatch bool
for i := range input.EvalDelta.InnerTxns {
innerMatch, err = f.search(&input.EvalDelta.InnerTxns[i])
if err != nil {
return false, err
}
if innerMatch {
return true, nil
}
}
}
return b, nil
}
// checks that the supplied tag exists in the struct and recovers from any panics
func checkTagAndExpressionExist(expressionType expression.Type, tag string) (outError error) {
_, err := LookupFieldByTag(tag, &sdk.SignedTxnWithAD{})
if err != nil {
return fmt.Errorf("%s does not exist in transactions.SignedTxnInBlock struct", tag)
}
if _, ok := expression.TypeMap[expressionType]; !ok {
return fmt.Errorf("expression type (%s) is not supported", expressionType)
}
return nil
}
// MakeFieldSearcher will check that the field exists and that it contains the necessary "conversion" function
// TODO: Remove expressionType. It is validated when the expression is created.
func MakeFieldSearcher(e expression.Expression, expressionType expression.Type, tag string, searchInner bool) (*Searcher, error) {
if err := checkTagAndExpressionExist(expressionType, tag); err != nil {
return nil, err
}
return &Searcher{Exp: e, Tag: tag, SearchInner: searchInner}, nil
}