From 63fb18feb4332ab09ad981fccb713709d1feed47 Mon Sep 17 00:00:00 2001 From: Felix Sun Date: Sat, 11 Sep 2021 15:20:47 +0800 Subject: [PATCH] No panic with reflectutils --- get.go | 6 ++++++ set.go | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/get.go b/get.go index 46870b5..fe40344 100644 --- a/get.go +++ b/get.go @@ -1,6 +1,7 @@ package reflectutils import ( + "errors" "fmt" "reflect" "strings" @@ -30,6 +31,11 @@ func IsNil(i interface{}) bool { // Get value of a struct by path using reflect. func Get(i interface{}, name string) (value interface{}, err error) { // printv(i, name) + defer func() { + if r := recover(); r != nil { + err = errors.New(fmt.Sprint(r)) + } + }() if IsNil(i) { return diff --git a/set.go b/set.go index f994bf5..2c91798 100644 --- a/set.go +++ b/set.go @@ -31,6 +31,11 @@ var NoSuchFieldError = errors.New("no such field") // Set value of a struct by path using reflect. func Set(i interface{}, name string, value interface{}) (err error) { + defer func() { + if r := recover(); r != nil { + err = errors.New(fmt.Sprint(r)) + } + }() v := reflect.ValueOf(i)