diff --git a/builtin/builtin_test.go b/builtin/builtin_test.go index aa324c9b..7f5045f4 100644 --- a/builtin/builtin_test.go +++ b/builtin/builtin_test.go @@ -612,3 +612,17 @@ func TestBuiltin_bitOpsFunc(t *testing.T) { }) } } + +type customInt int + +func Test_int_unwraps_underlying_value(t *testing.T) { + env := map[string]any{ + "customInt": customInt(42), + } + program, err := expr.Compile(`int(customInt) == 42`, expr.Env(env)) + require.NoError(t, err) + + out, err := expr.Run(program, env) + require.NoError(t, err) + assert.Equal(t, true, out) +} diff --git a/builtin/lib.go b/builtin/lib.go index 9ff9478a..e3a6c0ae 100644 --- a/builtin/lib.go +++ b/builtin/lib.go @@ -209,6 +209,10 @@ func Int(x any) any { } return i default: + val := reflect.ValueOf(x) + if val.CanConvert(integerType) { + return val.Convert(integerType).Interface() + } panic(fmt.Sprintf("invalid operation: int(%T)", x)) } }