From 87f26f69e5b6f3e49a419b9c91001ada5eaf77b9 Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Mon, 27 Jan 2025 15:34:57 +0000 Subject: [PATCH] fixes --- .../variables/double_underscore/output.txt | 14 ++++++-------- .../bundle/variables/double_underscore/script | 2 +- libs/dyn/dynvar/ref.go | 19 +++++-------------- libs/dyn/dynvar/ref_test.go | 3 ++- 4 files changed, 14 insertions(+), 24 deletions(-) diff --git a/acceptance/bundle/variables/double_underscore/output.txt b/acceptance/bundle/variables/double_underscore/output.txt index 8f319c369..45529038d 100644 --- a/acceptance/bundle/variables/double_underscore/output.txt +++ b/acceptance/bundle/variables/double_underscore/output.txt @@ -1,9 +1,7 @@ ->>> $CLI bundle validate -Name: double_underscore -Target: default -Workspace: - User: $USERNAME - Path: /Workspace/Users/$USERNAME/.bundle/double_underscore/default - -Validation OK! +>>> $CLI bundle validate -o json +[ + { + "task_key": "test default" + } +] diff --git a/acceptance/bundle/variables/double_underscore/script b/acceptance/bundle/variables/double_underscore/script index 535087615..a7394df77 100644 --- a/acceptance/bundle/variables/double_underscore/script +++ b/acceptance/bundle/variables/double_underscore/script @@ -1 +1 @@ -trace $CLI bundle validate +trace $CLI bundle validate -o json | jq .resources.jobs.test_job.tasks diff --git a/libs/dyn/dynvar/ref.go b/libs/dyn/dynvar/ref.go index 11c8d05e6..ba397267a 100644 --- a/libs/dyn/dynvar/ref.go +++ b/libs/dyn/dynvar/ref.go @@ -1,13 +1,16 @@ package dynvar import ( + "fmt" "regexp" - "strings" "github.com/databricks/cli/libs/dyn" ) -var re = regexp.MustCompile(`\$\{([a-zA-Z]+([-_]*[a-zA-Z0-9]+)*(\.[a-zA-Z]+([-_]*[a-zA-Z0-9]+)*(\[[0-9]+\])*)*(\[[0-9]+\])*)\}`) +var ( + baseVarDef = `[a-zA-Z]+([-_]*[a-zA-Z0-9]+)*` + re = regexp.MustCompile(fmt.Sprintf(`\$\{(%s(\.%s(\[[0-9]+\])*)*(\[[0-9]+\])*)\}`, baseVarDef, baseVarDef)) +) // ref represents a variable reference. // It is a string [dyn.Value] contained in a larger [dyn.Value]. @@ -23,8 +26,6 @@ type ref struct { matches [][]string } -var invalidSeq = []string{"-_", "_-"} - // newRef returns a new ref if the given [dyn.Value] contains a string // with one or more variable references. It returns false if the given // [dyn.Value] does not contain variable references. @@ -45,16 +46,6 @@ func newRef(v dyn.Value) (ref, bool) { return ref{}, false } - // Check that it does not have invalid sequences such as "-_" or "_-". - - for _, match := range m { - for _, seq := range invalidSeq { - if strings.Contains(match[1], seq) { - return ref{}, false - } - } - } - return ref{ value: v, str: s, diff --git a/libs/dyn/dynvar/ref_test.go b/libs/dyn/dynvar/ref_test.go index 96d8b26bb..637ecb98e 100644 --- a/libs/dyn/dynvar/ref_test.go +++ b/libs/dyn/dynvar/ref_test.go @@ -20,6 +20,8 @@ func TestNewRefValidPattern(t *testing.T) { "${hello-world.world-world}": {"hello-world.world-world"}, "${hello_world.world__world}": {"hello_world.world__world"}, "${hello_world.world--world}": {"hello_world.world--world"}, + "${hello_world.world-_world}": {"hello_world.world-_world"}, + "${hello_world.world_-world}": {"hello_world.world_-world"}, } { ref, ok := newRef(dyn.V(in)) require.True(t, ok, "should match valid pattern: %s", in) @@ -38,7 +40,6 @@ func TestNewRefInvalidPattern(t *testing.T) { "${_-_._-_.id}", // cannot use _- in sequence "${0helloworld.world-world}", // interpolated first section shouldn't start with number "${helloworld.9world-world}", // interpolated second section shouldn't start with number - "${a-a.a-_a-a.id}", // fails because of -_ in the second segment } for _, v := range invalid { _, ok := newRef(dyn.V(v))