Skip to content

Commit f98268b

Browse files
authored
Merge pull request #546 from Neal/feature/message-bytes
Add message.Bytes() to avoid string conversion
2 parents a18c889 + 65ca379 commit f98268b

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

message.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,6 @@ func ParseMessageWithDataDictionary(
272272
}
273273

274274
return
275-
276275
}
277276

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

392+
func (m *Message) Bytes() []byte {
393+
if m.rawMessage != nil {
394+
return m.rawMessage.Bytes()
395+
}
396+
397+
return m.build()
398+
}
399+
393400
func (m *Message) String() string {
394401
if m.rawMessage != nil {
395402
return m.rawMessage.String()

message_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,14 @@ func (s *MessageSuite) TestCopyIntoMessage() {
222222
s.Nil(ParseMessage(s.msg, bytes.NewBufferString(newMsgString)))
223223
s.True(s.msg.IsMsgTypeOf("A"))
224224
s.Equal(s.msg.String(), newMsgString)
225+
s.Equal(string(s.msg.Bytes()), newMsgString)
225226

226227
// clear the source buffer also
227228
msgBuf.Reset()
228229

229230
s.True(dest.IsMsgTypeOf("D"))
230231
s.Equal(dest.String(), renderedString)
232+
s.Equal(string(dest.Bytes()), renderedString)
231233
}
232234

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

0 commit comments

Comments
 (0)