Merged in meta-keys-func (pull request #140)

stream/mts/meta: added Keys() func and appropriate testing

Approved-by: kortschak <dan@kortschak.io>
This commit is contained in:
Saxon Milton 2019-02-11 06:09:10 +00:00
commit bfdefa97f8
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) {
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)

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