Skip to content

Commit

Permalink
feat(encoding): integrate Java properties codec into Viper
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
  • Loading branch information
sagikazarmark committed Jul 19, 2021
1 parent 02fc5c3 commit 59d08f0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 47 deletions.
25 changes: 17 additions & 8 deletions internal/encoding/javaproperties/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@ import (
// Codec implements the encoding.Encoder and encoding.Decoder interfaces for Java properties encoding.
type Codec struct {
KeyDelimiter string

// Store read properties on the object so that we can write back in order with comments.
// This will only be used if the configuration read is a properties file.
// TODO: drop this feature in v2
// TODO: make use of the global properties object optional
Properties *properties.Properties
}

func (c Codec) Encode(v map[string]interface{}) ([]byte, error) {
p := properties.NewProperties()
func (c *Codec) Encode(v map[string]interface{}) ([]byte, error) {
if c.Properties == nil {
c.Properties = properties.NewProperties()
}

flattened := map[string]interface{}{}

Expand All @@ -30,31 +38,32 @@ func (c Codec) Encode(v map[string]interface{}) ([]byte, error) {
sort.Strings(keys)

for _, key := range keys {
_, _, err := p.Set(key, cast.ToString(flattened[key]))
_, _, err := c.Properties.Set(key, cast.ToString(flattened[key]))
if err != nil {
return nil, err
}
}

var buf bytes.Buffer

_, err := p.WriteComment(&buf, "#", properties.UTF8)
_, err := c.Properties.WriteComment(&buf, "#", properties.UTF8)
if err != nil {
return nil, err
}

return buf.Bytes(), nil
}

func (c Codec) Decode(b []byte, v map[string]interface{}) error {
p, err := properties.Load(b, properties.UTF8)
func (c *Codec) Decode(b []byte, v map[string]interface{}) error {
var err error
c.Properties, err = properties.Load(b, properties.UTF8)
if err != nil {
return err
}

for _, key := range p.Keys() {
for _, key := range c.Properties.Keys() {
// ignore existence check: we know it's there
value, _ := p.Get(key)
value, _ := c.Properties.Get(key)

// recursively build nested maps
path := strings.Split(key, c.keyDelimiter())
Expand Down
57 changes: 18 additions & 39 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"time"

"github.com/fsnotify/fsnotify"
"github.com/magiconair/properties"
"github.com/mitchellh/mapstructure"
"github.com/spf13/afero"
"github.com/spf13/cast"
Expand All @@ -46,6 +45,7 @@ import (
"github.com/spf13/viper/internal/encoding"
"github.com/spf13/viper/internal/encoding/hcl"
"github.com/spf13/viper/internal/encoding/ini"
"github.com/spf13/viper/internal/encoding/javaproperties"
"github.com/spf13/viper/internal/encoding/json"
"github.com/spf13/viper/internal/encoding/toml"
"github.com/spf13/viper/internal/encoding/yaml"
Expand Down Expand Up @@ -216,10 +216,6 @@ type Viper struct {
aliases map[string]string
typeByDefValue bool

// Store read properties on the object so that we can write back in order with comments.
// This will only be used if the configuration read is a properties file.
properties *properties.Properties

onConfigChange func(fsnotify.Event)

// TODO: should probably be protected with a mutex
Expand Down Expand Up @@ -351,6 +347,21 @@ func (v *Viper) resetEncoding() {
decoderRegistry.RegisterDecoder("ini", codec)
}

{
codec := &javaproperties.Codec{
KeyDelimiter: v.keyDelim,
}

encoderRegistry.RegisterEncoder("properties", codec)
decoderRegistry.RegisterDecoder("properties", codec)

encoderRegistry.RegisterEncoder("props", codec)
decoderRegistry.RegisterDecoder("props", codec)

encoderRegistry.RegisterEncoder("prop", codec)
decoderRegistry.RegisterDecoder("prop", codec)
}

v.encoderRegistry = encoderRegistry
v.decoderRegistry = decoderRegistry
}
Expand Down Expand Up @@ -1644,7 +1655,7 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error {
buf.ReadFrom(in)

switch format := strings.ToLower(v.getConfigType()); format {
case "yaml", "yml", "json", "toml", "hcl", "ini":
case "yaml", "yml", "json", "toml", "hcl", "ini", "properties", "props", "prop":
err := v.decoderRegistry.Decode(format, buf.Bytes(), c)
if err != nil {
return ConfigParseError{err}
Expand All @@ -1658,22 +1669,6 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error {
for k, v := range env {
c[k] = v
}

case "properties", "props", "prop":
v.properties = properties.NewProperties()
var err error
if v.properties, err = properties.Load(buf.Bytes(), properties.UTF8); err != nil {
return ConfigParseError{err}
}
for _, key := range v.properties.Keys() {
value, _ := v.properties.Get(key)
// recursively build nested maps
path := strings.Split(key, ".")
lastKey := strings.ToLower(path[len(path)-1])
deepestMap := deepSearch(c, path[0:len(path)-1])
// set innermost value
deepestMap[lastKey] = value
}
}

insensitiviseMap(c)
Expand All @@ -1684,7 +1679,7 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error {
func (v *Viper) marshalWriter(f afero.File, configType string) error {
c := v.AllSettings()
switch configType {
case "yaml", "yml", "json", "toml", "hcl", "ini":
case "yaml", "yml", "json", "toml", "hcl", "ini", "prop", "props", "properties":
b, err := v.encoderRegistry.Encode(configType, c)
if err != nil {
return ConfigMarshalError{err}
Expand All @@ -1695,22 +1690,6 @@ func (v *Viper) marshalWriter(f afero.File, configType string) error {
return ConfigMarshalError{err}
}

case "prop", "props", "properties":
if v.properties == nil {
v.properties = properties.NewProperties()
}
p := v.properties
for _, key := range v.AllKeys() {
_, _, err := p.Set(key, v.GetString(key))
if err != nil {
return ConfigMarshalError{err}
}
}
_, err := p.WriteComment(f, "#", properties.UTF8)
if err != nil {
return ConfigMarshalError{err}
}

case "dotenv", "env":
lines := []string{}
for _, key := range v.AllKeys() {
Expand Down

0 comments on commit 59d08f0

Please sign in to comment.