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:
@@ -336,3 +336,27 @@ func (obj *StructLookupFunc) Stream(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Call returns the result of this function.
|
||||
func (obj *StructLookupFunc) Call(ctx context.Context, args []types.Value) (types.Value, error) {
|
||||
st := args[0].(*types.StructValue)
|
||||
field := args[1].Str()
|
||||
|
||||
if field == "" {
|
||||
return nil, fmt.Errorf("received empty field")
|
||||
}
|
||||
// TODO: Is it a hack to grab this first value?
|
||||
if obj.field == "" {
|
||||
// This can happen at compile time too. Bonus!
|
||||
obj.field = field // store first field
|
||||
}
|
||||
if field != obj.field {
|
||||
return nil, fmt.Errorf("input field changed from: `%s`, to: `%s`", obj.field, field)
|
||||
}
|
||||
result, exists := st.Lookup(obj.field)
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("could not lookup field: `%s` in struct", field)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user