Skip to content

Commit f5cecc3

Browse files
author
刘顺钰
committed
fix: pretty logic
1 parent 3ad84f4 commit f5cecc3

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

Diff for: copier.go

+17-8
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,14 @@ func set(to, from reflect.Value, deepCopy bool, converters map[converterPair]Typ
610610
}
611611
}
612612

613+
// try convert directly
613614
if from.Type().ConvertibleTo(to.Type()) {
614615
to.Set(from.Convert(to.Type()))
615-
} else if toScanner, ok := to.Addr().Interface().(sql.Scanner); ok {
616+
return true, nil
617+
}
618+
619+
// try Scanner
620+
if toScanner, ok := to.Addr().Interface().(sql.Scanner); ok {
616621
// `from` -> `to`
617622
// *string -> sql.NullString
618623
if from.Kind() == reflect.Ptr {
@@ -627,10 +632,13 @@ func set(to, from reflect.Value, deepCopy bool, converters map[converterPair]Typ
627632
// string -> sql.NullString
628633
// set `to` by invoking method Scan(`from`)
629634
err := toScanner.Scan(from.Interface())
630-
if err != nil {
631-
return false, nil
635+
if err == nil {
636+
return true, nil
632637
}
633-
} else if fromValuer, ok := driverValuer(from); ok {
638+
}
639+
640+
// try Valuer
641+
if fromValuer, ok := driverValuer(from); ok {
634642
// `from` -> `to`
635643
// sql.NullString -> string
636644
v, err := fromValuer.Value()
@@ -651,13 +659,14 @@ func set(to, from reflect.Value, deepCopy bool, converters map[converterPair]Typ
651659
return true, nil
652660
}
653661
return false, nil
654-
} else if from.Kind() == reflect.Ptr {
662+
}
663+
664+
// from is ptr
665+
if from.Kind() == reflect.Ptr {
655666
return set(to, from.Elem(), deepCopy, converters)
656-
} else {
657-
return false, nil
658667
}
659668

660-
return true, nil
669+
return false, nil
661670
}
662671

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

0 commit comments

Comments
 (0)