diff --git a/internal/encoding/yaml/codec_test.go b/internal/encoding/yaml/codec_test.go index d76a6c6579..d24a0137f7 100644 --- a/internal/encoding/yaml/codec_test.go +++ b/internal/encoding/yaml/codec_test.go @@ -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{} diff --git a/internal/encoding/yaml/yaml2_test.go b/internal/encoding/yaml/yaml2_test.go new file mode 100644 index 0000000000..e39d4298d4 --- /dev/null +++ b/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", + }, + }, + }, +} diff --git a/internal/encoding/yaml/yaml3_test.go b/internal/encoding/yaml/yaml3_test.go new file mode 100644 index 0000000000..6beb263c89 --- /dev/null +++ b/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", + }, + }, + }, +} diff --git a/viper_test.go b/viper_test.go index 8c864f117a..c7c846c51d 100644 --- a/viper_test.go +++ b/viper_test.go @@ -36,14 +36,14 @@ import ( var yamlExample = []byte(`Hacker: true name: steve hobbies: -- skateboarding -- snowboarding -- go + - skateboarding + - snowboarding + - go clothing: - jacket: leather - trousers: denim - pants: - size: large + jacket: leather + trousers: denim + pants: + size: large age: 35 eyes : brown beard: true @@ -1561,16 +1561,16 @@ p_batters.batter.type = Regular var yamlWriteExpected = []byte(`age: 35 beard: true clothing: - jacket: leather - pants: - size: large - trousers: denim + jacket: leather + pants: + size: large + trousers: denim eyes: brown hacker: true hobbies: -- skateboarding -- snowboarding -- go + - skateboarding + - snowboarding + - go name: steve `) @@ -2413,21 +2413,21 @@ func TestUnmarshal_DotSeparatorBackwardCompatibility(t *testing.T) { var yamlExampleWithDot = []byte(`Hacker: true name: steve hobbies: - - skateboarding - - snowboarding - - go + - skateboarding + - snowboarding + - go clothing: - jacket: leather - trousers: denim - pants: - size: large + jacket: leather + trousers: denim + pants: + size: large age: 35 eyes : brown beard: true emails: - steve@hacker.com: - created: 01/02/03 - active: true + steve@hacker.com: + created: 01/02/03 + active: true `) func TestKeyDelimiter(t *testing.T) {