From 960c110acb561ef8ff496885a63e9e6f39b063d6 Mon Sep 17 00:00:00 2001 From: saxon Date: Mon, 28 Jan 2019 17:45:38 +1030 Subject: [PATCH] stream/mts/meta.go: fixed Meta.Encode() func so that it calculates data length correctly --- stream/mts/meta.go | 25 +++++++++++++++++++++---- stream/mts/meta_test.go | 4 +++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/stream/mts/meta.go b/stream/mts/meta.go index c25d3e85..c12998bd 100644 --- a/stream/mts/meta.go +++ b/stream/mts/meta.go @@ -39,6 +39,11 @@ const ( minVer = 0 ) +const ( + dataLenIdx1 = 2 + dataLenIdx2 = 3 +) + var ( errKeyAbsent = errors.New("Key does not exist in map") ) @@ -104,11 +109,23 @@ func (m *Meta) Delete(key string) error { func (m *Meta) Encode() []byte { m.enc = m.enc[:headSize] + + // Iterate over map and append entries, only adding tab if we're not on the last entry + var i int + var entry string for k, v := range m.data { - entry := k + "=" + v + "\t" - m.enc = append(m.enc, []byte(entry)...) + i++ + entry += k + "=" + v + if i < len(m.data) { + entry += "\t" + } } - // Remove final tab - m.enc = m.enc[:len(m.enc)-1] + m.enc = append(m.enc, []byte(entry)...) + + // Calculate and set data length in encoded meta header. + dataLen := len(m.enc[headSize:]) + m.enc[dataLenIdx1] = byte(dataLen >> 8) + m.enc[dataLenIdx2] = byte(dataLen) + return m.enc } diff --git a/stream/mts/meta_test.go b/stream/mts/meta_test.go index 8b871dcb..07f0e5e2 100644 --- a/stream/mts/meta_test.go +++ b/stream/mts/meta_test.go @@ -125,4 +125,6 @@ func TestDeleteAbsentKey(t *testing.T) { } } -// TODO: add test function for Encoding the Meta map data +func TestEncode(t *testing.T) { + meta := NewMeta() +}