Skip to content

Type checking for function's arguments #474

@mralves

Description

@mralves

When calling a function that receives a single float (any other type seems to work properly), expr.Compile now allows the argument to be of any type.
These tests reflect the issue:

func TestIssue(t *testing.T) {
	t.Parallel()
	testCases := []struct {
		name string
		code string
		fail bool
	}{
		{
			name: `when the parameter is a string`,
			code: `func("invalid")`,
			fail: true,
		},
		{
			name: `when the parameter is a bool`,
			code: `func(true)`,
			fail: true,
		},
		{
			name: `when the parameter is an array`,
			code: `func([])`,
			fail: true,
		},
		{
			name: `when the parameter is a map`,
			code: `func({})`,
			fail: true,
		},
		{
			name: `when the parameter is an integer`,
			code: `func(1)`,
			fail: false,
		},
		{
			name: `when the parameter is a float`,
			code: `func(1.5)`,
			fail: false,
		},
	}

	for _, tc := range testCases {
		ltc := tc
		t.Run(ltc.name, func(t *testing.T) {
			t.Parallel()
			function := expr.Function("func", func(params ...any) (any, error) {
				return true, nil
			}, new(func(float64) bool))
			_, err := expr.Compile(ltc.code, function)
			if ltc.fail {
				if err == nil {
					t.Error("expected an error, but it was nil")
					t.FailNow()
				}
			} else {
				if err != nil {
					t.Errorf("expected nil, but it was %v", err)
					t.FailNow()
				}
			}
		})
	}
}

The behavior was changed in v1.15.0 and still persists in v1.15.4.

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