From ce92dd37d8ecf4c334db4a8e948662aad62a8376 Mon Sep 17 00:00:00 2001 From: saxon Date: Sun, 10 Feb 2019 08:58:38 +1030 Subject: [PATCH 1/2] stream/mts/meta: added Keys() func and appropriate testing A meta.Keys(d []byte) []string, error func has been added that will extract the keys of a metadata string. A test has also been added to test that this function performs as expected. --- stream/mts/meta/meta.go | 13 +++++++++++++ stream/mts/meta/meta_test.go | 14 ++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/stream/mts/meta/meta.go b/stream/mts/meta/meta.go index 4b5cf8e9..b0d0fc2f 100644 --- a/stream/mts/meta/meta.go +++ b/stream/mts/meta/meta.go @@ -164,6 +164,19 @@ func (m *Data) Encode() []byte { return m.enc } +// Keys returns all keys in a slice of metadata d. +func Keys(d []byte) ([]string, error) { + tmp, err := GetAll(d) + if err != nil { + return nil, err + } + keys := make([]string, len(tmp)) + for i, entry := range tmp { + keys[i] = entry[0] + } + return keys, nil +} + // Get returns the value for the given key in d. func Get(key string, d []byte) (string, error) { err := checkMeta(d) diff --git a/stream/mts/meta/meta_test.go b/stream/mts/meta/meta_test.go index 459b9912..38e4dbb6 100644 --- a/stream/mts/meta/meta_test.go +++ b/stream/mts/meta/meta_test.go @@ -188,3 +188,17 @@ func TestGetAll(t *testing.T) { t.Errorf("Did not get expected out. \nGot : %v, \nWant: %v\n", got, want) } } + +// TestKeys checks that we can successfully get keys from some metadata using +// the meta.Keys method. +func TestKeys(t *testing.T) { + tstMeta := append([]byte{0x00, 0x10, 0x00, 0x12}, "loc=a,b,c\tts=12345"...) + want := []string{"loc", "ts"} + got, err := Keys(tstMeta) + if err != nil { + t.Errorf("Unexpected error: %v\n", err) + } + if !reflect.DeepEqual(got, want) { + t.Errorf("Did not get expected out. \nGot : %v, \nWant: %v\n", got, want) + } +} From dad70b37b40c6b3005a3050e7fab44cdc5fa0c92 Mon Sep 17 00:00:00 2001 From: saxon Date: Mon, 11 Feb 2019 14:30:37 +1030 Subject: [PATCH 2/2] stream/mts/meta/meta.go: reduced verbosity of local vars in meta.Keys --- stream/mts/meta/meta.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/stream/mts/meta/meta.go b/stream/mts/meta/meta.go index b0d0fc2f..481b5ae5 100644 --- a/stream/mts/meta/meta.go +++ b/stream/mts/meta/meta.go @@ -166,15 +166,15 @@ func (m *Data) Encode() []byte { // Keys returns all keys in a slice of metadata d. func Keys(d []byte) ([]string, error) { - tmp, err := GetAll(d) + m, err := GetAll(d) if err != nil { return nil, err } - keys := make([]string, len(tmp)) - for i, entry := range tmp { - keys[i] = entry[0] + k := make([]string, len(m)) + for i, kv := range m { + k[i] = kv[0] } - return keys, nil + return k, nil } // Get returns the value for the given key in d.