Skip to content

Add support for preserving scientific notation for floats #225

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

Closed

Conversation

eranor
Copy link
Contributor

@eranor eranor commented Apr 17, 2025

Problem

With the hcl2.reverse_transform isn't capable of handling floats.

Implementation

Added case within HCLReverseTransformer._transform_value_to_expr_term to properly generate the expr_term for floats.

Added tests for various float representations to ensure functionality, including edge cases with scientific notation and negative decimals.

Introduced mechanisms to handle and preserve scientific notation formats across parsing, transformation, and reconstruction processes.

Introduced mechanisms to handle and preserve scientific notation formats across parsing, transformation, and reconstruction processes.

Added tests for various float representations to ensure functionality, including edge cases with scientific notation and negative decimals.
@kkozik-amplify
Copy link
Collaborator

Hi! Thank you for looking into the issue and taking time to prepare a PR.
I was able to come up with another approach in #226. Instead of transforming e-notation numbers into custom JSON objects, it's more consistent to transform them into string interpolations, e.g. "${-5.1e2}".
Besides not introducing a custom structure and metadata, the practical advantage of that is terraform compatibility - let's consider floats.json file consisting of e-notation numbers saved in both formats:

{
 "floats": {
  "inline_scientific": [
   "${-5.1e2}",
   "${-3.7e1}"
  ],
  "object_scientific": [
   {
    "__sci_float__": true,
    "value": -510.0,
    "format": "-5.1e2"
   },
   {
    "__sci_float__": true,
    "value": -37.0,
    "format": "-3.7e1"
   }
  ]
 }
}

and try to do the below in terraform:

output "o1" {
  value = sum(jsondecode(templatefile("./floats.json", {}))["floats"]["inline_scientific"])
}

output "o2" {
  value = sum(jsondecode(templatefile("./floats.json", {}))["floats"]["object_scientific"])
}

templatefile call will read the file and automatically evalute string interpolations, in our case this results in "${-5.1e2}" string being evaluated into actual number. This means that the o1 output will be able to calculate sum of inline_scientific list, while o2 will fail, as object_scientific is a list of objects.
I understand the purpose of preserve_format flag, however this seems like a compromise we can avoid by using terraform-friendly string interpolations.

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