Skip to content

Conversation

@httpdss
Copy link
Owner

@httpdss httpdss commented Jul 27, 2025

🧩 Feature Implementation

This PR implements support for multiple --mappings-file flags as requested in issue #66.

βœ… What Changed

Core Functionality

  • Modified argument parser to accept multiple --mappings-file flags using action='append'
  • Added deep merge capability with _deep_merge_dicts() method for intelligent merging
  • Implemented order-preserving merge where later files override earlier ones for conflicting keys
  • Deep merging for nested dictionaries to preserve hierarchical structure

Usage Examples

Basic multiple mappings:

struct generate \
  --mappings-file ./common-mappings.yaml \
  --mappings-file ./env-specific-mappings.yaml \
  file://my-struct.yaml .

Environment-based automation:

struct generate \
  --mappings-file ./mappings/common.yaml \
  --mappings-file ./mappings/${ENVIRONMENT}.yaml \
  file://infrastructure.yaml \
  ./output

Merging Behavior

Input files:

common.yaml:

mappings:
  teams:
    devops: devops-team
  environments:
    dev:
      database_url: postgres://dev-db:5432

production.yaml:

mappings:
  teams:
    frontend: frontend-team
  environments:
    dev:
      debug: true
    prod:
      database_url: postgres://prod-db:5432

Merged result:

mappings:
  teams:
    devops: devops-team      # from common.yaml
    frontend: frontend-team  # from production.yaml
  environments:
    dev:
      database_url: postgres://dev-db:5432  # from common.yaml
      debug: true                           # from production.yaml
    prod:
      database_url: postgres://prod-db:5432 # from production.yaml

πŸ› οΈ Technical Implementation

Deep Merge Algorithm

  • Recursively merges nested dictionaries
  • Preserves all keys from both dictionaries
  • Later values override earlier ones for conflicts
  • Maintains data type integrity

Error Handling

  • Validates each mappings file exists before processing
  • Stops execution if any mappings file is missing or invalid
  • Provides clear error messages for debugging

Backward Compatibility

  • Single --mappings-file usage remains unchanged
  • No breaking changes to existing functionality
  • Graceful handling when no mappings files are provided

πŸ“š Documentation Updates

  • Enhanced mappings.md with detailed multiple files documentation
  • Updated usage.md to reflect new capability
  • Added practical examples showing environment-specific patterns
  • Explained merging behavior with clear examples

πŸ§ͺ Testing

Added comprehensive tests covering:

  • Deep merge functionality with nested dictionaries
  • Multiple mappings file loading and merging
  • Order preservation and override behavior
  • Integration with existing generate command flow

πŸš€ Benefits

For CI/CD Automation

# Clean separation of common vs environment-specific config
struct generate \
  --mappings-file ./mappings/common.yaml \
  --mappings-file ./mappings/${ENVIRONMENT}.yaml \
  file://infrastructure.yaml \
  ./output

For DRY Principles

  • Reusable common configurations across environments
  • Environment-specific overrides without duplication
  • Modular mapping organization for better maintainability

For Complex Projects

  • Team-specific configurations with shared defaults
  • Multi-environment deployments with inheritance
  • Progressive configuration builds from base to specific

πŸ”— Resolves

Closes #66


This implementation provides the exact functionality requested in the issue while maintaining backward compatibility and adding comprehensive documentation and testing.

- Modified GenerateCommand to accept multiple --mappings-file arguments using action='append'
- Added _deep_merge_dicts method for intelligent merging of mapping files
- Later files override earlier ones for conflicting keys, with deep merging for nested dicts
- Updated documentation to explain merging behavior and provide examples
- Added comprehensive tests for deep merge functionality and multiple mappings file handling

Resolves #66
@codecov-commenter
Copy link

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 86.04651% with 6 lines in your changes missing coverage. Please review.
βœ… Project coverage is 66.47%. Comparing base (5b7cfc8) to head (44369a3).

Files with missing lines Patch % Lines
struct_module/commands/generate.py 70.00% 5 Missing and 1 partial ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #67      +/-   ##
==========================================
+ Coverage   65.33%   66.47%   +1.14%     
==========================================
  Files          22       22              
  Lines        1203     1238      +35     
  Branches      171      174       +3     
==========================================
+ Hits          786      823      +37     
+ Misses        378      376       -2     
  Partials       39       39              

β˜” View full report in Codecov by Sentry.
πŸ“’ Have feedback on the report? Share it here.

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@httpdss httpdss self-assigned this Jul 27, 2025
@httpdss httpdss added enhancement New feature or request good first issue Good for newcomers minor labels Jul 27, 2025
@httpdss httpdss moved this to Todo in struct-mainboard Jul 27, 2025
@httpdss httpdss merged commit 0b53db6 into main Jul 27, 2025
3 checks passed
@httpdss httpdss deleted the 66-support-multiple-mappings branch July 27, 2025 15:33
@github-project-automation github-project-automation bot moved this from Todo to Done in struct-mainboard Jul 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request good first issue Good for newcomers minor

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

πŸ”§ Support multiple --mappings-file flags and merge their contents automatically

3 participants