-
Notifications
You must be signed in to change notification settings - Fork 9
/
slice.go
32 lines (29 loc) · 884 Bytes
/
slice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package faker
import (
"reflect"
)
// Slice will build a random slice of interface{} of length n. The elements of
// the slice are genereted by the function fn.
func Slice(size int, fn func() interface{}) []interface{} {
slice := make([]interface{}, 0, size)
for i := 0; i < size; i++ {
slice = append(slice, fn())
}
return slice
}
// Sample return a random element of slice. Sample panics if slice is not a
// slice, is nil or is empty.
func Sample(slice interface{}) interface{} {
if reflect.TypeOf(slice).Kind() != reflect.Slice {
panic("faker.Sample param must be a slice")
}
sliceReflectValue := reflect.ValueOf(slice)
if sliceReflectValue.IsNil() {
panic("faker.Sample param is nil")
}
if sliceReflectValue.Len() == 0 {
panic("faker.Sample param is empty")
}
i := IntInRange(0, sliceReflectValue.Len()-1)
return sliceReflectValue.Index(i).Interface()
}