重构: User 相关类型拆分出 user_accessor.go/user_runtime.go

UserAccessor、UserRuntime 从 user.go 拆成独立文件,user.go 只保留 User 接口本身。
This commit is contained in:
2026-07-22 09:11:26 +08:00
parent a0ec297b61
commit 1868c8dd15
3 changed files with 103 additions and 29 deletions
Regular → Executable
-29
View File
@@ -19,32 +19,3 @@ type User interface {
// 是否匿名用户
IsAnonymous() bool
}
type UserRuntime struct {
platform string
saas string
}
// 账号运行平台
func (this UserRuntime) Platform() string {
return this.platform
}
// 账号运行租户
func (this UserRuntime) SaaS() string {
return this.saas
}
type UserRuntimeOption func(option *UserRuntime)
func UserRuntimePlatform(value string) UserRuntimeOption {
return func(option *UserRuntime) {
option.platform = value
}
}
func UserRuntimeSaaS(value string) UserRuntimeOption {
return func(option *UserRuntime) {
option.saas = value
}
}
+26
View File
@@ -0,0 +1,26 @@
package req
import "git.fsdpf.net/go/reflux/valuex"
type UserAccessor interface {
valuex.Accessor
User() User
}
type uAccessor struct {
valuex.Accessor
user User
}
func (u uAccessor) User() User {
return u.user
}
// NewUserAccessor 创建一个 UserAccessor 实例
// accessor 为 valuex.Accessor, user 为当前用户
func NewUserAccessor(accessor valuex.Accessor, user User) UserAccessor {
return &uAccessor{
Accessor: accessor,
user: user,
}
}
+77
View File
@@ -0,0 +1,77 @@
package req
import "context"
// UserRuntime 请求级别的运行时信息,随 req.User 一起传递
type UserRuntime interface {
// Platform 账号运行平台
Platform() string
// SaaS 账号运行租户
SaaS() string
// TraceId 请求追踪ID
TraceId() string
// Ctx 请求级别的上下文,用于挂载不确定/未来才需要的跨切面数据(如 ResWatcher 级联深度),
// 不用于传递 platform/saas/traceId 这类已知的核心参数
Ctx() context.Context
}
type uRuntime struct {
platform string
saas string
traceId string
ctx context.Context
}
func (this *uRuntime) Platform() string {
return this.platform
}
func (this *uRuntime) SaaS() string {
return this.saas
}
func (this *uRuntime) TraceId() string {
return this.traceId
}
func (this *uRuntime) Ctx() context.Context {
if this.ctx == nil {
this.ctx = context.Background()
}
return this.ctx
}
type UserRuntimeOption func(option *uRuntime)
func UserRuntimePlatform(value string) UserRuntimeOption {
return func(option *uRuntime) {
option.platform = value
}
}
func UserRuntimeSaaS(value string) UserRuntimeOption {
return func(option *uRuntime) {
option.saas = value
}
}
func UserRuntimeTraceId(value string) UserRuntimeOption {
return func(option *uRuntime) {
option.traceId = value
}
}
func UserRuntimeCtx(ctx context.Context) UserRuntimeOption {
return func(option *uRuntime) {
option.ctx = ctx
}
}
// NewUserRuntime 构造一个 UserRuntime
func NewUserRuntime(opts ...UserRuntimeOption) UserRuntime {
rt := &uRuntime{}
for _, opt := range opts {
opt(rt)
}
return rt
}