Exemple #1
0
// Wait for all waiters in list
// list must be a slice of Waiters (or types witch implement Waiter)
func WaitForAll(list interface{}) error {
	if list == nil {
		return nil
	}

	var (
		err errors.List
	)

	v := reflect.ValueOf(list)
	t := v.Type()

	if t.Kind() != reflect.Slice || !t.Elem().Implements(waiterType) {
		panic(fmt.Sprintf("WaitForAll(list): expected []Waiter (%v %+v)", v.Type(), list))
	}

	for i, l := 0, v.Len(); i < l; i++ {
		w := v.Index(i).Interface().(Waiter)
		err.Add(w.Wait())
	}

	return err.Normalize()
}
Exemple #2
0
func (in *C) select_or_reject(fv reflect.Value, has_err, must_be bool) *C {
	return in.Transform(in.elem_type, func(i_elems []interface{}) ([]interface{}, error) {
		var (
			o_elems  = make([]interface{}, 0, len(i_elems))
			in_args  = make([]reflect.Value, 1)
			out_args []reflect.Value
			err      errors.List
		)

		for _, elem := range i_elems {
			in_args[0] = reflect.ValueOf(elem)
			out_args = fv.Call(in_args)
			if has_err && !out_args[1].IsNil() {
				err.Add(out_args[1].Interface().(error))
				continue
			}
			if out_args[0].Bool() == must_be {
				o_elems = append(o_elems, elem)
			}
		}

		return o_elems, nil
	})
}