Skip to content

Add StructTag option to name struct fields with another tag - #439

Open
unknwon wants to merge 2 commits into
mainfrom
struct-tag-option
Open

unknwon wants to merge 2 commits into
mainfrom
struct-tag-option

Conversation

@unknwon

@unknwon unknwon commented Nov 15, 2026

Copy link
Copy Markdown
Member

Describe the pull request

Adds a StructTag(name) load option so MapTo, StrictMapTo and ReflectFrom read field names from another struct tag, such as json, instead of ini. This lets one struct carry a single set of tags for several formats.

var c Config
err := ini.MapTo(&c, "app.ini", ini.StructTag("json"))

The chosen tag keeps the ini tag's syntax: - skips the field, and omitempty, allowshadow, nonunique and extends after the name apply. Other options, such as json's string, are ignored. The delim and comment tags are unchanged. It's a load option, like NameMapping, because struct mapping reads its settings from the File. The default stays ini, so nothing changes for existing code.

Link to the issue: closes #376

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 ./... and go vet. TestStructTag maps and reflects a nested struct whose json and ini tags differ, with - and omitempty. It fails when the option is ignored.

MapTo, StrictMapTo and ReflectFrom read the ini tag. StructTag("json")
makes them read json tags instead, with the same syntax (#376).
@unknwon unknwon mentioned this pull request Nov 15, 2026
3 tasks done

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.

Need custom struct tag name

1 participant