fix: (toToml) renders int as float

This commit fixes the issue where the yaml.Unmarshaller converts all int values into float64, this passes in option to decoder,
which enables conversion of int into .
Signed-off-by: Althaf M <althafm@outlook.com>
pull/13533/head
Althaf M 9 months ago
parent 4aaacfcff1
commit 10d267ed8b

@ -22,6 +22,18 @@ import (
"testing" "testing"
) )
func TestTemplateCmdWithToml(t *testing.T) {
tests := []cmdTestCase{
{
name: "check toToml function rendering",
cmd: fmt.Sprintf("template '%s'", "testdata/testcharts/issue-totoml"),
golden: "output/issue-totoml.txt",
},
}
runTestCmd(t, tests)
}
var chartPath = "testdata/testcharts/subchart" var chartPath = "testdata/testcharts/subchart"
func TestTemplateCmd(t *testing.T) { func TestTemplateCmd(t *testing.T) {

@ -0,0 +1,8 @@
---
# Source: issue-totoml/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: issue-totoml
data: |
key = 13

@ -0,0 +1,3 @@
apiVersion: v2
name: issue-totoml
version: 0.1.0

@ -0,0 +1,6 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: issue-totoml
data: |
{{ .Values.global | toToml }}

@ -18,13 +18,13 @@ package loader
import ( import (
"bytes" "bytes"
"encoding/json"
"github.com/pkg/errors"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/pkg/errors"
"sigs.k8s.io/yaml" "sigs.k8s.io/yaml"
"strings"
"helm.sh/helm/v4/pkg/chart" "helm.sh/helm/v4/pkg/chart"
) )
@ -104,7 +104,10 @@ func LoadFiles(files []*BufferedFile) (*chart.Chart, error) {
} }
case f.Name == "values.yaml": case f.Name == "values.yaml":
c.Values = make(map[string]interface{}) c.Values = make(map[string]interface{})
if err := yaml.Unmarshal(f.Data, &c.Values); err != nil { if err := yaml.Unmarshal(f.Data, &c.Values, func(d *json.Decoder) *json.Decoder {
d.UseNumber()
return d
}); err != nil {
return c, errors.Wrap(err, "cannot load values.yaml") return c, errors.Wrap(err, "cannot load values.yaml")
} }
case f.Name == "values.schema.json": case f.Name == "values.schema.json":

@ -15,8 +15,10 @@ limitations under the License.
package chartutil package chartutil
import ( import (
"encoding/json"
"os" "os"
"path/filepath" "path/filepath"
"reflect"
"sort" "sort"
"strconv" "strconv"
"testing" "testing"
@ -237,9 +239,24 @@ func TestProcessDependencyImportValues(t *testing.T) {
if b := strconv.FormatBool(pv); b != vv { if b := strconv.FormatBool(pv); b != vv {
t.Errorf("failed to match imported bool value %v with expected %v for key %q", b, vv, kk) t.Errorf("failed to match imported bool value %v with expected %v for key %q", b, vv, kk)
} }
case json.Number:
if fv, err := pv.Float64(); err == nil {
if sfv := strconv.FormatFloat(fv, 'f', -1, 64); sfv != vv {
t.Errorf("failed to match imported float value %v with expected %v for key %q", sfv, vv, kk)
}
}
if iv, err := pv.Int64(); err == nil {
if siv := strconv.FormatInt(iv, 10); siv != vv {
t.Errorf("failed to match imported int value %v with expected %v for key %q", siv, vv, kk)
}
}
if pv.String() != vv {
t.Errorf("failed to match imported string value %q with expected %q for key %q", pv, vv, kk)
}
default: default:
if pv != vv { if pv != vv {
t.Errorf("failed to match imported string value %q with expected %q for key %q", pv, vv, kk) t.Errorf("failed to match imported string value %q with expected %q for key %q", pv, vv, kk)
t.Error(reflect.TypeOf(pv))
} }
} }
} }
@ -309,9 +326,14 @@ func TestProcessDependencyImportValuesMultiLevelPrecedence(t *testing.T) {
if s := strconv.FormatFloat(pv, 'f', -1, 64); s != vv { if s := strconv.FormatFloat(pv, 'f', -1, 64); s != vv {
t.Errorf("failed to match imported float value %v with expected %v", s, vv) t.Errorf("failed to match imported float value %v with expected %v", s, vv)
} }
case json.Number:
if pv.String() != vv {
t.Errorf("failed to match imported string value %q with expected %q", pv, vv)
}
default: default:
if pv != vv { if pv != vv {
t.Errorf("failed to match imported string value %q with expected %q", pv, vv) t.Errorf("failed to match imported string value %q with expected %q", pv, vv)
t.Error(reflect.TypeOf(pv))
} }
} }
} }

@ -17,13 +17,13 @@ limitations under the License.
package chartutil package chartutil
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/pkg/errors"
"io" "io"
"os" "os"
"strings"
"github.com/pkg/errors"
"sigs.k8s.io/yaml" "sigs.k8s.io/yaml"
"strings"
"helm.sh/helm/v4/pkg/chart" "helm.sh/helm/v4/pkg/chart"
) )
@ -105,7 +105,10 @@ func tableLookup(v Values, simple string) (Values, error) {
// ReadValues will parse YAML byte data into a Values. // ReadValues will parse YAML byte data into a Values.
func ReadValues(data []byte) (vals Values, err error) { func ReadValues(data []byte) (vals Values, err error) {
err = yaml.Unmarshal(data, &vals) err = yaml.Unmarshal(data, &vals, func(d *json.Decoder) *json.Decoder {
d.UseNumber()
return d
})
if len(vals) == 0 { if len(vals) == 0 {
vals = Values{} vals = Values{}
} }

Loading…
Cancel
Save