lang: core, funcs, types: Add ctx to simple func

Plumb through the standard context.Context so that a function can be
cancelled if someone requests this. It makes it less awkward to write
simple functions that might depend on io or network access.
This commit is contained in:
James Shubin
2024-05-09 19:25:46 -04:00
parent 3b754d5324
commit 415e22abe2
51 changed files with 166 additions and 108 deletions

View File

@@ -30,6 +30,8 @@
package coreexample
import (
"context"
"github.com/purpleidea/mgmt/lang/funcs/simple"
"github.com/purpleidea/mgmt/lang/types"
)
@@ -40,7 +42,7 @@ const Answer = 42
func init() {
simple.ModuleRegister(ModuleName, "answer", &types.FuncValue{
T: types.NewType("func() int"),
V: func([]types.Value) (types.Value, error) {
V: func(context.Context, []types.Value) (types.Value, error) {
return &types.IntValue{V: Answer}, nil
},
})

View File

@@ -30,6 +30,7 @@
package coreexample
import (
"context"
"fmt"
"github.com/purpleidea/mgmt/lang/funcs/simple"
@@ -39,7 +40,7 @@ import (
func init() {
simple.ModuleRegister(ModuleName, "errorbool", &types.FuncValue{
T: types.NewType("func(a bool) str"),
V: func(input []types.Value) (types.Value, error) {
V: func(ctx context.Context, input []types.Value) (types.Value, error) {
if input[0].Bool() {
return nil, fmt.Errorf("we errored on request")
}

View File

@@ -30,6 +30,7 @@
package coreexample
import (
"context"
"fmt"
"github.com/purpleidea/mgmt/lang/funcs/simple"
@@ -39,7 +40,7 @@ import (
func init() {
simple.ModuleRegister(ModuleName, "int2str", &types.FuncValue{
T: types.NewType("func(a int) str"),
V: func(input []types.Value) (types.Value, error) {
V: func(ctx context.Context, input []types.Value) (types.Value, error) {
return &types.StrValue{
V: fmt.Sprintf("%d", input[0].Int()),
}, nil

View File

@@ -30,6 +30,8 @@
package corenested
import (
"context"
coreexample "github.com/purpleidea/mgmt/lang/core/example"
"github.com/purpleidea/mgmt/lang/funcs/simple"
"github.com/purpleidea/mgmt/lang/types"
@@ -43,7 +45,7 @@ func init() {
}
// Hello returns some string. This is just to test nesting.
func Hello(input []types.Value) (types.Value, error) {
func Hello(ctx context.Context, input []types.Value) (types.Value, error) {
return &types.StrValue{
V: "Hello!",
}, nil

View File

@@ -30,6 +30,8 @@
package coreexample
import (
"context"
"github.com/purpleidea/mgmt/lang/funcs/simple"
"github.com/purpleidea/mgmt/lang/types"
)
@@ -42,7 +44,7 @@ func init() {
}
// Plus returns y + z.
func Plus(input []types.Value) (types.Value, error) {
func Plus(ctx context.Context, input []types.Value) (types.Value, error) {
y, z := input[0].Str(), input[1].Str()
return &types.StrValue{
V: y + z,

View File

@@ -30,6 +30,7 @@
package coreexample
import (
"context"
"strconv"
"github.com/purpleidea/mgmt/lang/funcs/simple"
@@ -39,7 +40,7 @@ import (
func init() {
simple.ModuleRegister(ModuleName, "str2int", &types.FuncValue{
T: types.NewType("func(a str) int"),
V: func(input []types.Value) (types.Value, error) {
V: func(ctx context.Context, input []types.Value) (types.Value, error) {
var i int64
if val, err := strconv.ParseInt(input[0].Str(), 10, 64); err == nil {
i = val