All checks were successful
Build / build (push) Successful in 2m32s
- call now supports calling invokables by string - call now takes as arguments a listable of positional args, and a hashable of keyword args - added strs:split, strs:join, and strs:has-prefix - added new lists module with lists:first - added time:sleep
61 lines
992 B
Go
61 lines
992 B
Go
package builtins
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"ucl.lmika.dev/ucl"
|
|
)
|
|
|
|
func Lists() ucl.Module {
|
|
return ucl.Module{
|
|
Name: "lists",
|
|
Builtins: map[string]ucl.BuiltinHandler{
|
|
"first": listFirst,
|
|
},
|
|
}
|
|
}
|
|
|
|
func listFirst(ctx context.Context, args ucl.CallArgs) (any, error) {
|
|
var (
|
|
what ucl.Object
|
|
count int
|
|
)
|
|
|
|
if err := args.Bind(&what, &count); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if count == 0 {
|
|
return ucl.NewListObject(), nil
|
|
}
|
|
|
|
newList := ucl.NewListObject()
|
|
|
|
switch t := what.(type) {
|
|
case ucl.Listable:
|
|
if count < 0 {
|
|
count = t.Len() + count
|
|
}
|
|
|
|
for i := 0; i < min(count, t.Len()); i++ {
|
|
newList.Append(t.Index(i))
|
|
}
|
|
case ucl.Iterable:
|
|
if count < 0 {
|
|
return nil, errors.New("negative counts not supported on iters")
|
|
}
|
|
|
|
for i := 0; t.HasNext() && i < count; i++ {
|
|
v, err := t.Next(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
newList.Append(v)
|
|
}
|
|
default:
|
|
return nil, errors.New("expected listable")
|
|
}
|
|
|
|
return newList, nil
|
|
}
|