Skip to content

Negative integers don't load correctly #102

Closed

Description

@wsams

I'm working on a project using JSON patches written in HCL. Something like the following,

# sample.hcl
patch_data = [
	{
		op = "add"
		path = "/path"
		value = {
			sample = {
				number = {
					negative = -1
				}
			}
		}
	}
]

When I load this file with python-hcl2 v3.0.5 and Python v3.10.4,

import hcl2

with open("sample.hcl", "r") as rf:
    hcl = hcl2.load(rf)
    print(hcl)

and print the contents it returns the -1 wrapped in a variable syntax. Something like this,

{
  "patch_data": [
    {
      "op": "add",
      "path": "/path",
      "value": {
        "sample": {
          "number": {
            "negative": "${-1}"
          }
        }
      }
    }
  ]
}

Does this look like a bug, or is there another way to load this file?

Activity

  1. htorianik commented on Jan 10, 2023

    @htorianik
    Contributor

    Hi, @wsams! The thing is -1 is not an integer literal but an expression with a unary operator '-'. Every numeric expression will be transformed into a dollar-sign string.

  2. wsams commented on Jan 10, 2023

    @wsams
    Author

    Hi, @wsams! The thing is -1 is not an integer literal but an expression with a unary operator '-'. Every numeric expression will be transformed into a dollar-sign string.

    Is this a limitation of HCL, or am I supposed to perform the interpolation myself on these dollar-sign variables? It probably wouldn't be hard to replace ${-1} with a literal -1 in the resulting JSON but it really seems like a bug in this library. I'm really confused why this isn't supported unless HCL doesn't support negative number types.

  3. jerschne commented on Jan 17, 2023

    @jerschne

    Hi, @wsams! The thing is -1 is not an integer literal but an expression with a unary operator '-'. Every numeric expression will be transformed into a dollar-sign string.

    I really don't understand why -1 is not an integer? https://github.com/kvz/json2hcl and Terrraform interpreted it correctly as an integer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions