From cd03c57ee48a3952affdbf783120344311a415d4 Mon Sep 17 00:00:00 2001 From: Fabio Mangione Date: Wed, 23 Mar 2022 10:07:14 +0000 Subject: [PATCH] Change properties codec to return flat keys --- internal/encoding/javaproperties/codec.go | 13 ++----------- internal/encoding/javaproperties/codec_test.go | 6 ++---- viper_test.go | 10 +++++++++- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/internal/encoding/javaproperties/codec.go b/internal/encoding/javaproperties/codec.go index b8a2251..ea2d711 100644 --- a/internal/encoding/javaproperties/codec.go +++ b/internal/encoding/javaproperties/codec.go @@ -2,11 +2,9 @@ package javaproperties import ( "bytes" - "sort" - "strings" - "github.com/magiconair/properties" "github.com/spf13/cast" + "sort" ) // Codec implements the encoding.Encoder and encoding.Decoder interfaces for Java properties encoding. @@ -64,14 +62,7 @@ func (c *Codec) Decode(b []byte, v map[string]interface{}) error { for _, key := range c.Properties.Keys() { // ignore existence check: we know it's there value, _ := c.Properties.Get(key) - - // recursively build nested maps - path := strings.Split(key, c.keyDelimiter()) - lastKey := strings.ToLower(path[len(path)-1]) - deepestMap := deepSearch(v, path[0:len(path)-1]) - - // set innermost value - deepestMap[lastKey] = value + v[key] = value } return nil diff --git a/internal/encoding/javaproperties/codec_test.go b/internal/encoding/javaproperties/codec_test.go index 0a33ebf..2307560 100644 --- a/internal/encoding/javaproperties/codec_test.go +++ b/internal/encoding/javaproperties/codec_test.go @@ -18,10 +18,8 @@ map.key = value // Viper's internal representation var data = map[string]interface{}{ - "key": "value", - "map": map[string]interface{}{ - "key": "value", - }, + "key": "value", + "map.key": "value", } func TestCodec_Encode(t *testing.T) { diff --git a/viper_test.go b/viper_test.go index c41a1e7..b2a4758 100644 --- a/viper_test.go +++ b/viper_test.go @@ -111,6 +111,7 @@ p_type: donut p_name: Cake p_ppu: 0.55 p_batters.batter.type: Regular +p_batters.batter: Foo `) var remoteExample = []byte(`{ @@ -570,6 +571,8 @@ func TestJSON(t *testing.T) { func TestProperties(t *testing.T) { initProperties() assert.Equal(t, "0001", Get("p_id")) + assert.Equal(t, "Regular", Get("p_batters.batter.type")) + assert.Equal(t, "Foo", Get("p_batters.batter")) } func TestTOML(t *testing.T) { @@ -765,6 +768,7 @@ func TestAllKeys(t *testing.T) { "p_id", "p_ppu", "p_batters.batter.type", + "p_batters.batter", "p_type", "p_name", "foos", @@ -822,7 +826,10 @@ func TestAllKeys(t *testing.T) { "p_ppu": "0.55", "p_name": "Cake", "p_batters": map[string]interface{}{ - "batter": map[string]interface{}{"type": "Regular"}, + "batter": []interface{}{ + "Foo", + map[string]interface{}{"type": "Regular"}, + }, }, "p_type": "donut", "foos": []map[string]interface{}{ @@ -1556,6 +1563,7 @@ p_type = donut p_name = Cake p_ppu = 0.55 p_batters.batter.type = Regular +p_batters.batter = Foo `) // var yamlWriteExpected = []byte(`age: 35