* unmarshal: support encoding.TextUnmarshaler
This PR adds support for decoding fields of primitive types that implement
encoding.TextUnmarshaler by calling the custom method.
Fields in anonymous structs are not supported at this point.
Co-authored-by: Lorenz Bauer <lmb@users.noreply.github.com>
With this PR the encoder now supports encoding.TextMarshaler.
Additionally, a bug is fixed, where the encoder does not notice a pointer
field that implements the toml.Marshaler interface.
fixes#373
This PR adds a strict mode to the Decoder. It can be enabled with the
`Strict` method.
In the strict mode, the decoder fails if any fields that were part
of the input do not have a corresponding field in the struct.
Fixes#277
Currently, the marshalling code encodes the embedded structs as sub-tables.
This is a bit unexpected, as it differs from what encoding/json does in
that case: https://play.golang.org/p/KDPaGtrijV1
Unmarshalling code handles this scenario gracefully.
This PR adapts the encoder to behave like encoding/json.
Fields in an embedded struct are promoted to the top level table.
In case the embedded struct is named in the tag, it will still
encode as a sub-table.
The added PromoteAnonymous option on the Encoder allows configuring
the old behavior, where anonymous structs are encoded as sub-tables.
On duplicate keys, the behavior of encoding/json is mimicked:
Fields from anonymous structs are shadowed by regular fields.
An example is added to show the affects of setting PromoteAnonymous.
* add test for unexported field preservation
* merge struct values instead of replacing them
* use struct merging on nested value structs
* unmarshalling merges nested struct pointers when non-nil
Previously, this would fail with:
```
panic: reflect.Value.SetMapIndex: value of type string is not assignable to type toml.letter [recovered]
panic: reflect.Value.SetMapIndex: value of type string is not assignable to type toml.letter
```
Now this only panics when the key type cannot be converted from a
string.
* Fixed misspell
* Fixed ineffassign
`user` and `password` always got overwritten
`orderedVals` was initialized with an empty array but always got overwritten by either `sortByLines()` or `sortAlphabetical`
`err` was assigned a `nil` value that was either overwritten or unused anyways
* Fix comment for DeletePath
The comment assumed the method was named Delete, i guess a rename happened at some point
* Update doc_test.go
When a struct is unmarshalled, go-toml now looks at the `default` tag to
provide a default value in case the key is not present in the TOML
document. This is only implemented for string, bool, int, int64,
float64. Additional types can be further implemented on a request-basis.
The name for each field in a struct is used to look up a key in the TOML
tree. A few different (case-sensitive) forms of this name are tried.
Previously, the current, lower-cased, and title-cased versions of the
name are tried. This precludes camelCased keys from mapping back to
fields in structs. This change adds camelCase to the set of keys to
try.
For example, the following TOML:
fooBar = 10
Would previously *not* map to the following struct:
type Foo struct {
FooBar int
}
This change corrects this.
Decoder: allow to customize default field name tag "toml" on decoding.
Example:
```
type doc struct {
title `file:"header"`
}
```
Encoder: allow to customize tags for encoding struct to toml.
Example:
```
type doc struct {
title `file:"header" description:"document title"`
}
```
Fixes#238
The new multiline tag works just like the existing 'commented' tag (i.e.
`multiline:"true"`), and tells go-toml to marshal the value as a
multi-line string. The tag currently has no impact on any non-string
fields.