Skip to content

Commit 036061f

Browse files
authored
[Python] Reformat variable regex (#2484)
## Changes Reformat variable regex in Python code to follow Golang. The effective regex is not changed. ## Why We need to keep both regexes in sync. ## Tests Using existing tests and manually inspecting resulting regex.
1 parent b5a499d commit 036061f

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

experimental/python/databricks/bundles/core/_transform.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,17 @@ def _unwrap_variable(tpe: type) -> Optional[type]:
270270
return None
271271

272272

273-
# from cli/libs/dyn/dynvar/ref.go
273+
# Regex for string corresponding to variables.
274+
#
275+
# The source of truth is regex in libs/dyn/dynvar/ref.go
276+
#
277+
# Example:
278+
# - "${a.b}"
279+
# - "${a.b.c}"
280+
# - "${a.b[0].c}"
281+
_base_var_def = r"[a-zA-Z]+([-_]*[a-zA-Z0-9]+)*"
274282
_variable_regex = re.compile(
275-
r"\$\{([a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\.[a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\[[0-9]+\])*)*(\[[0-9]+\])*)\}",
283+
r"\$\{(%s(\.%s(\[[0-9]+\])*)*(\[[0-9]+\])*)\}" % (_base_var_def, _base_var_def)
276284
)
277285

278286

libs/dyn/dynvar/ref.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
)
99

1010
var (
11+
// !!! Should be in sync with _variable_regex in Python code.
12+
// !!!
13+
// !!! See experimental/python/databricks/bundles/core/_transform.py
1114
baseVarDef = `[a-zA-Z]+([-_]*[a-zA-Z0-9]+)*`
1215
re = regexp.MustCompile(fmt.Sprintf(`\$\{(%s(\.%s(\[[0-9]+\])*)*(\[[0-9]+\])*)\}`, baseVarDef, baseVarDef))
1316
)
@@ -33,6 +36,7 @@ type ref struct {
3336
// Examples of a valid variable references:
3437
// - "${a.b}"
3538
// - "${a.b.c}"
39+
// - "${a.b[0].c}"
3640
// - "${a} ${b} ${c}"
3741
func newRef(v dyn.Value) (ref, bool) {
3842
s, ok := v.AsString()

0 commit comments

Comments
 (0)