From d3857262ea4e973a96cd3ed174b5cd6c089c5552 Mon Sep 17 00:00:00 2001 From: GreyXor Date: Fri, 5 Apr 2024 19:21:19 +0200 Subject: [PATCH 1/3] feat: new parser toml v2 --- README.md | 1 + parsers/toml/go.mod | 7 ++----- parsers/toml/go.sum | 16 +++++++++++----- parsers/toml/toml.go | 17 +++++++++-------- parsers/toml/toml_test.go | 26 ++++++++------------------ 5 files changed, 31 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index cfdb5ee9..4a9ebe72 100644 --- a/README.md +++ b/README.md @@ -677,6 +677,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 | diff --git a/parsers/toml/go.mod b/parsers/toml/go.mod index fa467bcb..af7cd688 100644 --- a/parsers/toml/go.mod +++ b/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/pelletier/go-toml/v2 v2.1.1 github.com/stretchr/testify v1.8.4 ) 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 ) diff --git a/parsers/toml/go.sum b/parsers/toml/go.sum index c10fd444..3683a053 100644 --- a/parsers/toml/go.sum +++ b/parsers/toml/go.sum @@ -1,13 +1,19 @@ +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.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI= +github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= 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/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/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 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +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= diff --git a/parsers/toml/toml.go b/parsers/toml/toml.go index 2b6ac34c..c42cf9d2 100644 --- a/parsers/toml/toml.go +++ b/parsers/toml/toml.go @@ -2,9 +2,7 @@ package toml import ( - "bytes" - - "github.com/pelletier/go-toml" + "github.com/pelletier/go-toml/v2" ) // TOML implements a TOML parser. @@ -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 } diff --git a/parsers/toml/toml_test.go b/parsers/toml/toml_test.go index 72132280..9f019456 100644 --- a/parsers/toml/toml_test.go +++ b/parsers/toml/toml_test.go @@ -1,8 +1,6 @@ package toml import ( - "sort" - "strings" "testing" "github.com/stretchr/testify/assert" @@ -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", @@ -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 `), }, @@ -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' `), }, } @@ -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") -} From 45f9ae9fa4121d795f7f83bde43e1dfc092cf987 Mon Sep 17 00:00:00 2001 From: GreyXor Date: Fri, 5 Apr 2024 19:23:03 +0200 Subject: [PATCH 2/3] chore: upgrade package dependencies --- parsers/toml/go.mod | 4 ++-- parsers/toml/go.sum | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/parsers/toml/go.mod b/parsers/toml/go.mod index af7cd688..7256e896 100644 --- a/parsers/toml/go.mod +++ b/parsers/toml/go.mod @@ -3,8 +3,8 @@ module github.com/knadh/koanf/parsers/toml/v2 go 1.18 require ( - github.com/pelletier/go-toml/v2 v2.1.1 - github.com/stretchr/testify v1.8.4 + github.com/pelletier/go-toml/v2 v2.2.0 + github.com/stretchr/testify v1.9.0 ) require ( diff --git a/parsers/toml/go.sum b/parsers/toml/go.sum index 3683a053..0e0348e1 100644 --- a/parsers/toml/go.sum +++ b/parsers/toml/go.sum @@ -1,17 +1,19 @@ 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/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI= -github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +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/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 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= 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/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From bcc4b9fa1946797a0803042dbb05666bcad44c22 Mon Sep 17 00:00:00 2001 From: GreyXor Date: Fri, 5 Apr 2024 19:32:17 +0200 Subject: [PATCH 3/3] doc: add tomlv2 in readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4a9ebe72..24df8a90 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ go get -u github.com/knadh/koanf/providers/file # Available: toml, json, yaml, dotenv, hcl, hjson, nestedtext # go get -u github.com/knadh/koanf/parsers/$parser -go get -u github.com/knadh/koanf/parsers/toml +go get -u github.com/knadh/koanf/parsers/toml/v2 ``` @@ -167,7 +167,7 @@ import ( "os" "github.com/knadh/koanf/v2" - "github.com/knadh/koanf/parsers/toml" + "github.com/knadh/koanf/parsers/toml/v2" "github.com/knadh/koanf/providers/file" "github.com/knadh/koanf/providers/posflag" flag "github.com/spf13/pflag"