Change the comment ID to use both bug and comment ID references.

vince created

Change summary

bug/comment.go        | 32 ++++++++++++++++++++++++++++++++
bug/comment_test.go   | 24 ++++++++++++++++++++++++
bug/op_add_comment.go |  2 +-
bug/op_create.go      |  2 +-
4 files changed, 58 insertions(+), 2 deletions(-)

Detailed changes

bug/comment.go 🔗

@@ -21,6 +21,8 @@ type Comment struct {
 	UnixTime timestamp.Timestamp
 }
 
+const compiledCommentIdFormat = "BCBCBCBBBCBBBBCBBBBCBBBBCBBBBCBBBBCBBBBC"
+
 // Id return the Comment identifier
 func (c Comment) Id() entity.Id {
 	if c.id == "" {
@@ -31,6 +33,36 @@ func (c Comment) Id() entity.Id {
 	return c.id
 }
 
+func CompileCommentId(bugId string, commentId string) string {
+	commentIdString := commentId
+	bugIdString := bugId
+	id := ""
+	for _, char := range compiledCommentIdFormat {
+		if char == 'B' {
+			id += string(bugIdString[0])
+			bugIdString = bugIdString[1:]
+		} else {
+			id += string(commentIdString[0])
+			commentIdString = commentIdString[1:]
+		}
+	}
+	return id
+}
+
+func UnpackCommentId(id string) (string, string) {
+	commentIdPrefix := ""
+	bugIdPrefix := ""
+
+	for i, char := range id {
+		if compiledCommentIdFormat[i] == 'B' {
+			bugIdPrefix += string(char)
+		} else {
+			commentIdPrefix += string(char)
+		}
+	}
+	return bugIdPrefix, commentIdPrefix
+}
+
 // FormatTimeRel format the UnixTime of the comment for human consumption
 func (c Comment) FormatTimeRel() string {
 	return humanize.Time(c.UnixTime.Time())

bug/comment_test.go 🔗

@@ -0,0 +1,24 @@
+package bug
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestCompileUnpackCommentId(t *testing.T) {
+	id1 := "abcdefghijklmnopqrstuvwxyz1234"
+	id2 := "ABCDEFGHIJ"
+	expectedId := "aAbBcCdefDghijEklmnFopqrGstuvHwxyzI1234J"
+
+	compiledId := CompileCommentId(id1, id2)
+	assert.Equal(t, expectedId, compiledId)
+
+	unpackedId1, unpackedId2 := UnpackCommentId(compiledId)
+	assert.Equal(t, id1, unpackedId1)
+	assert.Equal(t, id2, unpackedId2)
+
+	unpackedId1, unpackedId2 = UnpackCommentId(expectedId[:6])
+	assert.Equal(t, unpackedId1, id1[:3])
+	assert.Equal(t, unpackedId2, id2[:3])
+}

bug/op_add_comment.go 🔗

@@ -37,7 +37,7 @@ func (op *AddCommentOperation) Apply(snapshot *Snapshot) {
 	snapshot.addParticipant(op.Author)
 
 	comment := Comment{
-		id:       op.Id(),
+		id:       entity.Id(CompileCommentId(snapshot.Id().String(), op.Id().String())),
 		Message:  op.Message,
 		Author:   op.Author,
 		Files:    op.Files,

bug/op_create.go 🔗

@@ -40,7 +40,7 @@ func (op *CreateOperation) Apply(snapshot *Snapshot) {
 	snapshot.Title = op.Title
 
 	comment := Comment{
-		id:       op.Id(),
+		id:       entity.Id(CompileCommentId(snapshot.Id().String(), op.Id().String())),
 		Message:  op.Message,
 		Author:   op.Author,
 		UnixTime: timestamp.Timestamp(op.UnixTime),