From 66307134c1d34e34fc64e71fcc5c01acf819e677 Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Wed, 11 Sep 2024 11:49:58 +0200 Subject: [PATCH] Fixed generated YAML missing 'default' for empty values (#1765) ## Changes Fixed generated YAML missing 'default' for empty values ## Tests Added unit test --- bundle/config/generate/job.go | 14 ++++++++++++++ cmd/bundle/generate/generate_test.go | 9 +++++++++ libs/dyn/yamlsaver/saver.go | 2 ++ 3 files changed, 25 insertions(+) diff --git a/bundle/config/generate/job.go b/bundle/config/generate/job.go index 28bc864122..6cd7c1b328 100644 --- a/bundle/config/generate/job.go +++ b/bundle/config/generate/job.go @@ -25,6 +25,20 @@ func ConvertJobToValue(job *jobs.Job) (dyn.Value, error) { value["tasks"] = dyn.NewValue(tasks, []dyn.Location{{Line: jobOrder.Get("tasks")}}) } + // We're processing job.Settings.Parameters separately to retain empty default values. + if len(job.Settings.Parameters) > 0 { + params := make([]dyn.Value, 0) + for _, parameter := range job.Settings.Parameters { + p := map[string]dyn.Value{ + "name": dyn.NewValue(parameter.Name, []dyn.Location{{Line: 0}}), // We use Line: 0 to ensure that the name goes first. + "default": dyn.NewValue(parameter.Default, []dyn.Location{{Line: 1}}), + } + params = append(params, dyn.NewValue(p, []dyn.Location{})) + } + + value["parameters"] = dyn.NewValue(params, []dyn.Location{{Line: jobOrder.Get("parameters")}}) + } + return yamlsaver.ConvertToMapValue(job.Settings, jobOrder, []string{"format", "new_cluster", "existing_cluster_id"}, value) } diff --git a/cmd/bundle/generate/generate_test.go b/cmd/bundle/generate/generate_test.go index ae3710ac84..7de6805fb8 100644 --- a/cmd/bundle/generate/generate_test.go +++ b/cmd/bundle/generate/generate_test.go @@ -152,6 +152,12 @@ func TestGenerateJobCommand(t *testing.T) { }, }, }, + Parameters: []jobs.JobParameterDefinition{ + { + Name: "empty", + Default: "", + }, + }, }, }, nil) @@ -198,6 +204,9 @@ func TestGenerateJobCommand(t *testing.T) { - task_key: notebook_task notebook_task: notebook_path: %s + parameters: + - name: empty + default: "" `, filepath.Join("..", "src", "notebook.py")), string(data)) data, err = os.ReadFile(filepath.Join(srcDir, "notebook.py")) diff --git a/libs/dyn/yamlsaver/saver.go b/libs/dyn/yamlsaver/saver.go index f4c7157f2e..0fd81d5341 100644 --- a/libs/dyn/yamlsaver/saver.go +++ b/libs/dyn/yamlsaver/saver.go @@ -151,6 +151,8 @@ func isScalarValueInString(v dyn.Value) bool { switch v.MustString() { case "true", "false": return true + case "": + return true default: _, err := parseNumber(v.MustString()) return err == nil