diff --git a/libs/dyn/dynvar/ref.go b/libs/dyn/dynvar/ref.go index de25f44a212..29c24ce4bfa 100644 --- a/libs/dyn/dynvar/ref.go +++ b/libs/dyn/dynvar/ref.go @@ -4,6 +4,7 @@ import ( "fmt" "regexp" "strings" + "sync" "github.com/databricks/cli/libs/dyn" ) @@ -15,7 +16,9 @@ import ( // Behavioral parity is enforced by testdata/reference_vectors.json. const BaseVarDef = `_*\p{L}+([-_]*[\p{L}\p{N}]+)*` -var re = regexp.MustCompile(fmt.Sprintf(`\$\{(%s(\.%s(\[[0-9]+\])*)*(\[[0-9]+\])*)\}`, BaseVarDef, BaseVarDef)) +var re = sync.OnceValue(func() *regexp.Regexp { + return 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]. @@ -56,7 +59,7 @@ func NewRef(v dyn.Value) (Ref, bool) { } // Find all matches with their byte positions so we can detect escaped references. - indices := re.FindAllStringSubmatchIndex(s, -1) + indices := re().FindAllStringSubmatchIndex(s, -1) if len(indices) == 0 { return Ref{}, false } @@ -146,7 +149,7 @@ func (v Ref) References() []string { } func IsPureVariableReference(s string) bool { - return len(s) > 0 && re.FindString(s) == s + return len(s) > 0 && re().FindString(s) == s } // ContainsVariableReference reports whether s contains at least one reference that @@ -154,7 +157,7 @@ func IsPureVariableReference(s string) bool { // so an escaped "$${foo}" must not count: it is a literal, and treating it as // pending leaves the value permanently unresolved. func ContainsVariableReference(s string) bool { - for _, loc := range re.FindAllStringIndex(s, -1) { + for _, loc := range re().FindAllStringIndex(s, -1) { if !isEscapedAt(s, loc[0]) { return true }