OSDN Git Service

143f0fd5c1425fe7870d89bd2a3f3d360abff409
[bytom/vapor.git] / blockchain / query / filter / expr.go
1 package filter
2
3 import "fmt"
4
5 type expr interface {
6         String() string
7 }
8
9 type binaryExpr struct {
10         op   *binaryOp
11         l, r expr
12 }
13
14 func (e binaryExpr) String() string {
15         return e.l.String() + " " + e.op.name + " " + e.r.String()
16 }
17
18 type attrExpr struct {
19         attr string
20 }
21
22 func (e attrExpr) String() string {
23         return e.attr
24 }
25
26 type selectorExpr struct {
27         ident   string
28         objExpr expr
29 }
30
31 func (e selectorExpr) String() string {
32         return e.objExpr.String() + "." + e.ident
33 }
34
35 type parenExpr struct {
36         inner expr
37 }
38
39 func (e parenExpr) String() string {
40         return "(" + e.inner.String() + ")"
41 }
42
43 type valueExpr struct {
44         typ   token
45         value string
46 }
47
48 func (e valueExpr) String() string {
49         return e.value
50 }
51
52 type envExpr struct {
53         ident string
54         expr  expr
55 }
56
57 func (e envExpr) String() string {
58         return e.ident + "(" + e.expr.String() + ")"
59 }
60
61 type placeholderExpr struct {
62         num int
63 }
64
65 func (e placeholderExpr) String() string {
66         return fmt.Sprintf("$%d", e.num)
67 }
68
69 // Type defines the value types in filter expressions.
70 type Type int
71
72 //defines the value types in filter expressions.
73 const (
74         Any Type = iota
75         Bool
76         String
77         Integer
78         Object
79 )
80
81 func (t Type) String() string {
82         switch t {
83         case Any:
84                 return "any"
85         case Bool:
86                 return "bool"
87         case String:
88                 return "string"
89         case Integer:
90                 return "integer"
91         case Object:
92                 return "object"
93         }
94         panic("unknown type")
95 }