From a818aab2a4b48e399d5186c235149ef21011a4af Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Wed, 15 Apr 2026 18:54:36 +0200 Subject: before scanning, spawn member structs within pointers as necessary --- plan.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'plan.go') diff --git a/plan.go b/plan.go index d00b5c2..245deab 100644 --- a/plan.go +++ b/plan.go @@ -21,8 +21,10 @@ type plan struct { PrimaryKeyColumnNames []string // from info.PrimaryKeyIs marker (if any) AutoColumnNames []string // subset of AllColumnNames where field has `,auto` marker - // Argument for reflect.Value.FieldByIndex() for each column name. + // Field index (i.e. argument for reflect.Value.FieldByIndex()) for each column name. IndexByColumnName map[string][]int + // Indexes of pointer-typed fields that need to be initialized before scanning into this type. + IndexesOfTransparentPointerStructs [][]int // In dialects with UsesLastInsertID() == true, whether the ID column must be written with reflect.Value.SetInt() or reflect.Value.SetUint(). FillIDWithSetUint bool @@ -85,6 +87,11 @@ func buildPlan(t reflect.Type, dialect Dialect, opts planOpts) (plan, error) { if field.Type.Kind() == reflect.Struct || (field.Type.Kind() == reflect.Pointer && field.Type.Elem().Kind() == reflect.Struct) { if field.Tag.Get("db") == "" { indexesOfUnusedTransparentStructs = append(indexesOfUnusedTransparentStructs, field.Index) + if field.Type.Kind() == reflect.Pointer { + // remember that, when scanning into a record of type `t`, we need to write a non-nil zeroed struct into this field + // to enable taking an address of its mapped member fields + p.IndexesOfTransparentPointerStructs = append(p.IndexesOfTransparentPointerStructs, field.Index) + } continue } indexesOfOpaqueStructs = append(indexesOfOpaqueStructs, field.Index) -- cgit v1.2.3