From 02add7a414591048b236dd5c8ec31d221748adfc Mon Sep 17 00:00:00 2001 From: James Shubin Date: Wed, 6 Dec 2023 16:12:22 -0500 Subject: [PATCH] lang: funcs: Add CallableFunc implementation for simple and simplepoly Add initial implementations for these wrapper functions. --- lang/funcs/simple/simple.go | 21 ++++++++++++++++---- lang/funcs/simplepoly/simplepoly.go | 30 ++++++++++++++++------------- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/lang/funcs/simple/simple.go b/lang/funcs/simple/simple.go index 28e83e711d..7dd93a27ca 100644 --- a/lang/funcs/simple/simple.go +++ b/lang/funcs/simple/simple.go @@ -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? @@ -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 +} diff --git a/lang/funcs/simplepoly/simplepoly.go b/lang/funcs/simplepoly/simplepoly.go index d9f22a5993..ead365b2e4 100644 --- a/lang/funcs/simplepoly/simplepoly.go +++ b/lang/funcs/simplepoly/simplepoly.go @@ -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? @@ -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 +}