From e42d6ac3d82027c8edb04a2d265d1d8769757c0e Mon Sep 17 00:00:00 2001 From: GreyXor Date: Wed, 14 Feb 2024 10:34:29 +0100 Subject: [PATCH] feat: remove v1 toml parser --- parsers/toml/go.mod | 6 +- parsers/toml/go.sum | 8 +- parsers/toml/toml.go | 17 +++-- parsers/toml/toml_test.go | 16 ++-- parsers/toml/v2/go.mod | 14 ---- parsers/toml/v2/go.sum | 19 ----- parsers/toml/v2/toml.go | 35 --------- parsers/toml/v2/toml_test.go | 139 ----------------------------------- 8 files changed, 24 insertions(+), 230 deletions(-) delete mode 100644 parsers/toml/v2/go.mod delete mode 100644 parsers/toml/v2/go.sum delete mode 100644 parsers/toml/v2/toml.go delete mode 100644 parsers/toml/v2/toml_test.go diff --git a/parsers/toml/go.mod b/parsers/toml/go.mod index 5f89c601..af7cd688 100644 --- a/parsers/toml/go.mod +++ b/parsers/toml/go.mod @@ -1,10 +1,10 @@ -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.1 + github.com/pelletier/go-toml/v2 v2.1.1 + github.com/stretchr/testify v1.8.4 ) require ( diff --git a/parsers/toml/go.sum b/parsers/toml/go.sum index 4a439c49..3683a053 100644 --- a/parsers/toml/go.sum +++ b/parsers/toml/go.sum @@ -1,8 +1,8 @@ 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 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= @@ -10,8 +10,8 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS 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.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= -github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +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/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/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 719b01d9..9f019456 100644 --- a/parsers/toml/toml_test.go +++ b/parsers/toml/toml_test.go @@ -16,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", @@ -95,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 `), }, @@ -110,15 +110,15 @@ number = 2.0 "object": map[string]interface{}{"a": "b", "c": "d"}, "string": "Hello World", }, - output: []byte(`array = [1,2,3,4,5] + 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' `), }, } diff --git a/parsers/toml/v2/go.mod b/parsers/toml/v2/go.mod deleted file mode 100644 index af7cd688..00000000 --- a/parsers/toml/v2/go.mod +++ /dev/null @@ -1,14 +0,0 @@ -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 -) - -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect -) diff --git a/parsers/toml/v2/go.sum b/parsers/toml/v2/go.sum deleted file mode 100644 index 3683a053..00000000 --- a/parsers/toml/v2/go.sum +++ /dev/null @@ -1,19 +0,0 @@ -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/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/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/v2/toml.go b/parsers/toml/v2/toml.go deleted file mode 100644 index c42cf9d2..00000000 --- a/parsers/toml/v2/toml.go +++ /dev/null @@ -1,35 +0,0 @@ -// Package toml implements a koanf.Parser that parses TOML bytes as conf maps. -package toml - -import ( - "github.com/pelletier/go-toml/v2" -) - -// TOML implements a TOML parser. -type TOML struct{} - -// Parser returns a TOML Parser. -func Parser() *TOML { - return &TOML{} -} - -// Unmarshal parses the given TOML bytes. -func (p *TOML) Unmarshal(b []byte) (map[string]interface{}, error) { - var outMap map[string]interface{} - - if err := toml.Unmarshal(b, &outMap); err != nil { - return nil, 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.Marshal(&o) - if err != nil { - return nil, err - } - - return out, nil -} diff --git a/parsers/toml/v2/toml_test.go b/parsers/toml/v2/toml_test.go deleted file mode 100644 index 9f019456..00000000 --- a/parsers/toml/v2/toml_test.go +++ /dev/null @@ -1,139 +0,0 @@ -package toml - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestTOML_Unmarshal(t *testing.T) { - testCases := []struct { - name string - input []byte - output map[string]interface{} - isErr bool - }{ - { - name: "Empty TOML", - input: []byte(``), - output: map[string]interface{}(nil), - }, - { - name: "Valid TOML", - input: []byte(`key = "val" - name = "test" - number = 2 - `), - output: map[string]interface{}{ - "key": "val", - "name": "test", - "number": int64(2), - }, - }, - { - name: "Invalid TOML - missing end quotes", - input: []byte(`key = "val`), - isErr: true, - }, - { - name: "Complex TOML - All types", - input: []byte(`array = [ 1, 2, 3 ] - boolean = true - color = "gold" - number = 123 - string = "Hello World" - [object] - a = "b" - c = "d"`), - output: map[string]interface{}{ - "array": []interface{}{int64(1), int64(2), int64(3)}, - "boolean": true, - "color": "gold", - "number": int64(123), - "object": map[string]interface{}{"a": "b", "c": "d"}, - "string": "Hello World", - }, - }, - { - name: "Invalid TOML - missing equal", - input: []byte(`key "val"`), - isErr: true, - }, - } - - tp := Parser() - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - out, err := tp.Unmarshal(tc.input) - if tc.isErr { - assert.NotNil(t, err) - } else { - assert.Nil(t, err) - assert.Equal(t, tc.output, out) - } - }) - } -} - -func TestTOML_Marshal(t *testing.T) { - testCases := []struct { - name string - input map[string]interface{} - output []byte - isErr bool - }{ - { - name: "Empty TOML", - input: map[string]interface{}{}, - output: []byte(nil), - }, - { - name: "Valid TOML", - input: map[string]interface{}{ - "key": "val", - "name": "test", - "number": 2.0, - }, - output: []byte(`key = 'val' -name = 'test' -number = 2.0 -`), - }, - { - name: "Complex TOML - All types", - input: map[string]interface{}{ - "array": []interface{}{1, 2, 3, 4, 5}, - "boolean": true, - "color": "gold", - "number": 123, - "object": map[string]interface{}{"a": "b", "c": "d"}, - "string": "Hello World", - }, - output: []byte(`array = [1, 2, 3, 4, 5] -boolean = true -color = 'gold' -number = 123 -string = 'Hello World' - -[object] -a = 'b' -c = 'd' -`), - }, - } - - tp := Parser() - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - out, err := tp.Marshal(tc.input) - if tc.isErr { - assert.NotNil(t, err) - } else { - assert.Nil(t, err) - assert.Equal(t, tc.output, out) - } - }) - } -}