diff --git a/stream/mts/meta/meta.go b/stream/mts/meta/meta.go index 4b5cf8e9..481b5ae5 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) { + m, err := GetAll(d) + if err != nil { + return nil, err + } + k := make([]string, len(m)) + for i, kv := range m { + k[i] = kv[0] + } + return k, 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) + } +}