Skip to content

Commit

Permalink
fix a bug when field type is a pointer, which should be handled by th…
Browse files Browse the repository at this point in the history
…e default behavior
  • Loading branch information
kataras committed Feb 22, 2024
1 parent f272aa6 commit 2eba915
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 15 deletions.
1 change: 1 addition & 0 deletions desc/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type (
OrdinalPosition int // the position (starting from 1) of the corresponding column in the table.
FieldIndex []int // the index of the corresponding struct field
FieldType reflect.Type // the reflect.Type of the corresponding struct field
isPtr bool // reprots whether FieldType.Kind() == reflect.Ptr.
/* if nil then wasn't able to resolve it by builtin method */
FieldName string // the name of the corresponding struct field
TypeArgument string // an optional argument for the data type, e.g. 255 when Type is "varchar"
Expand Down
8 changes: 7 additions & 1 deletion desc/column_basic_info.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package desc

import "reflect"

// ColumnBasicInfo represents a basic column information, contains the table name, column name, ordinal position, column default value,
// data type, data type argument, whether the column is nullable, whether the column is identity and whether the column is generated.
type ColumnBasicInfo struct {
Expand Down Expand Up @@ -36,7 +38,11 @@ func (c *ColumnBasicInfo) BuildColumn(column *Column) error {

column.FieldIndex = []int{c.OrdinalPosition}
column.FieldName = ToStructFieldName(c.Name)
column.FieldType = c.DataType.GoType()

if typ := c.DataType.GoType(); typ != nil {
column.FieldType = typ
column.isPtr = typ.Kind() == reflect.Ptr
}

return nil
}
5 changes: 3 additions & 2 deletions desc/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ func findScanTargets(dstElemValue reflect.Value, td *Table, fieldDescs []pgconn.
}
}

if col.Nullable && (col.Type == UUID ||
col.Type == Text || col.Type == CharacterVarying) /* Allow receive null on uuid, text and varchar columns even if the field is not a string pointer. */ {
if !col.isPtr /* Edward report it, AI had a solution but this is a bit faster as the driver already handles pointers and nullables */ &&
col.Nullable &&
(col.Type == UUID || col.Type == Text || col.Type == CharacterVarying) { /* Allow receive null on uuid, text and varchar columns even if the field is not a string pointer. */
scanTargets[i] = &nullableScanner{
fieldPtr: dstElemValue.FieldByIndex(col.FieldIndex),
}
Expand Down
1 change: 1 addition & 0 deletions desc/struct_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func convertStructFieldToColumnDefinion(tableName string, field reflect.StructFi
Type: goTypeToDataType(field.Type),
FieldIndex: field.Index,
FieldType: field.Type,
isPtr: field.Type.Kind() == reflect.Ptr,
FieldName: field.Name,
}

Expand Down
8 changes: 6 additions & 2 deletions desc/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,12 @@ func (expressions Expressions) FilterTable(t *Table) bool {
t.FilterColumns(func(c *Column) bool {
for i, filter := range columnFilters {
if filter(c) {
c.FieldType = parsedExpressions[i].Data.(reflect.Type) // expressions[i].ResultFieldType // the indexes between expressions and column filters match.
break // stop on first filter match.
if typ := parsedExpressions[i].Data.(reflect.Type); typ != nil {
c.FieldType = typ // expressions[i].ResultFieldType // the indexes between expressions and column filters match.
c.isPtr = typ.Kind() == reflect.Ptr
}

break // stop on first filter match.
}
}

Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
module github.com/kataras/pg

go 1.21
go 1.22

require (
github.com/gertd/go-pluralize v0.2.1
github.com/jackc/pgx/v5 v5.5.1
golang.org/x/mod v0.14.0
github.com/jackc/pgx/v5 v5.5.3
golang.org/x/mod v0.15.0
)

require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/text v0.14.0 // indirect
)
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsI
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.5.1 h1:5I9etrGkLrN+2XPCsi6XLlV5DITbSL/xBZdmAxFcXPI=
github.com/jackc/pgx/v5 v5.5.1/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA=
github.com/jackc/pgx/v5 v5.5.3 h1:Ces6/M3wbDXYpM8JyyPD57ivTtJACFZJd885pdIaV2s=
github.com/jackc/pgx/v5 v5.5.3/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -18,10 +18,10 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8=
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
Expand Down

0 comments on commit 2eba915

Please sign in to comment.