建立订单读模型Port与架构依赖门禁
将订单列表筛选条件和展示行提取到 internal/application/orders,Server 适配器负责数据库模型转换。 架构检查器统一模块路径,禁止应用层依赖基础设施,并要求新增 Server 低层依赖登记临时白名单。 验证:make check。
This commit is contained in:
1 parent
0a45fd4a8b
commit
c2503645e0
7 files changed
+249
-16
No files matched your search
@@ -717,3 +717,4 @@ npm --prefix frontend run build
|
||||
| --- | --- | --- | --- |
|
||||
| 2026-08-15 | 审计重开与完成条件纠偏 | 已完成 | 已将阶段 2、4、5、8、9、10 从“已完成”改为“进行中”,建立九个可回滚 PR 切片,并明确“测试通过不等于架构完成”的新验收规则 |
|
||||
| 2026-08-15 | P0 敏感系统设置与运维输出脱敏 | 已完成 | 新增 `SystemSettings.Redacted`;管理端不再返回 `ai_api_key`、SMTP 密码和验证码密钥明文;敏感设置缺省保留、显式空值清除;dbverify 改为长度+指纹;Go/React 回归、类型检查、前端构建和 `make check` 通过。阶段 2 仍保持“进行中”,因为凭证锁审计、访问审计和三库回归尚未全部完成 |
|
||||
| 2026-08-15 | P1 订单读模型 Port 与架构依赖门禁 | 已完成 | 新增 `internal/application/orders` 纯业务 `OrderRow`/`ListFilter`/`Reader`;订单列表适配器负责 `db.*` 到应用模型的转换;架构门禁统一模块路径、禁止应用层依赖 `db/server/xianyu/browser/sql/http`,并对 Server 新增低层依赖要求临时白名单;新增门禁测试,`make check` 通过。订单写入事务 Port、其他应用服务和既有白名单仍未完成,阶段 4/8/9 继续保持“进行中” |
|
||||
@@ -0,0 +1,69 @@
|
||||
// Package orders 定义订单用例面向消费者的纯业务查询模型和 Port。
|
||||
// 本包不得依赖数据库、HTTP、平台协议或 Server 实现。
|
||||
package orders
|
||||
|
||||
import "context"
|
||||
|
||||
// OrderRow 是订单列表用例需要的纯业务展示行。
|
||||
type OrderRow struct {
|
||||
// OrderID 是订单稳定标识。
|
||||
OrderID string
|
||||
// ItemID 是关联商品标识。
|
||||
ItemID string
|
||||
// ItemTitle 是关联商品标题。
|
||||
ItemTitle string
|
||||
// ItemDetail 是关联商品详情 JSON。
|
||||
ItemDetail string
|
||||
// BuyerID 是买家标识。
|
||||
BuyerID string
|
||||
// SpecName 是规格名称。
|
||||
SpecName string
|
||||
// SpecValue 是规格值。
|
||||
SpecValue string
|
||||
// Quantity 是购买数量。
|
||||
Quantity string
|
||||
// Amount 是订单金额文本。
|
||||
Amount string
|
||||
// OrderStatus 是持久化的订单状态。
|
||||
OrderStatus string
|
||||
// CookieID 是订单所属账号标识。
|
||||
CookieID string
|
||||
// IsBargain 表示订单是否为砍价订单。
|
||||
IsBargain int
|
||||
// SystemShipped 表示是否由系统确认发货。
|
||||
SystemShipped bool
|
||||
// ReceiverName 是收货人姓名。
|
||||
ReceiverName string
|
||||
// ReceiverPhone 是收货人电话。
|
||||
ReceiverPhone string
|
||||
// ReceiverAddr 是收货地址。
|
||||
ReceiverAddr string
|
||||
// ReceiverCity 是收货城市。
|
||||
ReceiverCity string
|
||||
// CreatedAt 是订单创建时间。
|
||||
CreatedAt string
|
||||
// UpdatedAt 是订单更新时间。
|
||||
UpdatedAt string
|
||||
}
|
||||
|
||||
// ListFilter 是订单列表查询的纯业务筛选条件。
|
||||
type ListFilter struct {
|
||||
// UserID 是当前用户标识。
|
||||
UserID int64
|
||||
// CookieID 是可选的账号筛选条件。
|
||||
CookieID string
|
||||
// Status 是可选的订单状态筛选条件。
|
||||
Status string
|
||||
// Search 是订单号、商品或买家搜索词。
|
||||
Search string
|
||||
// Limit 是返回条数上限。
|
||||
Limit int
|
||||
// Offset 是分页偏移量。
|
||||
Offset int
|
||||
}
|
||||
|
||||
// Reader 定义订单列表用例需要的只读 Port。
|
||||
type Reader interface {
|
||||
// ListForUser 返回当前用户可见的订单展示行和总数。
|
||||
ListForUser(ctx context.Context, filter ListFilter) ([]OrderRow, int, error)
|
||||
}
|
||||
@@ -892,7 +892,7 @@ func TestOrderResponseMappingAndErrorClassification(t *testing.T) {
|
||||
// row 保存row,供当前处理流程使用
|
||||
row := db.OrderRow{OrderID: "mapped-order", ItemID: "mapped-item", ItemTitle: "测试商品", ItemDetail: `{"pic_info":{"picUrl":"https://img.example/mapped.png"}}`, OrderStatus: "2"}
|
||||
// view 保存view,供当前处理流程使用
|
||||
view := orderDTOFromRow(row)
|
||||
view := orderDTOFromRow(orderRowsFromDB([]db.OrderRow{row})[0])
|
||||
if view.OrderStatus != "pending_ship" || view.Status != "pending_ship" || view.ItemImage != "https://img.example/mapped.png" {
|
||||
t.Fatalf("订单响应映射异常: %+v", view)
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
orderapp "xianyu-go/internal/application/orders"
|
||||
"xianyu-go/internal/db"
|
||||
)
|
||||
|
||||
@@ -14,7 +15,7 @@ type orderRepository interface {
|
||||
// ListOwnedIDs 返回用户拥有的账号 ID。
|
||||
ListOwnedIDs(ctx context.Context, userID int64) ([]string, error)
|
||||
// ListOrdersForUser 查询用户范围内的订单列表。
|
||||
ListOrdersForUser(ctx context.Context, filter db.OrderListFilter) ([]db.OrderRow, int, error)
|
||||
ListOrdersForUser(ctx context.Context, filter orderapp.ListFilter) ([]orderapp.OrderRow, int, error)
|
||||
// GetOrder 查询单个订单。
|
||||
GetOrder(ctx context.Context, orderID string) (*db.Order, error)
|
||||
// GetItem 查询账号下的商品信息。
|
||||
@@ -60,8 +61,34 @@ func (r storeOrderRepository) ListOwnedIDs(ctx context.Context, userID int64) ([
|
||||
}
|
||||
|
||||
// ListOrdersForUser 委托用户订单列表查询。
|
||||
func (r storeOrderRepository) ListOrdersForUser(ctx context.Context, filter db.OrderListFilter) ([]db.OrderRow, int, error) {
|
||||
return r.store.Orders.ListForUser(ctx, filter)
|
||||
func (r storeOrderRepository) ListOrdersForUser(ctx context.Context, filter orderapp.ListFilter) ([]orderapp.OrderRow, int, error) {
|
||||
// rows、total、err 是数据库订单列表查询结果及其错误。
|
||||
rows, total, err := r.store.Orders.ListForUser(ctx, db.OrderListFilter{
|
||||
UserID: filter.UserID, CookieID: filter.CookieID, Status: filter.Status,
|
||||
Search: filter.Search, Limit: filter.Limit, Offset: filter.Offset,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return orderRowsFromDB(rows), total, nil
|
||||
}
|
||||
|
||||
// orderRowsFromDB 将数据库列表模型转换为订单应用层模型。
|
||||
func orderRowsFromDB(rows []db.OrderRow) []orderapp.OrderRow {
|
||||
// converted 保存转换后的应用层订单行。
|
||||
converted := make([]orderapp.OrderRow, 0, len(rows))
|
||||
for _, row := range rows { // row 是待转换的数据库订单列表行。
|
||||
converted = append(converted, orderapp.OrderRow{
|
||||
OrderID: row.OrderID, ItemID: row.ItemID, ItemTitle: row.ItemTitle,
|
||||
ItemDetail: row.ItemDetail, BuyerID: row.BuyerID, SpecName: row.SpecName,
|
||||
SpecValue: row.SpecValue, Quantity: row.Quantity, Amount: row.Amount,
|
||||
OrderStatus: row.OrderStatus, CookieID: row.CookieID, IsBargain: row.IsBargain,
|
||||
SystemShipped: row.SystemShipped, ReceiverName: row.ReceiverName,
|
||||
ReceiverPhone: row.ReceiverPhone, ReceiverAddr: row.ReceiverAddr,
|
||||
ReceiverCity: row.ReceiverCity, CreatedAt: row.CreatedAt, UpdatedAt: row.UpdatedAt,
|
||||
})
|
||||
}
|
||||
return converted
|
||||
}
|
||||
|
||||
// GetOrder 委托订单详情查询。
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
orderapp "xianyu-go/internal/application/orders"
|
||||
"xianyu-go/internal/db"
|
||||
"xianyu-go/internal/xianyu/cookierefresh"
|
||||
"xianyu-go/internal/xianyu/mtop"
|
||||
@@ -109,7 +110,7 @@ type orderListResult struct {
|
||||
}
|
||||
|
||||
// orderDTOFromRow 把数据库订单列表行转换为稳定的订单响应视图。
|
||||
func orderDTOFromRow(row db.OrderRow) orderDTO {
|
||||
func orderDTOFromRow(row orderapp.OrderRow) orderDTO {
|
||||
// status 保存状态,供当前处理流程使用
|
||||
status := db.NormalizeOrderStatus(row.OrderStatus)
|
||||
return orderDTO{
|
||||
@@ -168,7 +169,7 @@ func (a *orderApplicationService) List(ctx context.Context, query orderListQuery
|
||||
// offset 保存偏移量,供当前处理流程使用
|
||||
offset := (query.Page - 1) * query.PageSize
|
||||
// rows、total、err 保存rows、total、err,供当前处理流程使用
|
||||
rows, total, err := a.repository.ListOrdersForUser(ctx, db.OrderListFilter{
|
||||
rows, total, err := a.repository.ListOrdersForUser(ctx, orderapp.ListFilter{
|
||||
UserID: query.UserID, CookieID: query.CookieID, Status: query.Status,
|
||||
Search: query.Search, Limit: query.PageSize, Offset: offset,
|
||||
})
|
||||
|
||||
+103
-10
@@ -1,4 +1,4 @@
|
||||
// architecturecheck 检查 Go 低层包依赖方向和 Server 裸事务入口。
|
||||
// architecturecheck 检查 Go 依赖方向、应用 Port 边界和 Server 裸事务入口。
|
||||
package main
|
||||
|
||||
import (
|
||||
@@ -100,16 +100,35 @@ func checkGoFile(root, relativePath string, fset *token.FileSet) ([]violation, e
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !isForbiddenLowLevelImport(importPath, importedPath) {
|
||||
continue
|
||||
// normalizedImport 是去除模块前缀后的内部导入路径。
|
||||
normalizedImport := normalizeImportPath(importedPath)
|
||||
if isForbiddenLowLevelImport(importPath, normalizedImport) {
|
||||
// line 是低层包反向依赖所在的源码行号。
|
||||
line := fset.Position(imp.Pos()).Line
|
||||
violations = append(violations, violation{
|
||||
file: filepath.ToSlash(relativePath),
|
||||
line: line,
|
||||
message: fmt.Sprintf("低层包禁止依赖上层应用包 %q", importedPath),
|
||||
})
|
||||
}
|
||||
if isForbiddenApplicationImport(importPath, normalizedImport) {
|
||||
// line 是应用层导入基础设施所在的源码行号。
|
||||
line := fset.Position(imp.Pos()).Line
|
||||
violations = append(violations, violation{
|
||||
file: filepath.ToSlash(relativePath),
|
||||
line: line,
|
||||
message: fmt.Sprintf("应用层禁止依赖基础设施或 HTTP 层 %q", importedPath),
|
||||
})
|
||||
}
|
||||
if isForbiddenServerLowLevelImport(importPath, normalizedImport) {
|
||||
// line 是 Server 新增低层依赖所在的源码行号。
|
||||
line := fset.Position(imp.Pos()).Line
|
||||
violations = append(violations, violation{
|
||||
file: filepath.ToSlash(relativePath),
|
||||
line: line,
|
||||
message: fmt.Sprintf("Server 新增低层依赖必须先迁移到应用 Port 或登记临时白名单 %q", importedPath),
|
||||
})
|
||||
}
|
||||
// line 是导入声明所在的源码行号。
|
||||
line := fset.Position(imp.Pos()).Line
|
||||
violations = append(violations, violation{
|
||||
file: filepath.ToSlash(relativePath),
|
||||
line: line,
|
||||
message: fmt.Sprintf("低层包禁止依赖上层应用包 %q", importedPath),
|
||||
})
|
||||
}
|
||||
if strings.HasPrefix(filepath.ToSlash(relativePath), "internal/server/") && !strings.HasSuffix(relativePath, "_repository.go") {
|
||||
// sourceLine 是裸 BeginTx 调用首次出现的源码行号。
|
||||
@@ -125,6 +144,80 @@ func checkGoFile(root, relativePath string, fset *token.FileSet) ([]violation, e
|
||||
return violations, nil
|
||||
}
|
||||
|
||||
// normalizeImportPath 去除当前模块前缀,统一架构规则使用的内部包路径。
|
||||
func normalizeImportPath(importedPath string) string {
|
||||
return strings.TrimPrefix(importedPath, "xianyu-go/")
|
||||
}
|
||||
|
||||
// isForbiddenApplicationImport 判断应用层是否依赖了基础设施或 HTTP 层。
|
||||
func isForbiddenApplicationImport(filePath, importedPath string) bool {
|
||||
if !strings.HasPrefix(filePath, "internal/application/") {
|
||||
return false
|
||||
}
|
||||
for _, forbidden /* forbidden 是应用层禁止依赖的包前缀。 */ := range []string{
|
||||
"internal/db", "internal/server", "internal/xianyu", "internal/browser",
|
||||
"database/sql", "net/http", "github.com/go-chi/chi",
|
||||
} {
|
||||
if importedPath == forbidden || strings.HasPrefix(importedPath, forbidden+"/") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// isForbiddenServerLowLevelImport 判断 Server 是否新增了未登记的基础设施依赖。
|
||||
func isForbiddenServerLowLevelImport(filePath, importedPath string) bool {
|
||||
if !strings.HasPrefix(filePath, "internal/server/") || strings.HasSuffix(filePath, "_test.go") {
|
||||
return false
|
||||
}
|
||||
if importedPath != "internal/db" && !strings.HasPrefix(importedPath, "internal/db/") &&
|
||||
importedPath != "internal/xianyu" && !strings.HasPrefix(importedPath, "internal/xianyu/") &&
|
||||
importedPath != "internal/browser" && !strings.HasPrefix(importedPath, "internal/browser/") {
|
||||
return false
|
||||
}
|
||||
return !temporaryServerLowLevelAllowlist[filePath]
|
||||
}
|
||||
|
||||
// temporaryServerLowLevelAllowlist 是审计重开前已存在的 Server 低层依赖临时白名单。
|
||||
// 每迁移一个业务域,必须删除对应文件条目;新文件不得加入白名单代替迁移。
|
||||
var temporaryServerLowLevelAllowlist = map[string]bool{
|
||||
"internal/server/account_login_repository.go": true,
|
||||
"internal/server/account_login_service.go": true,
|
||||
"internal/server/account_task_handlers.go": true,
|
||||
"internal/server/analytics_handlers.go": true,
|
||||
"internal/server/analytics_repository.go": true,
|
||||
"internal/server/analytics_service.go": true,
|
||||
"internal/server/api_contract.go": true,
|
||||
"internal/server/auth_handlers.go": true,
|
||||
"internal/server/automation_handlers.go": true,
|
||||
"internal/server/card_batch_handlers.go": true,
|
||||
"internal/server/card_handlers.go": true,
|
||||
"internal/server/chat_handlers.go": true,
|
||||
"internal/server/communication_repository.go": true,
|
||||
"internal/server/communication_service.go": true,
|
||||
"internal/server/cookie_handlers.go": true,
|
||||
"internal/server/default_reply_handlers.go": true,
|
||||
"internal/server/item_handlers.go": true,
|
||||
"internal/server/item_publish_batch_handlers.go": true,
|
||||
"internal/server/item_publish_images.go": true,
|
||||
"internal/server/item_publish_repository.go": true,
|
||||
"internal/server/item_publish_service.go": true,
|
||||
"internal/server/keyword_handlers.go": true,
|
||||
"internal/server/login_audit.go": true,
|
||||
"internal/server/mtop_cookie_session.go": true,
|
||||
"internal/server/notification_handlers.go": true,
|
||||
"internal/server/order_handlers.go": true,
|
||||
"internal/server/order_repository.go": true,
|
||||
"internal/server/order_service.go": true,
|
||||
"internal/server/ownership_helpers.go": true,
|
||||
"internal/server/platform_runtime.go": true,
|
||||
"internal/server/server.go": true,
|
||||
"internal/server/settings_handlers.go": true,
|
||||
"internal/server/success_contract.go": true,
|
||||
"internal/server/transaction_repository.go": true,
|
||||
"internal/server/qrlogin_handlers.go": true,
|
||||
}
|
||||
|
||||
// isForbiddenLowLevelImport 判断低层包是否依赖了上层应用包。
|
||||
func isForbiddenLowLevelImport(filePath, importedPath string) bool {
|
||||
// lowLevelPackage 标识当前文件所属的低层包。
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
// TestNormalizeImportPath 验证架构检查器能够识别当前模块的内部包路径。
|
||||
func TestNormalizeImportPath(t *testing.T) {
|
||||
if got /* got 是规范化后的导入路径。 */ := normalizeImportPath("xianyu-go/internal/db"); got != "internal/db" {
|
||||
t.Fatalf("normalizeImportPath=%q", got)
|
||||
}
|
||||
if got /* got 是未带模块前缀的标准库导入路径。 */ := normalizeImportPath("database/sql"); got != "database/sql" {
|
||||
t.Fatalf("normalizeImportPath 标准库路径被错误修改为 %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplicationImportBoundary 验证应用层禁止依赖数据库和 HTTP 层。
|
||||
func TestApplicationImportBoundary(t *testing.T) {
|
||||
if !isForbiddenApplicationImport("internal/application/orders/read_model.go", "internal/db") {
|
||||
t.Fatal("应用层导入 internal/db 应被拒绝")
|
||||
}
|
||||
if !isForbiddenApplicationImport("internal/application/orders/read_model.go", "database/sql") {
|
||||
t.Fatal("应用层导入 database/sql 应被拒绝")
|
||||
}
|
||||
if isForbiddenApplicationImport("internal/application/orders/read_model.go", "context") {
|
||||
t.Fatal("应用层导入 context 不应被拒绝")
|
||||
}
|
||||
if isForbiddenApplicationImport("internal/server/order_service.go", "internal/db") {
|
||||
t.Fatal("Server 文件不应套用应用层导入规则")
|
||||
}
|
||||
}
|
||||
|
||||
// TestServerLowLevelTemporaryAllowlist 验证旧 Server 低层依赖必须显式登记。
|
||||
func TestServerLowLevelTemporaryAllowlist(t *testing.T) {
|
||||
if isForbiddenServerLowLevelImport("internal/server/order_service.go", "internal/db") {
|
||||
t.Fatal("现有白名单文件不应被当前门禁阻断")
|
||||
}
|
||||
if !isForbiddenServerLowLevelImport("internal/server/new_service.go", "internal/db") {
|
||||
t.Fatal("新增 Server 低层依赖必须被门禁拒绝")
|
||||
}
|
||||
if isForbiddenServerLowLevelImport("internal/server/new_service_test.go", "internal/db") {
|
||||
t.Fatal("测试文件不应被生产依赖门禁阻断")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user