Codebase list golang-github-spf13-viper / b13f096
feat(encoding): experimental toml v2 support Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com> Mark Sagi-Kazar authored 2 years ago Márk Sági-Kazár committed 2 years ago
7 changed file(s) with 141 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
1616 matrix:
1717 os: [ubuntu-latest, macos-latest, windows-latest]
1818 go: ['1.14', '1.15', '1.16', '1.17']
19 tags: ['', 'viper_yaml3']
19 tags: ['', 'viper_yaml3', 'viper_toml2']
2020 env:
2121 GOFLAGS: -mod=readonly
2222
77 github.com/magiconair/properties v1.8.5
88 github.com/mitchellh/mapstructure v1.4.3
99 github.com/pelletier/go-toml v1.9.4
10 github.com/pelletier/go-toml/v2 v2.0.0-beta.6
1011 github.com/sagikazarmark/crypt v0.4.0
1112 github.com/spf13/afero v1.7.1
1213 github.com/spf13/cast v1.4.1
1314 github.com/spf13/jwalterweatherman v1.1.0
1415 github.com/spf13/pflag v1.0.5
15 github.com/stretchr/testify v1.7.0
16 github.com/stretchr/testify v1.7.1-0.20210427113832-6241f9ab9942
1617 github.com/subosito/gotenv v1.2.0
1718 gopkg.in/ini.v1 v1.66.2
1819 gopkg.in/yaml.v2 v2.4.0
317317 github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
318318 github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM=
319319 github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
320 github.com/pelletier/go-toml/v2 v2.0.0-beta.6 h1:JFNqj2afbbhCqTiyN16D7Tudc/aaDzE2FBDk+VlBQnE=
321 github.com/pelletier/go-toml/v2 v2.0.0-beta.6/go.mod h1:ke6xncR3W76Ba8xnVxkrZG0js6Rd2BsQEAYrfgJ6eQA=
320322 github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
321323 github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
322324 github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
367369 github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
368370 github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
369371 github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
370 github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
371372 github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
373 github.com/stretchr/testify v1.7.1-0.20210427113832-6241f9ab9942 h1:t0lM6y/M5IiUZyvbBTcngso8SZEZICH7is9B6g/obVU=
374 github.com/stretchr/testify v1.7.1-0.20210427113832-6241f9ab9942/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
372375 github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
373376 github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
374377 github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
0 //go:build !viper_toml2
1 // +build !viper_toml2
2
03 package toml
14
25 import (
0 //go:build viper_toml2
1 // +build viper_toml2
2
3 package toml
4
5 import (
6 "github.com/pelletier/go-toml/v2"
7 )
8
9 // Codec implements the encoding.Encoder and encoding.Decoder interfaces for TOML encoding.
10 type Codec struct{}
11
12 func (Codec) Encode(v map[string]interface{}) ([]byte, error) {
13 return toml.Marshal(v)
14 }
15
16 func (Codec) Decode(b []byte, v map[string]interface{}) error {
17 return toml.Unmarshal(b, &v)
18 }
0 //go:build viper_toml2
1 // +build viper_toml2
2
3 package toml
4
5 import (
6 "reflect"
7 "testing"
8 )
9
10 // original form of the data
11 const original = `# key-value pair
12 key = "value"
13 list = ["item1", "item2", "item3"]
14
15 [map]
16 key = "value"
17
18 # nested
19 # map
20 [nested_map]
21 [nested_map.map]
22 key = "value"
23 list = [
24 "item1",
25 "item2",
26 "item3",
27 ]
28 `
29
30 // encoded form of the data
31 const encoded = `key = 'value'
32 list = ['item1', 'item2', 'item3']
33 [map]
34 key = 'value'
35
36 [nested_map]
37 [nested_map.map]
38 key = 'value'
39 list = ['item1', 'item2', 'item3']
40
41
42 `
43
44 // Viper's internal representation
45 var data = map[string]interface{}{
46 "key": "value",
47 "list": []interface{}{
48 "item1",
49 "item2",
50 "item3",
51 },
52 "map": map[string]interface{}{
53 "key": "value",
54 },
55 "nested_map": map[string]interface{}{
56 "map": map[string]interface{}{
57 "key": "value",
58 "list": []interface{}{
59 "item1",
60 "item2",
61 "item3",
62 },
63 },
64 },
65 }
66
67 func TestCodec_Encode(t *testing.T) {
68 codec := Codec{}
69
70 b, err := codec.Encode(data)
71 if err != nil {
72 t.Fatal(err)
73 }
74
75 if encoded != string(b) {
76 t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", string(b), encoded)
77 }
78 }
79
80 func TestCodec_Decode(t *testing.T) {
81 t.Run("OK", func(t *testing.T) {
82 codec := Codec{}
83
84 v := map[string]interface{}{}
85
86 err := codec.Decode([]byte(original), v)
87 if err != nil {
88 t.Fatal(err)
89 }
90
91 if !reflect.DeepEqual(data, v) {
92 t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", v, data)
93 }
94 })
95
96 t.Run("InvalidData", func(t *testing.T) {
97 codec := Codec{}
98
99 v := map[string]interface{}{}
100
101 err := codec.Decode([]byte(`invalid data`), v)
102 if err == nil {
103 t.Fatal("expected decoding to fail")
104 }
105
106 t.Logf("decoding failed as expected: %s", err)
107 })
108 }
0 //go:build !viper_toml2
1 // +build !viper_toml2
2
03 package toml
14
25 import (