-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: marshal toml with yaml.MapSlice and 3rdparty toml lib (#324)
Signed-off-by: peefy <[email protected]>
- Loading branch information
Showing
20 changed files
with
4,914 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2013 TOML authors | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
This is a fork of toml lib, which adds the yaml.MapSlice data structure support. | ||
|
||
TOML stands for Tom's Obvious, Minimal Language. This Go package provides a | ||
reflection interface similar to Go's standard library `json` and `xml` packages. | ||
|
||
Compatible with TOML version [v1.0.0](https://toml.io/en/v1.0.0). | ||
|
||
### Examples | ||
For the simplest example, consider some TOML file as just a list of keys and | ||
values: | ||
|
||
```toml | ||
Age = 25 | ||
Cats = [ "Cauchy", "Plato" ] | ||
Pi = 3.14 | ||
Perfection = [ 6, 28, 496, 8128 ] | ||
DOB = 1987-07-05T05:45:00Z | ||
``` | ||
|
||
Which can be decoded with: | ||
|
||
```go | ||
type Config struct { | ||
Age int | ||
Cats []string | ||
Pi float64 | ||
Perfection []int | ||
DOB time.Time | ||
} | ||
|
||
var conf Config | ||
_, err := toml.Decode(tomlData, &conf) | ||
``` | ||
|
||
You can also use struct tags if your struct field name doesn't map to a TOML key | ||
value directly: | ||
|
||
```toml | ||
some_key_NAME = "wat" | ||
``` | ||
|
||
```go | ||
type TOML struct { | ||
ObscureKey string `toml:"some_key_NAME"` | ||
} | ||
``` | ||
|
||
Beware that like other decoders **only exported fields** are considered when | ||
encoding and decoding; private fields are silently ignored. | ||
|
||
### Using the `Marshaler` and `encoding.TextUnmarshaler` interfaces | ||
Here's an example that automatically parses values in a `mail.Address`: | ||
|
||
```toml | ||
contacts = [ | ||
"Donald Duck <[email protected]>", | ||
"Scrooge McDuck <[email protected]>", | ||
] | ||
``` | ||
|
||
Can be decoded with: | ||
|
||
```go | ||
// Create address type which satisfies the encoding.TextUnmarshaler interface. | ||
type address struct { | ||
*mail.Address | ||
} | ||
|
||
func (a *address) UnmarshalText(text []byte) error { | ||
var err error | ||
a.Address, err = mail.ParseAddress(string(text)) | ||
return err | ||
} | ||
|
||
// Decode it. | ||
func decode() { | ||
blob := ` | ||
contacts = [ | ||
"Donald Duck <[email protected]>", | ||
"Scrooge McDuck <[email protected]>", | ||
] | ||
` | ||
|
||
var contacts struct { | ||
Contacts []address | ||
} | ||
|
||
_, err := toml.Decode(blob, &contacts) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
for _, c := range contacts.Contacts { | ||
fmt.Printf("%#v\n", c.Address) | ||
} | ||
|
||
// Output: | ||
// &mail.Address{Name:"Donald Duck", Address:"[email protected]"} | ||
// &mail.Address{Name:"Scrooge McDuck", Address:"[email protected]"} | ||
} | ||
``` | ||
|
||
To target TOML specifically you can implement `UnmarshalTOML` TOML interface in | ||
a similar way. | ||
|
||
### More complex usage | ||
See the [`_example/`](/_example) directory for a more complex example. |
Oops, something went wrong.