@@ -18,7 +18,11 @@ type fieldCacheKey struct {
1818 f string
1919}
2020
21- func Fetch (from , i any ) any {
21+ func Fetch (from , i any ) (any , bool ) {
22+ if from == nil {
23+ return nil , false
24+ }
25+
2226 v := reflect .ValueOf (from )
2327 if v .Kind () == reflect .Invalid {
2428 panic (fmt .Sprintf ("cannot fetch %v from %T" , i , from ))
@@ -29,7 +33,7 @@ func Fetch(from, i any) any {
2933 if methodName , ok := i .(string ); ok {
3034 method := v .MethodByName (methodName )
3135 if method .IsValid () {
32- return method .Interface ()
36+ return method .Interface (), true
3337 }
3438 }
3539 }
@@ -52,7 +56,7 @@ func Fetch(from, i any) any {
5256 }
5357 value := v .Index (index )
5458 if value .IsValid () {
55- return value .Interface ()
59+ return value .Interface (), true
5660 }
5761
5862 case reflect .Map :
@@ -63,10 +67,10 @@ func Fetch(from, i any) any {
6367 value = v .MapIndex (reflect .ValueOf (i ))
6468 }
6569 if value .IsValid () {
66- return value .Interface ()
70+ return value .Interface (), true
6771 } else {
6872 elem := reflect .TypeOf (from ).Elem ()
69- return reflect .Zero (elem ).Interface ()
73+ return reflect .Zero (elem ).Interface (), true
7074 }
7175
7276 case reflect .Struct :
@@ -77,7 +81,7 @@ func Fetch(from, i any) any {
7781 f : fieldName ,
7882 }
7983 if cv , ok := fieldCache .Load (key ); ok {
80- return v .FieldByIndex (cv .([]int )).Interface ()
84+ return v .FieldByIndex (cv .([]int )).Interface (), true
8185 }
8286 field , ok := t .FieldByNameFunc (func (name string ) bool {
8387 field , _ := t .FieldByName (name )
@@ -94,11 +98,12 @@ func Fetch(from, i any) any {
9498 value := v .FieldByIndex (field .Index )
9599 if value .IsValid () {
96100 fieldCache .Store (key , field .Index )
97- return value .Interface ()
101+ return value .Interface (), true
98102 }
99103 }
100104 }
101- panic (fmt .Sprintf ("cannot fetch %v from %T" , i , from ))
105+
106+ return nil , false
102107}
103108
104109type Field struct {
0 commit comments