Fix clean up of falsy "length" and "repeat" settings
Marco Ricci

Marco Ricci commited on 2024-10-14 23:11:27
Zeige 1 geänderte Dateien mit 23 Einfügungen und 3 Löschungen.


A falsy length behaves like the standard length, and a falsy repeat
setting forces(!) repetition to be disregarded.  So the former
normalizes to 20 and the latter to 0, regardless of any inherited
setting.  In both cases, we can't just remove the setting, we need to
explicitly set the default value.

(For repeat settings, and upper and lower etc. settings too, there's an
additional twist: the value `0.0` (float) is equivalent to `0` (int),
but the former is a type violation.  This means we need to also make
sure to check the type when normalizing, not just the value.)
... ...
@@ -493,7 +493,7 @@ def clean_up_falsy_vault_config_values(  # noqa: C901,PLR0912
493 493
                         )
494 494
                     )
495 495
                     service_obj[key] = ''
496
-            elif key in {'notes', 'key', 'length', 'repeat'}:
496
+            elif key in {'notes', 'key'}:
497 497
                 if not js_truthiness(value):
498 498
                     cleanup_completed.append(
499 499
                         CleanupStep(
... ...
@@ -501,6 +501,24 @@ def clean_up_falsy_vault_config_values(  # noqa: C901,PLR0912
501 501
                         )
502 502
                     )
503 503
                     service_obj.pop(key)
504
+            elif key == 'length':
505
+                if not js_truthiness(value):
506
+                    cleanup_completed.append(
507
+                        CleanupStep(
508
+                            (*path, key), service_obj[key], 'replace', 20
509
+                        )
510
+                    )
511
+                    service_obj[key] = 20
512
+            elif key == 'repeat':
513
+                if not js_truthiness(value) and not (
514
+                    isinstance(value, int) and value == 0
515
+                ):
516
+                    cleanup_completed.append(
517
+                        CleanupStep(
518
+                            (*path, key), service_obj[key], 'replace', 0
519
+                        )
520
+                    )
521
+                    service_obj[key] = 0
504 522
             elif key in {  # noqa: SIM102
505 523
                 'lower',
506 524
                 'upper',
... ...
@@ -509,10 +527,12 @@ def clean_up_falsy_vault_config_values(  # noqa: C901,PLR0912
509 527
                 'dash',
510 528
                 'symbol',
511 529
             }:
512
-                if not js_truthiness(value) and value != 0:
530
+                if not js_truthiness(value) and not (
531
+                    isinstance(value, int) and value == 0
532
+                ):
513 533
                     cleanup_completed.append(
514 534
                         CleanupStep(
515
-                            (*path, key), service_obj[key], 'replace', 0
535
+                            (*path, key), service_obj[key], 'remove', None
516 536
                         )
517 537
                     )
518 538
                     service_obj.pop(key)
519 539