refactor(cfn-lang-ext): extract shared Mapping-name primitives for Fn::ForEach#9018
Open
bnusunny wants to merge 1 commit into
Open
refactor(cfn-lang-ext): extract shared Mapping-name primitives for Fn::ForEach#9018bnusunny wants to merge 1 commit into
bnusunny wants to merge 1 commit into
Conversation
…::ForEach Follow-up to #9009 (issue #9005). Closes the second class of structural divergence the bot reviews on that PR repeatedly caught: the Mapping name, Fn::FindInMap lookup key, and compound Mapping table key for nested Fn::ForEach were each constructed inline in two places — once in the build- time pipeline (samcli/commands/build/build_context.py) and once in the package-time pipeline (samcli/lib/package/language_extensions_packaging.py). Bot review #2 caught the missing prefix list; bot review #3 caught the collision counter keyed on the dotted path. Both bugs were duplicate string-construction logic drifting between sites. This PR extracts the three computations into a single module: samcli/lib/cfn_language_extensions/foreach_mapping_helpers.py with three pure functions: - compute_mapping_name(leaf, nesting_path, *, has_collision, resource_template_key) - compute_lookup_key(loop_variable, referenced_outer_vars) - compute_compound_mapping_key(outer_values, inner_value) Both pipelines now call these helpers; neither constructs the strings inline. The cross-pipeline equivalence test in tests/unit/lib/cfn_language_extensions/test_foreach_mapping_helpers.py asserts that build's collision-counting input and package's collision- counting input produce byte-identical Mapping names for the same logical inputs — locking in the structural guarantee. Mechanical changes: - samcli/lib/cfn_language_extensions/foreach_mapping_helpers.py (new) — three pure helpers + module docstring + __all__. - tests/unit/lib/cfn_language_extensions/test_foreach_mapping_helpers.py (new) — 19 tests including parameterized construction cases and three cross-pipeline equivalence cases. - samcli/lib/package/language_extensions_packaging.py — _compute_mapping_name body becomes a 3-line delegation; _replace_dynamic_artifact_with_findmap's default mapping_name and inline lookup_key construction become helper calls; _generate_artifact_mappings's inline compound-key construction becomes a helper call. - samcli/commands/build/build_context.py — _update_foreach_artifact_paths's inline mapping_name and lookup_key constructions become helper calls; _collect_nested_mapping_entry's inline compound-key construction becomes a helper call. Collapses two if/else branches in the process. Removes the now-unused sanitize_resource_key_for_mapping import. What this PR does NOT do: - It does not unify orchestration. The build walker stays a recursive walker; the package detect-then-emit pipeline stays a detect-then-emit pipeline. They just compute Mapping identifiers through one shared source of truth. - It does not touch auto-dependency-layer Mapping emission (_collect_foreach_layer_mappings). That stays in build_context.py. - It does not change DynamicArtifactProperty or PACKAGEABLE_RESOURCE_ARTIFACT_PROPERTIES. No detection changes. Verification: - baseline 9191 passed, 25 skipped → after 9211 passed, 25 skipped (+20) - targeted: 2052 passed across cfn_language_extensions, package, build - ruff check, mypy, black --check all clean - end-to-end #9005 repro (sam package against language-extensions/tc-026-nested-application) still rewrites Properties.Location to an https://s3... URL - leaf-collision regression tests from #9009 commit 5e81820 still pass
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Follow-up to #9009 (issue #9005). Closes the second class of structural divergence the bot reviews on that PR repeatedly caught: the Mapping name, Fn::FindInMap lookup key, and compound Mapping table key for nested Fn::ForEach were each constructed inline in two places — once in the build-time pipeline (`samcli/commands/build/build_context.py`) and once in the package-time pipeline (`samcli/lib/package/language_extensions_packaging.py`). Bot review #2 caught a missing prefix list; bot review #3 caught a collision counter keyed on the dotted path. Both bugs were duplicate string-construction logic drifting between sites.
This PR extracts the three computations into a single module so neither pipeline can drift from the other again.
Changes
What this PR does NOT do
Test plan
Relationship to PR #9017
Independent. PR #9017 consolidates the copy helpers and relocates the prop-name helpers into a public module. PR B (this one) extracts the Mapping-name primitives. They touch different functions in the same files; the merge order doesn't matter.