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,7 @@
package coremath
import (
"context"
"fmt"
"github.com/purpleidea/mgmt/lang/funcs/simplepoly"
@@ -57,8 +58,8 @@ func init() {
// in a sig field, like how we demonstrate in the implementation of FortyTwo. If
// the API doesn't change, then this is an example of how to build this as a
// wrapper.
func fortyTwo(sig *types.Type) func([]types.Value) (types.Value, error) {
return func(input []types.Value) (types.Value, error) {
func fortyTwo(sig *types.Type) func(context.Context, []types.Value) (types.Value, error) {
return func(ctx context.Context, input []types.Value) (types.Value, error) {
return FortyTwo(sig, input)
}
}

View File

@@ -30,6 +30,8 @@
package coremath
import (
"context"
"github.com/purpleidea/mgmt/lang/funcs/simple"
"github.com/purpleidea/mgmt/lang/types"
)
@@ -42,7 +44,7 @@ func init() {
}
// Minus1 takes an int and subtracts one from it.
func Minus1(input []types.Value) (types.Value, error) {
func Minus1(ctx context.Context, input []types.Value) (types.Value, error) {
// TODO: check for overflow
return &types.IntValue{
V: input[0].Int() - 1,

View File

@@ -30,6 +30,7 @@
package coremath
import (
"context"
"fmt"
"math"
@@ -54,7 +55,7 @@ func init() {
// both of KindInt or both of KindFloat, and it will return the same kind. If
// you pass in a divisor of zero, this will error, eg: mod(x, 0) = NaN.
// TODO: consider returning zero instead of erroring?
func Mod(input []types.Value) (types.Value, error) {
func Mod(ctx context.Context, input []types.Value) (types.Value, error) {
var x, y float64
var float bool
k := input[0].Type().Kind

View File

@@ -30,6 +30,7 @@
package coremath
import (
"context"
"fmt"
"math"
@@ -45,7 +46,7 @@ func init() {
}
// Pow returns x ^ y, the base-x exponential of y.
func Pow(input []types.Value) (types.Value, error) {
func Pow(ctx context.Context, input []types.Value) (types.Value, error) {
x, y := input[0].Float(), input[1].Float()
// FIXME: check for overflow
z := math.Pow(x, y)

View File

@@ -30,6 +30,7 @@
package coremath
import (
"context"
"fmt"
"math"
@@ -45,7 +46,7 @@ func init() {
}
// Sqrt returns sqrt(x), the square root of x.
func Sqrt(input []types.Value) (types.Value, error) {
func Sqrt(ctx context.Context, input []types.Value) (types.Value, error) {
x := input[0].Float()
y := math.Sqrt(x)
if math.IsNaN(y) {

View File

@@ -30,6 +30,7 @@
package coremath
import (
"context"
"fmt"
"math"
"testing"
@@ -40,7 +41,7 @@ import (
func testSqrtSuccess(input, sqrt float64) error {
inputVal := &types.FloatValue{V: input}
val, err := Sqrt([]types.Value{inputVal})
val, err := Sqrt(context.Background(), []types.Value{inputVal})
if err != nil {
return err
}
@@ -52,7 +53,7 @@ func testSqrtSuccess(input, sqrt float64) error {
func testSqrtError(input float64) error {
inputVal := &types.FloatValue{V: input}
_, err := Sqrt([]types.Value{inputVal})
_, err := Sqrt(context.Background(), []types.Value{inputVal})
if err == nil {
return fmt.Errorf("expected error for input %f, got nil", input)
}