Skip to content

fix: handle json.Number in parseInt, parseFloat, and parseUint - #2150

Open
toller892 wants to merge 1 commit into
hashicorp:mainfrom
toller892:fix/parse-functions-json-number
Open

fix: handle json.Number in parseInt, parseFloat, and parseUint#2150
toller892 wants to merge 1 commit into
hashicorp:mainfrom
toller892:fix/parse-functions-json-number

Conversation

@toller892

Copy link
Copy Markdown

Problem

The parseInt, parseFloat, and parseUint template functions only accepted string arguments. When JSON data is parsed with json.Decoder.UseNumber() (or returned from Vault with numeric values), numeric values become json.Number — a named string type that Go's template engine cannot auto-convert to string.

This means templates like {{ .Data.ttl | parseInt }} fail when the value comes from a source that uses json.Number.

Fix

Changed the function signatures from func(s string) to func(s interface{}) with type switches that handle both string and json.Number inputs. Unsupported types return a clear error message.

Testing

  • All existing tests pass (template integration tests for parseInt, parseFloat, parseUint)
  • Added 20 new unit tests covering json.Number inputs, empty strings, invalid values, and unsupported types
  • go vet and goimports clean

Fixes #1584

The parseInt, parseFloat, and parseUint template functions only accepted
string arguments. When JSON data is parsed with json.Decoder.UseNumber(),
numeric values become json.Number (a named string type), which Go's
template engine cannot auto-convert to string. This caused these
functions to fail when used with json.Number values from Vault or other
sources that emit json.Number.

Changed the function signatures to accept interface{} and added type
switches to handle both string and json.Number inputs, returning a
clear error for unsupported types.

Fixes hashicorp#1584
@toller892
toller892 requested review from a team as code owners June 1, 2026 18:44
@hashicorp-cla-app

Copy link
Copy Markdown

CLA assistant check

Thank you for your submission! We require that all contributors sign our Contributor License Agreement ("CLA") before we can accept the contribution. Read and sign the agreement

Learn more about why HashiCorp requires a CLA and what the CLA includes


toller892 seems not to be a GitHub user.
You need a GitHub account to be able to sign the CLA.
If you have already a GitHub account, please add the email address used for this commit to your account.

Have you signed the CLA already but the status is still pending? Recheck it.

1 similar comment
@hashicorp-cla-app

Copy link
Copy Markdown

CLA assistant check

Thank you for your submission! We require that all contributors sign our Contributor License Agreement ("CLA") before we can accept the contribution. Read and sign the agreement

Learn more about why HashiCorp requires a CLA and what the CLA includes


toller892 seems not to be a GitHub user.
You need a GitHub account to be able to sign the CLA.
If you have already a GitHub account, please add the email address used for this commit to your account.

Have you signed the CLA already but the status is still pending? Recheck it.

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.

Feature: parseInt and parseFloat should handle json.Number

1 participant