Skip to content

fix: support pointer slices in nonunique section mapping#381

Open
Yanhu007 wants to merge 1 commit intogo-ini:mainfrom
Yanhu007:fix/slice-struct-pointer-panic
Open

fix: support pointer slices in nonunique section mapping#381
Yanhu007 wants to merge 1 commit intogo-ini:mainfrom
Yanhu007:fix/slice-struct-pointer-panic

Conversation

@Yanhu007
Copy link
Copy Markdown

Fixes #370

Problem

When mapping INI sections to a struct with a slice of struct pointers:

type Config struct {
    Peers []*Peer `ini:"Peer,nonunique"`
}

mapToSlice calls reflect.New(typ) where typ is *Peer, creating a **Peer. Then mapToField only dereferences one level of pointer, causing a panic.

Fix

Detect when the slice element type is a pointer:

  • Allocate the underlying struct type (Peer) instead of the pointer type
  • Append the pointer (*Peer) to the slice instead of the struct value

Testing

All existing tests pass. Manually verified with the reproduction case from the issue:

// Before: panic: reflect: call of reflect.Value.NumField on ptr Value
// After:
// Peer 0: PublicKey=abc123 AllowedIPs=[10.0.0.1/32]
// Peer 1: PublicKey=def456 AllowedIPs=[10.0.0.2/32]

When a struct field is a slice of struct pointers (e.g. []*Peer),
mapToSlice used reflect.New on the pointer type, creating a **Peer.
mapToField only dereferences one level, causing a panic.

Detect when the slice element type is a pointer, allocate the
underlying struct type, and append the pointer to the slice.

Fixes go-ini#370
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

panic: slice of struct pointers

1 participant