-
Notifications
You must be signed in to change notification settings - Fork 11
Convert the if elif for to hcl to switch case #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,33 +8,33 @@ | |
|
|
||
| def to_hcl(obj, indent_level=0): | ||
| """ | ||
| Recursively converts a Python object to an HCL string. | ||
| Recursively converts a Python object to an HCL string using structural pattern matching. | ||
| """ | ||
| indent = " " * indent_level | ||
| if isinstance(obj, dict): | ||
| lines = [] | ||
| for key, value in obj.items(): | ||
| # Recursively format the value | ||
| formatted_value = to_hcl(value, indent_level + 1) | ||
| match obj: | ||
| case dict(): | ||
| lines = [] | ||
| for key, value in obj.items(): | ||
| formatted_value = to_hcl(value, indent_level + 1) | ||
|
|
||
| # If the value is a dictionary, we format it as a block: key = { ... } | ||
| if isinstance(value, dict): | ||
| lines.append(f"{indent}{key} = {{\n{formatted_value}\n{indent}}}") | ||
| else: | ||
| lines.append(f"{indent}{key} = {formatted_value}") | ||
| return "\n".join(lines) | ||
| elif isinstance(obj, list): | ||
| items = [to_hcl(item, 0) for item in obj] | ||
| return f"[{', '.join(items)}]" | ||
| elif isinstance(obj, str): | ||
| safe_str = obj.replace('"', '\\"') | ||
| return f'"{safe_str}"' | ||
| elif isinstance(obj, bool): | ||
| return str(obj).lower() # True -> true | ||
| elif obj is None: | ||
| return "null" | ||
| else: | ||
| return str(obj) | ||
| if isinstance(value, dict): | ||
| lines.append(f"{indent}{key} = {{\n{formatted_value}\n{indent}}}") | ||
| else: | ||
| lines.append(f"{indent}{key} = {formatted_value}") | ||
|
Comment on lines
+20
to
+23
|
||
| return "\n".join(lines) | ||
| case list(): | ||
| items = [to_hcl(item, 0) for item in obj] | ||
| return f"[{', '.join(items)}]" | ||
| case str(): | ||
| safe_str = obj.replace('"', '\\"') | ||
| return f'"{safe_str}"' | ||
| case bool(): | ||
| return str(obj).lower() | ||
| case None: | ||
| return "null" | ||
| case _: | ||
| # This is the 'else' or wildcard case | ||
| return str(obj) | ||
|
|
||
| def get_default_keep_list(env_config, delete_all): | ||
| """ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of structural pattern matching (match-case) requires Python 3.10 or later. Consider adding a python_requires='>=3.10' constraint in setup.py to prevent installation on incompatible Python versions, as the codebase now depends on this feature.