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
16 | 16 | matrix: |
17 | 17 | os: [ubuntu-latest, macos-latest, windows-latest] |
18 | 18 | go: ['1.14', '1.15', '1.16', '1.17'] |
19 | tags: ['', 'viper_yaml3'] | |
19 | tags: ['', 'viper_yaml3', 'viper_toml2'] | |
20 | 20 | env: |
21 | 21 | GOFLAGS: -mod=readonly |
22 | 22 |
7 | 7 | github.com/magiconair/properties v1.8.5 |
8 | 8 | github.com/mitchellh/mapstructure v1.4.3 |
9 | 9 | github.com/pelletier/go-toml v1.9.4 |
10 | github.com/pelletier/go-toml/v2 v2.0.0-beta.6 | |
10 | 11 | github.com/sagikazarmark/crypt v0.4.0 |
11 | 12 | github.com/spf13/afero v1.7.1 |
12 | 13 | github.com/spf13/cast v1.4.1 |
13 | 14 | github.com/spf13/jwalterweatherman v1.1.0 |
14 | 15 | 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 | |
16 | 17 | github.com/subosito/gotenv v1.2.0 |
17 | 18 | gopkg.in/ini.v1 v1.66.2 |
18 | 19 | gopkg.in/yaml.v2 v2.4.0 |
317 | 317 | github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= |
318 | 318 | github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM= |
319 | 319 | 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= | |
320 | 322 | github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= |
321 | 323 | github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= |
322 | 324 | github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= |
367 | 369 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= |
368 | 370 | github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= |
369 | 371 | github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= |
370 | github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= | |
371 | 372 | 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= | |
372 | 375 | github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= |
373 | 376 | github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= |
374 | 377 | github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM= |
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 | } |