-
Notifications
You must be signed in to change notification settings - Fork 493
/
ast_stringer.go
125 lines (102 loc) · 2.26 KB
/
ast_stringer.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
117
118
119
120
121
122
123
124
125
package traceql
import (
"fmt"
"strconv"
"strings"
)
func (r RootExpr) String() string {
return r.p.String()
}
func (p Pipeline) String() string {
s := make([]string, 0, len(p.p))
for _, p := range p.p {
s = append(s, p.String())
}
return strings.Join(s, "|")
}
func (o GroupOperation) String() string {
return "by(" + o.e.String() + ")"
}
func (o CoalesceOperation) String() string {
return "coalesce()"
}
func (o ScalarOperation) String() string {
return binaryOp(o.op, o.lhs, o.rhs)
}
func (a Aggregate) String() string {
if a.e == nil {
return a.agg.String() + "()"
}
return a.agg.String() + "(" + a.e.String() + ")"
}
func (o SpansetOperation) String() string {
return binaryOp(o.op, o.lhs, o.rhs)
}
func (f SpansetFilter) String() string {
return "{ " + f.e.String() + " }"
}
func (f ScalarFilter) String() string {
return binaryOp(f.op, f.lhs, f.rhs)
}
func (o BinaryOperation) String() string {
return binaryOp(o.op, o.lhs, o.rhs)
}
func (o UnaryOperation) String() string {
return unaryOp(o.op, o.e)
}
func (n Static) String() string {
switch n.staticType {
case typeInt:
return strconv.Itoa(n.n)
case typeFloat:
return strconv.FormatFloat(n.f, 'f', 5, 64)
case typeString:
return "`" + n.s + "`"
case typeBoolean:
return strconv.FormatBool(n.b)
case typeNil:
return "nil"
case typeDuration:
return n.d.String()
case typeStatus:
return n.status.String()
}
return fmt.Sprintf("static(%d)", n.staticType)
}
func (a Attribute) String() string {
scopes := []string{}
if a.parent {
scopes = append(scopes, "parent")
}
if a.scope != attributeScopeNone {
attributeScope := a.scope.String()
scopes = append(scopes, attributeScope)
}
att := a.name
if a.intrinsic != intrinsicNone {
att = a.intrinsic.String()
}
scope := ""
if len(scopes) > 0 {
scope = strings.Join(scopes, ".")
}
scope += "."
return scope + att
}
func binaryOp(op Operator, lhs element, rhs element) string {
return wrapElement(lhs) + " " + op.String() + " " + wrapElement(rhs)
}
func unaryOp(op Operator, e element) string {
return op.String() + wrapElement(e)
}
func wrapElement(e element) string {
static, ok := e.(Static)
if ok {
return static.String()
}
att, ok := e.(Attribute)
if ok {
return att.String()
}
return "(" + e.String() + ")"
}