- 支持指针模式: New(&data) - 直接修改原始数据
- 支持值模式: New(data) - 创建深度克隆,不影响原始数据
- 新增 interface{} 类型支持,自动解析到实际类型
- 对 map 和 slice 等引用类型也进行完全深度克隆
- 新增 9 个单元测试覆盖指针/值/interface 场景
- 新增 9 个示例测试展示各种用法
- 所有测试通过,保持性能
- 更新 README.md 详细说明指针/值传递的区别
- 新增 interface 类型使用示例
- 新增泛型数据处理使用场景
- 更新注意事项说明
- 使用递归解引用处理 interface 和指针类型
- 利用 DeepClone 确保引用类型的完全独立
- 保持向后兼容,现有代码无需修改
168 lines
4.6 KiB
Go
168 lines
4.6 KiB
Go
package reflux
|
|
|
|
import "reflect"
|
|
|
|
// R 提供了一个统一的接口,用于访问和操作嵌套的结构体字段、切片元素和映射值
|
|
type R interface {
|
|
// Get 通过路径获取嵌套字段的值,返回一个新的 R 实例
|
|
// 参数 p 为路径片段,例如 Get("user", "profile", "name")
|
|
Get(p ...string) R
|
|
|
|
// Scope 类似 Get,但用于创建一个指定路径的作用域视图
|
|
// 后续操作将基于这个作用域进行
|
|
Scope(p ...string) R
|
|
|
|
// Set 设置指定路径的值,支持链式调用
|
|
// 参数 key 为路径(支持点号分割),v 为要设置的值
|
|
// 返回当前 R 实例以支持链式调用
|
|
// 示例: rfx.Set("name", "Alice").Set("age", 30)
|
|
Set(key string, v any) R
|
|
|
|
// Delete 删除指定路径的值
|
|
// 参数 p 为路径片段
|
|
// 返回当前 R 实例以支持链式调用
|
|
Delete(p ...string) R
|
|
|
|
// Exists 检查指定路径的值是否存在
|
|
// 参数 p 为路径片段
|
|
// 返回 true 表示存在,false 表示不存在
|
|
Exists(p ...string) bool
|
|
|
|
// Array 将当前值转换为 R 切片
|
|
// 适用于数组或切片类型的值
|
|
Array() []R
|
|
|
|
// Keys 返回当前映射或结构体的所有键名
|
|
Keys() []string
|
|
|
|
// Value 返回底层的 reflect.Value
|
|
// 用于需要直接操作反射值的场景
|
|
Value() reflect.Value
|
|
|
|
// Ptr 返回指向当前值的指针
|
|
Ptr() any
|
|
|
|
// Any 将当前值转换为 any 类型
|
|
Any() any
|
|
|
|
// Bool 将当前值转换为 bool 类型
|
|
Bool() bool
|
|
|
|
// Float64 将当前值转换为 float64 类型
|
|
Float64() float64
|
|
|
|
// Float32 将当前值转换为 float32 类型
|
|
Float32() float32
|
|
|
|
// Int64 将当前值转换为 int64 类型
|
|
Int64() int64
|
|
|
|
// Int32 将当前值转换为 int32 类型
|
|
Int32() int32
|
|
|
|
// Int16 将当前值转换为 int16 类型
|
|
Int16() int16
|
|
|
|
// Int8 将当前值转换为 int8 类型
|
|
Int8() int8
|
|
|
|
// Int 将当前值转换为 int 类型
|
|
Int() int
|
|
|
|
// Uint 将当前值转换为 uint 类型
|
|
Uint() uint
|
|
|
|
// Uint64 将当前值转换为 uint64 类型
|
|
Uint64() uint64
|
|
|
|
// Uint32 将当前值转换为 uint32 类型
|
|
Uint32() uint32
|
|
|
|
// Uint16 将当前值转换为 uint16 类型
|
|
Uint16() uint16
|
|
|
|
// Uint8 将当前值转换为 uint8 类型
|
|
Uint8() uint8
|
|
|
|
// String 将当前值转换为 string 类型
|
|
String() string
|
|
|
|
// StringMapString 将当前值转换为 map[string]string 类型
|
|
StringMapString() map[string]string
|
|
|
|
// StringMapStringSlice 将当前值转换为 map[string][]string 类型
|
|
StringMapStringSlice() map[string][]string
|
|
|
|
// StringMapBool 将当前值转换为 map[string]bool 类型
|
|
StringMapBool() map[string]bool
|
|
|
|
// StringMapInt 将当前值转换为 map[string]int 类型
|
|
StringMapInt() map[string]int
|
|
|
|
// StringMapInt64 将当前值转换为 map[string]int64 类型
|
|
StringMapInt64() map[string]int64
|
|
|
|
// StringMap 将当前值转换为 map[string]any 类型
|
|
StringMap() map[string]any
|
|
|
|
// Slice 将当前值转换为 []any 切片
|
|
Slice() []any
|
|
|
|
// BoolSlice 将当前值转换为 []bool 切片
|
|
BoolSlice() []bool
|
|
|
|
// StringSlice 将当前值转换为 []string 切片
|
|
StringSlice() []string
|
|
|
|
// IntSlice 将当前值转换为 []int 切片
|
|
IntSlice() []int
|
|
}
|
|
|
|
// New 创建一个新的 R 实例
|
|
// 参数 v 可以是指针或非指针类型
|
|
// - 如果传入指针: 将直接使用该指针,可以修改原始数据
|
|
// - 如果传入值: 会自动创建一个深度克隆的指针副本,修改不影响原始数据
|
|
// 支持的类型: map、struct、slice、array
|
|
// 也支持 interface 类型,会自动解析到实际类型
|
|
// 返回一个 R 接口实例,可用于访问和操作嵌套的字段、元素和键值对
|
|
func New(v any) R {
|
|
rv := reflect.ValueOf(v)
|
|
|
|
if !rv.IsValid() {
|
|
panic("rfx: invalid value")
|
|
}
|
|
|
|
// 记录原始是否为指针
|
|
isPtr := rv.Kind() == reflect.Ptr
|
|
|
|
// 递归解引用指针和接口,直到获取实际的值类型
|
|
actualValue := rv
|
|
for actualValue.Kind() == reflect.Ptr || actualValue.Kind() == reflect.Interface {
|
|
if actualValue.IsNil() {
|
|
panic("rfx: cannot use nil pointer or nil interface")
|
|
}
|
|
actualValue = actualValue.Elem()
|
|
// 如果解引用后的类型是指针,标记为指针模式
|
|
if actualValue.Kind() == reflect.Ptr {
|
|
isPtr = true
|
|
}
|
|
}
|
|
|
|
// 检查最终的实际类型是否为支持的类型
|
|
switch actualValue.Kind() {
|
|
case reflect.Map, reflect.Struct, reflect.Slice, reflect.Array:
|
|
// 支持的类型
|
|
default:
|
|
panic("rfx: unsupported type, only map, struct, slice, and array are allowed")
|
|
}
|
|
|
|
// 如果原始传入的不是指针类型,需要进行深度克隆以避免修改原始数据
|
|
// 对于引用类型(map, slice)这尤其重要
|
|
if !isPtr {
|
|
// 使用深度克隆创建一个完全独立的副本
|
|
rv = DeepClone(actualValue)
|
|
}
|
|
|
|
return &rfx{value: rv}
|
|
}
|