-
-
Notifications
You must be signed in to change notification settings - Fork 177
/
utils.go
84 lines (73 loc) · 1.31 KB
/
utils.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
package qcode
func GetQType(gql string) (QType, string) {
var tok string
s := -1
var skip byte
sc := 0
bc := 0
for i := range gql {
b := gql[i]
switch {
case b == '#':
skip = '\n'
sc++
case sc == 0 && (b == '\'' || b == '"'):
skip = b
sc++
case sc != 0 && i != 0 && gql[i-1] != '\\' && (b == skip):
sc--
case sc != 0:
continue
case b == '{':
t := tok
if s != -1 {
if t == "" {
t = gql[s:i]
} else {
n := gql[s:i]
if (bc % 2) == 0 {
switch t {
case "query":
return QTQuery, n
case "mutation":
return QTMutation, n
case "subscription":
return QTSubscription, n
}
}
}
}
switch t {
case "", "query":
return QTQuery, ""
case "mutation":
return QTMutation, ""
case "subscription":
return QTSubscription, ""
}
bc++
case b == '}':
bc++
case s == -1 && al(b):
s = i
case s != -1 && !al(b):
ct := gql[s:i]
if (bc % 2) == 0 {
switch tok {
case "query":
return QTQuery, ct
case "mutation":
return QTMutation, ct
case "subscription":
return QTSubscription, ct
}
}
tok = ct
s = -1
}
}
return QTUnknown, ""
}
func al(b byte) bool {
return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || (b >= '0' && b <= '9')
}