magiconair / properties

Java properties scanner for Go
BSD 2-Clause "Simplified" License
323 stars 77 forks source link

Decode(map[string]interface{}) #9

Open doublerebel opened 8 years ago

doublerebel commented 8 years ago

Add feature to Decode/Unmarshal configuration into a map[string]interface{}.

This functionality is available in HCL, JSON, YAML and TOML decoders, as we can see in Viper. This change enables Viper to Unmarshal dot-separated values from a .properties file into a nested Struct or Map.

doublerebel commented 8 years ago

Thanks @magiconair, I have removed DecodeToStringMap and fixed the empty map syntax.

I am not looking to add Interface as a value type, my naive decoder is indifferent to the values. My goal is to create a map of maps, so that the parsed properties are easier to iterate over and manipulate. This way properties can be read without requiring a predefined Struct. This is the same behavior supported by the decoders for other formats.

magiconair commented 8 years ago

The current Decode() method already supports decoding nested maps with the dot notation and array values. Your code duplicates this behavior. The only pieces that are missing are decoding a string into interface{} and not requiring a struct as the root element. Once you implement the latter (with my branch as the starting point for supporting string -> interface{}) you can just also just add one more test case to TestDecodeMap()

magiconair commented 8 years ago

The change in the current form would create the situation that decoding to map[string]interface{} is supported whereas decoding to struct{A map[string]interface{}} is not. Can you try to work on the approach I've outlined, i.e. use my branch as starting point and remove the special case function for decoding a map?

magiconair commented 8 years ago

I can live with the fact that map[string]interface{} isn't supported as a struct field type for now. I'll cross that bridge when I get there. I've refactored your code a bit and added comments as a separate commit. Those would have to be squashed before merging. See https://github.com/magiconair/properties/tree/pr9 since I'm used to gerrit and don't know how multiple people can collaborate on a pull request on Github.

Some things I've stumbled upon:

  1. All values are strings except a;b;c which is decoded as []string. Why is that?
  2. Why use the ; as delimiter? The Decode method uses the comma by default and through the tags I have the option to support other delimiters if the need arises.