Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: get classification from comment (#11029)
ecmadaocd85af99dd96
diff --git a/backend/common/db_schema.go b/backend/common/db_schema.goindex b3f6f213a4..48eba355c3 100644--- a/backend/common/db_schema.go+++ b/backend/common/db_schema.go@@ -5,17 +5,22 @@ import ("strings")-var getClassificationFromCommentReg = regexp.MustCompile("^(([0-9]+)-)+")+var numberReg = regexp.MustCompile("^[0-9]+$")// GetClassificationAndUserComment parses classification and user comment from the given comment.func GetClassificationAndUserComment(comment string) (string, string) {- classification := getClassificationFromCommentReg.FindString(comment)- if classification == "" {- return "", comment+ 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+ }}- userComment := strings.TrimPrefix(comment, classification)- classification = strings.TrimSuffix(classification, "-")- return classification, userComment+ return strings.Join(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.gonew file mode 100644index 0000000000..a66211fa95--- /dev/null+++ b/backend/common/db_schema_test.go@@ -0,0 +1,62 @@+package common++import (+ "fmt"+ "testing"++ "github.com/stretchr/testify/assert"+)++func TestClassification(t *testing.T) {+ tests := []struct {+ rawComment string+ classification string+ userComment string+ }{+ {+ rawComment: "1abc",+ classification: "",+ userComment: "1abc",+ },+ {+ rawComment: "abc",+ classification: "",+ userComment: "abc",+ },+ {+ rawComment: "-abc",+ classification: "",+ userComment: "-abc",+ },+ {+ rawComment: "0-abc",+ classification: "0",+ userComment: "abc",+ },+ {+ rawComment: "1-2-abc",+ classification: "1-2",+ userComment: "abc",+ },+ {+ rawComment: "1",+ classification: "1",+ userComment: "",+ },+ {+ rawComment: "1-2",+ classification: "1-2",+ userComment: "",+ },+ }++ for _, test := range tests {+ t.Run(fmt.Sprintf("test classification for comment: %v", test.rawComment), func(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)+ })+ }+}