forked from wundergraph/graphql-go-tools
/
buf_pair.go
75 lines (63 loc) · 1.39 KB
/
buf_pair.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
package resolve
import "github.com/TykTechnologies/graphql-go-tools/v2/pkg/fastbuffer"
type BufPair struct {
Data *fastbuffer.FastBuffer
Errors *fastbuffer.FastBuffer
}
func NewBufPair() *BufPair {
return &BufPair{
Data: fastbuffer.New(),
Errors: fastbuffer.New(),
}
}
func (b *BufPair) HasData() bool {
return b.Data.Len() != 0
}
func (b *BufPair) HasErrors() bool {
return b.Errors.Len() != 0
}
func (b *BufPair) Reset() {
b.Data.Reset()
b.Errors.Reset()
}
func (b *BufPair) writeErrors(data []byte) {
b.Errors.WriteBytes(data)
}
func (b *BufPair) WriteErr(message, locations, path, extensions []byte) {
if b.HasErrors() {
b.writeErrors(comma)
}
b.writeErrors(lBrace)
b.writeErrors(quote)
b.writeErrors(literalMessage)
b.writeErrors(quote)
b.writeErrors(colon)
b.writeErrors(quote)
b.writeErrors(message)
b.writeErrors(quote)
if locations != nil {
b.writeErrors(comma)
b.writeErrors(quote)
b.writeErrors(literalLocations)
b.writeErrors(quote)
b.writeErrors(colon)
b.writeErrors(locations)
}
if path != nil {
b.writeErrors(comma)
b.writeErrors(quote)
b.writeErrors(literalPath)
b.writeErrors(quote)
b.writeErrors(colon)
b.writeErrors(path)
}
if extensions != nil {
b.writeErrors(comma)
b.writeErrors(quote)
b.writeErrors(literalExtensions)
b.writeErrors(quote)
b.writeErrors(colon)
b.writeErrors(extensions)
}
b.writeErrors(rBrace)
}