Skip to content

Commit

Permalink
lang: core, funcs, types: Add ctx to simple func
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
purpleidea committed May 9, 2024
1 parent 3b754d5 commit ca9d11e
Show file tree
Hide file tree
Showing 50 changed files with 159 additions and 102 deletions.
4 changes: 3 additions & 1 deletion lang/core/concat_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
package core

import (
"context"

"github.com/purpleidea/mgmt/lang/funcs"
"github.com/purpleidea/mgmt/lang/funcs/simple"
"github.com/purpleidea/mgmt/lang/types"
Expand All @@ -48,7 +50,7 @@ func init() {
}

// Concat concatenates two strings together.
func Concat(input []types.Value) (types.Value, error) {
func Concat(ctx context.Context, input []types.Value) (types.Value, error) {
return &types.StrValue{
V: input[0].Str() + input[1].Str(),
}, nil
Expand Down
3 changes: 2 additions & 1 deletion lang/core/convert/format_bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package convert

import (
"context"
"strconv"

"github.com/purpleidea/mgmt/lang/funcs/simple"
Expand All @@ -45,7 +46,7 @@ func init() {

// FormatBool converts a boolean to a string representation that can be consumed
// by ParseBool. This value will be `"true"` or `"false"`.
func FormatBool(input []types.Value) (types.Value, error) {
func FormatBool(ctx context.Context, input []types.Value) (types.Value, error) {
return &types.StrValue{
V: strconv.FormatBool(input[0].Bool()),
}, nil
Expand Down
3 changes: 2 additions & 1 deletion lang/core/convert/parse_bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package convert

import (
"context"
"fmt"
"strconv"

Expand All @@ -48,7 +49,7 @@ func init() {
// it an invalid value. Valid values match what is accepted by the golang
// strconv.ParseBool function. It's recommended to use the strings `true` or
// `false` if you are undecided about what string representation to choose.
func ParseBool(input []types.Value) (types.Value, error) {
func ParseBool(ctx context.Context, input []types.Value) (types.Value, error) {
s := input[0].Str()
b, err := strconv.ParseBool(s)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion lang/core/convert/to_float.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
package convert

import (
"context"

"github.com/purpleidea/mgmt/lang/funcs/simple"
"github.com/purpleidea/mgmt/lang/types"
)
Expand All @@ -42,7 +44,7 @@ func init() {
}

// ToFloat converts an integer to a float.
func ToFloat(input []types.Value) (types.Value, error) {
func ToFloat(ctx context.Context, input []types.Value) (types.Value, error) {
return &types.FloatValue{
V: float64(input[0].Int()),
}, nil
Expand Down
3 changes: 2 additions & 1 deletion lang/core/convert/to_float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@
package convert

import (
"context"
"testing"

"github.com/purpleidea/mgmt/lang/types"
)

func testToFloat(t *testing.T, input int64, expected float64) {
got, err := ToFloat([]types.Value{&types.IntValue{V: input}})
got, err := ToFloat(context.Background(), []types.Value{&types.IntValue{V: input}})
if err != nil {
t.Error(err)
return
Expand Down
4 changes: 3 additions & 1 deletion lang/core/convert/to_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
package convert

import (
"context"

"github.com/purpleidea/mgmt/lang/funcs/simple"
"github.com/purpleidea/mgmt/lang/types"
)
Expand All @@ -42,7 +44,7 @@ func init() {
}

// ToInt converts a float to an integer.
func ToInt(input []types.Value) (types.Value, error) {
func ToInt(ctx context.Context, input []types.Value) (types.Value, error) {
return &types.IntValue{
V: int64(input[0].Float()),
}, nil
Expand Down
3 changes: 2 additions & 1 deletion lang/core/convert/to_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@
package convert

import (
"context"
"testing"

"github.com/purpleidea/mgmt/lang/types"
)

func testToInt(t *testing.T, input float64, expected int64) {

got, err := ToInt([]types.Value{&types.FloatValue{V: input}})
got, err := ToInt(context.Background(), []types.Value{&types.FloatValue{V: input}})
if err != nil {
t.Error(err)
return
Expand Down
3 changes: 2 additions & 1 deletion lang/core/convert/to_str.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package convert

import (
"context"
"strconv"

"github.com/purpleidea/mgmt/lang/funcs/simple"
Expand All @@ -49,7 +50,7 @@ func init() {
}

// IntToStr converts an integer to a string.
func IntToStr(input []types.Value) (types.Value, error) {
func IntToStr(ctx context.Context, input []types.Value) (types.Value, error) {
return &types.StrValue{
V: strconv.Itoa(int(input[0].Int())),
}, nil
Expand Down
3 changes: 2 additions & 1 deletion lang/core/datetime/format_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package coredatetime

import (
"context"
"fmt"
"time"

Expand All @@ -48,7 +49,7 @@ func init() {
// has to be defined like specified by the golang "time" package. The time is
// the number of seconds since the epoch, and matches what comes from our Now
// function. Golang documentation: https://golang.org/pkg/time/#Time.Format
func Format(input []types.Value) (types.Value, error) {
func Format(ctx context.Context, input []types.Value) (types.Value, error) {
epochDelta := input[0].Int()
if epochDelta < 0 {
return nil, fmt.Errorf("epoch delta must be positive")
Expand Down
3 changes: 2 additions & 1 deletion lang/core/datetime/format_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
package coredatetime

import (
"context"
"testing"

"github.com/purpleidea/mgmt/lang/types"
Expand All @@ -41,7 +42,7 @@ func TestFormat(t *testing.T) {
inputVal := &types.IntValue{V: 1443158163}
inputFormat := &types.StrValue{V: "2006"}

val, err := Format([]types.Value{inputVal, inputFormat})
val, err := Format(context.Background(), []types.Value{inputVal, inputFormat})
if err != nil {
t.Error(err)
}
Expand Down
3 changes: 2 additions & 1 deletion lang/core/datetime/hour_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package coredatetime

import (
"context"
"fmt"
"time"

Expand All @@ -47,7 +48,7 @@ func init() {
// Hour returns the hour of the day corresponding to the input time. The time is
// the number of seconds since the epoch, and matches what comes from our Now
// function.
func Hour(input []types.Value) (types.Value, error) {
func Hour(ctx context.Context, input []types.Value) (types.Value, error) {
epochDelta := input[0].Int()
if epochDelta < 0 {
return nil, fmt.Errorf("epoch delta must be positive")
Expand Down
3 changes: 2 additions & 1 deletion lang/core/datetime/print_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package coredatetime

import (
"context"
"fmt"
"time"

Expand All @@ -41,7 +42,7 @@ func init() {
// FIXME: consider renaming this to printf, and add in a format string?
simple.ModuleRegister(ModuleName, "print", &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) {
epochDelta := input[0].Int()
if epochDelta < 0 {
return nil, fmt.Errorf("epoch delta must be positive")
Expand Down
3 changes: 2 additions & 1 deletion lang/core/datetime/weekday_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package coredatetime

import (
"context"
"fmt"
"strings"
"time"
Expand All @@ -48,7 +49,7 @@ func init() {
// Weekday returns the lowercased day of the week corresponding to the input
// time. The time is the number of seconds since the epoch, and matches what
// comes from our Now function.
func Weekday(input []types.Value) (types.Value, error) {
func Weekday(ctx context.Context, input []types.Value) (types.Value, error) {
epochDelta := input[0].Int()
if epochDelta < 0 {
return nil, fmt.Errorf("epoch delta must be positive")
Expand Down
5 changes: 4 additions & 1 deletion lang/core/embedded/provisioner/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,14 @@ func (obj *provisioner) Register(moduleName string) error {
// Build a few separately...
simple.ModuleRegister(moduleName, "cli_password", &types.FuncValue{
T: types.NewType("func() str"),
V: func(input []types.Value) (types.Value, error) {
V: func(ctx context.Context, input []types.Value) (types.Value, error) {
if obj.localArgs == nil {
// programming error
return nil, fmt.Errorf("could not convert/access our struct")
}

// TODO: plumb through the password lookup here instead?

//localArgs := *obj.localArgs // optional
return &types.StrValue{
V: obj.password,
Expand Down
4 changes: 3 additions & 1 deletion lang/core/example/answer_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
package coreexample

import (
"context"

"github.com/purpleidea/mgmt/lang/funcs/simple"
"github.com/purpleidea/mgmt/lang/types"
)
Expand All @@ -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
},
})
Expand Down
3 changes: 2 additions & 1 deletion lang/core/example/errorbool_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package coreexample

import (
"context"
"fmt"

"github.com/purpleidea/mgmt/lang/funcs/simple"
Expand All @@ -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")
}
Expand Down
3 changes: 2 additions & 1 deletion lang/core/example/int2str_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package coreexample

import (
"context"
"fmt"

"github.com/purpleidea/mgmt/lang/funcs/simple"
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion lang/core/example/nested/hello_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion lang/core/example/plus_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
package coreexample

import (
"context"

"github.com/purpleidea/mgmt/lang/funcs/simple"
"github.com/purpleidea/mgmt/lang/types"
)
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion lang/core/example/str2int_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package coreexample

import (
"context"
"strconv"

"github.com/purpleidea/mgmt/lang/funcs/simple"
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lang/core/iter/map_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ func (obj *MapFunc) replaceSubGraph(subgraphInput interfaces.Func) error {
outputListFunc := structs.SimpleFnToDirectFunc(
"mapOutputList",
&types.FuncValue{
V: func(args []types.Value) (types.Value, error) {
V: func(_ context.Context, args []types.Value) (types.Value, error) {
listValue := &types.ListValue{
V: args,
T: obj.outputListType,
Expand All @@ -788,7 +788,7 @@ func (obj *MapFunc) replaceSubGraph(subgraphInput interfaces.Func) error {
inputElemFunc := structs.SimpleFnToDirectFunc(
fmt.Sprintf("mapInputElem[%d]", i),
&types.FuncValue{
V: func(args []types.Value) (types.Value, error) {
V: func(_ context.Context, args []types.Value) (types.Value, error) {
if len(args) != 1 {
return nil, fmt.Errorf("inputElemFunc: expected a single argument")
}
Expand Down
3 changes: 2 additions & 1 deletion lang/core/len_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package core

import (
"context"
"fmt"

"github.com/purpleidea/mgmt/lang/funcs/simplepoly"
Expand All @@ -56,7 +57,7 @@ func init() {

// Len returns the number of elements in a list or the number of key pairs in a
// map. It can operate on either of these types.
func Len(input []types.Value) (types.Value, error) {
func Len(ctx context.Context, input []types.Value) (types.Value, error) {
var length int
switch k := input[0].Type().Kind; k {
case types.KindStr:
Expand Down
5 changes: 3 additions & 2 deletions lang/core/math/fortytwo_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package coremath

import (
"context"
"fmt"

"github.com/purpleidea/mgmt/lang/funcs/simplepoly"
Expand Down Expand Up @@ -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)
}
}
Expand Down
Loading

0 comments on commit ca9d11e

Please sign in to comment.