-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat(globals): add per-property merge strategy for list properties #3945
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
Open
vicheey
wants to merge
8
commits into
develop
Choose a base branch
from
feat/globals-merge-strategy
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
46a50d4
feat(globals): add per-property merge strategy with dot-notation schema
vicheey 052b861
fix(globals): deduplicate _merge_by_key output when inputs have dupli…
vicheey 69940b4
feat(globals): add REPLACE strategy for CapacityProvider.InstanceRequ…
vicheey d663ae0
fix(globals): make _merge_by_key first-wins consistent across both pa…
vicheey c652d25
feat(globals): add REPLACE_KEYS_MERGE_VALUES strategy for dict proper…
vicheey be73d7c
Merge branch 'develop' into feat/globals-merge-strategy
vicheey ade81e2
feat(globals): add REPLACE_KEYS_MERGE_VALUES strategy for ManagedReso…
vicheey 62be02d
Merge commit 'f29d329cb8b69c15796aa1dc975228d01ec05b68' into feat/glo…
vicheey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| """Per-property merge strategy types for the Globals merge engine.""" | ||
|
|
||
| from dataclasses import dataclass | ||
| from enum import Enum | ||
|
|
||
|
|
||
| class MergeOp(Enum): | ||
| CONCATENATE = "concatenate" | ||
| REPLACE = "replace" | ||
| MERGE_BY_KEY = "merge_by_key" | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class MergeRule: | ||
| op: MergeOp | ||
| key: str | None = None | ||
|
|
||
| def __post_init__(self) -> None: | ||
| if self.op == MergeOp.MERGE_BY_KEY and not self.key: | ||
| raise ValueError("MERGE_BY_KEY requires a 'key' field") | ||
| if self.op != MergeOp.MERGE_BY_KEY and self.key is not None: | ||
| raise ValueError(f"'key' is only valid with MERGE_BY_KEY, not {self.op.value}") | ||
|
|
||
|
|
||
| # Explicit default; not needed in CUSTOM_STRATEGIES (unlisted paths already concatenate). | ||
| CONCATENATE = MergeRule(MergeOp.CONCATENATE) | ||
| REPLACE = MergeRule(MergeOp.REPLACE) | ||
|
|
||
|
|
||
| def merge_by_key(key: str) -> MergeRule: | ||
| """Factory for MERGE_BY_KEY rules. Merges list-of-dicts by the named key field.""" | ||
| return MergeRule(MergeOp.MERGE_BY_KEY, key=key) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| """Unit tests for merge_strategy.py types.""" | ||
|
|
||
| import unittest | ||
|
|
||
| from parameterized import parameterized | ||
| from samtranslator.plugins.globals.merge_strategy import ( | ||
| CONCATENATE, | ||
| REPLACE, | ||
| MergeOp, | ||
| MergeRule, | ||
| merge_by_key, | ||
| ) | ||
|
|
||
|
|
||
| class TestMergeOp(unittest.TestCase): | ||
| @parameterized.expand( | ||
| [ | ||
| ("concatenate", MergeOp.CONCATENATE, "concatenate"), | ||
| ("replace", MergeOp.REPLACE, "replace"), | ||
| ("merge_by_key", MergeOp.MERGE_BY_KEY, "merge_by_key"), | ||
| ] | ||
| ) | ||
| def test_enum_values(self, _name, member, expected): | ||
| self.assertEqual(member.value, expected) | ||
|
|
||
|
|
||
| class TestMergeRule(unittest.TestCase): | ||
| @parameterized.expand( | ||
| [ | ||
| ("replace", MergeOp.REPLACE, None), | ||
| ("concatenate", MergeOp.CONCATENATE, None), | ||
| ("merge_by_key", MergeOp.MERGE_BY_KEY, "Key"), | ||
| ] | ||
| ) | ||
| def test_valid_creation(self, _name, op, key): | ||
| rule = MergeRule(op, key=key) if key else MergeRule(op) | ||
| self.assertEqual(rule.op, op) | ||
| self.assertEqual(rule.key, key) | ||
|
|
||
| @parameterized.expand( | ||
| [ | ||
| ("merge_by_key_no_key", MergeOp.MERGE_BY_KEY, None, "MERGE_BY_KEY requires a 'key' field"), | ||
| ("replace_with_key", MergeOp.REPLACE, "Bad", "only valid with MERGE_BY_KEY"), | ||
| ("concatenate_with_key", MergeOp.CONCATENATE, "Bad", "only valid with MERGE_BY_KEY"), | ||
| ] | ||
| ) | ||
| def test_invalid_creation_raises(self, _name, op, key, expected_msg): | ||
| with self.assertRaises(ValueError) as ctx: | ||
| MergeRule(op, key=key) | ||
| self.assertIn(expected_msg, str(ctx.exception)) | ||
|
|
||
| def test_frozen_immutable(self): | ||
| rule = MergeRule(MergeOp.REPLACE) | ||
| with self.assertRaises(AttributeError): | ||
| rule.op = MergeOp.CONCATENATE | ||
|
|
||
|
|
||
| class TestConvenienceConstructors(unittest.TestCase): | ||
| @parameterized.expand( | ||
| [ | ||
| ("CONCATENATE", CONCATENATE, MergeOp.CONCATENATE, None), | ||
| ("REPLACE", REPLACE, MergeOp.REPLACE, None), | ||
| ("MERGE_BY_KEY", merge_by_key("Key"), MergeOp.MERGE_BY_KEY, "Key"), | ||
| ] | ||
| ) | ||
| def test_constructor(self, _name, rule, expected_op, expected_key): | ||
| self.assertEqual(rule.op, expected_op) | ||
| self.assertEqual(rule.key, expected_key) | ||
|
|
||
|
|
||
| class TestSchemaKeyFormat(unittest.TestCase): | ||
| """Dot-notation schema keys support nested property paths.""" | ||
|
|
||
| @parameterized.expand( | ||
| [ | ||
| ("top_level", "Architectures"), | ||
| ("one_level_nested", "VpcConfig.SecurityGroupIds"), | ||
| ("two_levels_nested", "VpcConfig.SubnetConfig.SubnetIds"), | ||
| ] | ||
| ) | ||
| def test_valid_dot_notation_keys(self, _name, key): | ||
| """Dot-separated paths are the schema key format — all valid.""" | ||
| schema = {key: REPLACE} | ||
| # Should not raise — dots are path separators, not errors | ||
| self.assertIn(key, schema) | ||
| self.assertEqual(schema[key], REPLACE) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
21 changes: 21 additions & 0 deletions
21
tests/translator/input/globals_merge_strategy_architectures.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Merge strategy translator-level tests. | ||
| # Add new test cases here when new rules are added to CUSTOM_STRATEGIES. | ||
| Globals: | ||
| Function: | ||
| Runtime: python3.12 | ||
| Handler: app.handler | ||
| Architectures: | ||
| - x86_64 | ||
|
|
||
| Resources: | ||
| FunctionInheritsGlobalArch: | ||
| Type: AWS::Serverless::Function | ||
| Properties: | ||
| CodeUri: s3://bucket/code.zip | ||
|
|
||
| FunctionOverridesArch: | ||
| Type: AWS::Serverless::Function | ||
| Properties: | ||
| CodeUri: s3://bucket/code.zip | ||
| Architectures: | ||
| - arm64 |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[BUG] _merge_by_key produces inconsistent results when local_list contains duplicate key values, depending on whether global_list happens to contain the same key.
The dict comprehension causes the last local entry to win for any given key. Then in Pass 2, the loop iterates local_list directly and appends the first non-seen entry. The two passes disagree:
Trace with key="Key":
The existing test list_with_merge_by_key_deduplicates_local_duplicates covers Case A and asserts first-wins; Case B is untested and silently flips the precedence. Recommend picking one rule and using it in both passes — e.g. build local_by_key by skipping already-present keys (if item[key] not in local_by_key) so first-wins is consistent across both branches, or by iterating local_by_key.values() in Pass 2 so last-wins is consistent.
This is dormant for now (no CUSTOM_STRATEGIES entry uses MERGE_BY_KEY), but the test suite locks in the inconsistent behavior, which will be harder to change once Tags merge-by-key is enabled.