1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-09 15:12:26 +08:00

fix: fix issue #275

This commit is contained in:
dudaodong
2024-12-04 16:37:46 +08:00
parent 0f9683a764
commit f58b706285
6 changed files with 182 additions and 7 deletions

View File

@@ -240,7 +240,8 @@ func setStructField(structObj interface{}, fieldName string, fieldValue interfac
if fieldVal.Type() != val.Type() {
if val.CanConvert(fieldVal.Type()) {
// fix: issue #275
if canConvert(fieldValue, fieldVal.Type()) {
fieldVal.Set(val.Convert(fieldVal.Type()))
return nil
}
@@ -284,3 +285,14 @@ func getFieldNameByJsonTag(structObj interface{}, jsonTag string) string {
return ""
}
func canConvert(value interface{}, targetType reflect.Type) bool {
v := reflect.ValueOf(value)
defer func() {
if r := recover(); r != nil {
}
}()
v.Convert(targetType)
return true
}