lang: core, funcs: Port some functions to CallableFunc API
Some modern features of our function engine and language might require this new API, so port what we can and figure out the rest later.
This commit is contained in:
@@ -108,15 +108,20 @@ func (obj *ChannelBasedSinkFunc) Stream(ctx context.Context) error {
|
||||
return nil // can't output any more
|
||||
}
|
||||
|
||||
value, exists := input.Struct()[obj.EdgeName]
|
||||
if !exists {
|
||||
return fmt.Errorf("programming error, can't find edge")
|
||||
args, err := interfaces.StructToCallableArgs(input) // []types.Value, error)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if obj.last != nil && value.Cmp(obj.last) == nil {
|
||||
result, err := obj.Call(ctx, args) // get the value...
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if obj.last != nil && result.Cmp(obj.last) == nil {
|
||||
continue // value didn't change, skip it
|
||||
}
|
||||
obj.last = value // store so we can send after this select
|
||||
obj.last = result // store so we can send after this select
|
||||
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
@@ -139,3 +144,13 @@ func (obj *ChannelBasedSinkFunc) Stream(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Call this function with the input args and return the value if it is possible
|
||||
// to do so at this time.
|
||||
// XXX: Is is correct to implement this here for this particular function?
|
||||
func (obj *ChannelBasedSinkFunc) Call(ctx context.Context, args []types.Value) (types.Value, error) {
|
||||
if len(args) != 1 {
|
||||
return nil, fmt.Errorf("programming error, can't find edge")
|
||||
}
|
||||
return args[0], nil
|
||||
}
|
||||
|
||||
@@ -113,3 +113,13 @@ func (obj *ChannelBasedSourceFunc) Stream(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// XXX: Is is correct to implement this here for this particular function?
|
||||
// XXX: tricky since this really receives input from a secret channel...
|
||||
// XXX: ADD A MUTEX AROUND READING obj.last ???
|
||||
//func (obj *ChannelBasedSourceFunc) Call(ctx context.Context, args []types.Value) (types.Value, error) {
|
||||
// if obj.last == nil {
|
||||
// return nil, fmt.Errorf("programming error")
|
||||
// }
|
||||
// return obj.last, nil
|
||||
//}
|
||||
|
||||
@@ -115,12 +115,14 @@ func (obj *IfFunc) Stream(ctx context.Context) error {
|
||||
}
|
||||
obj.last = input // store for next
|
||||
|
||||
var result types.Value
|
||||
args, err := interfaces.StructToCallableArgs(input) // []types.Value, error)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if input.Struct()["c"].Bool() {
|
||||
result = input.Struct()["a"] // true branch
|
||||
} else {
|
||||
result = input.Struct()["b"] // false branch
|
||||
result, err := obj.Call(ctx, args) // get the value...
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// skip sending an update...
|
||||
@@ -141,3 +143,13 @@ func (obj *IfFunc) Stream(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Call this function with the input args and return the value if it is possible
|
||||
// to do so at this time.
|
||||
// XXX: Is is correct to implement this here for this particular function?
|
||||
func (obj *IfFunc) Call(ctx context.Context, args []types.Value) (types.Value, error) {
|
||||
if c := args[0].Bool(); c {
|
||||
return args[1], nil // true branch
|
||||
}
|
||||
return args[2], nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user