From 50575270b9da50936e52bc55d554235005fddb41 Mon Sep 17 00:00:00 2001 From: saxon Date: Sat, 9 Feb 2019 21:35:35 +1030 Subject: [PATCH] stream/mts/meta: checking if given slice is nil or empty and returning error if either. Also updated some func comments --- stream/mts/meta/meta.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/stream/mts/meta/meta.go b/stream/mts/meta/meta.go index 29ed0a06..d69a2b60 100644 --- a/stream/mts/meta/meta.go +++ b/stream/mts/meta/meta.go @@ -165,9 +165,7 @@ func (m *Data) Encode() []byte { return m.enc } -// ReadFrom gets a value from a metadata string d, for the given key. If the -// key is not present in the metadata string, an error is returned. If the -// metadata header is not present in the string, an error is returned. +// Get returns the value for the given key in d. func Get(key string, d []byte) (string, error) { err := checkHeader(d) if err != nil { @@ -184,9 +182,14 @@ func Get(key string, d []byte) (string, error) { return "", errKeyAbsent } -// GetAll gets all metadata entries from given data. An Error is returned -// if the metadata does not have a valid header, or if the meta format is unexpected. +// GetAll returns metadata keys and values from d. func GetAll(d []byte) ([][2]string, error) { + if d == nil { + return nil, errors.New("nil slice given") + } + if len(d) == 0 { + return nil, errors.New("empty slice given") + } err := checkHeader(d) if err != nil { return nil, err @@ -204,8 +207,7 @@ func GetAll(d []byte) ([][2]string, error) { return all, nil } -// checkHeader checks that a valid metadata header exists in the given data. An -// error is returned if the header is absent, or if the header is not valid. +// checkHeader checks that a valid metadata header exists in the given data. func checkHeader(d []byte) error { if d[0] != 0 { return errNoHeader