-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdsl.go
532 lines (457 loc) · 11.4 KB
/
dsl.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
package triplestore
import (
"errors"
"fmt"
"reflect"
"strconv"
"time"
)
func SubjPredRes(s, p, r string) *triple {
return &triple{
sub: s, pred: p,
obj: Resource(r).(object),
}
}
func BnodePredRes(s, p, r string) *triple {
return &triple{
isSubBnode: true,
sub: s,
pred: p,
obj: Resource(r).(object),
}
}
func SubjPredBnode(s, p, r string) *triple {
return &triple{
sub: s,
pred: p,
obj: object{bnode: r, isBnode: true},
}
}
func SubjPredLit(s, p string, l interface{}) (*triple, error) {
o, err := ObjectLiteral(l)
if err != nil {
return nil, err
}
return &triple{
sub: s,
pred: p,
obj: o.(object),
}, nil
}
type tripleBuilder struct {
sub, pred string
isSubBnode bool
langtag string
}
func SubjPred(s, p string) *tripleBuilder {
return &tripleBuilder{sub: s, pred: p}
}
func BnodePred(s, p string) *tripleBuilder {
return &tripleBuilder{sub: s, isSubBnode: true, pred: p}
}
func Resource(s string) Object {
return object{resource: s}
}
func (b *tripleBuilder) Lang(l string) *tripleBuilder {
b.langtag = l
return b
}
func (b *tripleBuilder) Resource(s string) *triple {
return &triple{
isSubBnode: b.isSubBnode,
sub: b.sub,
pred: b.pred,
obj: Resource(s).(object),
}
}
func (b *tripleBuilder) Object(o Object) *triple {
return &triple{
isSubBnode: b.isSubBnode,
sub: b.sub,
pred: b.pred,
obj: o.(object),
}
}
func (b *tripleBuilder) Bnode(s string) *triple {
return &triple{
isSubBnode: b.isSubBnode,
sub: b.sub,
pred: b.pred,
obj: object{bnode: s, isBnode: true},
}
}
type UnsupportedLiteralTypeError struct {
i interface{}
}
func (e UnsupportedLiteralTypeError) Error() string {
return fmt.Sprintf("unsupported literal type %T", e.i)
}
func ObjectLiteral(i interface{}) (Object, error) {
switch ii := i.(type) {
case string:
return StringLiteral(ii), nil
case bool:
return BooleanLiteral(ii), nil
case int:
return IntegerLiteral(ii), nil
case int64, int32:
r := reflect.ValueOf(ii)
return IntegerLiteral(int(r.Int())), nil
case int16:
return Int16Literal(ii), nil
case int8:
return Int8Literal(ii), nil
case float32:
return Float32Literal(ii), nil
case float64:
return Float64Literal(ii), nil
case uint:
return UintegerLiteral(uint(ii)), nil
case uint64, uint32:
r := reflect.ValueOf(ii)
return UintegerLiteral(uint(r.Uint())), nil
case uint16:
return Uint16Literal(ii), nil
case uint8:
return Uint8Literal(ii), nil
case time.Time:
return DateTimeLiteral(ii), nil
case *time.Time:
return DateTimeLiteral(*ii), nil
case fmt.Stringer:
return StringLiteral(ii.String()), nil
default:
return nil, UnsupportedLiteralTypeError{i}
}
}
func ParseLiteral(obj Object) (interface{}, error) {
if lit, ok := obj.Literal(); ok {
switch lit.Type() {
case XsdBoolean:
return ParseBoolean(obj)
case XsdDateTime:
return ParseDateTime(obj)
case XsdInteger:
return ParseInteger(obj)
case XsdByte:
return ParseInt8(obj)
case XsdShort:
return ParseInt16(obj)
case XsdUinteger:
return ParseUinteger(obj)
case XsdUnsignedByte:
return ParseUint8(obj)
case XsdUnsignedShort:
return ParseUint16(obj)
case XsdDouble:
return ParseFloat64(obj)
case XsdFloat:
return ParseFloat32(obj)
case XsdString:
return ParseString(obj)
default:
return nil, fmt.Errorf("unknown literal type: %s", lit.Type())
}
}
return nil, errors.New("cannot parse literal: object is not literal")
}
func BooleanLiteral(bl bool) Object {
return object{
isLit: true,
lit: literal{typ: XsdBoolean, val: fmt.Sprint(bl)},
}
}
func (b *tripleBuilder) BooleanLiteral(bl bool) *triple {
return &triple{
isSubBnode: b.isSubBnode,
sub: b.sub,
pred: b.pred,
obj: BooleanLiteral(bl).(object),
}
}
func ParseBoolean(obj Object) (bool, error) {
if lit, ok := obj.Literal(); ok {
if lit.Type() != XsdBoolean {
return false, fmt.Errorf("literal is not an %s but %s", XsdBoolean, lit.Type())
}
return strconv.ParseBool(lit.Value())
}
return false, fmt.Errorf("cannot parse %s: object is not literal", XsdBoolean)
}
func IntegerLiteral(i int) Object {
return object{
isLit: true,
lit: literal{typ: XsdInteger, val: fmt.Sprint(i)},
}
}
func (b *tripleBuilder) IntegerLiteral(i int) *triple {
return &triple{
isSubBnode: b.isSubBnode,
sub: b.sub,
pred: b.pred,
obj: IntegerLiteral(i).(object),
}
}
func ParseInteger(obj Object) (int, error) {
if lit, ok := obj.Literal(); ok {
if lit.Type() != XsdInteger {
return 0, fmt.Errorf("literal is not an %s but %s", XsdInteger, lit.Type())
}
return strconv.Atoi(lit.Value())
}
return 0, fmt.Errorf("cannot parse %s: object is not literal", XsdInteger)
}
func Int8Literal(i int8) Object {
return object{
isLit: true,
lit: literal{typ: XsdByte, val: fmt.Sprint(i)},
}
}
func (b *tripleBuilder) Int8Literal(i int8) *triple {
return &triple{
isSubBnode: b.isSubBnode,
sub: b.sub,
pred: b.pred,
obj: Int8Literal(i).(object),
}
}
func ParseInt8(obj Object) (int8, error) {
if lit, ok := obj.Literal(); ok {
if lit.Type() != XsdByte {
return 0, fmt.Errorf("literal is not an %s but %s", XsdByte, lit.Type())
}
num, err := strconv.ParseInt(lit.Value(), 10, 8)
if err != nil {
return 0, err
}
return int8(num), nil
}
return 0, fmt.Errorf("cannot parse %s: object is not literal", XsdByte)
}
func Int16Literal(i int16) Object {
return object{
isLit: true,
lit: literal{typ: XsdShort, val: fmt.Sprint(i)},
}
}
func (b *tripleBuilder) Int16Literal(i int16) *triple {
return &triple{
isSubBnode: b.isSubBnode,
sub: b.sub,
pred: b.pred,
obj: Int16Literal(i).(object),
}
}
func ParseInt16(obj Object) (int16, error) {
if lit, ok := obj.Literal(); ok {
if lit.Type() != XsdShort {
return 0, fmt.Errorf("literal is not an %s but %s", XsdShort, lit.Type())
}
num, err := strconv.ParseInt(lit.Value(), 10, 16)
if err != nil {
return 0, err
}
return int16(num), nil
}
return 0, fmt.Errorf("cannot parse %s: object is not literal", XsdShort)
}
func UintegerLiteral(i uint) Object {
return object{
isLit: true,
lit: literal{typ: XsdUinteger, val: fmt.Sprint(i)},
}
}
func (b *tripleBuilder) UintegerLiteral(i uint) *triple {
return &triple{
isSubBnode: b.isSubBnode,
sub: b.sub,
pred: b.pred,
obj: UintegerLiteral(i).(object),
}
}
func ParseUinteger(obj Object) (uint, error) {
if lit, ok := obj.Literal(); ok {
if lit.Type() != XsdUinteger {
return 0, fmt.Errorf("literal is not an %s but %s", XsdUinteger, lit.Type())
}
num, err := strconv.ParseUint(lit.Value(), 10, 64)
if err != nil {
return 0, err
}
return uint(num), nil
}
return 0, fmt.Errorf("cannot parse %s: object is not literal", XsdUinteger)
}
func Uint8Literal(i uint8) Object {
return object{
isLit: true,
lit: literal{typ: XsdUnsignedByte, val: fmt.Sprint(i)},
}
}
func (b *tripleBuilder) Uint8(i uint8) *triple {
return &triple{
isSubBnode: b.isSubBnode,
sub: b.sub,
pred: b.pred,
obj: Uint8Literal(i).(object),
}
}
func ParseUint8(obj Object) (uint8, error) {
if lit, ok := obj.Literal(); ok {
if lit.Type() != XsdUnsignedByte {
return 0, fmt.Errorf("literal is not an %s but %s", XsdUnsignedByte, lit.Type())
}
num, err := strconv.ParseUint(lit.Value(), 10, 8)
if err != nil {
return 0, err
}
return uint8(num), nil
}
return 0, fmt.Errorf("cannot parse %s: object is not literal", XsdUnsignedByte)
}
func Uint16Literal(i uint16) Object {
return object{
isLit: true,
lit: literal{typ: XsdUnsignedShort, val: fmt.Sprint(i)},
}
}
func (b *tripleBuilder) Uint16(i uint16) *triple {
return &triple{
isSubBnode: b.isSubBnode,
sub: b.sub,
pred: b.pred,
obj: Uint16Literal(i).(object),
}
}
func ParseUint16(obj Object) (uint16, error) {
if lit, ok := obj.Literal(); ok {
if lit.Type() != XsdUnsignedShort {
return 0, fmt.Errorf("literal is not an %s but %s", XsdUnsignedShort, lit.Type())
}
num, err := strconv.ParseUint(lit.Value(), 10, 16)
if err != nil {
return 0, err
}
return uint16(num), nil
}
return 0, fmt.Errorf("cannot parse %s: object is not literal", XsdUnsignedShort)
}
func Float64Literal(i float64) Object {
return object{
isLit: true,
lit: literal{typ: XsdDouble, val: fmt.Sprint(i)},
}
}
func (b *tripleBuilder) Float64Literal(i float64) *triple {
return &triple{
isSubBnode: b.isSubBnode,
sub: b.sub,
pred: b.pred,
obj: Float64Literal(i).(object),
}
}
func ParseFloat64(obj Object) (float64, error) {
if lit, ok := obj.Literal(); ok {
if lit.Type() != XsdDouble {
return 0, fmt.Errorf("literal is not an %s but %s", XsdDouble, lit.Type())
}
return strconv.ParseFloat(lit.Value(), 64)
}
return 0, fmt.Errorf("cannot parse %s: object is not literal", XsdDouble)
}
func Float32Literal(i float32) Object {
return object{
isLit: true,
lit: literal{typ: XsdFloat, val: fmt.Sprint(i)},
}
}
func (b *tripleBuilder) Float32Literal(i float32) *triple {
return &triple{
isSubBnode: b.isSubBnode,
sub: b.sub,
pred: b.pred,
obj: Float32Literal(i).(object),
}
}
func ParseFloat32(obj Object) (float32, error) {
if lit, ok := obj.Literal(); ok {
if lit.Type() != XsdFloat {
return 0, fmt.Errorf("literal is not an %s but %s", XsdFloat, lit.Type())
}
conv, err := strconv.ParseFloat(lit.Value(), 32)
if err != nil {
return 0, err
}
return float32(conv), nil
}
return 0, fmt.Errorf("cannot parse %s: object is not literal", XsdFloat)
}
func StringLiteral(s string) Object {
return object{
isLit: true,
lit: literal{typ: XsdString, val: s},
}
}
func StringLiteralWithLang(s, l string) Object {
return object{
isLit: true,
lit: literal{typ: XsdString, val: s, langtag: l},
}
}
func (b *tripleBuilder) StringLiteral(s string) *triple {
return &triple{
isSubBnode: b.isSubBnode,
sub: b.sub,
pred: b.pred,
obj: StringLiteral(s).(object),
}
}
func (b *tripleBuilder) StringLiteralWithLang(s, l string) *triple {
return &triple{
isSubBnode: b.isSubBnode,
sub: b.sub,
pred: b.pred,
obj: StringLiteralWithLang(s, l).(object),
}
}
func ParseString(obj Object) (string, error) {
if lit, ok := obj.Literal(); ok {
if lit.Type() != XsdString {
return "", fmt.Errorf("literal is not a %s but %s", XsdString, lit.Type())
}
return lit.Value(), nil
}
return "", fmt.Errorf("cannot parse %s: object is not literal", XsdString)
}
func DateTimeLiteral(tm time.Time) Object {
text, err := tm.UTC().MarshalText()
if err != nil {
panic(fmt.Errorf("date time literal: %s", err))
}
return object{
isLit: true,
lit: literal{typ: XsdDateTime, val: string(text)},
}
}
func (b *tripleBuilder) DateTimeLiteral(tm time.Time) *triple {
return &triple{
isSubBnode: b.isSubBnode,
sub: b.sub,
pred: b.pred,
obj: DateTimeLiteral(tm).(object),
}
}
func ParseDateTime(obj Object) (time.Time, error) {
var t time.Time
if lit, ok := obj.Literal(); ok {
if lit.Type() != XsdDateTime {
return t, fmt.Errorf("literal is not an %s but %s", XsdDateTime, lit.Type())
}
err := t.UnmarshalText([]byte(lit.Value()))
if err != nil {
return t, err
}
return t, nil
}
return t, fmt.Errorf("cannot parse %s: object is not literal", XsdDateTime)
}