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.
This commit is contained in:
saxon 2019-02-10 08:58:38 +10:30
parent 07d0b7cd5b
commit ce92dd37d8
2 changed files with 27 additions and 0 deletions

View File

@ -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)

View File

@ -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)
}
}