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

[Feature]: corefunc_map_deepmerge #155

Open
skyzyx opened this issue Feb 14, 2024 · 0 comments
Open

[Feature]: corefunc_map_deepmerge #155

skyzyx opened this issue Feb 14, 2024 · 0 comments
Assignees

Comments

@skyzyx
Copy link
Member

skyzyx commented Feb 14, 2024

What functionality would you like to see?

From Terraform merge():

merge takes an arbitrary number of maps or objects, and returns a single map or object that contains a merged set of elements from all arguments.

If more than one given map or object defines the same key or attribute, then the one that is later in the argument sequence takes precedence. If the argument types do not match, the resulting type will be an object matching the type structure of the attributes after the merging rules have been applied.

From hashicorp/terraform#31815:

merge(): does not recursively merge values that are objects and maps

Kyle Kotowik's deepmerge module: is computationally intensive (since the tree must be flattened, merged, then re-created) and is currently not usable if infracost is used for PR costing […]

The merge algorithm for list_of_items_to_merge is as follows:

items of different types are handled based on the type_mismatch_strategy:

  • complex_wins: object and map win over lists which win over literals; the losers are removed from the merge;
  • abort: abort the terraform plan or apply

if all items are literals, the rightmost item is used

if the remaining items are lists, the list_merge_strategy specified (which defaults to "replace") is applied:

  • replace: the list of the right most argument applies
  • concat: the sequence of lists is concatenated in the same order as arguments
  • merge: each list is appended with null so they all have the same length, then the deepmerge() is applied to each slice through the lists

if the remaining items are maps or objects (the most common case), if more than one defines the same key or attribute, the deepmerge() is applied to the sequence of associated values across all the items

Proposed UX

corefunc_map_deepmerge "merged" {
  type_mismatch_strategy = (enum)
  list_merge_strategy = (enum)
  keep_nulls = (bool)

  merge = [
    map1,
    map2,
  ]
}

Problem

Possible solutions

@skyzyx skyzyx self-assigned this Feb 14, 2024
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

No branches or pull requests

1 participant