slice_util.go 571 Bytes
Newer Older
Brian Tiger Chow's avatar
Brian Tiger Chow committed
1 2 3 4 5
package internal

import (
	"io"

6
	u "github.com/ipfs/go-ipfs/util"
7
)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
8

9
func CastToReaders(slice []interface{}) ([]io.Reader, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
10 11 12 13
	readers := make([]io.Reader, 0)
	for _, arg := range slice {
		reader, ok := arg.(io.Reader)
		if !ok {
14
			return nil, u.ErrCast()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
15 16 17 18 19 20
		}
		readers = append(readers, reader)
	}
	return readers, nil
}

21
func CastToStrings(slice []interface{}) ([]string, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
22 23 24 25
	strs := make([]string, 0)
	for _, maybe := range slice {
		str, ok := maybe.(string)
		if !ok {
26
			return nil, u.ErrCast()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
27 28 29 30 31
		}
		strs = append(strs, str)
	}
	return strs, nil
}