reflux/reflux.go
2025-12-02 19:52:29 +08:00

132 lines
3.4 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 必须是指针类型,否则会 panic
// 返回一个 R 接口实例,可用于访问和操作嵌套的字段、元素和键值对
func New(v any) R {
rv := reflect.ValueOf(v)
// 如果传入的不是指针,panic
if rv.Kind() != reflect.Ptr {
panic("structmap: New() requires a pointer argument")
}
return &rfx{value: rv}
}