getValueFromFields doesn't panic on nil pointers (#2021)

* `IsValid()` won't return `false` for nil pointers unless Value
  is wrapped in a `reflect.Indirect`.
This commit is contained in:
Gustavo Brunoro 2018-09-09 19:47:18 -03:00 committed by Jinzhu
parent 012d147974
commit 26fde9110f
1 changed files with 1 additions and 1 deletions

View File

@ -206,7 +206,7 @@ func getValueFromFields(value reflect.Value, fieldNames []string) (results []int
// as FieldByName could panic // as FieldByName could panic
if indirectValue := reflect.Indirect(value); indirectValue.IsValid() { if indirectValue := reflect.Indirect(value); indirectValue.IsValid() {
for _, fieldName := range fieldNames { for _, fieldName := range fieldNames {
if fieldValue := indirectValue.FieldByName(fieldName); fieldValue.IsValid() { if fieldValue := reflect.Indirect(indirectValue.FieldByName(fieldName)); fieldValue.IsValid() {
result := fieldValue.Interface() result := fieldValue.Interface()
if r, ok := result.(driver.Valuer); ok { if r, ok := result.(driver.Valuer); ok {
result, _ = r.Value() result, _ = r.Value()