Skip to content

Commit

Permalink
Merge pull request #546 from Neal/feature/message-bytes
Browse files Browse the repository at this point in the history
Add message.Bytes() to avoid string conversion
  • Loading branch information
ackleymi authored May 8, 2023
2 parents a18c889 + 65ca379 commit f98268b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 8 additions & 1 deletion message.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ func ParseMessageWithDataDictionary(
}

return

}

func isHeaderField(tag Tag, dataDict *datadictionary.DataDictionary) bool {
Expand Down Expand Up @@ -390,6 +389,14 @@ func extractField(parsedFieldBytes *TagValue, buffer []byte) (remBytes []byte, e
return buffer[(endIndex + 1):], err
}

func (m *Message) Bytes() []byte {
if m.rawMessage != nil {
return m.rawMessage.Bytes()
}

return m.build()
}

func (m *Message) String() string {
if m.rawMessage != nil {
return m.rawMessage.String()
Expand Down
2 changes: 2 additions & 0 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,14 @@ func (s *MessageSuite) TestCopyIntoMessage() {
s.Nil(ParseMessage(s.msg, bytes.NewBufferString(newMsgString)))
s.True(s.msg.IsMsgTypeOf("A"))
s.Equal(s.msg.String(), newMsgString)
s.Equal(string(s.msg.Bytes()), newMsgString)

// clear the source buffer also
msgBuf.Reset()

s.True(dest.IsMsgTypeOf("D"))
s.Equal(dest.String(), renderedString)
s.Equal(string(dest.Bytes()), renderedString)
}

func checkFieldInt(s *MessageSuite, fields FieldMap, tag, expected int) {
Expand Down

0 comments on commit f98268b

Please sign in to comment.