Skip to content

Commit

Permalink
fix: added raiseAnErrorIfSysIs32AndDriverReturns64 that raises panic …
Browse files Browse the repository at this point in the history
…for 32 bit sys due to limitation of go expr
  • Loading branch information
wwoytenko committed Oct 27, 2024
1 parent 97ae125 commit f3553ee
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/toolkit/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package toolkit

import (
"fmt"
"unsafe"

"github.com/expr-lang/expr"
"github.com/expr-lang/expr/ast"
Expand Down Expand Up @@ -108,6 +109,7 @@ func compileCond(whenCond string, driver *Driver, meta map[string]any) (
// column values by the column name. For instance if the column name is "name", the function __name will return
// the value
func newRecordContext(driver *Driver) (*RecordContext, []expr.Option) {
intSize := unsafe.Sizeof(int(0)) * 8
var funcs []expr.Option
rctx := NewRecordContext()
for _, c := range driver.Table.Columns {
Expand All @@ -128,6 +130,7 @@ func newRecordContext(driver *Driver) (*RecordContext, []expr.Option) {
case float32:
return float64(vv), nil
case int64:
raiseAnErrorIfSysIs32AndDriverReturns64(intSize)
return int(vv), nil
case int32:
return int(vv), nil
Expand Down Expand Up @@ -241,3 +244,12 @@ func patchRecordOp(node *ast.Node) {
ast.Patch(node, newOp)

}

// raiseAnErrorIfSysIs32AndDriverReturns64 - raises an error if the system is 32 bit and the driver returns 64 bit
// values. In 32-bit system int type is 32 bit but int 64 is 64 bit. In this case the int8 postgresql type cannot be
// handled using int type in go because it cast to int32 and loses the data. This is limitation of the go expr library
func raiseAnErrorIfSysIs32AndDriverReturns64(sysBytes uintptr) {
if sysBytes == 32 {
panic("go expr and pgx driver are not compatible to handle int8 postgresql type using int type in go")
}
}

0 comments on commit f3553ee

Please sign in to comment.