Skip to content

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

chore: update classification match (#11164)

ecmadao77a8d50f1c99
diff --git a/backend/common/db_schema.go b/backend/common/db_schema.go
index 48eba355c3..6b1b6fd612 100644
--- a/backend/common/db_schema.go
+++ b/backend/common/db_schema.go
@@ -1,26 +1,31 @@
package common
import (
+ "fmt"
"regexp"
"strings"
)
-var numberReg = regexp.MustCompile("^[0-9]+$")
+// classificationIDPattern is the pattern for classification, it should joined id (numbers) by "-", like 1-2, 1-2-3.
+const classificationIDPattern = "^[0-9]+(-([0-9])+){0,}"
// GetClassificationAndUserComment parses classification and user comment from the given comment.
func GetClassificationAndUserComment(comment string) (string, string) {
- sections := strings.Split(comment, "-")
- classification := []string{}
- userComment := ""
- for i, section := range sections {
- if numberReg.MatchString(section) {
- classification = append(classification, section)
- } else {
- userComment = strings.Join(sections[i:], "-")
- break
- }
+ classificationIDReg := regexp.MustCompile(classificationIDPattern)
+ classification := classificationIDReg.FindString(comment)
+ if classification == comment {
+ // the extract classification id matches full comment, for example, raw comment is "1-2-3"
+ return classification, ""
}
- return strings.Join(classification, "-"), userComment
+
+ // we will handle "{classification id}-{comment}" and "{classification id} {comment}"
+ classificationIDReg = regexp.MustCompile(fmt.Sprintf(`%s[-|\s]{1}`, classificationIDPattern))
+ classification = classificationIDReg.FindString(comment)
+
+ userComment := strings.TrimPrefix(comment, classification)
+ classification = strings.TrimSuffix(classification, "-")
+ classification = strings.TrimSuffix(classification, " ")
+ return classification, userComment
}
// GetCommentFromClassificationAndUserComment returns the comment from the given classification and user comment.
diff --git a/backend/common/db_schema_test.go b/backend/common/db_schema_test.go
index a66211fa95..01ddda9216 100644
--- a/backend/common/db_schema_test.go
+++ b/backend/common/db_schema_test.go
@@ -12,41 +12,85 @@ func TestClassification(t *testing.T) {
rawComment string
classification string
userComment string
+ rebuildComment string
}{
{
rawComment: "1abc",
classification: "",
userComment: "1abc",
+ rebuildComment: "1abc",
},
{
rawComment: "abc",
classification: "",
userComment: "abc",
+ rebuildComment: "abc",
},
{
rawComment: "-abc",
classification: "",
userComment: "-abc",
+ rebuildComment: "-abc",
},
{
rawComment: "0-abc",
classification: "0",
userComment: "abc",
+ rebuildComment: "0-abc",
},
{
rawComment: "1-2-abc",
classification: "1-2",
userComment: "abc",
+ rebuildComment: "1-2-abc",
+ },
+ {
+ rawComment: "1-2 abc",
+ classification: "1-2",
+ userComment: "abc",
+ rebuildComment: "1-2-abc",
+ },
+ {
+ rawComment: "1 abc",
+ classification: "1",
+ userComment: "abc",
+ rebuildComment: "1-abc",
},
{
rawComment: "1",
classification: "1",
userComment: "",
+ rebuildComment: "1",
+ },
+ {
+ rawComment: "1- 2",
+ classification: "1",
+ userComment: " 2",
+ rebuildComment: "1- 2",
+ },
+ {
+ rawComment: "1 2",
+ classification: "1",
+ userComment: "2",
+ rebuildComment: "1-2",
},
{
rawComment: "1-2",
classification: "1-2",
userComment: "",
+ rebuildComment: "1-2",
+ },
+ {
+ rawComment: "1-2a",
+ classification: "1",
+ userComment: "2a",
+ rebuildComment: "1-2a",
+ },
+ {
+ rawComment: "1:a",
+ classification: "",
+ userComment: "1:a",
+ rebuildComment: "1:a",
},
}
@@ -55,8 +99,8 @@ func TestClassification(t *testing.T) {
classification, userComment := GetClassificationAndUserComment(test.rawComment)
assert.Equal(t, test.classification, classification)
assert.Equal(t, test.userComment, userComment)
- rawComment := GetCommentFromClassificationAndUserComment(classification, userComment)
- assert.Equal(t, test.rawComment, rawComment)
+ rebuildComment := GetCommentFromClassificationAndUserComment(classification, userComment)
+ assert.Equal(t, test.rebuildComment, rebuildComment)
})
}
}