Skip to content

Commit

Permalink
add mapValue function to get the map value
Browse files Browse the repository at this point in the history
  • Loading branch information
SimFG committed Sep 19, 2024
1 parent 50845ee commit 9ca3d59
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,37 @@ var Builtins = []*Function{
return anyType, fmt.Errorf("cannot get values from %s", args[0])
},
},
{
Name: "mapValue",
Func: func(args ...any) (any, error) {
if len(args) != 2 {
return nil, fmt.Errorf("invalid number of arguments (expected 2, got %d)", len(args))
}
v := reflect.ValueOf(args[0])
if v.Kind() != reflect.Map {
return nil, fmt.Errorf("cannot get values from %s", v.Kind())
}
keys := v.MapKeys()
for _, key := range keys {
if key.Interface() == args[1] {
return v.MapIndex(key).Interface(), nil
}
}
return nil, nil
},
Validate: func(args []reflect.Type) (reflect.Type, error) {
if len(args) != 2 {
return anyType, fmt.Errorf("invalid number of arguments (expected 2, got %d)", len(args))
}
switch kind(args[0]) {
case reflect.Interface:
return arrayType, nil
case reflect.Map:
return arrayType, nil
}
return anyType, fmt.Errorf("cannot get values from %s", args[0])
},
},
{
Name: "toPairs",
Func: func(args ...any) (any, error) {
Expand Down

0 comments on commit 9ca3d59

Please sign in to comment.