Skip to content

Commit

Permalink
builtin int unwraps underlying int value (#611)
Browse files Browse the repository at this point in the history
  • Loading branch information
bizywizy authored Mar 20, 2024
1 parent cfe9787 commit 3452f5b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
14 changes: 14 additions & 0 deletions builtin/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions builtin/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Expand Down

0 comments on commit 3452f5b

Please sign in to comment.