refactor: 改进错误处理,使用预定义错误替代硬编码字符串
- fieldx.Schema.Generate: 添加 panic 捕获机制,优雅处理 reflux.New 的异常 - fieldx.Schema.Generate: 支持 any 类型参数,增强通用性 - reflux.New: 使用预定义错误(ErrInvalidValue, ErrTargetNilPointer, NewErrUnsupportedTargetType)替代硬编码错误字符串 - 提高错误信息的一致性和可读性
This commit is contained in:
@@ -65,14 +65,14 @@ func New(v any) R {
|
||||
}
|
||||
|
||||
if !rv.IsValid() {
|
||||
panic("rfx: invalid value")
|
||||
panic(ErrInvalidValue)
|
||||
}
|
||||
|
||||
// 递归解引用指针和接口,直到获取实际的值类型
|
||||
actualValue := rv
|
||||
for actualValue.Kind() == reflect.Ptr || actualValue.Kind() == reflect.Interface {
|
||||
if actualValue.IsNil() {
|
||||
panic("rfx: cannot use nil pointer or nil interface")
|
||||
panic(ErrTargetNilPointer)
|
||||
}
|
||||
actualValue = actualValue.Elem()
|
||||
// 如果解引用后的类型是指针,标记为指针模式
|
||||
@@ -93,7 +93,7 @@ func New(v any) R {
|
||||
reflect.Float64:
|
||||
// 支持的类型
|
||||
default:
|
||||
panic("rfx: unsupported type, only map, struct, slice, and array are allowed")
|
||||
panic(NewErrUnsupportedTargetType(actualValue.Kind()))
|
||||
}
|
||||
|
||||
// 如果原始传入的不是指针类型,需要进行深度克隆以避免修改原始数据
|
||||
|
||||
Reference in New Issue
Block a user