-
Notifications
You must be signed in to change notification settings - Fork 0
/
boolExpr.go
52 lines (44 loc) · 1.26 KB
/
boolExpr.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
package squl
import (
"bytes"
fmt "golang.org/x/xerrors"
"github.com/trivigy/squl/internal/global"
)
// BoolExpr describes the AND/OR expression combinations.
type BoolExpr struct {
Xpr Node `json:"xpr"`
Type BoolExprType `json:"type"`
Wrap bool `json:"wrap"` /* indicate if expr should be wrapped with parentheses */
Args []Node `json:"args"` /* arguments to this expression */
}
func (r *BoolExpr) dump(counter *ordinalMarker) (string, error) {
buffer := bytes.NewBuffer(nil)
for i, each := range r.Args {
eachDump, err := each.dump(counter)
if err != nil {
return "", err
}
if _, err := buffer.WriteString(eachDump); err != nil {
return "", fmt.Errorf(global.ErrFmt, pkg.Name(), err)
}
if i < len(r.Args)-1 {
switch r.Type {
case BoolExprTypeAnd:
if _, err := buffer.WriteString(" AND "); err != nil {
return "", fmt.Errorf(global.ErrFmt, pkg.Name(), err)
}
case BoolExprTypeOr:
if _, err := buffer.WriteString(" OR "); err != nil {
return "", fmt.Errorf(global.ErrFmt, pkg.Name(), err)
}
default:
return "", fmt.Errorf(global.ErrFmt, pkg.Name(), fmt.Errorf("unknown type %q", r.Type))
}
}
}
expr := buffer.String()
if r.Wrap {
expr = "(" + expr + ")"
}
return expr, nil
}