-
-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathlib.go
More file actions
622 lines (594 loc) · 13.8 KB
/
lib.go
File metadata and controls
622 lines (594 loc) · 13.8 KB
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
package builtin
import (
"fmt"
"math"
"reflect"
"strconv"
"unicode/utf8"
"github.com/expr-lang/expr/internal/deref"
"github.com/expr-lang/expr/vm/runtime"
)
func Len(x any) any {
v := reflect.ValueOf(x)
switch v.Kind() {
case reflect.Array, reflect.Slice, reflect.Map:
return v.Len()
case reflect.String:
return utf8.RuneCountInString(v.String())
default:
panic(fmt.Sprintf("invalid argument for len (type %T)", x))
}
}
func Type(arg any) any {
if arg == nil {
return "nil"
}
v := reflect.ValueOf(arg)
if v.Type().Name() != "" && v.Type().PkgPath() != "" {
return fmt.Sprintf("%s.%s", v.Type().PkgPath(), v.Type().Name())
}
switch v.Type().Kind() {
case reflect.Invalid:
return "invalid"
case reflect.Bool:
return "bool"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return "int"
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return "uint"
case reflect.Float32, reflect.Float64:
return "float"
case reflect.String:
return "string"
case reflect.Array, reflect.Slice:
return "array"
case reflect.Map:
return "map"
case reflect.Func:
return "func"
case reflect.Struct:
return "struct"
default:
return "unknown"
}
}
func Abs(x any) any {
switch x := x.(type) {
case float32:
if x < 0 {
return -x
} else {
return x
}
case float64:
if x < 0 {
return -x
} else {
return x
}
case int:
if x < 0 {
return -x
} else {
return x
}
case int8:
if x < 0 {
return -x
} else {
return x
}
case int16:
if x < 0 {
return -x
} else {
return x
}
case int32:
if x < 0 {
return -x
} else {
return x
}
case int64:
if x < 0 {
return -x
} else {
return x
}
case uint:
return x
case uint8:
return x
case uint16:
return x
case uint32:
return x
case uint64:
return x
}
panic(fmt.Sprintf("invalid argument for abs (type %T)", x))
}
func Ceil(x any) any {
switch x := x.(type) {
case float32:
return math.Ceil(float64(x))
case float64:
return math.Ceil(x)
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return Float(x)
}
panic(fmt.Sprintf("invalid argument for ceil (type %T)", x))
}
func Floor(x any) any {
switch x := x.(type) {
case float32:
return math.Floor(float64(x))
case float64:
return math.Floor(x)
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return Float(x)
}
panic(fmt.Sprintf("invalid argument for floor (type %T)", x))
}
func Round(x any) any {
switch x := x.(type) {
case float32:
return math.Round(float64(x))
case float64:
return math.Round(x)
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return Float(x)
}
panic(fmt.Sprintf("invalid argument for round (type %T)", x))
}
func Int(x any) any {
switch x := x.(type) {
case float32:
return int(x)
case float64:
return int(x)
case int:
return x
case int8:
return int(x)
case int16:
return int(x)
case int32:
return int(x)
case int64:
return int(x)
case uint:
return int(x)
case uint8:
return int(x)
case uint16:
return int(x)
case uint32:
return int(x)
case uint64:
return int(x)
case string:
i, err := strconv.Atoi(x)
if err != nil {
panic(fmt.Sprintf("invalid operation: int(%s)", x))
}
return i
default:
val := reflect.ValueOf(x)
if val.CanConvert(integerType) {
return val.Convert(integerType).Interface()
}
panic(fmt.Sprintf("invalid operation: int(%T)", x))
}
}
func Float(x any) any {
switch x := x.(type) {
case float32:
return float64(x)
case float64:
return x
case int:
return float64(x)
case int8:
return float64(x)
case int16:
return float64(x)
case int32:
return float64(x)
case int64:
return float64(x)
case uint:
return float64(x)
case uint8:
return float64(x)
case uint16:
return float64(x)
case uint32:
return float64(x)
case uint64:
return float64(x)
case string:
f, err := strconv.ParseFloat(x, 64)
if err != nil {
panic(fmt.Sprintf("invalid operation: float(%s)", x))
}
return f
default:
panic(fmt.Sprintf("invalid operation: float(%T)", x))
}
}
func String(arg any) any {
return fmt.Sprintf("%v", arg)
}
func minMax(name string, fn func(any, any) bool, depth int, args ...any) (any, error) {
if depth > MaxDepth {
return nil, ErrorMaxDepth
}
var val any
for _, arg := range args {
// Fast paths for common typed slices - avoid reflection and allocations
switch arr := arg.(type) {
case []int:
if len(arr) == 0 {
continue
}
m := arr[0]
for i := 1; i < len(arr); i++ {
if fn(m, arr[i]) {
m = arr[i]
}
}
if val == nil || fn(val, m) {
val = m
}
continue
case []float64:
if len(arr) == 0 {
continue
}
m := arr[0]
for i := 1; i < len(arr); i++ {
if fn(m, arr[i]) {
m = arr[i]
}
}
if val == nil || fn(val, m) {
val = m
}
continue
case []any:
// Fast path for []any with simple numeric types
for _, elem := range arr {
switch e := elem.(type) {
case int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64,
float32, float64:
if val == nil || fn(val, e) {
val = e
}
case []int, []float64, []any:
// Nested array - recurse
nested, err := minMax(name, fn, depth+1, e)
if err != nil {
return nil, err
}
if nested != nil && (val == nil || fn(val, nested)) {
val = nested
}
default:
// Could be another slice type, use reflection
rv := reflect.ValueOf(e)
if rv.Kind() == reflect.Slice || rv.Kind() == reflect.Array {
nested, err := minMax(name, fn, depth+1, e)
if err != nil {
return nil, err
}
if nested != nil && (val == nil || fn(val, nested)) {
val = nested
}
} else {
return nil, fmt.Errorf("invalid argument for %s (type %T)", name, e)
}
}
}
continue
}
// Slow path: use reflection for other types
rv := reflect.ValueOf(arg)
switch rv.Kind() {
case reflect.Array, reflect.Slice:
size := rv.Len()
for i := 0; i < size; i++ {
elemVal, err := minMax(name, fn, depth+1, rv.Index(i).Interface())
if err != nil {
return nil, err
}
switch elemVal.(type) {
case int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64,
float32, float64:
if elemVal != nil && (val == nil || fn(val, elemVal)) {
val = elemVal
}
default:
return nil, fmt.Errorf("invalid argument for %s (type %T)", name, elemVal)
}
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
elemVal := rv.Interface()
if val == nil || fn(val, elemVal) {
val = elemVal
}
default:
if len(args) == 1 {
return args[0], nil
}
return nil, fmt.Errorf("invalid argument for %s (type %T)", name, arg)
}
}
return val, nil
}
func mean(depth int, args ...any) (int, float64, error) {
if depth > MaxDepth {
return 0, 0, ErrorMaxDepth
}
var total float64
var count int
for _, arg := range args {
// Fast paths for common typed slices - avoid reflection and allocations
switch arr := arg.(type) {
case []int:
for _, v := range arr {
total += float64(v)
}
count += len(arr)
continue
case []float64:
for _, v := range arr {
total += v
}
count += len(arr)
continue
case []any:
// Fast path for []any - single pass without recursive calls for flat arrays
for _, elem := range arr {
switch e := elem.(type) {
case int:
total += float64(e)
count++
case float64:
total += e
count++
case []int, []float64, []any:
// Nested array - recurse
nestedCount, nestedSum, err := mean(depth+1, e)
if err != nil {
return 0, 0, err
}
total += nestedSum
count += nestedCount
default:
// Other numeric types or slices - use reflection
rv := reflect.ValueOf(e)
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
total += float64(rv.Int())
count++
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
total += float64(rv.Uint())
count++
case reflect.Float32, reflect.Float64:
total += rv.Float()
count++
case reflect.Slice, reflect.Array:
nestedCount, nestedSum, err := mean(depth+1, e)
if err != nil {
return 0, 0, err
}
total += nestedSum
count += nestedCount
default:
return 0, 0, fmt.Errorf("invalid argument for mean (type %T)", e)
}
}
}
continue
}
// Slow path: use reflection for other types
rv := reflect.ValueOf(arg)
switch rv.Kind() {
case reflect.Array, reflect.Slice:
size := rv.Len()
for i := 0; i < size; i++ {
elemCount, elemSum, err := mean(depth+1, rv.Index(i).Interface())
if err != nil {
return 0, 0, err
}
total += elemSum
count += elemCount
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
total += float64(rv.Int())
count++
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
total += float64(rv.Uint())
count++
case reflect.Float32, reflect.Float64:
total += rv.Float()
count++
default:
return 0, 0, fmt.Errorf("invalid argument for mean (type %T)", arg)
}
}
return count, total, nil
}
func median(depth int, args ...any) ([]float64, error) {
if depth > MaxDepth {
return nil, ErrorMaxDepth
}
var values []float64
for _, arg := range args {
// Fast paths for common typed slices - avoid reflection and allocations
switch arr := arg.(type) {
case []int:
for _, v := range arr {
values = append(values, float64(v))
}
continue
case []float64:
values = append(values, arr...)
continue
case []any:
// Fast path for []any - single pass without recursive calls for flat arrays
for _, elem := range arr {
switch e := elem.(type) {
case int:
values = append(values, float64(e))
case float64:
values = append(values, e)
case []int, []float64, []any:
// Nested array - recurse
elems, err := median(depth+1, e)
if err != nil {
return nil, err
}
values = append(values, elems...)
default:
// Other numeric types or slices - use reflection
rv := reflect.ValueOf(e)
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
values = append(values, float64(rv.Int()))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
values = append(values, float64(rv.Uint()))
case reflect.Float32, reflect.Float64:
values = append(values, rv.Float())
case reflect.Slice, reflect.Array:
elems, err := median(depth+1, e)
if err != nil {
return nil, err
}
values = append(values, elems...)
default:
return nil, fmt.Errorf("invalid argument for median (type %T)", e)
}
}
}
continue
}
// Slow path: use reflection for other types
rv := reflect.ValueOf(arg)
switch rv.Kind() {
case reflect.Array, reflect.Slice:
size := rv.Len()
for i := 0; i < size; i++ {
elems, err := median(depth+1, rv.Index(i).Interface())
if err != nil {
return nil, err
}
values = append(values, elems...)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
values = append(values, float64(rv.Int()))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
values = append(values, float64(rv.Uint()))
case reflect.Float32, reflect.Float64:
values = append(values, rv.Float())
default:
return nil, fmt.Errorf("invalid argument for median (type %T)", arg)
}
}
return values, nil
}
func flatten(arg reflect.Value, depth int) ([]any, error) {
if depth > MaxDepth {
return nil, ErrorMaxDepth
}
ret := []any{}
for i := 0; i < arg.Len(); i++ {
v := deref.Value(arg.Index(i))
if v.Kind() == reflect.Array || v.Kind() == reflect.Slice {
x, err := flatten(v, depth+1)
if err != nil {
return nil, err
}
ret = append(ret, x...)
} else {
ret = append(ret, v.Interface())
}
}
return ret, nil
}
func get(params ...any) (out any, err error) {
if len(params) < 2 {
return nil, fmt.Errorf("invalid number of arguments (expected 2, got %d)", len(params))
}
from := params[0]
i := params[1]
v := reflect.ValueOf(from)
if from == nil {
return nil, nil
}
if v.Kind() == reflect.Invalid {
panic(fmt.Sprintf("cannot fetch %v from %T", i, from))
}
// Methods can be defined on any type.
if v.NumMethod() > 0 {
if methodName, ok := i.(string); ok {
method := v.MethodByName(methodName)
if method.IsValid() {
return method.Interface(), nil
}
}
}
switch v.Kind() {
case reflect.Array, reflect.Slice, reflect.String:
index := runtime.ToInt(i)
l := v.Len()
if index < 0 {
index = l + index
}
if 0 <= index && index < l {
value := v.Index(index)
if value.IsValid() {
return value.Interface(), nil
}
}
case reflect.Map:
var value reflect.Value
if i == nil {
value = v.MapIndex(reflect.Zero(v.Type().Key()))
} else {
value = v.MapIndex(reflect.ValueOf(i))
}
if value.IsValid() {
return value.Interface(), nil
}
case reflect.Struct:
fieldName := i.(string)
t := v.Type()
field, ok := t.FieldByNameFunc(func(name string) bool {
f, _ := t.FieldByName(name)
switch f.Tag.Get("expr") {
case "-":
return false
case fieldName:
return true
default:
return name == fieldName
}
})
if ok && (field.IsExported() || t.PkgPath() == "") {
value := v.FieldByIndex(field.Index)
if value.IsValid() {
return runtime.ValueInterface(value), nil
}
}
}
// Main difference from runtime.Fetch
// is that we return `nil` instead of panic.
return nil, nil
}