-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
util.go
73 lines (64 loc) · 1.27 KB
/
util.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
package qcode
import (
"bytes"
"github.com/dosco/graphjin/v2/core/internal/graph"
"github.com/dosco/graphjin/v2/core/internal/util"
)
func (co *Compiler) ParseName(name string) string {
if co.c.EnableCamelcase {
return util.ToSnake(name)
}
return name
}
func GetQType(t graph.ParserType) QType {
switch t {
case graph.OpQuery:
return QTQuery
case graph.OpSub:
return QTSubscription
case graph.OpMutate:
return QTMutation
default:
return QTUnknown
}
}
func GetQTypeByName(t string) QType {
switch t {
case "query":
return QTQuery
case "subscription":
return QTSubscription
case "mutation":
return QTMutation
default:
return QTUnknown
}
}
func graphNodeToJSON(node *graph.Node, w *bytes.Buffer) {
switch node.Type {
case graph.NodeStr:
w.WriteString(`"` + node.Val + `"`)
case graph.NodeNum, graph.NodeBool:
w.WriteString(node.Val)
case graph.NodeObj:
w.WriteString(`{`)
for i, c := range node.Children {
if i == 0 {
w.WriteString(`"` + c.Name + `": `)
} else {
w.WriteString(`,"` + c.Name + `": `)
}
graphNodeToJSON(c, w)
}
w.WriteString(`}`)
case graph.NodeList:
w.WriteString(`[`)
for i, c := range node.Children {
if i != 0 {
w.WriteString(`,`)
}
graphNodeToJSON(c, w)
}
w.WriteString(`]`)
}
}