mirror of
https://github.com/spf13/viper
synced 2024-12-22 11:37:02 +00:00
feat!: drop toml v1 support
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
parent
e42b933dbe
commit
77fc1c3084
7 changed files with 10 additions and 167 deletions
2
.github/workflows/ci.yaml
vendored
2
.github/workflows/ci.yaml
vendored
|
@ -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
|
||||
|
||||
|
|
1
go.mod
1
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
|
||||
|
|
2
go.sum
2
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=
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -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)
|
||||
})
|
||||
}
|
|
@ -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"]
|
||||
key = 'value'
|
||||
list = ['item1', 'item2', 'item3']
|
||||
`
|
||||
|
||||
// Viper's internal representation
|
||||
|
|
Loading…
Reference in a new issue