Files
contracts/base/res_configure.go
T
what 9446571363 重构: base 包资源核心类型迁移到 req/resx,新增 res_watcher/res_api_param
base/resource.go、resource_hooks.go、resource_test.go、query_field.go 删除,ResField 等具体实现搬到 req/resx(见 res_field.go 里的类型别名)。res_listener.go 替换成 res_watcher.go,对应资源变更监听概念改名。新增 res_api_param.go 及配套测试(ResApi 参数建模,给 MCP tool 的 JSON Schema 生成用)。
2026-07-22 09:13:05 +08:00

67 lines
1.7 KiB
Go

package base
import (
"encoding/json"
"git.fsdpf.net/go/reflux"
"git.fsdpf.net/go/reflux/valuex"
)
// role_uuid => value
type ResRoleValue map[string]string
type ResConfigure struct {
Uuid string `db:"uuid"`
ResourceUuid string `db:"resource_uuid"`
Key string `db:"key"`
Value string `db:"value"`
Label string `db:"label"`
Type string `db:"type"`
IsPrivate bool `db:"isPrivate"`
Desc string `db:"desc"`
RoleValues []ResRoleValue `db:"role_values"`
UpdatedAt string `db:"updated_at"`
CreatedAt string `db:"created_at"`
}
// GetValueByRoles 根据角色优先级获取配置值
//
// - roles: 角色UUIDs
//
// 返回: valuex.Accessor 对象和是否找到角色特定值的标志
func (rc *ResConfigure) GetValueByRoles(roles ...string) (valuex.Accessor, bool) {
// 如果没有角色值配置或没有传入角色,返回默认值
if len(rc.RoleValues) == 0 || len(roles) == 0 {
return reflux.New(rc.toTyped(rc.Value)), true
}
for _, rv := range rc.RoleValues {
for _, role := range roles {
if value, exists := rv[role]; exists {
return reflux.New(rc.toTyped(value)), true
}
}
}
// 没有找到匹配的角色值,返回默认值
return reflux.New(rc.toTyped(rc.Value)), false
}
func (rc *ResConfigure) toTyped(s string) any {
switch rc.Type {
case "json":
var v map[string]any
if json.Unmarshal([]byte(s), &v) == nil {
return v
}
case "array":
var v []any
if json.Unmarshal([]byte(s), &v) == nil {
return v
}
}
return s
}
type GetResConfigure func(key string, roles ...string) (valuex.Accessor, bool)