mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2024-12-19 09:13:12 +00:00
57 lines
984 B
Go
57 lines
984 B
Go
package internal
|
|
|
|
import (
|
|
"reflect"
|
|
)
|
|
|
|
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 := reflect.New(elemType)
|
|
v.Set(reflect.Append(v, elem))
|
|
return 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()
|
|
}
|