properties icon indicating copy to clipboard operation
properties copied to clipboard

Decode(map[string]interface{})

Open doublerebel opened this issue 8 years ago • 4 comments

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 avatar Mar 02 '16 23:03 doublerebel

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.

doublerebel avatar Mar 06 '16 03:03 doublerebel

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 avatar Mar 06 '16 07:03 magiconair

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 avatar Mar 19 '16 01:03 magiconair

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.

magiconair avatar Mar 19 '16 02:03 magiconair