feat: 添加 Lookuper 接口和 MustLookup 方法
- 新增 Lookuper 接口,提供 Lookup 和 MustLookup 两种路径查找方式 - Accessor 接口继承 Lookuper,保持向后兼容 - MustLookup 在路径不存在时返回 Nil 访问器,简化调用代码 - 更新 fieldx.Schema 使用 Lookuper 接口,支持更灵活的数据源 - 添加 Required 字段选项,控制字段不存在时的行为
This commit is contained in:
@@ -2,11 +2,20 @@ package valuex
|
||||
|
||||
import "reflect"
|
||||
|
||||
// Accessor 为参数值转换相关接口
|
||||
type Accessor interface {
|
||||
// Lookuper 提供基于路径查找值的能力
|
||||
type Lookuper interface {
|
||||
// Lookup 根据路径查找并返回对应值的访问器
|
||||
Lookup(path string) (Accessor, bool)
|
||||
|
||||
// MustLookup 根据路径查找并直接返回对应值的访问器
|
||||
// 如果路径不存在,返回 Nil 访问器(所有方法返回零值)
|
||||
MustLookup(path string) Accessor
|
||||
}
|
||||
|
||||
// Accessor 为参数值转换相关接口
|
||||
type Accessor interface {
|
||||
Lookuper
|
||||
|
||||
// Raw 返回底层的 reflect.Value
|
||||
Raw() reflect.Value
|
||||
|
||||
|
||||
@@ -14,6 +14,11 @@ func (n *nilAccessor) Lookup(path string) (Accessor, bool) {
|
||||
return n, false
|
||||
}
|
||||
|
||||
// MustLookup 总是返回自身,表示路径不存在
|
||||
func (n *nilAccessor) MustLookup(path string) Accessor {
|
||||
return n
|
||||
}
|
||||
|
||||
func (n *nilAccessor) Raw() reflect.Value {
|
||||
return reflect.Value{}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user