mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-11-01 23:10:01 +00:00
98263a7de6
* start fixing up tests * fix up tests + automate with drone * fiddle with linting * messing about with drone.yml * some more fiddling * hmmm * add cache * add vendor directory * verbose * ci updates * update some little things * update sig
72 lines
1.2 KiB
Go
72 lines
1.2 KiB
Go
package internal
|
|
|
|
import (
|
|
"context"
|
|
"reflect"
|
|
"time"
|
|
)
|
|
|
|
func Sleep(ctx context.Context, dur time.Duration) error {
|
|
t := time.NewTimer(dur)
|
|
defer t.Stop()
|
|
|
|
select {
|
|
case <-t.C:
|
|
return nil
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
}
|
|
}
|
|
|
|
func MakeSliceNextElemFunc(v reflect.Value) func() reflect.Value {
|
|
if v.Kind() == reflect.Array {
|
|
var pos int
|
|
return func() reflect.Value {
|
|
v := v.Index(pos)
|
|
pos++
|
|
return v
|
|
}
|
|
}
|
|
|
|
elemType := v.Type().Elem()
|
|
|
|
if elemType.Kind() == reflect.Ptr {
|
|
elemType = elemType.Elem()
|
|
return func() reflect.Value {
|
|
if v.Len() < v.Cap() {
|
|
v.Set(v.Slice(0, v.Len()+1))
|
|
elem := v.Index(v.Len() - 1)
|
|
if elem.IsNil() {
|
|
elem.Set(reflect.New(elemType))
|
|
}
|
|
return elem.Elem()
|
|
}
|
|
|
|
elem := reflect.New(elemType)
|
|
v.Set(reflect.Append(v, elem))
|
|
return elem.Elem()
|
|
}
|
|
}
|
|
|
|
zero := reflect.Zero(elemType)
|
|
return func() reflect.Value {
|
|
if v.Len() < v.Cap() {
|
|
v.Set(v.Slice(0, v.Len()+1))
|
|
return v.Index(v.Len() - 1)
|
|
}
|
|
|
|
v.Set(reflect.Append(v, zero))
|
|
return v.Index(v.Len() - 1)
|
|
}
|
|
}
|
|
|
|
func Unwrap(err error) error {
|
|
u, ok := err.(interface {
|
|
Unwrap() error
|
|
})
|
|
if !ok {
|
|
return nil
|
|
}
|
|
return u.Unwrap()
|
|
}
|