强化应用端口架构门禁
This commit is contained in:
1 parent
1857a7c6ef
commit
e953464df9
3 files changed
+77
-2
No files matched your search
@@ -730,3 +730,4 @@ npm --prefix frontend run build
|
||||
| 2026-08-15 | P1 订单运行时 Port 边界切片 | 已完成 | 订单应用服务不再持有 `*Server`,改由 `orderRuntimePort` 接收平台、自动化、通知和运行时 Cookie 能力;`serverOrderRuntimeAdapter` 仅在 Server 装配边界桥接现有实现;订单行为、凭证锁和错误语义保持不变;Server 全量测试、架构门禁和中文注释门禁通过。订单 Port 仍位于 `internal/server`,repository 仍有基础设施适配职责,其他应用服务和 `sql.Tx`/`db.*` 全面隔离尚未完成 |
|
||||
| 2026-08-15 | P1 订单 Repository Port 迁移切片 | 已完成 | 将 `orderRepository` 从 `internal/server` 迁移为 `internal/application/orders.Repository`;应用 Port 只暴露领域模型、`Writer`/`UnitOfWork`、凭证协调和平台运行视图,不出现 `sql.Tx`、`db.*`、HTTP 或 Server 类型;Server 仅保留 `storeOrderRepository` 基础设施适配器;订单全量测试、架构门禁和中文注释门禁通过。订单运行时 Port 仍依赖 Server 适配边界,其他应用服务及事务/批量治理仍未完成 |
|
||||
| 2026-08-15 | P0 敏感设置跨方言回归补强 | 已完成 | `TestMultiDB_SettingsQuoteKey` 新增敏感设置 `replace/retain/clear`、脱敏读取和密文解密回归;SQLite 在当前环境执行通过,MySQL/Postgres 仅在 `TEST_MYSQL_URL`/`TEST_POSTGRES_URL` 提供时自动加入,当前环境未提供外部数据库,因此不宣称三库实测完成。P0 的运维输出和敏感访问审计仍待完成 |
|
||||
| 2026-08-15 | P1 应用 Port 类型架构门禁切片 | 已完成 | `architecturecheck` 改用完整 AST 扫描 `internal/application` 类型声明,主动拒绝 `db.*`、`sql.Tx` 和 `*Server` 泄露;新增正反例门禁测试;现有订单 Port 通过扫描,架构门禁与中文注释门禁通过。Server 允许边、订单平台 Port 和其他应用服务仍需继续迁移,不能据此宣称目标架构完成 |
|
||||
@@ -3,6 +3,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"io/fs"
|
||||
@@ -85,7 +86,7 @@ func checkGoFile(root, relativePath string, fset *token.FileSet) ([]violation, e
|
||||
return nil, err
|
||||
}
|
||||
// syntax 是当前文件的 AST。
|
||||
syntax, err := parser.ParseFile(fset, filePath, source, parser.ImportsOnly)
|
||||
syntax, err := parser.ParseFile(fset, filePath, source, parser.ParseComments)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -130,6 +131,7 @@ func checkGoFile(root, relativePath string, fset *token.FileSet) ([]violation, e
|
||||
})
|
||||
}
|
||||
}
|
||||
violations = append(violations, checkApplicationTypeLeaks(relativePath, syntax, fset)...)
|
||||
if strings.HasPrefix(filepath.ToSlash(relativePath), "internal/server/") && !strings.HasSuffix(relativePath, "_repository.go") {
|
||||
// sourceLine 是裸 BeginTx 调用首次出现的源码行号。
|
||||
sourceLine := firstLineContaining(string(source), ".DB.BeginTx(")
|
||||
@@ -144,6 +146,44 @@ func checkGoFile(root, relativePath string, fset *token.FileSet) ([]violation, e
|
||||
return violations, nil
|
||||
}
|
||||
|
||||
// checkApplicationTypeLeaks 检查应用 Port 是否泄露数据库、事务或 Server 类型。
|
||||
func checkApplicationTypeLeaks(relativePath string, syntax *ast.File, fset *token.FileSet) []violation {
|
||||
if !strings.HasPrefix(filepath.ToSlash(relativePath), "internal/application/") {
|
||||
return nil
|
||||
}
|
||||
// violations 保存当前应用文件发现的类型泄露。
|
||||
var violations []violation
|
||||
ast.Inspect(syntax, func(node ast.Node) bool {
|
||||
switch typedNode /* typedNode 是当前应用声明中的 AST 类型节点。 */ := node.(type) {
|
||||
case *ast.SelectorExpr:
|
||||
// packageName、typeName 保存选择器左侧包名和右侧类型名。
|
||||
packageName, ok := typedNode.X.(*ast.Ident)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
// typeName 是选择器右侧的类型或字段名称。
|
||||
typeName := typedNode.Sel.Name
|
||||
if (packageName.Name == "sql" && typeName == "Tx") || packageName.Name == "db" {
|
||||
violations = append(violations, violation{
|
||||
file: filepath.ToSlash(relativePath), line: fset.Position(typedNode.Pos()).Line,
|
||||
message: fmt.Sprintf("应用 Port 禁止暴露基础设施类型 %s.%s", packageName.Name, typeName),
|
||||
})
|
||||
}
|
||||
case *ast.StarExpr:
|
||||
// ident 是指针类型的目标标识符。
|
||||
ident, ok := typedNode.X.(*ast.Ident)
|
||||
if ok && ident.Name == "Server" {
|
||||
violations = append(violations, violation{
|
||||
file: filepath.ToSlash(relativePath), line: fset.Position(typedNode.Pos()).Line,
|
||||
message: "应用 Port 禁止暴露 *Server 类型",
|
||||
})
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
return violations
|
||||
}
|
||||
|
||||
// normalizeImportPath 去除当前模块前缀,统一架构规则使用的内部包路径。
|
||||
func normalizeImportPath(importedPath string) string {
|
||||
return strings.TrimPrefix(importedPath, "xianyu-go/")
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestNormalizeImportPath 验证架构检查器能够识别当前模块的内部包路径。
|
||||
func TestNormalizeImportPath(t *testing.T) {
|
||||
@@ -40,3 +44,33 @@ func TestServerLowLevelTemporaryAllowlist(t *testing.T) {
|
||||
t.Fatal("测试文件不应被生产依赖门禁阻断")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplicationTypeLeakBoundary 验证应用 Port 类型扫描会拒绝基础设施和 Server 类型。
|
||||
func TestApplicationTypeLeakBoundary(t *testing.T) {
|
||||
// fset 是测试源代码的文件位置集合。
|
||||
fset := token.NewFileSet()
|
||||
// syntax、err 是包含违规字段的模拟应用文件及解析错误。
|
||||
syntax, err := parser.ParseFile(fset, "port.go", []byte(`package orders
|
||||
type Bad struct { Tx *sql.Tx; Row db.Order; Runtime *Server }
|
||||
`), parser.ParseComments)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// violations 是应用 Port 类型扫描结果。
|
||||
violations := checkApplicationTypeLeaks("internal/application/orders/port.go", syntax, fset)
|
||||
if len(violations) != 3 {
|
||||
t.Fatalf("violations=%+v", violations)
|
||||
}
|
||||
// cleanSyntax 是不泄露基础设施类型的模拟应用文件。
|
||||
cleanSyntax, err := parser.ParseFile(fset, "clean.go", []byte(`package orders
|
||||
type Good struct { ID string }
|
||||
`), parser.ParseComments)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// cleanViolations 是干净应用文件的类型扫描结果。
|
||||
cleanViolations := checkApplicationTypeLeaks("internal/application/orders/clean.go", cleanSyntax, fset)
|
||||
if len(cleanViolations) != 0 {
|
||||
t.Fatalf("clean violations=%+v", cleanViolations)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user