hashicorp / hashicorp/consul-template

parseJSON | toTOML incorrectly casts integers to float

Open
#1,296 4 comments 1 reaction 1 assignee Claimed by @eikenb View on GitHub
bug waiting-reply
Dominant language
Go
Stars
4.8k
Forks
801
Avg merge
4h 5m
Merged PRs (30d)
6

Description

Tested with Consul v0.22.0

When using the parseJSON and toTOML functions together, integers are incorrectly cast to floats

The following template:

```
{{ scratch.MapSet "vars" "foo" 1 }}
{{ scratch.Get "vars" | toJSON }}
{{ scratch.Get "vars" | toTOML }}
{{ scratch.Get "vars" | toJSON | parseJSON | toTOML }}

{{ $json := "{\"foo\": 1}" }}
{{ $json }}
{{ ($json | parseJSON).foo }}
{{ $json | parseJSON | toTOML }}
```

Produces this output:

```
{"foo":1}
foo = 1
foo = 1.0

{"foo": 1}
1
foo = 1.0
```

I have not tested on earlier versions of consul-template.

As a workaround, I've been using regexReplaceAll to strip floating 0s.

```
{{ $json | parseJSON | toTOML | regexReplaceAll "\\.0\n" "\n" }}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.