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

Experimental yaml v3 library support #1273

Merged
merged 2 commits into from Jan 1, 2022
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: 3 additions & 2 deletions .github/workflows/ci.yml
Expand Up @@ -17,6 +17,7 @@ jobs:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
go: ['1.14', '1.15', '1.16', '1.17']
tags: ['', 'viper_yaml3']
env:
GOFLAGS: -mod=readonly

Expand All @@ -30,11 +31,11 @@ jobs:
uses: actions/checkout@v2

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

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

lint:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -17,6 +17,7 @@ require (
github.com/subosito/gotenv v1.2.0
gopkg.in/ini.v1 v1.66.2
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)

require (
Expand Down Expand Up @@ -65,5 +66,4 @@ require (
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
google.golang.org/grpc v1.43.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
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.v2"

// Codec implements the encoding.Encoder and encoding.Decoder interfaces for YAML encoding.
type Codec struct{}
Expand Down
87 changes: 0 additions & 87 deletions internal/encoding/yaml/codec_test.go
Expand Up @@ -5,93 +5,6 @@ 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[interface{}]interface{}{
"key": "value",
},
"nested_map": map[interface{}]interface{}{
"map": map[interface{}]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: 14 additions & 0 deletions internal/encoding/yaml/yaml2.go
@@ -0,0 +1,14 @@
//go:build !viper_yaml3
// +build !viper_yaml3

package yaml

import yamlv2 "gopkg.in/yaml.v2"

var yaml = struct {
Marshal func(in interface{}) (out []byte, err error)
Unmarshal func(in []byte, out interface{}) (err error)
}{
Marshal: yamlv2.Marshal,
Unmarshal: yamlv2.Unmarshal,
}
91 changes: 91 additions & 0 deletions internal/encoding/yaml/yaml2_test.go
@@ -0,0 +1,91 @@
//go:build !viper_yaml3
// +build !viper_yaml3

package yaml

// 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[interface{}]interface{}{
"key": "value",
},
"nested_map": map[interface{}]interface{}{
"map": map[interface{}]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",
},
},
},
}
14 changes: 14 additions & 0 deletions internal/encoding/yaml/yaml3.go
@@ -0,0 +1,14 @@
//go:build viper_yaml3
// +build viper_yaml3

package yaml

import yamlv3 "gopkg.in/yaml.v3"

var yaml = struct {
Marshal func(in interface{}) (out []byte, err error)
Unmarshal func(in []byte, out interface{}) (err error)
}{
Marshal: yamlv3.Marshal,
Unmarshal: yamlv3.Unmarshal,
}
91 changes: 91 additions & 0 deletions internal/encoding/yaml/yaml3_test.go
@@ -0,0 +1,91 @@
//go:build viper_yaml3
// +build viper_yaml3

package yaml

// 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",
},
},
},
}