feat: Append 方法支持 R 接口包装的指针类型

增强 setValue 函数,支持将 R 接口包装的指针值追加到非指针类型的切片中。当源值是指针但目标不是指针时,自动解引用后再赋值,使得 New(&item) 可以追加到 []Item 类型的切片。同时统一使用 Raw() 方法替代 Value() 方法名。
This commit is contained in:
2025-12-08 17:24:45 +08:00
parent f5f261e541
commit bacec92841
4 changed files with 264 additions and 6 deletions

11
rfx.go
View File

@@ -289,6 +289,17 @@ func (r *rfx) setValue(field reflect.Value, v any) bool {
return true
}
// 如果源值是指针但目标不是指针,尝试解引用后再赋值
if val.Kind() == reflect.Ptr && !val.IsNil() && targetType.Kind() != reflect.Ptr {
derefVal := val.Elem()
if derefVal.Type().AssignableTo(targetType) {
field.Set(derefVal)
return true
}
// 解引用后继续使用下面的逻辑处理
val = derefVal
}
switch targetType.Kind() {
case reflect.Ptr:
// 处理指针类型