From 77fc1c3084ea7064db16816828cca7af47c04410 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Thu, 19 Jan 2023 15:15:44 +0100 Subject: [PATCH] feat!: drop toml v1 support Signed-off-by: Mark Sagi-Kazar --- .github/workflows/ci.yaml | 2 +- go.mod | 1 - go.sum | 2 - internal/encoding/toml/codec.go | 29 +------ internal/encoding/toml/codec2.go | 19 ----- internal/encoding/toml/codec2_test.go | 108 -------------------------- internal/encoding/toml/codec_test.go | 16 ++-- 7 files changed, 10 insertions(+), 167 deletions(-) delete mode 100644 internal/encoding/toml/codec2.go delete mode 100644 internal/encoding/toml/codec2_test.go diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 4fc1a10..abcaed7 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -44,7 +44,7 @@ jobs: matrix: os: [ubuntu-latest, macos-latest, windows-latest] go: ['1.16', '1.17', '1.18', '1.19'] - tags: ['', 'viper_toml1'] + tags: [''] env: GOFLAGS: -mod=readonly diff --git a/go.mod b/go.mod index 5ad60b7..48c7ca6 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,6 @@ require ( github.com/hashicorp/hcl v1.0.0 github.com/magiconair/properties v1.8.7 github.com/mitchellh/mapstructure v1.5.0 - github.com/pelletier/go-toml v1.9.5 github.com/pelletier/go-toml/v2 v2.0.6 github.com/sagikazarmark/crypt v0.8.0 github.com/spf13/afero v1.9.3 diff --git a/go.sum b/go.sum index c9fa72f..f35df56 100644 --- a/go.sum +++ b/go.sum @@ -480,8 +480,6 @@ github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRW github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY= github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= -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.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU= github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= diff --git a/internal/encoding/toml/codec.go b/internal/encoding/toml/codec.go index 45fddc8..a993c59 100644 --- a/internal/encoding/toml/codec.go +++ b/internal/encoding/toml/codec.go @@ -1,39 +1,16 @@ -//go:build viper_toml1 -// +build viper_toml1 - package toml import ( - "github.com/pelletier/go-toml" + "github.com/pelletier/go-toml/v2" ) // Codec implements the encoding.Encoder and encoding.Decoder interfaces for TOML encoding. type Codec struct{} func (Codec) Encode(v map[string]interface{}) ([]byte, error) { - t, err := toml.TreeFromMap(v) - if err != nil { - return nil, err - } - - s, err := t.ToTomlString() - if err != nil { - return nil, err - } - - return []byte(s), nil + return toml.Marshal(v) } func (Codec) Decode(b []byte, v map[string]interface{}) error { - tree, err := toml.LoadBytes(b) - if err != nil { - return err - } - - tmap := tree.ToMap() - for key, value := range tmap { - v[key] = value - } - - return nil + return toml.Unmarshal(b, &v) } diff --git a/internal/encoding/toml/codec2.go b/internal/encoding/toml/codec2.go deleted file mode 100644 index 112c6d3..0000000 --- a/internal/encoding/toml/codec2.go +++ /dev/null @@ -1,19 +0,0 @@ -//go:build !viper_toml1 -// +build !viper_toml1 - -package toml - -import ( - "github.com/pelletier/go-toml/v2" -) - -// Codec implements the encoding.Encoder and encoding.Decoder interfaces for TOML encoding. -type Codec struct{} - -func (Codec) Encode(v map[string]interface{}) ([]byte, error) { - return toml.Marshal(v) -} - -func (Codec) Decode(b []byte, v map[string]interface{}) error { - return toml.Unmarshal(b, &v) -} diff --git a/internal/encoding/toml/codec2_test.go b/internal/encoding/toml/codec2_test.go deleted file mode 100644 index 5cf3f34..0000000 --- a/internal/encoding/toml/codec2_test.go +++ /dev/null @@ -1,108 +0,0 @@ -//go:build !viper_toml1 -// +build !viper_toml1 - -package toml - -import ( - "reflect" - "testing" -) - -// original form of the data -const original = `# key-value pair -key = "value" -list = ["item1", "item2", "item3"] - -[map] -key = "value" - -# nested -# map -[nested_map] -[nested_map.map] -key = "value" -list = [ - "item1", - "item2", - "item3", -] -` - -// encoded form of the data -const encoded = `key = 'value' -list = ['item1', 'item2', 'item3'] - -[map] -key = 'value' - -[nested_map] -[nested_map.map] -key = 'value' -list = ['item1', 'item2', 'item3'] -` - -// Viper's internal representation -var data = map[string]interface{}{ - "key": "value", - "list": []interface{}{ - "item1", - "item2", - "item3", - }, - "map": map[string]interface{}{ - "key": "value", - }, - "nested_map": map[string]interface{}{ - "map": map[string]interface{}{ - "key": "value", - "list": []interface{}{ - "item1", - "item2", - "item3", - }, - }, - }, -} - -func TestCodec_Encode(t *testing.T) { - codec := Codec{} - - b, err := codec.Encode(data) - if err != nil { - t.Fatal(err) - } - - if encoded != string(b) { - t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", string(b), encoded) - } -} - -func TestCodec_Decode(t *testing.T) { - t.Run("OK", func(t *testing.T) { - codec := Codec{} - - v := map[string]interface{}{} - - err := codec.Decode([]byte(original), v) - if err != nil { - t.Fatal(err) - } - - if !reflect.DeepEqual(data, v) { - t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", v, data) - } - }) - - t.Run("InvalidData", func(t *testing.T) { - codec := Codec{} - - v := map[string]interface{}{} - - err := codec.Decode([]byte(`invalid data`), v) - if err == nil { - t.Fatal("expected decoding to fail") - } - - t.Logf("decoding failed as expected: %s", err) - }) -} diff --git a/internal/encoding/toml/codec_test.go b/internal/encoding/toml/codec_test.go index 4f265be..acbe905 100644 --- a/internal/encoding/toml/codec_test.go +++ b/internal/encoding/toml/codec_test.go @@ -1,6 +1,3 @@ -//go:build viper_toml1 -// +build viper_toml1 - package toml import ( @@ -29,17 +26,16 @@ list = [ ` // encoded form of the data -const encoded = `key = "value" -list = ["item1", "item2", "item3"] +const encoded = `key = 'value' +list = ['item1', 'item2', 'item3'] [map] - key = "value" +key = 'value' [nested_map] - - [nested_map.map] - key = "value" - list = ["item1", "item2", "item3"] +[nested_map.map] +key = 'value' +list = ['item1', 'item2', 'item3'] ` // Viper's internal representation