diff --git a/.generator/src/generator/formatter.py b/.generator/src/generator/formatter.py index ca3bcb435..18db24214 100644 --- a/.generator/src/generator/formatter.py +++ b/.generator/src/generator/formatter.py @@ -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)}" diff --git a/.generator/src/generator/setup.py b/.generator/src/generator/setup.py index ae4ae3a99..24031fa79 100644 --- a/.generator/src/generator/setup.py +++ b/.generator/src/generator/setup.py @@ -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 diff --git a/.generator/src/generator/utils.py b/.generator/src/generator/utils.py index 3b62661ab..6cacfb84c 100644 --- a/.generator/src/generator/utils.py +++ b/.generator/src/generator/utils.py @@ -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