Skip to content

Commit

Permalink
allow receive text and varchars nulls if specified as optional column…
Browse files Browse the repository at this point in the history
… even if they are not declared as *string
  • Loading branch information
kataras committed Nov 14, 2023
1 parent 01caa52 commit d0cde4f
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions desc/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,10 @@ func findScanTargets(dstElemValue reflect.Value, td *Table, fieldDescs []pgconn.
}
}

if col.Type == UUID && col.Nullable {
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. */ {
scanTargets[i] = &nullableScanner{
uuidFieldPtr: dstElemValue.FieldByIndex(col.FieldIndex),
fieldPtr: dstElemValue.FieldByIndex(col.FieldIndex),
}

continue
Expand All @@ -164,15 +165,15 @@ type noOpScanner struct{}
func (t *noOpScanner) Scan(src interface{}) error { return nil }

type nullableScanner struct { // useful for UUIDs with null values.
uuidFieldPtr reflect.Value
fieldPtr reflect.Value
}

func (t *nullableScanner) Scan(src interface{}) error {
if src == nil {
if src == nil { // <- IMPORTANT.
return nil
}

t.uuidFieldPtr.Set(reflect.ValueOf(src))
t.fieldPtr.Set(reflect.ValueOf(src))

return nil
}
Expand Down

0 comments on commit d0cde4f

Please sign in to comment.