Skip to content

Commit

Permalink
fix: failed valuer convert returning unexpected true pick
Browse files Browse the repository at this point in the history
  • Loading branch information
laushunyu committed Jul 1, 2024
1 parent 60a1fd2 commit 48d54cf
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,9 +610,14 @@ func set(to, from reflect.Value, deepCopy bool, converters map[converterPair]Typ
}
}

// try convert directly
if from.Type().ConvertibleTo(to.Type()) {
to.Set(from.Convert(to.Type()))
} else if toScanner, ok := to.Addr().Interface().(sql.Scanner); ok {
return true, nil
}

// try Scanner
if toScanner, ok := to.Addr().Interface().(sql.Scanner); ok {
// `from` -> `to`
// *string -> sql.NullString
if from.Kind() == reflect.Ptr {
Expand All @@ -627,10 +632,13 @@ func set(to, from reflect.Value, deepCopy bool, converters map[converterPair]Typ
// string -> sql.NullString
// set `to` by invoking method Scan(`from`)
err := toScanner.Scan(from.Interface())
if err != nil {
return false, nil
if err == nil {
return true, nil
}
} else if fromValuer, ok := driverValuer(from); ok {
}

// try Valuer
if fromValuer, ok := driverValuer(from); ok {
// `from` -> `to`
// sql.NullString -> string
v, err := fromValuer.Value()
Expand All @@ -644,16 +652,21 @@ func set(to, from reflect.Value, deepCopy bool, converters map[converterPair]Typ
rv := reflect.ValueOf(v)
if rv.Type().AssignableTo(to.Type()) {
to.Set(rv)
} else if to.CanSet() && rv.Type().ConvertibleTo(to.Type()) {
return true, nil
}
if to.CanSet() && rv.Type().ConvertibleTo(to.Type()) {
to.Set(rv.Convert(to.Type()))
return true, nil
}
} else if from.Kind() == reflect.Ptr {
return set(to, from.Elem(), deepCopy, converters)
} else {
return false, nil
}

return true, nil
// from is ptr
if from.Kind() == reflect.Ptr {
return set(to, from.Elem(), deepCopy, converters)
}

return false, nil
}

// lookupAndCopyWithConverter looks up the type pair, on success the TypeConverter Fn func is called to copy src to dst field.
Expand Down

0 comments on commit 48d54cf

Please sign in to comment.