-
Notifications
You must be signed in to change notification settings - Fork 9
/
ugo.go
565 lines (527 loc) · 10.4 KB
/
ugo.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
// Copyright (c) 2020-2023 Ozan Hacıbekiroğlu.
// Use of this source code is governed by a MIT License
// that can be found in the LICENSE file.
package ugo
//go:generate go run ./cmd/mkcallable -output zfuncs.go ugo.go
import (
"fmt"
"strconv"
"unicode/utf8"
"github.com/ozanh/ugo/registry"
)
const (
// AttrModuleName is a special attribute injected into modules to identify
// the modules by name.
AttrModuleName = "__module_name__"
)
// CallableFunc is a function signature for a callable function.
type CallableFunc = func(args ...Object) (ret Object, err error)
// CallableExFunc is a function signature for a callable function that accepts
// a Call struct.
type CallableExFunc = func(Call) (ret Object, err error)
// ToObject will try to convert an interface{} v to an Object.
func ToObject(v interface{}) (ret Object, err error) {
switch v := v.(type) {
case nil:
ret = Undefined
case string:
ret = String(v)
case int64:
ret = Int(v)
case int:
ret = Int(v)
case uint:
ret = Uint(v)
case uint64:
ret = Uint(v)
case uintptr:
ret = Uint(v)
case bool:
if v {
ret = True
} else {
ret = False
}
case rune:
ret = Char(v)
case byte:
ret = Char(v)
case float64:
ret = Float(v)
case float32:
ret = Float(v)
case []byte:
if v != nil {
ret = Bytes(v)
} else {
ret = Bytes{}
}
case map[string]Object:
if v != nil {
ret = Map(v)
} else {
ret = Map{}
}
case map[string]interface{}:
m := make(Map, len(v))
for vk, vv := range v {
vo, err := ToObject(vv)
if err != nil {
return nil, err
}
m[vk] = vo
}
ret = m
case []Object:
if v != nil {
ret = Array(v)
} else {
ret = Array{}
}
case []interface{}:
arr := make(Array, len(v))
for i, vv := range v {
obj, err := ToObject(vv)
if err != nil {
return nil, err
}
arr[i] = obj
}
ret = arr
case Object:
ret = v
case CallableFunc:
if v != nil {
ret = &Function{Value: v}
} else {
ret = Undefined
}
case error:
ret = &Error{Message: v.Error(), Cause: v}
default:
if out, ok := registry.ToObject(v); ok {
ret, ok = out.(Object)
if ok {
return
}
}
err = fmt.Errorf("cannot convert to object: %T", v)
}
return
}
// ToObjectAlt is analogous to ToObject but it will always convert signed integers to
// Int and unsigned integers to Uint. It is an alternative to ToObject.
// Note that, this function is subject to change in the future.
func ToObjectAlt(v interface{}) (ret Object, err error) {
switch v := v.(type) {
case nil:
ret = Undefined
case string:
ret = String(v)
case bool:
if v {
ret = True
} else {
ret = False
}
case int:
ret = Int(v)
case int64:
ret = Int(v)
case uint64:
ret = Uint(v)
case float64:
ret = Float(v)
case float32:
ret = Float(v)
case int32:
ret = Int(v)
case int16:
ret = Int(v)
case int8:
ret = Int(v)
case uint:
ret = Uint(v)
case uint32:
ret = Uint(v)
case uint16:
ret = Uint(v)
case uint8:
ret = Uint(v)
case uintptr:
ret = Uint(v)
case []byte:
if v != nil {
ret = Bytes(v)
} else {
ret = Bytes{}
}
case map[string]interface{}:
m := make(Map, len(v))
for vk, vv := range v {
vo, err := ToObjectAlt(vv)
if err != nil {
return nil, err
}
m[vk] = vo
}
ret = m
case map[string]Object:
if v != nil {
ret = Map(v)
} else {
ret = Map{}
}
case []interface{}:
arr := make(Array, len(v))
for i, vv := range v {
obj, err := ToObjectAlt(vv)
if err != nil {
return nil, err
}
arr[i] = obj
}
ret = arr
case []Object:
if v != nil {
ret = Array(v)
} else {
ret = Array{}
}
case Object:
ret = v
case CallableFunc:
if v != nil {
ret = &Function{Value: v}
} else {
ret = Undefined
}
case error:
ret = &Error{Message: v.Error(), Cause: v}
default:
if out, ok := registry.ToObject(v); ok {
ret, ok = out.(Object)
if ok {
return
}
}
err = fmt.Errorf("cannot convert to object: %T", v)
}
return
}
// ToInterface tries to convert an Object o to an interface{} value.
func ToInterface(o Object) (ret interface{}) {
switch o := o.(type) {
case Int:
ret = int64(o)
case String:
ret = string(o)
case Bytes:
ret = []byte(o)
case Array:
arr := make([]interface{}, len(o))
for i, val := range o {
arr[i] = ToInterface(val)
}
ret = arr
case Map:
m := make(map[string]interface{}, len(o))
for key, v := range o {
m[key] = ToInterface(v)
}
ret = m
case Uint:
ret = uint64(o)
case Char:
ret = rune(o)
case Float:
ret = float64(o)
case Bool:
ret = bool(o)
case *SyncMap:
if o == nil {
return map[string]interface{}{}
}
o.RLock()
defer o.RUnlock()
m := make(map[string]interface{}, len(o.Value))
for key, v := range o.Value {
m[key] = ToInterface(v)
}
ret = m
case *UndefinedType:
ret = nil
default:
if out, ok := registry.ToInterface(o); ok {
ret = out
} else {
ret = o
}
}
return
}
// ToString will try to convert an Object to uGO string value.
func ToString(o Object) (v String, ok bool) {
if v, ok = o.(String); ok {
return
}
vv, ok := ToGoString(o)
if ok {
v = String(vv)
}
return
}
// ToBytes will try to convert an Object to uGO bytes value.
func ToBytes(o Object) (v Bytes, ok bool) {
if v, ok = o.(Bytes); ok {
return
}
vv, ok := ToGoByteSlice(o)
if ok {
v = Bytes(vv)
}
return
}
// ToInt will try to convert an Object to uGO int value.
func ToInt(o Object) (v Int, ok bool) {
if v, ok = o.(Int); ok {
return
}
vv, ok := ToGoInt64(o)
if ok {
v = Int(vv)
}
return
}
// ToUint will try to convert an Object to uGO uint value.
func ToUint(o Object) (v Uint, ok bool) {
if v, ok = o.(Uint); ok {
return
}
vv, ok := ToGoUint64(o)
if ok {
v = Uint(vv)
}
return
}
// ToFloat will try to convert an Object to uGO float value.
func ToFloat(o Object) (v Float, ok bool) {
if v, ok = o.(Float); ok {
return
}
vv, ok := ToGoFloat64(o)
if ok {
v = Float(vv)
}
return
}
// ToChar will try to convert an Object to uGO char value.
func ToChar(o Object) (v Char, ok bool) {
if v, ok = o.(Char); ok {
return
}
vv, ok := ToGoRune(o)
if ok {
v = Char(vv)
}
return
}
// ToBool will try to convert an Object to uGO bool value.
func ToBool(o Object) (v Bool, ok bool) {
if v, ok = o.(Bool); ok {
return
}
vv, ok := ToGoBool(o)
v = Bool(vv)
return
}
// ToArray will try to convert an Object to uGO array value.
func ToArray(o Object) (v Array, ok bool) {
v, ok = o.(Array)
return
}
// ToMap will try to convert an Object to uGO map value.
func ToMap(o Object) (v Map, ok bool) {
v, ok = o.(Map)
return
}
// ToSyncMap will try to convert an Object to uGO syncMap value.
func ToSyncMap(o Object) (v *SyncMap, ok bool) {
v, ok = o.(*SyncMap)
return
}
// ToGoString will try to convert an Object to Go string value.
func ToGoString(o Object) (v string, ok bool) {
if o == Undefined {
return
}
v, ok = o.String(), true
return
}
// ToGoByteSlice will try to convert an Object to Go byte slice.
func ToGoByteSlice(o Object) (v []byte, ok bool) {
switch o := o.(type) {
case Bytes:
v, ok = o, true
case String:
v, ok = make([]byte, len(o)), true
copy(v, o)
}
return
}
// ToGoInt will try to convert a numeric, bool or string Object to Go int value.
func ToGoInt(o Object) (v int, ok bool) {
switch o := o.(type) {
case Int:
v, ok = int(o), true
case Uint:
v, ok = int(o), true
case Float:
v, ok = int(o), true
case Char:
v, ok = int(o), true
case Bool:
ok = true
if o {
v = 1
}
case String:
if vv, err := strconv.ParseInt(string(o), 0, 0); err == nil {
v = int(vv)
ok = true
}
}
return
}
// ToGoInt64 will try to convert a numeric, bool or string Object to Go int64
// value.
func ToGoInt64(o Object) (v int64, ok bool) {
switch o := o.(type) {
case Int:
v, ok = int64(o), true
case Uint:
v, ok = int64(o), true
case Float:
v, ok = int64(o), true
case Char:
v, ok = int64(o), true
case Bool:
ok = true
if o {
v = 1
}
case String:
if vv, err := strconv.ParseInt(string(o), 0, 64); err == nil {
v = vv
ok = true
}
}
return
}
// ToGoUint64 will try to convert a numeric, bool or string Object to Go uint64
// value.
func ToGoUint64(o Object) (v uint64, ok bool) {
switch o := o.(type) {
case Int:
v, ok = uint64(o), true
case Uint:
v, ok = uint64(o), true
case Float:
v, ok = uint64(o), true
case Char:
v, ok = uint64(o), true
case Bool:
ok = true
if o {
v = 1
}
case String:
if vv, err := strconv.ParseUint(string(o), 0, 64); err == nil {
v = vv
ok = true
}
}
return
}
// ToGoFloat64 will try to convert a numeric, bool or string Object to Go
// float64 value.
func ToGoFloat64(o Object) (v float64, ok bool) {
switch o := o.(type) {
case Int:
v, ok = float64(o), true
case Uint:
v, ok = float64(o), true
case Float:
v, ok = float64(o), true
case Char:
v, ok = float64(o), true
case Bool:
ok = true
if o {
v = 1
}
case String:
if vv, err := strconv.ParseFloat(string(o), 64); err == nil {
v = vv
ok = true
}
}
return
}
// ToGoRune will try to convert a int like Object to Go rune value.
func ToGoRune(o Object) (v rune, ok bool) {
switch o := o.(type) {
case Int:
v, ok = rune(o), true
case Uint:
v, ok = rune(o), true
case Char:
v, ok = rune(o), true
case Float:
v, ok = rune(o), true
case String:
ok = true
v, _ = utf8.DecodeRuneInString(string(o))
case Bool:
ok = true
if o {
v = 1
}
}
return
}
// ToGoBool will try to convert an Object to Go bool value.
func ToGoBool(o Object) (v bool, ok bool) {
v, ok = !o.IsFalsy(), true
return
}
// functions to generate with mkcallable
// builtin delete
//
//ugo:callable func(o Object, k string) (err error)
// builtin copy, len, error, typeName, bool, string, isInt, isUint
// isFloat, isChar, isBool, isString, isBytes, isMap, isSyncMap, isArray
// isUndefined, isFunction, isCallable, isIterable
//
//ugo:callable func(o Object) (ret Object)
// builtin repeat
//
//ugo:callable func(o Object, n int) (ret Object, err error)
// builtin :makeArray
//
//ugo:callable func(n int, o Object) (ret Object, err error)
// builtin contains
//
//ugo:callable func(o Object, v Object) (ret Object, err error)
// builtin sort, sortReverse, int, uint, float, char, chars
//
//ugo:callable func(o Object) (ret Object, err error)
// builtin int
//
//ugo:callable func(v int64) (ret Object)
// builtin uint
//
//ugo:callable func(v uint64) (ret Object)
// builtin float
//
//ugo:callable func(v float64) (ret Object)