Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions libs/dyn/dynvar/ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"regexp"
"strings"
"sync"

"github.com/databricks/cli/libs/dyn"
)
Expand All @@ -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].
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -146,15 +149,15 @@ 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
// is not escaped. Callers use it to decide whether a string still needs resolution,
// 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
}
Expand Down
Loading