Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/migrate-v1-to-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type loadOptions struct {
Locking bool
NameMapper NameMapper
ValueMapper ValueMapper
StructTag string
}

func newLoadOptions(opts []LoadOption) loadOptions {
Expand All @@ -50,6 +51,7 @@ func newLoadOptions(opts []LoadOption) loadOptions {
ChildSectionDelimiter: ".",
DefaultSection: DefaultSection,
CommentSymbols: "#;",
StructTag: "ini",
}
for _, opt := range opts {
opt(&o)
Expand All @@ -62,6 +64,15 @@ 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 } }

// 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 }
}

// NameMapping sets the NameMapper that maps struct field names to section
// and key names, for MapTo, StrictMapTo and ReflectFrom.
func NameMapping(mapper NameMapper) LoadOption {
Expand Down
4 changes: 2 additions & 2 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
39 changes: 39 additions & 0 deletions struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down
Loading