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 return m.enc
} }
// ReadFrom gets a value from a metadata string d, for the given key. If the // Get returns the value for the given key in d.
// 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.
func Get(key string, d []byte) (string, error) { func Get(key string, d []byte) (string, error) {
err := checkHeader(d) err := checkHeader(d)
if err != nil { if err != nil {
@ -184,9 +182,14 @@ func Get(key string, d []byte) (string, error) {
return "", errKeyAbsent return "", errKeyAbsent
} }
// GetAll gets all metadata entries from given data. An Error is returned // GetAll returns metadata keys and values from d.
// if the metadata does not have a valid header, or if the meta format is unexpected.
func GetAll(d []byte) ([][2]string, error) { 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) err := checkHeader(d)
if err != nil { if err != nil {
return nil, err return nil, err
@ -204,8 +207,7 @@ func GetAll(d []byte) ([][2]string, error) {
return all, nil return all, nil
} }
// checkHeader checks that a valid metadata header exists in the given data. An // checkHeader checks that a valid metadata header exists in the given data.
// error is returned if the header is absent, or if the header is not valid.
func checkHeader(d []byte) error { func checkHeader(d []byte) error {
if d[0] != 0 { if d[0] != 0 {
return errNoHeader return errNoHeader