fix: handle json.Number in parseInt, parseFloat, and parseUint - #2150
fix: handle json.Number in parseInt, parseFloat, and parseUint#2150toller892 wants to merge 1 commit into
Conversation
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
|
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. Have you signed the CLA already but the status is still pending? Recheck it. |
1 similar comment
|
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. Have you signed the CLA already but the status is still pending? Recheck it. |
Problem
The
parseInt,parseFloat, andparseUinttemplate functions only acceptedstringarguments. When JSON data is parsed withjson.Decoder.UseNumber()(or returned from Vault with numeric values), numeric values becomejson.Number— a named string type that Go's template engine cannot auto-convert tostring.This means templates like
{{ .Data.ttl | parseInt }}fail when the value comes from a source that usesjson.Number.Fix
Changed the function signatures from
func(s string)tofunc(s interface{})with type switches that handle bothstringandjson.Numberinputs. Unsupported types return a clear error message.Testing
parseInt,parseFloat,parseUint)json.Numberinputs, empty strings, invalid values, and unsupported typesgo vetandgoimportscleanFixes #1584