From 2b7ac003a809360e7c15b65081fc0881435e5d36 Mon Sep 17 00:00:00 2001 From: what Date: Mon, 8 Dec 2025 17:25:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Accessor=20=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- valuex/accessor.go | 85 ++++++++++++++++++++++++ valuex/nil_accessor.go | 143 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 228 insertions(+) create mode 100644 valuex/accessor.go create mode 100644 valuex/nil_accessor.go diff --git a/valuex/accessor.go b/valuex/accessor.go new file mode 100644 index 0000000..e7a12c7 --- /dev/null +++ b/valuex/accessor.go @@ -0,0 +1,85 @@ +package valuex + +// Accessor 为参数值转换相关接口 +type Accessor interface { + // Lookup 根据路径查找并返回对应值的访问器 + Lookup(path string) (Accessor, bool) + + // 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 +} diff --git a/valuex/nil_accessor.go b/valuex/nil_accessor.go new file mode 100644 index 0000000..4cb2d07 --- /dev/null +++ b/valuex/nil_accessor.go @@ -0,0 +1,143 @@ +package valuex + +// nilAccessor 是一个空实现,用于表示不存在或空值的访问器 +// 所有转换方法都会返回对应类型的零值 +type nilAccessor struct{} + +// Nil 为全局唯一的 nilAccessor 实例 +var Nil = &nilAccessor{} + +// Lookup 总是返回自身和 false,表示路径不存在 +func (n *nilAccessor) Lookup(path string) (Accessor, bool) { + return n, false +} + +// Ptr 返回 nil +func (n *nilAccessor) Ptr() any { + return nil +} + +// Any 返回 nil +func (n *nilAccessor) Any() any { + return nil +} + +// Bool 返回 false +func (n *nilAccessor) Bool() bool { + return false +} + +// Float64 返回 0 +func (n *nilAccessor) Float64() float64 { + return 0 +} + +// Float32 返回 0 +func (n *nilAccessor) Float32() float32 { + return 0 +} + +// Int64 返回 0 +func (n *nilAccessor) Int64() int64 { + return 0 +} + +// Int32 返回 0 +func (n *nilAccessor) Int32() int32 { + return 0 +} + +// Int16 返回 0 +func (n *nilAccessor) Int16() int16 { + return 0 +} + +// Int8 返回 0 +func (n *nilAccessor) Int8() int8 { + return 0 +} + +// Int 返回 0 +func (n *nilAccessor) Int() int { + return 0 +} + +// Uint 返回 0 +func (n *nilAccessor) Uint() uint { + return 0 +} + +// Uint64 返回 0 +func (n *nilAccessor) Uint64() uint64 { + return 0 +} + +// Uint32 返回 0 +func (n *nilAccessor) Uint32() uint32 { + return 0 +} + +// Uint16 返回 0 +func (n *nilAccessor) Uint16() uint16 { + return 0 +} + +// Uint8 返回 0 +func (n *nilAccessor) Uint8() uint8 { + return 0 +} + +// String 返回空字符串 +func (n *nilAccessor) String() string { + return "" +} + +// StringMapString 返回 nil +func (n *nilAccessor) StringMapString() map[string]string { + return nil +} + +// StringMapStringSlice 返回 nil +func (n *nilAccessor) StringMapStringSlice() map[string][]string { + return nil +} + +// StringMapBool 返回 nil +func (n *nilAccessor) StringMapBool() map[string]bool { + return nil +} + +// StringMapInt 返回 nil +func (n *nilAccessor) StringMapInt() map[string]int { + return nil +} + +// StringMapInt64 返回 nil +func (n *nilAccessor) StringMapInt64() map[string]int64 { + return nil +} + +// StringMap 返回 nil +func (n *nilAccessor) StringMap() map[string]any { + return nil +} + +// Slice 返回 nil +func (n *nilAccessor) Slice() []any { + return nil +} + +// BoolSlice 返回 nil +func (n *nilAccessor) BoolSlice() []bool { + return nil +} + +// StringSlice 返回 nil +func (n *nilAccessor) StringSlice() []string { + return nil +} + +// IntSlice 返回 nil +func (n *nilAccessor) IntSlice() []int { + return nil +}