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

Collection merge does not work as expected #24236

Closed
aryni01 opened this issue Feb 27, 2020 · 5 comments
Closed

Collection merge does not work as expected #24236

aryni01 opened this issue Feb 27, 2020 · 5 comments

Comments

@aryni01
Copy link

aryni01 commented Feb 27, 2020

Terraform Version

Terraform v0.12.20

Terraform Configuration Files

...

Debug Output

Crash Output

Expected Behavior

While merging two or more collections, the output is not an expected output, for example with:
merge({"x"={"a"="b", "c"="d"}}, {"x"={"e"="f"}})
The result should be {"x"={"a"="b", "c"="d", "e"="f"}}

Actual Behavior

result is:
{"x" = {"e" = "f"}}

Steps to Reproduce

This can be reproduced by just terraform console

Additional Context

References

@aryni01
Copy link
Author

aryni01 commented Feb 28, 2020

more finding..
the AND conditional does not work as expected with any other programming languages:
for example:
contains(keys({"x"={"y"="z"}}),"x") && contains(keys({"x"={"y"="z"}}.x), "y") returns true as expected
but following throws error:
contains(keys({"x"={"y"="z"}}),"m") && contains(keys({"x"={"y"="z"}}.m), "z")
once the first expression is evaluated and is false, by definition, the second expression should not be evaluated for any AND condition, at least almost all (I am saying almost because I have not worked with all available) programming languages works this way

@jbardin
Copy link
Member

jbardin commented Feb 28, 2020

Hi @aryni01,

Thanks for taking the time to file an issue.

While the merge command takes an arbitrary number of maps or objects, it makes no mention of nested maps or objects, and indeed performs no flattening of the objects being passed in. Each object in your example has a single attribute of x, and according the merging rule, the final assignment "wins", meaning the value of {"e"="f"} is what is expected.

As for the && operator behavior, that is recorded in issue #24128, and will require a change in the upstream dependency for the expression evaluation.

Thanks!

@jbardin jbardin closed this as completed Feb 28, 2020
@aryni01
Copy link
Author

aryni01 commented Feb 28, 2020

haha.. closed the issue without even very good explanation, a merge by definition is merging two or more objects and becoming bigger object, there is no such thing as winning in merging, may be in software engineering it is, as two companies merge, most of the people from smaller companies get laid off, very realistic approach... but still not quite right because in my example the people from bigger company are laid off..

@jbardin
Copy link
Member

jbardin commented Feb 28, 2020

Sorry, maybe I can phrase it differently. The merge function operates only on the map or object value passed in, and purposely takes no account of the type or value of the nested elements. This follows the documented and historical behavior of merge, and changing that would be a breaking change to any configuration currently using merge. While "merge" may have different linguistic meanings in different contexts, we have to remain compatible with the current implementation at this time.

A function that recursively merges nested maps and/or objects would need to be a different function entirely. A new function is not entirely out of the question, but should be filed as a proposal with the desired behavior and use cases.

@ghost
Copy link

ghost commented Mar 30, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Mar 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants