Skip to content

Commit

Permalink
[Generator] (feat) Modified the previously added filter to be more ge…
Browse files Browse the repository at this point in the history
…neric

added a printing filter
  • Loading branch information
Supam committed Feb 25, 2025
1 parent 343e882 commit 5541d48
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
22 changes: 15 additions & 7 deletions .generator/src/generator/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,26 @@ def get_terraform_schema_type(schema):
}[schema.get("type")]


def date_time_formatter(name: str, schema: dict) -> str:
def go_to_terraform_type_formatter(name: str, schema: dict) -> str:
"""
This function is intended to be used in the Jinja2 templates.
It was made to support the "date-time" format of the OpenAPI schema.
Go's time.Time type is used to represent date-time values and should be instead transformed into a string.
It was made to support the format enrichment of the OpenAPI schema.
The format enrichment allows for a more appropriate Go type to be used in the provider (eg: string + date-time enrichment -> time.Time).
However when updating the state we wish to use the primitive type that Terraform support instead.
Args:
name (str): The name of the variable to format.
schema (dict): OpenApi spec as a dictionary. May contain a "format" key.
Returns:
str: The string representation of the variable in Go.
"""

if schema.get("format") == "date-time":
return f"{variable_name(name)}.String()"
return f"*{variable_name(name)}"
match schema.get("format"):
case "date-time":
return f"{variable_name(name)}.String()"
case "date":
return f"{variable_name(name)}.String()"
case "binary":
return f"string({variable_name(name)})"

# primitive types should fall through
case _:
return f"*{variable_name(name)}"
3 changes: 2 additions & 1 deletion .generator/src/generator/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ def load_environment(version: str) -> Environment:
env.filters["snake_case"] = formatter.snake_case
env.filters["untitle_case"] = formatter.untitle_case
env.filters["variable_name"] = formatter.variable_name
env.filters["date_time_formatter"] = formatter.date_time_formatter
env.filters["date_time_formatter"] = formatter.go_to_terraform_type_formatter
env.filters["parameter_schema"] = openapi.parameter_schema
env.filters["parameters"] = openapi.parameters
env.filters["is_json_api"] = openapi.is_json_api
env.filters["capitalize"] = utils.capitalize
env.filters["is_primitive"] = utils.is_primitive
env.filters["print"] = utils.print_filter
env.filters["response_type"] = type.get_type_for_response
env.filters["return_type"] = type.return_type
env.filters["tf_sort_params_by_type"] = type.tf_sort_params_by_type
Expand Down
5 changes: 5 additions & 0 deletions .generator/src/generator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,8 @@ def is_enum(schema):

def is_nullable(schema):
return schema.get("nullable", False)


def print_filter(value):
print(value)
return value

0 comments on commit 5541d48

Please sign in to comment.