From 7d798d2cac08af0bf37e98e045da07d13718ca70 Mon Sep 17 00:00:00 2001 From: Joe Chen Date: Sun, 15 Nov 2026 01:31:56 +0000 Subject: [PATCH 1/2] Add StructTag option to name struct fields with another tag MapTo, StrictMapTo and ReflectFrom read the ini tag. StructTag("json") makes them read json tags instead, with the same syntax (#376). --- doc.go | 3 ++- docs/migrate-v1-to-v2.md | 2 +- options.go | 11 +++++++++++ struct.go | 4 ++-- struct_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 55 insertions(+), 4 deletions(-) diff --git a/doc.go b/doc.go index 51eba1f..89f0148 100644 --- a/doc.go +++ b/doc.go @@ -36,7 +36,8 @@ // [MapTo] and [StrictMapTo] fill a struct from a data source or a [File], // and [File.ReflectFrom] fills a File from a struct. Field names map to key // and section names unchanged unless [NameMapping] sets a mapper, and the -// ini, delim and comment struct tags adjust each field. A type that +// ini, delim and comment struct tags adjust each field. [StructTag] reads +// another tag, such as json, in place of ini. A type that // implements [Marshaler] or [Unmarshaler] formats or parses its own values. // // # Writing diff --git a/docs/migrate-v1-to-v2.md b/docs/migrate-v1-to-v2.md index f3a58a8..035c32e 100644 --- a/docs/migrate-v1-to-v2.md +++ b/docs/migrate-v1-to-v2.md @@ -121,7 +121,7 @@ Each `LoadOptions` field becomes an option function of the same name, such as `i | `KeyValueDelimiterOnWrite` | `WriteDelimiter(d)`, a write option | | `ReaderBufferSize` | removed, since nothing is cut at a buffer size | -New options: `CommentSymbols`, `PreserveEmptyShadowValues` and `DefaultSectionName`. +New options: `CommentSymbols`, `PreserveEmptyShadowValues`, `DefaultSectionName` and `StructTag`. ### Writing diff --git a/options.go b/options.go index 5744e25..903d3bc 100644 --- a/options.go +++ b/options.go @@ -41,6 +41,7 @@ type loadOptions struct { Locking bool NameMapper NameMapper ValueMapper ValueMapper + StructTag string } func newLoadOptions(opts []LoadOption) loadOptions { @@ -50,6 +51,7 @@ func newLoadOptions(opts []LoadOption) loadOptions { ChildSectionDelimiter: ".", DefaultSection: DefaultSection, CommentSymbols: "#;", + StructTag: "ini", } for _, opt := range opts { opt(&o) @@ -68,6 +70,15 @@ func NameMapping(mapper NameMapper) LoadOption { return func(o *loadOptions) { o.NameMapper = mapper } } +// StructTag sets the struct tag that names fields for MapTo, StrictMapTo and +// ReflectFrom, such as "json". The default is "ini". The tag keeps the ini +// tag's syntax: "-" skips the field, and of the options after the name, +// omitempty, allowshadow, nonunique and extends apply and others are +// ignored. The delim and comment tags are read as before. +func StructTag(name string) LoadOption { + return func(o *loadOptions) { o.StructTag = name } +} + // ValueMapping sets the ValueMapper applied to raw values, such as // os.ExpandEnv, by Key.String and the accessors built on it. func ValueMapping(mapper ValueMapper) LoadOption { diff --git a/struct.go b/struct.go index 25db8ff..b9a9e48 100644 --- a/struct.go +++ b/struct.go @@ -406,7 +406,7 @@ func (s *Section) decodeStruct(val reflect.Value, isStrict bool, sectionIndex in field := val.Field(i) tpField := typ.Field(i) - tag := tpField.Tag.Get("ini") + tag := tpField.Tag.Get(s.f.options.StructTag) if tag == "-" { continue } @@ -735,7 +735,7 @@ func (s *Section) encodeStruct(val reflect.Value, path []reflect.Type) error { field := val.Field(i) tpField := typ.Field(i) - tag := tpField.Tag.Get("ini") + tag := tpField.Tag.Get(s.f.options.StructTag) if tag == "-" { continue } diff --git a/struct_test.go b/struct_test.go index 59625b4..4e2b5b8 100644 --- a/struct_test.go +++ b/struct_test.go @@ -822,6 +822,45 @@ func Test_NameGetter(t *testing.T) { }) } +func TestStructTag(t *testing.T) { + type server struct { + Host string `json:"host" ini:"ini_host"` + Port int `json:"port,omitempty"` + Debug bool `json:"-"` + } + type config struct { + Name string `json:"app_name"` + Server server `json:"server"` + } + + t.Run("MapTo", func(t *testing.T) { + var c config + err := MapTo(&c, []byte(` +app_name = demo +[server] +host = example.com +ini_host = wrong +port = 8080 +Debug = true +`), StructTag("json")) + require.NoError(t, err) + assert.Equal(t, config{Name: "demo", Server: server{Host: "example.com", Port: 8080}}, c) + }) + + t.Run("ReflectFrom", func(t *testing.T) { + f := Empty(StructTag("json")) + require.NoError(t, f.ReflectFrom(&config{Name: "demo", Server: server{Host: "example.com", Debug: true}})) + var buf bytes.Buffer + _, err := f.WriteTo(&buf) + require.NoError(t, err) + assert.Equal(t, `app_name = demo + +[server] +host = example.com +`, buf.String()) + }) +} + type testDurationStruct struct { Duration time.Duration `ini:"Duration"` } From f3be74770d64cf2130d09fba5f5db50503a928c6 Mon Sep 17 00:00:00 2001 From: Joe Chen Date: Sun, 15 Nov 2026 01:49:06 +0000 Subject: [PATCH 2/2] Move StructTag above NameMapping --- options.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/options.go b/options.go index 903d3bc..8237cc3 100644 --- a/options.go +++ b/options.go @@ -64,12 +64,6 @@ func newLoadOptions(opts []LoadOption) loadOptions { // File only one goroutine uses, to save the cost of locking. func Locking(v bool) LoadOption { return func(o *loadOptions) { o.Locking = v } } -// NameMapping sets the NameMapper that maps struct field names to section -// and key names, for MapTo, StrictMapTo and ReflectFrom. -func NameMapping(mapper NameMapper) LoadOption { - return func(o *loadOptions) { o.NameMapper = mapper } -} - // StructTag sets the struct tag that names fields for MapTo, StrictMapTo and // ReflectFrom, such as "json". The default is "ini". The tag keeps the ini // tag's syntax: "-" skips the field, and of the options after the name, @@ -79,6 +73,12 @@ func StructTag(name string) LoadOption { return func(o *loadOptions) { o.StructTag = name } } +// NameMapping sets the NameMapper that maps struct field names to section +// and key names, for MapTo, StrictMapTo and ReflectFrom. +func NameMapping(mapper NameMapper) LoadOption { + return func(o *loadOptions) { o.NameMapper = mapper } +} + // ValueMapping sets the ValueMapper applied to raw values, such as // os.ExpandEnv, by Key.String and the accessors built on it. func ValueMapping(mapper ValueMapper) LoadOption {