Skip to content

WithContext doesn't work with 2 functions within 1 call #823

@ykalchevskiy

Description

@ykalchevskiy

Hi @antonmedv

If there are 2 patched function calls within 1 function call, it doesn't compile.

https://go.dev/play/p/9dH2e9KuF2d

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/expr-lang/expr"
)

type env struct {
	Ctx context.Context `expr:"ctx"`
}

func main() {
	p, err := expr.Compile(
		"now2().After(date2())",
		expr.Env(env{}),
		expr.WithContext("ctx"),
		expr.Function(
			"now2",
			func(params ...any) (any, error) { return time.Now(), nil },
			new(func(context.Context) time.Time),
		),
		expr.Function(
			"date2",
			func(params ...any) (any, error) { return time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), nil },
			new(func(context.Context) time.Time),
		),
	)
	if err != nil {
		panic(err)
	}
	r, err := expr.Run(p, &env{Ctx: context.Background()})
	if err != nil {
		panic(err)
	}
	fmt.Println(r)
}

Output:

panic: not enough arguments to call date2 (1:14)
	 | now2().After(date2())
	 | .............^

goroutine 1 [running]:
main.main()
	/tmp/sandbox2261374222/prog.go:32 +0x2d5

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions