Skip to content

Commit

Permalink
Add support for parser TOML v2 spec (#286)
Browse files Browse the repository at this point in the history
Closes #272 contributed by @GreyXor

Co-authored-by: GreyXor <greyxor@protonmail.com>
  • Loading branch information
knadh and GreyXor committed Apr 7, 2024
1 parent 0d18f1a commit 281ea77
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 40 deletions.
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -26,11 +26,10 @@ go get -u github.com/knadh/koanf/providers/file


# Install the necessary Parser(s).
# Available: toml, json, yaml, dotenv, hcl, hjson, nestedtext
# Available: toml, toml/v2, json, yaml, dotenv, hcl, hjson, nestedtext
# go get -u github.com/knadh/koanf/parsers/$parser

go get -u github.com/knadh/koanf/parsers/toml

```

[See the list](#api) of all bundled Providers and Parsers.
Expand Down Expand Up @@ -168,6 +167,10 @@ import (

"github.com/knadh/koanf/v2"
"github.com/knadh/koanf/parsers/toml"

// TOML version 2 is available at:
// "github.com/knadh/koanf/parsers/toml/v2"

"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/posflag"
flag "github.com/spf13/pflag"
Expand Down Expand Up @@ -677,6 +680,7 @@ Install with `go get -u github.com/knadh/koanf/parsers/$parser`
| json | `json.Parser()` | Parses JSON bytes into a nested map |
| yaml | `yaml.Parser()` | Parses YAML bytes into a nested map |
| toml | `toml.Parser()` | Parses TOML bytes into a nested map |
| toml/v2 | `toml.Parser()` | Parses TOML bytes into a nested map (using go-toml v2) |
| dotenv | `dotenv.Parser()` | Parses DotEnv bytes into a flat map |
| hcl | `hcl.Parser(flattenSlices bool)` | Parses Hashicorp HCL bytes into a nested map. `flattenSlices` is recommended to be set to true. [Read more](https://github.com/hashicorp/hcl/issues/162). |
| nestedtext | `nestedtext.Parser()` | Parses NestedText bytes into a flat map |
Expand Down
9 changes: 3 additions & 6 deletions parsers/toml/go.mod
@@ -1,17 +1,14 @@
module github.com/knadh/koanf/parsers/toml
module github.com/knadh/koanf/parsers/toml/v2

go 1.18

require (
github.com/pelletier/go-toml v1.9.5
github.com/stretchr/testify v1.8.4
github.com/pelletier/go-toml/v2 v2.2.0
github.com/stretchr/testify v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.2.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
20 changes: 14 additions & 6 deletions parsers/toml/go.sum
@@ -1,13 +1,21 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pelletier/go-toml/v2 v2.2.0 h1:QLgLl2yMN7N+ruc31VynXs1vhMZa7CeHHejIeBAsoHo=
github.com/pelletier/go-toml/v2 v2.2.0/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
17 changes: 9 additions & 8 deletions parsers/toml/toml.go
Expand Up @@ -2,9 +2,7 @@
package toml

import (
"bytes"

"github.com/pelletier/go-toml"
"github.com/pelletier/go-toml/v2"
)

// TOML implements a TOML parser.
Expand All @@ -17,18 +15,21 @@ func Parser() *TOML {

// Unmarshal parses the given TOML bytes.
func (p *TOML) Unmarshal(b []byte) (map[string]interface{}, error) {
r, err := toml.LoadReader(bytes.NewBuffer(b))
if err != nil {
var outMap map[string]interface{}

if err := toml.Unmarshal(b, &outMap); err != nil {
return nil, err
}
return r.ToMap(), err

return outMap, nil
}

// Marshal marshals the given config map to TOML bytes.
func (p *TOML) Marshal(o map[string]interface{}) ([]byte, error) {
out, err := toml.TreeFromMap(o)
out, err := toml.Marshal(&o)
if err != nil {
return nil, err
}
return out.Marshal()

return out, nil
}
26 changes: 8 additions & 18 deletions parsers/toml/toml_test.go
@@ -1,8 +1,6 @@
package toml

import (
"sort"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -18,7 +16,7 @@ func TestTOML_Unmarshal(t *testing.T) {
{
name: "Empty TOML",
input: []byte(``),
output: map[string]interface{}{},
output: map[string]interface{}(nil),
},
{
name: "Valid TOML",
Expand Down Expand Up @@ -97,8 +95,8 @@ func TestTOML_Marshal(t *testing.T) {
"name": "test",
"number": 2.0,
},
output: []byte(`key = "val"
name = "test"
output: []byte(`key = 'val'
name = 'test'
number = 2.0
`),
},
Expand All @@ -114,13 +112,13 @@ number = 2.0
},
output: []byte(`array = [1, 2, 3, 4, 5]
boolean = true
color = "gold"
color = 'gold'
number = 123
string = "Hello World"
string = 'Hello World'
[object]
a = "b"
c = "d"
a = 'b'
c = 'd'
`),
},
}
Expand All @@ -134,16 +132,8 @@ string = "Hello World"
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
assert.Equal(t, sortLines(tc.output), sortLines(out))
assert.Equal(t, tc.output, out)
}
})
}
}

// toml marshal is not guaranteed to produce the same output every time
// so we sort the lines before comparing.
func sortLines(s []byte) string {
lines := strings.Split(string(s), "\n")
sort.Strings(lines)
return strings.Join(lines, "\n")
}

0 comments on commit 281ea77

Please sign in to comment.