lang: funcs: structs: Add Call method for const

This commit is contained in:
James Shubin
2024-12-08 13:51:27 -05:00
parent 28f5b8331a
commit e9dbb7b86c

View File

@@ -94,8 +94,12 @@ func (obj *ConstFunc) Init(init *interfaces.Init) error {
// Stream returns the single value that this const has, and then closes.
func (obj *ConstFunc) Stream(ctx context.Context) error {
value, err := obj.Call(ctx, nil)
if err != nil {
return err
}
select {
case obj.init.Output <- obj.Value:
case obj.init.Output <- value:
// pass
case <-ctx.Done():
return nil
@@ -103,3 +107,12 @@ func (obj *ConstFunc) Stream(ctx context.Context) error {
close(obj.init.Output) // signal that we're done sending
return nil
}
// Call this function with the input args and return the value if it is possible
// to do so at this time.
func (obj *ConstFunc) Call(ctx context.Context, args []types.Value) (types.Value, error) {
if obj.Value == nil {
return nil, fmt.Errorf("no value available from const")
}
return obj.Value, nil
}