diff --git a/NEXT_CHANGELOG.md b/NEXT_CHANGELOG.md index c196e389c0..8fc3c33c3f 100644 --- a/NEXT_CHANGELOG.md +++ b/NEXT_CHANGELOG.md @@ -5,5 +5,6 @@ ### CLI ### Bundles +* Fix float precision loss when parsing number-type template variables during `bundle init` (denik/random-bugfixes-3) ### Dependency updates diff --git a/acceptance/bundle/templates-machinery/number-precision/databricks_template_schema.json b/acceptance/bundle/templates-machinery/number-precision/databricks_template_schema.json new file mode 100644 index 0000000000..9a1bfb55a5 --- /dev/null +++ b/acceptance/bundle/templates-machinery/number-precision/databricks_template_schema.json @@ -0,0 +1,9 @@ +{ + "properties": { + "n": { + "type": "number", + "description": "A number variable", + "default": 1.1 + } + } +} diff --git a/acceptance/bundle/templates-machinery/number-precision/out.test.toml b/acceptance/bundle/templates-machinery/number-precision/out.test.toml new file mode 100644 index 0000000000..d560f1de04 --- /dev/null +++ b/acceptance/bundle/templates-machinery/number-precision/out.test.toml @@ -0,0 +1,5 @@ +Local = true +Cloud = false + +[EnvMatrix] + DATABRICKS_BUNDLE_ENGINE = ["terraform", "direct"] diff --git a/acceptance/bundle/templates-machinery/number-precision/output.txt b/acceptance/bundle/templates-machinery/number-precision/output.txt new file mode 100644 index 0000000000..7b07f8a5b5 --- /dev/null +++ b/acceptance/bundle/templates-machinery/number-precision/output.txt @@ -0,0 +1,6 @@ + +>>> [CLI] bundle init . +✨ Successfully initialized template + +>>> cat number.txt +n: 1.1 diff --git a/acceptance/bundle/templates-machinery/number-precision/script b/acceptance/bundle/templates-machinery/number-precision/script new file mode 100644 index 0000000000..e15c9e54dc --- /dev/null +++ b/acceptance/bundle/templates-machinery/number-precision/script @@ -0,0 +1,4 @@ +trace $CLI bundle init . + +trace cat number.txt +rm number.txt diff --git a/acceptance/bundle/templates-machinery/number-precision/template/number.txt.tmpl b/acceptance/bundle/templates-machinery/number-precision/template/number.txt.tmpl new file mode 100644 index 0000000000..cb1344deb3 --- /dev/null +++ b/acceptance/bundle/templates-machinery/number-precision/template/number.txt.tmpl @@ -0,0 +1 @@ +n: {{ .n }} diff --git a/libs/jsonschema/utils.go b/libs/jsonschema/utils.go index b9df7da515..cb25393075 100644 --- a/libs/jsonschema/utils.go +++ b/libs/jsonschema/utils.go @@ -110,7 +110,7 @@ func fromString(s string, T Type) (any, error) { case BooleanType: v, err = strconv.ParseBool(s) case NumberType: - v, err = strconv.ParseFloat(s, 32) + v, err = strconv.ParseFloat(s, 64) case IntegerType: v, err = strconv.ParseInt(s, 10, 64) case ArrayType, ObjectType: diff --git a/libs/jsonschema/utils_test.go b/libs/jsonschema/utils_test.go index 954c723d3f..d0a46e6788 100644 --- a/libs/jsonschema/utils_test.go +++ b/libs/jsonschema/utils_test.go @@ -95,8 +95,7 @@ func TestTemplateFromString(t *testing.T) { v, err = fromString("1.1", NumberType) assert.NoError(t, err) - // Floating point conversions are not perfect - assert.Less(t, (v.(float64) - 1.1), 0.000001) + assert.Equal(t, 1.1, v) v, err = fromString("12345", IntegerType) assert.NoError(t, err) @@ -104,7 +103,7 @@ func TestTemplateFromString(t *testing.T) { v, err = fromString("123", NumberType) assert.NoError(t, err) - assert.InDelta(t, float64(123), v.(float64), 0.0001) + assert.Equal(t, float64(123), v) _, err = fromString("qrt", ArrayType) assert.EqualError(t, err, "cannot parse string as object of type array. Value of string: \"qrt\"")