-
Notifications
You must be signed in to change notification settings - Fork 0
/
match.go
116 lines (97 loc) · 3.02 KB
/
match.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
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) 2022 The Go-Enjin Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pageql
import (
"fmt"
"strings"
"github.com/go-enjin/be/pkg/cmp"
"github.com/go-enjin/be/pkg/context"
"github.com/go-enjin/be/pkg/regexps"
)
func Match(query string, ctx context.Context) (matched bool, err error) {
var stmnt *Statement
var pErr *ParseError
if stmnt, pErr = CompileQuery(query); pErr != nil {
err = error(pErr)
return
}
stmnt = stmnt.Render()
matched, err = processQueryExpression(stmnt.Expression, ctx)
return
}
func processQueryExpression(expr *Expression, ctx context.Context) (matched bool, err error) {
switch {
case expr.Condition != nil:
matched, err = processQueryCondition(expr.Condition, ctx)
case expr.Operation != nil:
matched, err = processQueryOperation(expr.Operation, ctx)
}
return
}
func processQueryCondition(cond *Condition, ctx context.Context) (matched bool, err error) {
if cond.Left != nil && cond.Right != nil {
var leftMatch, rightMatch bool
if leftMatch, err = processQueryExpression(cond.Left, ctx); err != nil {
return
}
if rightMatch, err = processQueryExpression(cond.Right, ctx); err != nil {
return
}
switch strings.ToUpper(cond.Type) {
case "OR":
matched = leftMatch || rightMatch
case "AND":
matched = leftMatch && rightMatch
}
}
return
}
func processQueryOperation(op *Operation, ctx context.Context) (matched bool, err error) {
switch op.Type {
case "==":
matched, err = processQueryOperationEquals(*op.Left, op.Right, ctx)
case "!=":
if matched, err = processQueryOperationEquals(*op.Left, op.Right, ctx); err == nil {
matched = !matched
}
default:
err = fmt.Errorf(`%v not implemented`, op.Type)
}
return
}
func processQueryOperationEquals(key string, opValue *Value, ctx context.Context) (matched bool, err error) {
switch {
case opValue.ContextKey != nil:
lValue := ctx.Get(key)
rValue := ctx.Get(*opValue.ContextKey)
matched, err = cmp.Compare(lValue, rValue)
case opValue.Regexp != nil:
if value, ok := ctx.Get(key).(string); ok {
if rx, e := regexps.Compile(*opValue.Regexp); e != nil {
err = fmt.Errorf("error compiling regular expression")
} else {
matched = rx.MatchString(value)
}
} else {
err = fmt.Errorf("page.%v is of type %T, expected string", key, ctx.Get(key))
}
case opValue.String != nil:
if value, ok := ctx.Get(key).(string); ok {
matched = value == *opValue.String
} else {
err = fmt.Errorf("page.%v is of type %T, expected string", key, ctx.Get(key))
}
}
return
}