-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathparser_mutation.go
208 lines (185 loc) · 5.19 KB
/
parser_mutation.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* SPDX-FileCopyrightText: © Hypermode Inc. <[email protected]>
* SPDX-License-Identifier: Apache-2.0
*/
package dql
import (
"github.com/dgraph-io/dgo/v250/protos/api"
"github.com/hypermodeinc/dgraph/v25/lex"
)
// ParseMutation parses a block into a mutation. Returns an object with a mutation or
// an upsert block with mutation, otherwise returns nil with an error.
func ParseMutation(mutation string) (req *api.Request, err error) {
var lexer lex.Lexer
lexer.Reset(mutation)
lexer.Run(lexTopLevel)
if err := lexer.ValidateResult(); err != nil {
return nil, err
}
it := lexer.NewIterator()
if !it.Next() {
return nil, it.Errorf("Invalid mutation")
}
item := it.Item()
switch item.Typ {
case itemUpsertBlock:
if req, err = parseUpsertBlock(it); err != nil {
return nil, err
}
case itemLeftCurl:
mu, err := parseMutationBlock(it)
if err != nil {
return nil, err
}
req = &api.Request{Mutations: []*api.Mutation{mu}}
default:
return nil, it.Errorf("Unexpected token: [%s]", item.Val)
}
// mutations must be enclosed in a single block.
if it.Next() && it.Item().Typ != lex.ItemEOF {
return nil, it.Errorf("Unexpected %s after the end of the block", it.Item().Val)
}
return req, nil
}
// parseUpsertBlock parses the upsert block
func parseUpsertBlock(it *lex.ItemIterator) (*api.Request, error) {
var req *api.Request
var queryText, condText string
var queryFound bool
// ===>upsert<=== {...}
if !it.Next() {
return nil, it.Errorf("Unexpected end of upsert block")
}
// upsert ===>{<=== ....}
item := it.Item()
if item.Typ != itemLeftCurl {
return nil, it.Errorf("Expected { at the start of block. Got: [%s]", item.Val)
}
for it.Next() {
item = it.Item()
switch {
// upsert {... ===>}<===
case item.Typ == itemRightCurl:
switch {
case req == nil:
return nil, it.Errorf("Empty mutation block")
case !queryFound:
return nil, it.Errorf("Query op not found in upsert block")
default:
req.Query = queryText
return req, nil
}
// upsert { mutation{...} ===>query<==={...}}
case item.Typ == itemUpsertBlockOp && item.Val == "query":
if queryFound {
return nil, it.Errorf("Multiple query ops inside upsert block")
}
queryFound = true
if !it.Next() {
return nil, it.Errorf("Unexpected end of upsert block")
}
item = it.Item()
if item.Typ != itemUpsertBlockOpContent {
return nil, it.Errorf("Expecting brace, found '%s'", item.Val)
}
queryText += item.Val
// upsert { ===>mutation<=== {...} query{...}}
case item.Typ == itemUpsertBlockOp && item.Val == "mutation":
if !it.Next() {
return nil, it.Errorf("Unexpected end of upsert block")
}
// upsert { mutation ===>@if(...)<=== {....} query{...}}
item = it.Item()
if item.Typ == itemUpsertBlockOpContent {
condText = item.Val
if !it.Next() {
return nil, it.Errorf("Unexpected end of upsert block")
}
}
// upsert @if(...) ===>{<=== ....}
mu, err := parseMutationBlock(it)
if err != nil {
return nil, err
}
mu.Cond = condText
if req == nil {
req = &api.Request{Mutations: []*api.Mutation{mu}}
} else {
req.Mutations = append(req.Mutations, mu)
}
// upsert { mutation{...} ===>fragment<==={...}}
case item.Typ == itemUpsertBlockOp && item.Val == "fragment":
if !it.Next() {
return nil, it.Errorf("Unexpected end of upsert block")
}
item = it.Item()
if item.Typ != itemUpsertBlockOpContent {
return nil, it.Errorf("Expecting brace, found '%s'", item.Val)
}
queryText += "fragment" + item.Val
default:
return nil, it.Errorf("Unexpected token in upsert block [%s]", item.Val)
}
}
return nil, it.Errorf("Invalid upsert block")
}
// parseMutationBlock parses the mutation block
func parseMutationBlock(it *lex.ItemIterator) (*api.Mutation, error) {
var mu api.Mutation
item := it.Item()
if item.Typ != itemLeftCurl {
return nil, it.Errorf("Expected { at the start of block. Got: [%s]", item.Val)
}
for it.Next() {
item := it.Item()
if item.Typ == itemText {
continue
}
if item.Typ == itemRightCurl {
return &mu, nil
}
if item.Typ == itemMutationOp {
if err := parseMutationOp(it, item.Val, &mu); err != nil {
return nil, err
}
}
}
return nil, it.Errorf("Invalid mutation.")
}
// parseMutationOp parses and stores set or delete operation string in Mutation.
func parseMutationOp(it *lex.ItemIterator, op string, mu *api.Mutation) error {
parse := false
for it.Next() {
item := it.Item()
if item.Typ == itemText {
continue
}
if item.Typ == itemLeftCurl {
if parse {
return it.Errorf("Too many left curls in set mutation.")
}
parse = true
}
if item.Typ == itemMutationOpContent {
if !parse {
return it.Errorf("Mutation syntax invalid.")
}
switch op {
case "set":
mu.SetNquads = []byte(item.Val)
case "delete":
mu.DelNquads = []byte(item.Val)
case "schema":
return it.Errorf("Altering schema not supported through http client.")
case "dropall":
return it.Errorf("Dropall not supported through http client.")
default:
return it.Errorf("Invalid mutation operation.")
}
}
if item.Typ == itemRightCurl {
return nil
}
}
return it.Errorf("Invalid mutation formatting.")
}