Skip to content

Convert the if elif for to hcl to switch case#54

Open
maximenoel8 wants to merge 1 commit into
uyuni-project:masterfrom
maximenoel8:use_switch_case_tohcl
Open

Convert the if elif for to hcl to switch case#54
maximenoel8 wants to merge 1 commit into
uyuni-project:masterfrom
maximenoel8:use_switch_case_tohcl

Conversation

@maximenoel8

Copy link
Copy Markdown
Contributor

What does this PR?

Code improvement of to hcl.
Still need to be verified

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the to_hcl function in terracumber/tfvars_cleaner.py by converting an if-elif-else chain to Python's structural pattern matching (match-case statement), which was introduced in Python 3.10. The refactoring modernizes the code while maintaining functional equivalence.

Changes:

  • Converted if-elif-else type checking chain to match-case statement using structural pattern matching
  • Updated the function docstring to reflect the use of structural pattern matching
  • Removed some inline comments during the refactoring

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +20 to +23
if isinstance(value, dict):
lines.append(f"{indent}{key} = {{\n{formatted_value}\n{indent}}}")
else:
lines.append(f"{indent}{key} = {formatted_value}")

Copilot AI Feb 5, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The helpful comment explaining the dict block formatting logic was removed during refactoring. Consider preserving the comment 'If the value is a dictionary, we format it as a block: key = { ... }' as it explains the formatting decision, which may not be immediately obvious to future maintainers.

Copilot uses AI. Check for mistakes.
for key, value in obj.items():
# Recursively format the value
formatted_value = to_hcl(value, indent_level + 1)
match obj:

Copilot AI Feb 5, 2026

Copy link

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants