@@ -461,4 +461,60 @@ var Functions = []*Function{
461
461
return runtime .Fetch (args [0 ], args [1 ]), nil
462
462
},
463
463
},
464
+ {
465
+ Name : "keys" ,
466
+ Func : func (args ... interface {}) (interface {}, error ) {
467
+ if len (args ) != 1 {
468
+ return nil , fmt .Errorf ("invalid number of arguments (expected 1, got %d)" , len (args ))
469
+ }
470
+ v := reflect .ValueOf (args [0 ])
471
+ if v .Kind () != reflect .Map {
472
+ return nil , fmt .Errorf ("cannot get keys from %s" , v .Kind ())
473
+ }
474
+ keys := v .MapKeys ()
475
+ out := make ([]interface {}, len (keys ))
476
+ for i , key := range keys {
477
+ out [i ] = key .Interface ()
478
+ }
479
+ return out , nil
480
+ },
481
+ Validate : func (args []reflect.Type ) (reflect.Type , error ) {
482
+ if len (args ) != 1 {
483
+ return anyType , fmt .Errorf ("invalid number of arguments (expected 1, got %d)" , len (args ))
484
+ }
485
+ switch kind (args [0 ]) {
486
+ case reflect .Map :
487
+ return arrayType , nil
488
+ }
489
+ return anyType , fmt .Errorf ("cannot get keys from %s" , args [0 ])
490
+ },
491
+ },
492
+ {
493
+ Name : "values" ,
494
+ Func : func (args ... interface {}) (interface {}, error ) {
495
+ if len (args ) != 1 {
496
+ return nil , fmt .Errorf ("invalid number of arguments (expected 1, got %d)" , len (args ))
497
+ }
498
+ v := reflect .ValueOf (args [0 ])
499
+ if v .Kind () != reflect .Map {
500
+ return nil , fmt .Errorf ("cannot get values from %s" , v .Kind ())
501
+ }
502
+ keys := v .MapKeys ()
503
+ out := make ([]interface {}, len (keys ))
504
+ for i , key := range keys {
505
+ out [i ] = v .MapIndex (key ).Interface ()
506
+ }
507
+ return out , nil
508
+ },
509
+ Validate : func (args []reflect.Type ) (reflect.Type , error ) {
510
+ if len (args ) != 1 {
511
+ return anyType , fmt .Errorf ("invalid number of arguments (expected 1, got %d)" , len (args ))
512
+ }
513
+ switch kind (args [0 ]) {
514
+ case reflect .Map :
515
+ return arrayType , nil
516
+ }
517
+ return anyType , fmt .Errorf ("cannot get values from %s" , args [0 ])
518
+ },
519
+ },
464
520
}
0 commit comments