util.go (984B)
1 package internal 2 3 import ( 4 "reflect" 5 ) 6 7 func MakeSliceNextElemFunc(v reflect.Value) func() reflect.Value { 8 if v.Kind() == reflect.Array { 9 var pos int 10 return func() reflect.Value { 11 v := v.Index(pos) 12 pos++ 13 return v 14 } 15 } 16 17 elemType := v.Type().Elem() 18 19 if elemType.Kind() == reflect.Ptr { 20 elemType = elemType.Elem() 21 return func() reflect.Value { 22 if v.Len() < v.Cap() { 23 v.Set(v.Slice(0, v.Len()+1)) 24 elem := v.Index(v.Len() - 1) 25 if elem.IsNil() { 26 elem.Set(reflect.New(elemType)) 27 } 28 return elem 29 } 30 31 elem := reflect.New(elemType) 32 v.Set(reflect.Append(v, elem)) 33 return elem 34 } 35 } 36 37 zero := reflect.Zero(elemType) 38 return func() reflect.Value { 39 if v.Len() < v.Cap() { 40 v.Set(v.Slice(0, v.Len()+1)) 41 return v.Index(v.Len() - 1) 42 } 43 44 v.Set(reflect.Append(v, zero)) 45 return v.Index(v.Len() - 1) 46 } 47 } 48 49 func Unwrap(err error) error { 50 u, ok := err.(interface { 51 Unwrap() error 52 }) 53 if !ok { 54 return nil 55 } 56 return u.Unwrap() 57 }