Skip to content

Commit

Permalink
lang: funcs: Add CallableFunc implementation for simple and simplepoly
Browse files Browse the repository at this point in the history
Add initial implementations for these wrapper functions.
  • Loading branch information
purpleidea committed Dec 6, 2023
1 parent 3ed0a8e commit 02add7a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
21 changes: 17 additions & 4 deletions lang/funcs/simple/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,18 @@ func (obj *WrappedFunc) Stream(ctx context.Context) error {
obj.last = input // store for next
}

values := []types.Value{}
// Use the existing implementation instead of this one
// which can't handle this case at the moment.
//args, err := interfaces.StructToCallableArgs(input)
args := []types.Value{}
for _, name := range obj.Fn.Type().Ord {
x := input.Struct()[name]
values = append(values, x)
args = append(args, x)
}

result, err := obj.Fn.Call(values) // (Value, error)
result, err := obj.Call(args)
if err != nil {
return errwrap.Wrapf(err, "simple function errored")
return err
}

// TODO: do we want obj.result to be a pointer instead?
Expand All @@ -184,3 +187,13 @@ func (obj *WrappedFunc) Stream(ctx context.Context) error {
}
}
}

// Call means this function implements the CallableFunc interface and can be
// called statically if we want to do it speculatively or from a resource.
func (obj *WrappedFunc) Call(args []types.Value) (types.Value, error) {
result, err := obj.Fn.Call(args) // (Value, error)
if err != nil {
return nil, errwrap.Wrapf(err, "simple function errored")
}
return result, err
}
30 changes: 17 additions & 13 deletions lang/funcs/simplepoly/simplepoly.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,24 +581,18 @@ func (obj *WrappedFunc) Stream(ctx context.Context) error {
obj.last = input // store for next
}

values := []types.Value{}
// Use the existing implementation instead of this one
// which can't handle this case at the moment.
//args, err := interfaces.StructToCallableArgs(input)
args := []types.Value{}
for _, name := range obj.fn.Type().Ord {
x := input.Struct()[name]
values = append(values, x)
args = append(args, x)
}

if obj.init.Debug {
obj.init.Logf("Calling function with: %+v", values)
}
result, err := obj.fn.Call(values) // (Value, error)
result, err := obj.Call(args)
if err != nil {
if obj.init.Debug {
obj.init.Logf("Function returned error: %+v", err)
}
return errwrap.Wrapf(err, "simple poly function errored")
}
if obj.init.Debug {
obj.init.Logf("Function returned with: %+v", result)
return err
}

// TODO: do we want obj.result to be a pointer instead?
Expand All @@ -621,3 +615,13 @@ func (obj *WrappedFunc) Stream(ctx context.Context) error {
}
}
}

// Call means this function implements the CallableFunc interface and can be
// called statically if we want to do it speculatively or from a resource.
func (obj *WrappedFunc) Call(args []types.Value) (types.Value, error) {
result, err := obj.fn.Call(args) // (Value, error)
if err != nil {
return nil, errwrap.Wrapf(err, "simple poly function errored")
}
return result, err
}

0 comments on commit 02add7a

Please sign in to comment.