Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

doublerebel
Copy link

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants