-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquery.go
79 lines (62 loc) · 1.49 KB
/
query.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
package plusminus
import (
"bytes"
"strings"
)
// Query is a top-level query to dgraph.
func Query(name string) *query {
return &query{
name: name,
}
}
type query struct {
name string
variables []string
blocks []*block
}
// Variables allows you to add a number of variables to a query.
func (q *query) Variables(v ...string) *query {
q.variables = append(q.variables, v...)
return q
}
// Blocks allows you to add a number of blocks to the query.
func (q *query) Blocks(b ...*block) *query {
q.blocks = append(q.blocks, b...)
return q
}
// String creates a string representation of the whole query, which can be used to query dgraph.
func (q *query) String() string {
s := "query " + q.name
if len(q.variables) > 0 {
s += "(" + strings.Join(q.variables, ", ") + ") "
}
s += "{\n"
for i := range q.blocks {
s += q.blocks[i].toString()
}
s += "}"
return s
}
// StringIndented creates a query string like String(), but with appropriate indentation.
// This is not required for dgraph to parse the query and it is more computationally expensive, so use only when human readability of the query is desired.
func (q *query) StringIndented() string {
var (
buf = new(bytes.Buffer)
depth = 0
indent = []byte{' ', ' '}
)
str := q.String()
for i := range str {
if str[i] == '{' {
depth++
}
if i+1 < len(str) && str[i+1] == '}' {
depth--
}
buf.WriteByte(str[i])
if str[i] == '\n' {
buf.Write(bytes.Repeat(indent, depth))
}
}
return buf.String()
}