/
gettypes.go
660 lines (560 loc) · 12.5 KB
/
gettypes.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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
// Copyright 2012-2018, Rolf Veen and contributors.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ogdl
import (
"errors"
"fmt"
"math"
"reflect"
"regexp"
"strconv"
)
// Find returns a Graph with all subnodes that match the regular
// expression given. It only walks through the subnodes of the current Graph.
// If the regex doesn't compile, an error will be returned. If the result set
// is empty, both return values are nil (no error is signaled).
func (g *Graph) Find(re string) (*Graph, error) {
exp, err := regexp.Compile(re)
if err != nil {
return nil, err
}
r := New("_")
for _, node := range g.Out {
if exp.MatchString(node.ThisString()) {
r.Add(node)
}
}
return r, nil
}
// Int64 returns the node as an int64. If the node is not a number, it
// returns 0, or the default value if given.
func (g *Graph) Int64(def ...int64) int64 {
n, ok := _int64f(g.String())
if !ok {
if len(def) == 0 {
return 0
}
return def[0]
}
return n
}
// Float64 returns the node as a float64. If the node is not a number, it
// return NaN, or the default value if given.
func (g *Graph) Float64(def ...float64) float64 {
n, ok := _float64f(g.String())
if !ok {
if len(def) == 0 {
return math.NaN()
}
return def[0]
}
return n
}
// Bool returns the node as a boolean. If the node is not a
// boolean, it returns false, or the default value if given.
func (g *Graph) Bool(def ...bool) bool {
n, ok := _boolf(g.String())
if !ok {
if len(def) == 0 {
return false
}
return def[0]
}
return n
}
// ThisValue returns this node as a reflect.Value.
func (g *Graph) ThisValue() reflect.Value {
return reflect.ValueOf(g.This)
}
// Value returns the node as a reflect.Value.
func (g *Graph) Value() reflect.Value {
return reflect.ValueOf(g.Interface())
}
// String returns a string representation of this node, or an empty string.
// This function doesn't return an error, because it is mostly used in single
// variable return situations.
// String accepts one default value, which will be returned instead of an
// empty string.
func (g *Graph) String(def ...string) string {
// If g is nil, return default or nothing
if g == nil {
if len(def) == 0 {
return ""
}
return def[0]
}
return _string(g.Interface())
}
// StringCSV returns a comma separated value representation of all direct subnodes.
// StringCSV accepts one default value, which will be returned instead of an
// empty string.
func (g *Graph) StringCSV(def ...string) string {
// If g is nil, return default or nothing
if g == nil {
if len(def) == 0 {
return ""
}
return def[0]
}
s := ""
for _, n := range g.Out {
s += ", " + _string(n.This)
}
return s[2:]
}
// ThisString returns the current node content as a string
func (g *Graph) ThisString(def ...string) string {
// If g is nil, return default or nothing
if g == nil {
if len(def) == 0 {
return ""
}
return def[0]
}
return _string(g.This)
}
// Bytes returns the graph as []byte, or nil if not possble.
func (g *Graph) Bytes() []byte {
return _bytes(g.Interface())
}
// ThisBytes returns the node as []byte, or nil if not possble.
func (g *Graph) ThisBytes() []byte {
return _bytes(g.This)
}
// Number returns either a float64, int64 or nil
func (g *Graph) Number() interface{} {
return number(g.Interface())
}
// ThisNumber returns either a float64, int64 or nil
func (g *Graph) ThisNumber() interface{} {
return number(g.This)
}
// ThisInt64 returns a int64 or nil
func (g *Graph) ThisInt64() (int64, bool) {
return _int64f(g.This)
}
// ThisFloat64 returns a float64
func (g *Graph) ThisFloat64() (float64, bool) {
return _float64f(g.This)
}
// Scalar returns the current node content, reducing the number of types
// following these rules:
//
// uint* -> int64
// int* -> int64
// float* -> float64
// byte -> int64
// rune -> int64
// bool -> bool
// string, []byte: if it represents an int or float or bool,
// convert to int64, float64 or bool
//
// Any other type is returned as is.
//
func (g *Graph) Scalar() interface{} {
itf := g.Interface()
if itf == nil && g.Out != nil {
itf = g.Out[0].This
}
// If it can be parsed as a number, return it.
n := number(itf)
if n != nil {
return n
}
// If it can be parsed as a bool, return it.
b, ok := _boolf(itf)
if ok {
return b
}
// Else return as is.
return itf
}
// ThisScalar returns this node's content as an interface
func (g *Graph) ThisScalar() interface{} {
itf := g.This
if itf == nil && g.Out != nil {
itf = g.Out[0].This
}
// If it ca be parsed as a number, return it.
n := number(itf)
if n != nil {
return n
}
// If it can be parsed as a bool, return it.
b, ok := _boolf(itf)
if ok {
return b
}
// Else return as is.
return itf
}
// Interface returns the first child of this node as an interface
func (g *Graph) Interface() interface{} {
if g.Out != nil && len(g.Out) != 0 {
return g.Out[0].This
}
return nil
}
// number tries hard to convert the parameter to an int64 or float64. If it
// cannot, then it returns nil.
func number(itf interface{}) interface{} {
if itf == nil {
return nil
}
f, ok := _float64(itf)
if ok {
return f
}
i, ok := _int64(itf)
if ok {
return i
}
s := _string(itf)
if len(s) == 0 {
return nil
}
if isInteger(s) {
n, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return nil
}
return n
}
n, err := strconv.ParseFloat(s, 64)
if err != nil {
return nil
}
return n
}
// GetString returns the result of applying a path to the given Graph.
// The result is returned as a string.
// If the error information is not used, then this method is equivalent
// to Get(path).String()
func (g *Graph) GetString(path string) (string, error) {
// If path is "", return .String()
if len(path) == 0 {
return g.String(), nil
}
i := g.Get(path)
if i == nil {
return "", errors.New("not found")
}
if i.Len() == 0 {
return "", errors.New("Get() design error: not subnodes")
}
return _string(i.Out[0].This), nil
}
// GetBytes returns the result of applying a path to the given Graph.
// The result is returned as a byte slice.
func (g *Graph) GetBytes(path string) ([]byte, error) {
if len(path) == 0 {
return g.Bytes(), nil
}
i := g.Get(path)
if i == nil {
return nil, errors.New("not found")
}
if i.Len() == 0 {
return nil, errors.New("Get() design error: not subnodes")
}
return _bytes(i.Out[0].This), nil
}
// GetInt64 returns the result of applying a path to the given Graph.
// The result is returned as an int64. If the path result cannot be converted
// to an integer, then an error is returned.
func (g *Graph) GetInt64(path string) (int64, error) {
if len(path) == 0 {
return g.Int64(), nil
}
i := g.Get(path)
if i == nil {
return 0, errors.New("not found")
}
if i.Len() == 0 {
return 0, errors.New("Get() design error: not subnodes")
}
j, ok := _int64f(i.Out[0].This)
if !ok {
return 0, errors.New("incompatible type")
}
return j, nil
}
// GetFloat64 returns the result of applying a path to the given Graph.
// The result is returned as a float64. If the path result cannot be converted
// to a float, then an error is returned.
func (g *Graph) GetFloat64(path string) (float64, error) {
if len(path) == 0 {
return g.Float64(), nil
}
i := g.Get(path)
if i == nil {
return 0, errors.New("not found")
}
if i.Len() == 0 {
return 0, errors.New("Get() design error: not subnodes")
}
j, ok := _float64f(i.Out[0].This)
if !ok {
return 0, errors.New("not a number")
}
return j, nil
}
// GetBool returns the result of applying a path to the given Graph.
// The result is returned as a bool. If the path result cannot be converted
// to a boolean, then an error is returned.
func (g *Graph) GetBool(path string) (bool, error) {
if len(path) == 0 {
return g.Bool(), nil
}
i := g.Get(path)
if i == nil {
return false, errors.New("not found")
}
if i.Len() == 0 {
return false, errors.New("Get() design error: not subnodes")
}
j, ok := _boolf(i.Out[0].This)
if !ok {
return false, errors.New("not a boolean")
}
return j, nil
}
// _float64 converts an interface{} to a float64 iff its native type is
// a float, integer or a string representing a number.
func _float64f(v interface{}) (float64, bool) {
f, ok := _float64(v)
if ok {
return f, true
}
i, ok := _int64(v)
if ok {
return float64(i), true
}
s := _string(v)
f, err := strconv.ParseFloat(s, 64)
if err == nil {
return f, true
}
return 0, false
}
// _float64 converts an interface{} to a float64 iff its native type is
// a floating point.
func _float64(i interface{}) (float64, bool) {
switch v := i.(type) {
case float32:
return float64(v), true
case float64:
return v, true
}
return 0, false
}
// _int64f converts an interface{} to an int64 if its native type is
// a integer (whether 8, 16, 32 or 64 bits), or can be converted to one.
func _int64f(i interface{}) (int64, bool) {
if i2, ok := i.(*Graph); ok {
i = i2.This
}
switch v := i.(type) {
case []byte:
n, error := strconv.ParseInt(string(v), 10, 64)
if error == nil {
return n, true
}
case string:
n, error := strconv.ParseInt(v, 10, 64)
if error == nil {
return n, true
}
case float32:
return int64(v), true
case float64:
return int64(v), true
}
return _int64(i)
}
// _int64 converts an interface{} to an int64 if its native type is
// a integer (whether 8, 16, 32 or 64 bits, rune or byte).
func _int64(i interface{}) (int64, bool) {
switch v := i.(type) {
case int:
return int64(v), true
case int8:
return int64(v), true
case int16:
return int64(v), true
case int32:
return int64(v), true
case int64:
return v, true
case uint:
return int64(v), true
case uint8:
return int64(v), true
case uint16:
return int64(v), true
case uint32:
return int64(v), true
case uint64:
return int64(v), true
}
return 0, false
}
// _boolf converts an interface{} to a boolean if possible.
func _boolf(i interface{}) (bool, bool) {
if i2, ok := i.(*Graph); ok {
// i2 can still be nil (of type *Graph!)
if i2 == nil {
return false, false
}
i = i2.This
}
switch v := i.(type) {
case []byte:
s := string(v)
if falseStr == s {
return false, true
}
if trueStr == s {
return true, true
}
return false, false
case string:
if falseStr == v {
return false, true
}
if trueStr == v {
return true, true
}
return false, false
case bool:
return v, true
}
return false, false
}
func _string(i interface{}) string {
if i == nil {
return ""
}
if v, ok := i.([]byte); ok {
return string(v)
}
if v, ok := i.(string); ok {
return v
}
if v, ok := i.(*Graph); ok {
return v.String()
}
return fmt.Sprint(i)
}
// simplify graphs in the form:
// *
// something
// to a scalar (return something)
func _simplify(i interface{}) interface{} {
if v, ok := i.(*Graph); ok {
if v.Len() == 1 && v.Out[0].Len() == 0 {
return v.Out[0].This
}
}
return i
}
func _bytes(i interface{}) []byte {
if i == nil {
return nil
}
if v, ok := i.([]byte); ok {
return v
}
if v, ok := i.(string); ok {
return []byte(v)
}
if v, ok := i.(*Graph); ok {
return []byte(v.ThisString())
}
return []byte(fmt.Sprint(i))
}
func _text(i interface{}) string {
if i == nil {
return ""
}
if v, ok := i.([]byte); ok {
return string(v)
}
if v, ok := i.(string); ok {
return v
}
if v, ok := i.(*Graph); ok {
return v.Text()
}
return fmt.Sprint(i)
}
// _show: this function is used in tests
func _show(i interface{}) string {
if i == nil {
return ""
}
if v, ok := i.([]byte); ok {
return string(v)
}
if v, ok := i.(string); ok {
return v
}
if v, ok := i.(*Graph); ok {
return v.Show()
}
return fmt.Sprint(i)
}
// _typeOf: this function is used in tests
func _typeOf(i interface{}) string {
if i == nil {
return ""
}
return reflect.TypeOf(i).String()
}
// isNumber is only used in eval.go
func isNumber(s string) bool {
if len(s) == 0 {
return false
}
if !IsDigit(rune(s[0])) {
if len(s) < 2 || s[0] != '-' || !IsDigit(rune(s[1])) {
return false
}
}
return true
}
// IsInteger returns true for strings containing exclusively digits, with an
// optional minus sign at the beginning. Starting and trailing spaces are
// allowed.
func isInteger(s string) bool {
l := len(s)
if l == 0 {
return false
}
i := 0
for ; i < l; i++ {
if !IsSpaceChar(s[i]) {
break
}
}
if s[i] == '-' {
i++
}
n := 0
for ; i < l; i++ {
if !IsDigit(rune(s[i])) {
break
}
n++
}
if n == 0 {
return false
}
for ; i < l; i++ {
if !IsSpaceChar(s[i]) {
return false
}
}
return true
}