Commit 0b53db6
authored
🔧 Support multiple --mappings-file flags with deep merge capability (#67)
## 🧩 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:**
```bash
struct generate \
--mappings-file ./common-mappings.yaml \
--mappings-file ./env-specific-mappings.yaml \
file://my-struct.yaml .
```
**Environment-based automation:**
```bash
struct generate \
--mappings-file ./mappings/common.yaml \
--mappings-file ./mappings/${ENVIRONMENT}.yaml \
file://infrastructure.yaml \
./output
```
### Merging Behavior
**Input files:**
`common.yaml`:
```yaml
mappings:
teams:
devops: devops-team
environments:
dev:
database_url: postgres://dev-db:5432
```
`production.yaml`:
```yaml
mappings:
teams:
frontend: frontend-team
environments:
dev:
debug: true
prod:
database_url: postgres://prod-db:5432
```
**Merged result:**
```yaml
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
```bash
# 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.1 parent 5b7cfc8 commit 0b53db6
4 files changed
Lines changed: 166 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
151 | 151 | | |
152 | 152 | | |
153 | 153 | | |
154 | | - | |
| 154 | + | |
155 | 155 | | |
156 | 156 | | |
157 | 157 | | |
| |||
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
163 | | - | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
164 | 179 | | |
165 | 180 | | |
166 | 181 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | | - | |
| 47 | + | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
26 | | - | |
| 25 | + | |
| 26 | + | |
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
31 | 43 | | |
32 | 44 | | |
33 | 45 | | |
| |||
75 | 87 | | |
76 | 88 | | |
77 | 89 | | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
86 | 104 | | |
87 | 105 | | |
88 | 106 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
400 | 400 | | |
401 | 401 | | |
402 | 402 | | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
0 commit comments