Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: add comments about template formatting functions for clarity (#1296)

Yilong Lib21bc95cc821
diff --git a/api/project.go b/api/project.go
index d6422bdfc6..6dff06f980 100644
--- a/api/project.go
+++ b/api/project.go
@@ -171,7 +171,7 @@ var (
// ValidateRepositoryFilePathTemplate validates the repository file path template.
func ValidateRepositoryFilePathTemplate(filePathTemplate string, tenantMode ProjectTenantMode) error {
- tokens, _ := getTemplateTokens(filePathTemplate)
+ tokens, _ := parseTemplateTokens(filePathTemplate)
tokenMap := make(map[string]bool)
for _, token := range tokens {
tokenMap[token] = true
@@ -202,7 +202,7 @@ func ValidateRepositorySchemaPathTemplate(schemaPathTemplate string, tenantMode
if schemaPathTemplate == "" {
return nil
}
- tokens, _ := getTemplateTokens(schemaPathTemplate)
+ tokens, _ := parseTemplateTokens(schemaPathTemplate)
tokenMap := make(map[string]bool)
for _, token := range tokens {
tokenMap[token] = true
@@ -233,7 +233,7 @@ func ValidateProjectDBNameTemplate(template string) error {
if template == "" {
return nil
}
- tokens, _ := getTemplateTokens(template)
+ tokens, _ := parseTemplateTokens(template)
// Must contain {{DB_NAME}}
hasDBName := false
for _, token := range tokens {
@@ -250,9 +250,10 @@ func ValidateProjectDBNameTemplate(template string) error {
return nil
}
-// FormatTemplate formats the template.
+// FormatTemplate formats the template by using the tokens as a replacement mapping.
+// Note that the returned (modified) template should not be used as a regexp.
func FormatTemplate(template string, tokens map[string]string) (string, error) {
- keys, _ := getTemplateTokens(template)
+ keys, _ := parseTemplateTokens(template)
for _, key := range keys {
if _, ok := tokens[key]; !ok {
return "", fmt.Errorf("token %q not found", key)
@@ -262,18 +263,21 @@ func FormatTemplate(template string, tokens map[string]string) (string, error) {
return template, nil
}
-func buildTemplateExpr(template string, tokens map[string]string) (string, error) {
- keys, fixed := getTemplateTokens(template)
+// Similar to FormatTemplate, except that it will also escape special regexp characters in the delimiters
+// of the template string, which will produce the correct regexp string.
+func formatTemplateRegexp(template string, tokens map[string]string) (string, error) {
+ keys, delimiters := parseTemplateTokens(template)
for _, key := range keys {
if _, ok := tokens[key]; !ok {
return "", fmt.Errorf("token %q not found", key)
}
template = strings.ReplaceAll(template, key, tokens[key])
}
- for _, key := range fixed {
- quoteKey := regexp.QuoteMeta(key)
- if quoteKey != key {
- template = strings.Replace(template, key, quoteKey, 1)
+ for _, delimiter := range delimiters {
+ quoted := regexp.QuoteMeta(delimiter)
+ if quoted != delimiter {
+ // The delimiter is a special regexp character, we should escape it.
+ template = strings.Replace(template, delimiter, quoted, 1)
}
}
return template, nil
@@ -301,7 +305,7 @@ func GetBaseDatabaseName(databaseName, dbNameTemplate, labelsJSON string) (strin
}
labelMap["{{DB_NAME}}"] = "(?P<NAME>.+)"
- expr, err := buildTemplateExpr(dbNameTemplate, labelMap)
+ expr, err := formatTemplateRegexp(dbNameTemplate, labelMap)
if err != nil {
return "", fmt.Errorf("FormatTemplate(%q, %+v) failed with error: %v", dbNameTemplate, labelMap, err)
}
@@ -316,18 +320,23 @@ func GetBaseDatabaseName(databaseName, dbNameTemplate, labelsJSON string) (strin
return names[1], nil
}
-func getTemplateTokens(template string) ([]string, []string) {
+// Parses the template and returns template tokens and their delimiters.
+// For example, if the template is "{{DB_NAME}}_hello_{{LOCATION}}", then the tokens will be ["{{DB_NAME}}", "{{LOCATION}}"],
+// and the delimiters will be ["_hello_"].
+// The caller will usually replace the tokens with a normal string, or a regexp. In the latter case, it will be a problem
+// if there are special regexp characters like "$" in the delimiters. The caller should escape the delimiters in such cases.
+func parseTemplateTokens(template string) ([]string, []string) {
r := regexp.MustCompile(`{{[^{}]+}}`)
tokens := r.FindAllString(template, -1)
if len(tokens) > 0 {
split := r.Split(template, -1)
- var fixed []string
+ var delimiters []string
for _, s := range split {
if s != "" {
- fixed = append(fixed, s)
+ delimiters = append(delimiters, s)
}
}
- return tokens, fixed
+ return tokens, delimiters
}
return nil, nil
}
diff --git a/api/project_test.go b/api/project_test.go
index 14eadace8c..28bbfed891 100644
--- a/api/project_test.go
+++ b/api/project_test.go
@@ -8,10 +8,10 @@ import (
func TestGetTemplateTokens(t *testing.T) {
tests := []struct {
- name string
- template string
- tokens []string
- fixed []string
+ name string
+ template string
+ tokens []string
+ delimiters []string
}{
{
"NoToken",
@@ -32,9 +32,9 @@ func TestGetTemplateTokens(t *testing.T) {
}
for _, test := range tests {
- tokens, fixed := getTemplateTokens(test.template)
+ tokens, delimiters := parseTemplateTokens(test.template)
require.Equal(t, test.tokens, tokens)
- require.Equal(t, test.fixed, fixed)
+ require.Equal(t, test.delimiters, delimiters)
}
}
@@ -253,7 +253,7 @@ func TestBuildTemplateExpr(t *testing.T) {
}
for _, test := range tests {
- got, err := buildTemplateExpr(test.template, test.tokens)
+ got, err := formatTemplateRegexp(test.template, test.tokens)
if test.errPart == "" {
require.NoError(t, err)
} else {
diff --git a/api/schema_system.go b/api/schema_system.go
index 4fcc839745..e40f255b16 100644
--- a/api/schema_system.go
+++ b/api/schema_system.go
@@ -167,7 +167,7 @@ func UnmarshalNamingRulePayloadAsTemplate(ruleType SchemaReviewRuleType, payload
}
template := nr.Format
- keys, _ := getTemplateTokens(template)
+ keys, _ := parseTemplateTokens(template)
for _, key := range keys {
if _, ok := TemplateNamingTokens[ruleType][key]; !ok {