Skip to content

Commit

Permalink
[ISSUE #1030] avoid to append unexpected separator to the last messag…
Browse files Browse the repository at this point in the history
…e key
  • Loading branch information
gaofeihang committed Apr 7, 2023
1 parent 7ae788d commit b39872a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions primitive/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,12 @@ func (m *Message) WithTag(tags string) *Message {

func (m *Message) WithKeys(keys []string) *Message {
var sb strings.Builder
for _, k := range keys {
for i, k := range keys {
sb.WriteString(k)
sb.WriteString(PropertyKeySeparator)
// avoid to append separator to the last key
if i < len(keys)-1 {
sb.WriteString(PropertyKeySeparator)
}
}

m.WithProperty(PropertyKeys, sb.String())
Expand Down
10 changes: 10 additions & 0 deletions primitive/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,13 @@ func TestMessageID(t *testing.T) {
}
t.Log(msgID)
}

func TestMessageKey(t *testing.T) {
msg := &Message{}
expected := "testKey"
msg.WithKeys([]string{expected})
actual := msg.GetKeys()
if actual != expected {
t.Fatalf("get message key error: expected is '%s', actual is '%s'", expected, actual)
}
}

0 comments on commit b39872a

Please sign in to comment.