Skip to content

Commit 3452f5b

Browse files
authored
builtin int unwraps underlying int value (#611)
1 parent cfe9787 commit 3452f5b

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

builtin/builtin_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -612,3 +612,17 @@ func TestBuiltin_bitOpsFunc(t *testing.T) {
612612
})
613613
}
614614
}
615+
616+
type customInt int
617+
618+
func Test_int_unwraps_underlying_value(t *testing.T) {
619+
env := map[string]any{
620+
"customInt": customInt(42),
621+
}
622+
program, err := expr.Compile(`int(customInt) == 42`, expr.Env(env))
623+
require.NoError(t, err)
624+
625+
out, err := expr.Run(program, env)
626+
require.NoError(t, err)
627+
assert.Equal(t, true, out)
628+
}

builtin/lib.go

+4
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ func Int(x any) any {
209209
}
210210
return i
211211
default:
212+
val := reflect.ValueOf(x)
213+
if val.CanConvert(integerType) {
214+
return val.Convert(integerType).Interface()
215+
}
212216
panic(fmt.Sprintf("invalid operation: int(%T)", x))
213217
}
214218
}

0 commit comments

Comments
 (0)