Commit Graph

35 Commits

Author SHA1 Message Date
Thomas Pelletier 3a4d7af89e Local DateTime support (#317) 2019-10-25 14:07:46 -04:00
Thomas Pelletier 8a362ad712 Short-date support (#298) 2019-10-25 13:21:44 -04:00
Marcin Białoń e87c92d4f4 Marshal arrays (#310)
Fixes #285
2019-10-09 12:33:56 -04:00
Marcin Białoń 75654e60b8 Tree.Marshal returns the TOML encoding of Tree (#306)
The Tree.Marshal tried to marshal the Tree struct itself rather than the nodes being part of the tree.

Fixes #295
2019-09-26 13:39:15 -07:00
Roberth Kulbin 84da2c4a25 Merge struct fields in Unmarshal (#284)
* 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
2019-07-25 00:06:17 -07:00
Kamil Samigullin dba45d427f Handle anonymous structs (#281)
Handle anonymous structs during Unmarshal.

Fixes #279
2019-05-29 20:55:49 -07:00
Gregory Oschwald 728039f679 Handle other key types in Unmarshal (#276)
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.
2019-04-29 20:50:10 -07:00
Gregory Oschwald 1d8903f1d0 Allow unmarshaling to top level maps (#273) 2019-04-24 23:15:40 -07:00
Brent DeSpain 65b27e6823 Order map keys alphabetically (#270)
This makes sure we have a stable output when marshaling
maps.

Fixes #268
2019-04-11 13:52:29 +01:00
Ceriath 51edd0ca49 Fix goreportcard issues (#271)
* 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
2019-04-11 12:11:29 +01:00
Brent DeSpain 63909f0a90 Option to keep fields ordered when marshal struct (#266)
Adds a new `Order()` option to preserve order of struct fields when
marshaling.
2019-04-02 09:47:51 -07:00
Thomas Pelletier d9a27b8052 Provide "default" tag for unmarshal (#259)
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.
2019-03-01 17:18:23 -08:00
Andriy Senyshyn 19cbd226da Allow to marshal pointer to struct and map (#247) 2018-11-19 10:31:15 -05:00
Tom Wambold 0a1666a81f Map camelCased keys to fields in structs (#251)
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.
2018-11-19 10:29:38 -05:00
Andriy Senyshyn aa79e12a97 Support time.duration (#248) 2018-11-12 09:02:56 -08:00
Veselkov Konstantin 81a861c69d Fix typeSwitchVar warnings (#243)
Fixes #242
2018-09-30 13:58:32 -07:00
Andriy Senyshyn 90d6f96e9e Allow to change default tags for Decoder and Encoder (#241)
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
2018-09-21 09:41:11 -07:00
Jayi e33f654429 fix panic when type unmatch between toml and struct (#236) 2018-09-17 21:16:20 -07:00
Alan Murtagh c01d1270ff Multiline Marshal tag (#221)
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.
2018-06-05 13:47:19 -07:00
Chris 05bcc0fb0d Make multi-line arrays always use trailing commas (#217)
This makes ArraysWithOneElementPerLine output arrays with commas after every element.

```
A = [1,2,3]
```

Now becomes:

```
A = [
  1,
  2,
  3,
]
```
2018-02-28 15:36:31 -08:00
Thomas Pelletier acdc450948 Fix backward incompatibility for Set* methods (#213)
Patch #185 introduced a backward incompatibility by changing the arguments
of the `Set*` methods on `Tree`.

This change restores the arguments to what they previous were, and
introduces `SetWithComment` and `SetPathWithComment` to perform the same
action.
2018-01-18 14:54:55 -08:00
Jelte Fennema a1e8a8d702 Unmarshal into custom types and error on overflows (#209)
This fixes two unmarshalling issues:

1. Unmarshalling into a custom integer/float type (e.g. `time.Duration`).
2. Checks for overflows happen before unmarshalling, erroring if an overflow
would happen.

Apart from this it also reduces code duplication a bit.
2018-01-18 14:08:34 -08:00
Thomas Pelletier 0131db6d73 Lint (#206) 2017-12-22 12:45:48 +01:00
Thomas Pelletier b8b5e76965 Add Encoder opt to emit arrays on multiple lines (#203)
A new Encoder option emits arrays with more than one line on multiple lines.
This is off by default and toggled with `ArraysWithOneElementPerLine`.

For example:

```
A = [1,2,3]
```

Becomes:

```
A = [
  1,
  2,
  3
]
```

Fixes #200
2017-12-18 14:57:16 +01:00
Robert Günzler 4e9e0ee19b Add Encoder/Decoder types (#192)
Usage is similar to the stdlibs JSON encoder/decoder but I tried to
leave the general structure of the code the same.

Main motivation was to support encoding/decoding options to allow
encoding string-type map keys as quoted TOML keys.
This was implemented on the Encoder with QuoteMapKeys(bool).

> The TOML spec supports using UTF-8 strings as keys.
> https://github.com/toml-lang/toml/blob/master/versions/en/toml-v0.4.0.md#table
2017-10-24 14:10:38 -07:00
Kazuyoshi Kato 878c11e70e Unmarshal should report a type mismatch as an error (#196)
Fixes #186
2017-10-21 15:29:03 -07:00
Kazuyoshi Kato 19ece5dc77 Fix typos (#195)
Most of them are caught by Go Report Card.
https://goreportcard.com/report/github.com/pelletier/go-toml
2017-10-21 15:26:06 -07:00
Thomas Pelletier 2009e44b6f Improve doc for un/marshal functions (#189) 2017-10-01 15:47:47 -07:00
Yvonnick Esnault 690dbc9ee7 Comment annotation for Marshal (#185) 2017-10-01 15:05:24 -07:00
Thomas Pelletier 4a000a21a4 Benchmark against other libraries (#175)
BenchmarkUnmarshalToml-8             1000  1320117 ns/op  450932 B/op  15072 allocs/op
BenchmarkUnmarshalBurntSushiToml-8   3000   402897 ns/op   82900 B/op   1761 allocs/op
BenchmarkUnmarshalJson-8            20000    66092 ns/op    3536 B/op    101 allocs/op
BenchmarkUnmarshalYaml-8            10000   189600 ns/op   44872 B/op   1058 allocs/op
2017-06-25 13:05:13 -07:00
Thomas Pelletier 40ecdac242 Clean up documentation (#168)
Fixes #135
2017-05-30 18:33:25 -07:00
Thomas Pelletier 685a1f1cb7 Rename TomlTree to Tree (#159)
Avoid stutter.

Fixes #55
2017-05-10 17:53:23 -07:00
John K. Luebs 53be957dac Allow unmarshal from any TomlTree (#157)
Fixes #153
2017-05-07 15:55:38 -07:00
Carolyn Van Slyck fe206efb84 Provide Marshaler interface (#151)
The toml.Marhshaler interface allows marshalling custom objects implementing
the interface. Design based off json.Marshaler.
2017-04-04 18:41:05 -07:00
tro3 e32a2e0474 Reflection-based marshaling / unmarshaling (#149)
Fixes #146
2017-03-29 14:49:41 -07:00