Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop YAML v2 and TOML v1 #1493

Merged
merged 3 commits into from Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions .github/workflows/ci.yaml
Expand Up @@ -44,7 +44,6 @@ jobs:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
go: ['1.16', '1.17', '1.18', '1.19']
tags: ['', 'viper_yaml2', 'viper_toml1']
env:
GOFLAGS: -mod=readonly

Expand All @@ -58,11 +57,11 @@ jobs:
uses: actions/checkout@v3

- name: Test
run: go test -race -tags '${{ matrix.tags }}' -v ./...
run: go test -race -v ./...
if: runner.os != 'Windows'

- name: Test (without race detector)
run: go test -tags '${{ matrix.tags }}' -v ./...
run: go test -v ./...
if: runner.os == 'Windows'

lint:
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Expand Up @@ -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
Expand All @@ -17,7 +16,6 @@ require (
github.com/stretchr/testify v1.8.1
github.com/subosito/gotenv v1.4.1
gopkg.in/ini.v1 v1.67.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
)

Expand Down
2 changes: 0 additions & 2 deletions go.sum
Expand Up @@ -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=
Expand Down
29 changes: 3 additions & 26 deletions 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)
}
19 changes: 0 additions & 19 deletions internal/encoding/toml/codec2.go

This file was deleted.

108 changes: 0 additions & 108 deletions internal/encoding/toml/codec2_test.go

This file was deleted.

16 changes: 6 additions & 10 deletions internal/encoding/toml/codec_test.go
@@ -1,6 +1,3 @@
//go:build viper_toml1
// +build viper_toml1

package toml

import (
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/encoding/yaml/codec.go
@@ -1,6 +1,6 @@
package yaml

// import "gopkg.in/yaml.v2"
import "gopkg.in/yaml.v3"

// Codec implements the encoding.Encoder and encoding.Decoder interfaces for YAML encoding.
type Codec struct{}
Expand Down
87 changes: 87 additions & 0 deletions internal/encoding/yaml/codec_test.go
Expand Up @@ -5,6 +5,93 @@ import (
"testing"
)

// original form of the data
const original = `# key-value pair
key: value
list:
- item1
- item2
- item3
map:
key: value

# 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:
map:
key: value
list:
- item1
- item2
- item3
`

// decoded form of the data
//
// in case of YAML it's slightly different from Viper's internal representation
// (eg. map is decoded into a map with interface key)
var decoded = 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",
},
},
},
}

// 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{}

Expand Down
14 changes: 0 additions & 14 deletions internal/encoding/yaml/yaml2.go

This file was deleted.