Skip to content

Commit

Permalink
No panic with reflectutils
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfmin committed Sep 11, 2021
1 parent cdeab1a commit 63fb18f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions get.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package reflectutils

import (
"errors"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 63fb18f

Please sign in to comment.