feat: support complex map types #363
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds support for complex map types, enabling users to configure environment variables that map to Go map structures. References #361 (Note: This is the same as this PR #362, The source branch was changed thus the PR was reopened separately)
This is useful when users might have a structure such as
Where the key to access the
SubEntry
struct can be dynamic.We need to figure out the key of the map, the algorithm for this essentially reads as follows
This results in the following restrictions, if a user was to use "A map of structs inside a map of structs", it becomes harder to figure out the key, thus in these cases instead of trimming the suffix, we split the string using the envPrefix of the sub (inner) map of the last recurring (rightmost) substring. Users should be more aware with the naming of the tags (
env
andenvPrefix
) and should make them unique, when there are nested maps involved.Test cases with examples for these cases are available, e.g. :- https://github.com/caarlos0/env/pull/362/files#diff-19e9f6a4b37808acd7acef7094d89f6f9ca8e7c6409ab9b583fa3fac69000ec6R2529
We also ignore env tag options like
,init
etc on themap field
itself, this is similar to how slices of structs behaves