forked from lyz-code/yamlfix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix block scalar mangling bug lyz-code#231
The regex based parsing for fixing comments was breaking block scalars. By using the ruyaml round trip handler, instead the comment formatting now can correctly identify block-scalars and avoid mangling them.
- Loading branch information
Showing
3 changed files
with
160 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from typing import Any, Callable, Iterable, Mapping, Optional | ||
|
||
from typing_extensions import Protocol | ||
|
||
|
||
class ObjectCallback(Protocol): | ||
def __call__(self, value: Any, key: Optional[Any] = None) -> None: | ||
... | ||
|
||
|
||
def walk_object(o: Any, fn: ObjectCallback): | ||
"""Walk a YAML/JSON-like object and call a function on all values""" | ||
|
||
# Call the callback and whatever we received. | ||
fn(o) | ||
|
||
if isinstance(o, Mapping): | ||
# Map type | ||
for key, value in o.items(): | ||
walk_object(value, fn) | ||
elif isinstance(o, Iterable) and not isinstance(o, (bytes, str)): | ||
# List type | ||
for idx, value in enumerate(o): | ||
walk_object(value, fn) |
This file contains 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