stream/mts/meta: checking if given slice is nil or empty and returning error if either. Also updated some func comments

This commit is contained in:
saxon 2019-02-09 21:35:35 +10:30
parent e796a5a3b7
commit 50575270b9
1 changed files with 9 additions and 7 deletions

View File

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