@@ -345,7 +345,7 @@ fn process_validation_result(
345
345
/// with the highest priority. The merge priority chain looks like this where '->' means
346
346
/// "overwrites if existing or adds":
347
347
///
348
- /// group overrides -> group config -> role overrides -> role config (TODO: -> common_config)
348
+ /// group overrides -> role overrides -> group config -> role config (TODO: -> common_config)
349
349
///
350
350
/// The output is a map where the [`crate::role_utils::RoleGroup] name points to another map of
351
351
/// [`product_config::types::PropertyValidationResult`] that points to the mapped configuration
@@ -368,24 +368,43 @@ where
368
368
{
369
369
let mut result = HashMap :: new ( ) ;
370
370
371
+ // Properties from the role have the lowest priority, so they are computed first...
371
372
let role_properties = parse_role_config ( resource, role_name, & role. config , property_kinds) ?;
373
+ let role_overrides = parse_role_overrides ( & role. config , property_kinds) ?;
372
374
373
375
// for each role group ...
374
376
for ( role_group_name, role_group) in & role. role_groups {
375
- // ... compute the group properties ...
377
+ let mut rg_properties_merged = role_properties. clone ( ) ;
378
+
379
+ // ... compute the group properties and merge them into role properties.
376
380
let role_group_properties =
377
381
parse_role_config ( resource, role_name, & role_group. config , property_kinds) ?;
378
-
379
- // ... and merge them with the role properties.
380
- let mut role_properties_copy = role_properties. clone ( ) ;
381
382
for ( property_kind, properties) in role_group_properties {
382
- role_properties_copy
383
+ rg_properties_merged
383
384
. entry ( property_kind)
384
385
. or_default ( )
385
386
. extend ( properties) ;
386
387
}
387
388
388
- result. insert ( role_group_name. clone ( ) , role_properties_copy) ;
389
+ // ... copy role overrides and merge them into `rg_properties_merged`.
390
+ let role_overrides_copy = role_overrides. clone ( ) ;
391
+ for ( property_kind, property_overrides) in role_overrides_copy {
392
+ rg_properties_merged
393
+ . entry ( property_kind)
394
+ . or_default ( )
395
+ . extend ( property_overrides) ;
396
+ }
397
+
398
+ // ... compute the role group overrides and merge them into `rg_properties_merged`.
399
+ let role_group_overrides = parse_role_overrides ( & role_group. config , property_kinds) ?;
400
+ for ( property_kind, property_overrides) in role_group_overrides {
401
+ rg_properties_merged
402
+ . entry ( property_kind)
403
+ . or_default ( )
404
+ . extend ( property_overrides) ;
405
+ }
406
+
407
+ result. insert ( role_group_name. clone ( ) , rg_properties_merged) ;
389
408
}
390
409
391
410
Ok ( result)
@@ -411,86 +430,80 @@ where
411
430
T : Configuration ,
412
431
{
413
432
let mut result = HashMap :: new ( ) ;
414
-
415
433
for property_kind in property_kinds {
416
434
match property_kind {
417
435
PropertyNameKind :: File ( file) => result. insert (
418
436
property_kind. clone ( ) ,
419
- parse_file_properties ( resource, role_name, config , file) ?,
437
+ config . config . compute_files ( resource, role_name, file) ?,
420
438
) ,
421
439
PropertyNameKind :: Env => result. insert (
422
440
property_kind. clone ( ) ,
423
- parse_env_properties ( resource, role_name, config ) ?,
441
+ config . config . compute_env ( resource, role_name) ?,
424
442
) ,
425
443
PropertyNameKind :: Cli => result. insert (
426
444
property_kind. clone ( ) ,
427
- parse_cli_properties ( resource, role_name, config ) ?,
445
+ config . config . compute_cli ( resource, role_name) ?,
428
446
) ,
429
447
} ;
430
448
}
431
449
432
450
Ok ( result)
433
451
}
434
452
435
- fn parse_cli_properties < T > (
436
- resource : & <T as Configuration >:: Configurable ,
437
- role_name : & str ,
453
+ fn parse_role_overrides < T > (
438
454
config : & CommonConfiguration < T > ,
439
- ) -> Result < BTreeMap < String , Option < String > > >
440
- where
441
- T : Configuration ,
442
- {
443
- // Properties from the role have the lowest priority, so they are computed and added first...
444
- let mut final_properties = config. config . compute_cli ( resource, role_name) ?;
445
-
446
- // ...followed by config_overrides from the role
447
- for ( key, value) in & config. cli_overrides {
448
- final_properties. insert ( key. clone ( ) , Some ( value. clone ( ) ) ) ;
449
- }
450
-
451
- Ok ( final_properties)
452
- }
453
-
454
- fn parse_env_properties < T > (
455
- resource : & <T as Configuration >:: Configurable ,
456
- role_name : & str ,
457
- config : & CommonConfiguration < T > ,
458
- ) -> Result < BTreeMap < String , Option < String > > >
455
+ property_kinds : & [ PropertyNameKind ] ,
456
+ ) -> Result < HashMap < PropertyNameKind , BTreeMap < String , Option < String > > > >
459
457
where
460
458
T : Configuration ,
461
459
{
462
- // Properties from the role have the lowest priority, so they are computed and added first...
463
- let mut final_properties = config. config . compute_env ( resource, role_name) ?;
464
-
465
- // ...followed by config_overrides from the role
466
- for ( key, value) in & config. env_overrides {
467
- final_properties. insert ( key. clone ( ) , Some ( value. clone ( ) ) ) ;
460
+ let mut result = HashMap :: new ( ) ;
461
+ for property_kind in property_kinds {
462
+ match property_kind {
463
+ PropertyNameKind :: File ( file) => {
464
+ result. insert ( property_kind. clone ( ) , parse_file_overrides ( config, file) ?)
465
+ }
466
+ PropertyNameKind :: Env => result. insert (
467
+ property_kind. clone ( ) ,
468
+ config
469
+ . env_overrides
470
+ . clone ( )
471
+ . into_iter ( )
472
+ . map ( |( k, v) | ( k, Some ( v) ) )
473
+ . collect ( ) ,
474
+ ) ,
475
+ PropertyNameKind :: Cli => result. insert (
476
+ property_kind. clone ( ) ,
477
+ config
478
+ . cli_overrides
479
+ . clone ( )
480
+ . into_iter ( )
481
+ . map ( |( k, v) | ( k, Some ( v) ) )
482
+ . collect ( ) ,
483
+ ) ,
484
+ } ;
468
485
}
469
486
470
- Ok ( final_properties )
487
+ Ok ( result )
471
488
}
472
489
473
- fn parse_file_properties < T > (
474
- resource : & <T as Configuration >:: Configurable ,
475
- role_name : & str ,
490
+ fn parse_file_overrides < T > (
476
491
config : & CommonConfiguration < T > ,
477
492
file : & str ,
478
493
) -> Result < BTreeMap < String , Option < String > > >
479
494
where
480
495
T : Configuration ,
481
496
{
482
- // Properties from the role have the lowest priority, so they are computed and added first...
483
- let mut final_properties = config. config . compute_files ( resource, role_name, file) ?;
497
+ let mut final_overrides: BTreeMap < String , Option < String > > = BTreeMap :: new ( ) ;
484
498
485
- // ...followed by config_overrides from the role
486
499
// For Conf files only process overrides that match our file name
487
500
if let Some ( config) = config. config_overrides . get ( file) {
488
501
for ( key, value) in config {
489
- final_properties . insert ( key. clone ( ) , Some ( value. clone ( ) ) ) ;
502
+ final_overrides . insert ( key. clone ( ) , Some ( value. clone ( ) ) ) ;
490
503
}
491
504
}
492
505
493
- Ok ( final_properties )
506
+ Ok ( final_overrides )
494
507
}
495
508
496
509
#[ cfg( test) ]
@@ -1085,7 +1098,7 @@ mod tests {
1085
1098
) ,
1086
1099
PropertyNameKind :: Cli =>
1087
1100
collection!(
1088
- "cli" . to_string( ) => Some ( GROUP_CLI . to_string( ) ) ,
1101
+ "cli" . to_string( ) => Some ( "cli" . to_string( ) ) ,
1089
1102
) ,
1090
1103
}
1091
1104
} ;
0 commit comments