Skip to content

Map auto-increment keys to slice fields tagged autoincrement - #443

Open
unknwon wants to merge 1 commit into
mainfrom
autoincrement-tag
Open

unknwon wants to merge 1 commit into
mainfrom
autoincrement-tag

Conversation

@unknwon

@unknwon unknwon commented Nov 15, 2026

Copy link
Copy Markdown
Member

Describe the pull request

- = value lines are read as auto-increment keys named #1, #2…, but struct mapping had no way to reach them. The issue's ini:"-" already means "skip this field", so this adds a tag option instead:

type Agents struct {
	Installed []string `ini:",autoincrement"`
}
[Agents]
- = installRoot/agent_1.0.0
- = installRoot/agent_1.0.1
  • MapTo: the slice gets the values of the section's auto-increment keys, in order, expanded like any value (%(name)s works). Elements parse as in other slice fields: an invalid one is the zero value in MapTo and an error in StrictMapTo. A section without such keys leaves the field unchanged.
  • ReflectFrom: the section's auto-increment keys are replaced by one - = element line per element, after its other keys. Since each element is its own line, elements may contain the delimiter.
  • A field with the option that isn't a slice is an error in both directions.

parseTagOptions now splits on every comma, since a sixth option no longer fit in SplitN(tag, ",", 5).

Link to the issue: closes #240

Checklist

  • I agree to follow the Code of Conduct by submitting this pull request.
  • I have read and acknowledge the Contributing guide.
  • I have added test cases to cover the new code.

AI usage

Written by Amp (an AI coding agent) at the maintainer's request. Validation: go test ./..., go vet, and 30 s of FuzzStructMapping. TestAutoIncrement uses the issue's file with a regular key between the - lines, typed elements in strict and lenient mode, a ReflectFrom that replaces existing lines and round-trips, and a non-slice field. Every case fails when the option is ignored.

A slice field tagged ini:",autoincrement" gets the values of its
section's "- = value" lines from MapTo, and ReflectFrom replaces those
lines with one per element (#240).

This branch has not been deployed

No deployments
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.

MapTo() and ReflectFrom() does not handle incrementing key values

1 participant