-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathparse.go
302 lines (274 loc) · 8.43 KB
/
parse.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* Copyright 2015-2018 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rdf
import (
"errors"
"strconv"
"strings"
"unicode"
"github.com/dgraph-io/dgo/protos/api"
"github.com/dgraph-io/dgraph/lex"
"github.com/dgraph-io/dgraph/types"
"github.com/dgraph-io/dgraph/types/facets"
"github.com/dgraph-io/dgraph/x"
)
var (
ErrEmpty = errors.New("RDF: harmless error, e.g. comment line")
ErrInvalidUID = errors.New("UID has to be greater than zero")
)
// Function to do sanity check for subject, predicate, object and label strings.
func sane(s string) bool {
// Label and ObjectId can be "", we already check that subject and predicate
// shouldn't be empty.
if len(s) == 0 {
return true
}
// s should have atleast one alphanumeric character.
for _, r := range s {
if unicode.IsLetter(r) || unicode.IsDigit(r) {
return true
}
}
return false
}
// Parse parses a mutation string and returns the N-Quad representation for it.
func Parse(line string) (api.NQuad, error) {
var rnq api.NQuad
l := lex.NewLexer(line)
l.Run(lexText)
if err := l.ValidateResult(); err != nil {
return rnq, err
}
it := l.NewIterator()
var oval string
var seenOval bool
var vend bool
isCommentLine := false
// We read items from the l.Items channel to which the lexer sends items.
L:
for it.Next() {
item := it.Item()
switch item.Typ {
case itemSubject:
rnq.Subject = strings.Trim(item.Val, " ")
case itemVarKeyword:
it.Next()
if item = it.Item(); item.Typ != itemLeftRound {
return rnq, x.Errorf("Expected '(', found: %s", item.Val)
}
it.Next()
item = it.Item()
switch item.Typ {
case itemSubjectVarName:
rnq.SubjectVar = item.Val
case itemObjectVarName:
rnq.ObjectVar = item.Val
default:
return rnq, x.Errorf("Expected variable name, found: %s", item.Val)
}
it.Next() // parse ')'
case itemPredicate:
// Here we split predicate and lang directive (ex: "name@en"), if needed.
rnq.Predicate, rnq.Lang = x.PredicateLang(strings.Trim(item.Val, " "))
case itemObject:
rnq.ObjectId = strings.Trim(item.Val, " ")
case itemStar:
if rnq.Subject == "" {
rnq.Subject = x.Star
} else if rnq.Predicate == "" {
rnq.Predicate = x.Star
} else {
rnq.ObjectValue = &api.Value{Val: &api.Value_DefaultVal{DefaultVal: x.Star}}
}
case itemLiteral:
var err error
oval, err = strconv.Unquote(item.Val)
if err != nil {
return rnq, x.Wrapf(err, "while unquoting")
}
seenOval = true
case itemLanguage:
rnq.Lang = item.Val
case itemObjectType:
if rnq.Predicate == x.Star || rnq.Subject == x.Star {
return rnq, x.Errorf("If predicate/subject is *, value should be * as well")
}
val := strings.Trim(item.Val, " ")
// TODO: Check if this condition is required.
if strings.Trim(val, " ") == "*" {
return rnq, x.Errorf("itemObject can't be *")
}
// Lets find out the storage type from the type map.
t, ok := typeMap[val]
if !ok {
return rnq, x.Errorf("Unrecognized rdf type %s", val)
}
if oval == "" && t != types.StringID {
return rnq, x.Errorf("Invalid ObjectValue")
}
src := types.ValueForType(types.StringID)
src.Value = []byte(oval)
// if this is a password value dont re-encrypt. issue#2765
if t == types.PasswordID {
src.Tid = t
}
p, err := types.Convert(src, t)
if err != nil {
return rnq, err
}
if rnq.ObjectValue, err = types.ObjectValue(t, p.Value); err != nil {
return rnq, err
}
case itemComment:
isCommentLine = true
vend = true
case itemValidEnd:
vend = true
if !it.Next() {
return rnq, x.Errorf("Invalid end of input. Input: [%s]", line)
}
// RDF spec says N-Quads should be terminated with a newline. Since we break the input
// by newline already. We should get EOF or # after dot(.)
item = it.Item()
if !(item.Typ == lex.ItemEOF || item.Typ == itemComment) {
return rnq, x.Errorf("Invalid end of input. Expected newline or # after ."+
" Input: [%s]", line)
}
break L
case itemLabel:
rnq.Label = strings.Trim(item.Val, " ")
case itemLeftRound:
it.Prev() // backup '('
if err := parseFacets(it, &rnq); err != nil {
return rnq, x.Errorf(err.Error())
}
}
}
if !vend {
return rnq, x.Errorf("Invalid end of input. Input: [%s]", line)
}
if isCommentLine {
return rnq, ErrEmpty
}
// We only want to set default value if we have seen ObjectValue within "" and if we didn't
// already set it.
if seenOval && rnq.ObjectValue == nil {
rnq.ObjectValue = &api.Value{Val: &api.Value_DefaultVal{DefaultVal: oval}}
}
if (len(rnq.Subject) == 0 && len(rnq.SubjectVar) == 0) || len(rnq.Predicate) == 0 {
return rnq, x.Errorf("Empty required fields in NQuad. Input: [%s]", line)
}
if len(rnq.ObjectId) == 0 && rnq.ObjectValue == nil && len(rnq.ObjectVar) == 0 {
return rnq, x.Errorf("No Object in NQuad. Input: [%s]", line)
}
if (!sane(rnq.Subject) && len(rnq.SubjectVar) == 0) || !sane(rnq.Predicate) ||
(!sane(rnq.ObjectId) && len(rnq.ObjectVar) == 0) || !sane(rnq.Label) {
return rnq, x.Errorf("NQuad failed sanity check:%+v", rnq)
}
return rnq, nil
}
func parseFacets(it *lex.ItemIterator, rnq *api.NQuad) error {
if !it.Next() {
return x.Errorf("Unexpected end of facets.")
}
item := it.Item()
if item.Typ != itemLeftRound {
return x.Errorf("Expected '(' but found %v at Facet.", item.Val)
}
for it.Next() { // parse one key value pair
// parse key
item = it.Item()
if item.Typ != itemText {
return x.Errorf("Expected key but found %v.", item.Val)
}
facetKey := strings.TrimSpace(item.Val)
if len(facetKey) == 0 {
return x.Errorf("Empty facetKeys not allowed.")
}
// parse =
if !it.Next() {
return x.Errorf("Unexpected end of facets.")
}
item = it.Item()
if item.Typ != itemEqual {
return x.Errorf("Expected = after facetKey. Found %v", item.Val)
}
// parse value or empty value
if !it.Next() {
return x.Errorf("Unexpected end of facets.")
}
item = it.Item()
facetVal := ""
if item.Typ == itemText {
facetVal = item.Val
}
facet, err := facets.FacetFor(facetKey, facetVal)
if err != nil {
return err
}
rnq.Facets = append(rnq.Facets, facet)
// empty value case..
if item.Typ == itemRightRound {
break
}
if item.Typ == itemComma {
continue
}
if item.Typ != itemText {
return x.Errorf("Expected , or ) or text but found %s", item.Val)
}
// value was present..
if !it.Next() { // get either ')' or ','
return x.Errorf("Unexpected end of facets.")
}
item = it.Item()
if item.Typ == itemRightRound {
break
}
if item.Typ == itemComma {
continue
}
return x.Errorf("Expected , or ) after facet. Received %s", item.Val)
}
return nil
}
func isNewline(r rune) bool {
return r == '\n' || r == '\r'
}
var typeMap = map[string]types.TypeID{
"xs:password": types.PasswordID,
"xs:string": types.StringID,
"xs:date": types.DateTimeID,
"xs:dateTime": types.DateTimeID,
"xs:int": types.IntID,
"xs:positiveInteger": types.IntID,
"xs:boolean": types.BoolID,
"xs:double": types.FloatID,
"xs:float": types.FloatID,
"xs:base64Binary": types.BinaryID,
"geo:geojson": types.GeoID,
"http://www.w3.org/2001/XMLSchema#string": types.StringID,
"http://www.w3.org/2001/XMLSchema#dateTime": types.DateTimeID,
"http://www.w3.org/2001/XMLSchema#date": types.DateTimeID,
"http://www.w3.org/2001/XMLSchema#int": types.IntID,
"http://www.w3.org/2001/XMLSchema#positiveInteger": types.IntID,
"http://www.w3.org/2001/XMLSchema#integer": types.IntID,
"http://www.w3.org/2001/XMLSchema#boolean": types.BoolID,
"http://www.w3.org/2001/XMLSchema#double": types.FloatID,
"http://www.w3.org/2001/XMLSchema#float": types.FloatID,
"http://www.w3.org/2001/XMLSchema#gYear": types.DateTimeID,
"http://www.w3.org/2001/XMLSchema#gYearMonth": types.DateTimeID,
}