calibre-web-automated: keep large integer quantities out of exponential notation

The null-safe fallback helpers from #99 pass values through fromYaml, which
parses every number as float64. Go's "%v" switches float64 to exponential
notation from about 1e6 upwards, so a bare byte count like 1048576 reached
multiplyQuantity as "1.048576e+06" and the render aborted on the suffix check.

Route both the helper's input and its non-integral product through a new
formatNumber helper, which forces decimal notation and trims the padding
zeros again. Strings such as "512Mi" pass through untouched.

Part of #157.
This commit is contained in:
jklein committed 2026-08-29 00:19:13 +02:00
1 parent 52a02c5472
commit 5cc53b0772
1 file changed
+19 -2
+19 -2
View File
@@ -67,7 +67,7 @@ Handles plain numbers ("2", "0.5", 1073741824), decimal ("1.5Gi"), binary
results are printed without a decimal point. results are printed without a decimal point.
*/}} */}}
{{- define "calibre-web-automated.multiplyQuantity" -}} {{- define "calibre-web-automated.multiplyQuantity" -}}
{{- $quantity := printf "%v" .quantity -}} {{- $quantity := include "calibre-web-automated.formatNumber" .quantity -}}
{{- $number := regexFind "^[0-9]+(\\.[0-9]+)?" $quantity -}} {{- $number := regexFind "^[0-9]+(\\.[0-9]+)?" $quantity -}}
{{- if not $number -}} {{- if not $number -}}
{{- fail (printf "cannot parse resource quantity %q" $quantity) -}} {{- fail (printf "cannot parse resource quantity %q" $quantity) -}}
@@ -80,7 +80,24 @@ results are printed without a decimal point.
{{- if eq $product (floor $product) -}} {{- if eq $product (floor $product) -}}
{{- printf "%d%s" (int64 $product) $suffix -}} {{- printf "%d%s" (int64 $product) $suffix -}}
{{- else -}} {{- else -}}
{{- printf "%v%s" $product $suffix -}} {{- printf "%s%s" (include "calibre-web-automated.formatNumber" $product) $suffix -}}
{{- end -}}
{{- end -}}
{{/*
Render a number as a plain decimal string, never in exponential notation.
Why this exists (issue #157): the null-safe fallback helpers pass values
through fromYaml, which parses every number as float64. Go's "%v" switches
float64 to exponential notation from about 1e6 upwards, so a bare byte count
like 1048576 became "1.048576e+06" - not a valid Kubernetes quantity, and the
render aborted. Strings ("512Mi") are passed through untouched.
*/}}
{{- define "calibre-web-automated.formatNumber" -}}
{{- if kindIs "float64" . -}}
{{- regexReplaceAll "\\.?0+$" (printf "%.10f" .) "" -}}
{{- else -}}
{{- printf "%v" . -}}
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}