Compare commits
20 Commits
v0.10.0-pr
...
v0.18.0-pr
| Author | SHA1 | Date | |
|---|---|---|---|
| 4563a6354f | |||
| 5fc82509a1 | |||
| 8b588a24cd | |||
| 8493f9dae9 | |||
| 64e3bd6cfc | |||
| 1f2d8b6a6a | |||
| 16deead70e | |||
| adcb6f60b6 | |||
| b8ab6b3521 | |||
| da8356e18b | |||
| f5478cdc76 | |||
| f9afb8f8dd | |||
| f0991d3ff2 | |||
| 8a348951eb | |||
| 158b8002b3 | |||
| eb69260586 | |||
| a2a07a39ea | |||
| bca2ef92cc | |||
| 6e3f025aad | |||
| 2041e7f661 |
85
API.md
85
API.md
@@ -1,9 +1,13 @@
|
||||
# API 契约与项目生成
|
||||
|
||||
当前生成目录和启动扩展采用新布局,见 [项目维护指南](PROJECT.md)。`@app(defaultResponse: Name)` 在入口选择 App 默认响应。`@body(mode: raw|multipart)` 和 `@produces` 支持自动 Handler,手写 Handler 仅为显式选择的扩展。
|
||||
|
||||
首次接入请先看 [README](README.md):包含固定远程版本安装、可运行项目、完整 `.api` 示例和已有服务接入步骤。本页用于查阅语法与生成规则;下方带 `--framework-dir` 的命令用于框架源码开发。
|
||||
|
||||
本阶段目标:从 `.api` 定义生成可运行的 Go 项目;定义、绑定、校验与 OpenAPI 保持一致;重复生成不覆盖业务实现;业务代码继续独立于网络层。实现入口是 `cmd/go-omni/apigen`,运行时契约编译器是 `contract`。
|
||||
|
||||
v0.14 的 [完整开发指南](DEVELOPMENT.md) 说明响应根类型、`@request`、multipart 字段、注释/格式化、Go 错误目录导出与共享契约。
|
||||
|
||||
## 与 go-zero 的对应关系
|
||||
|
||||
调研参考了 go-zero 官方的 [API DSL](https://go-zero.dev/reference/api-dsl/)、[参数规则](https://go-zero.dev/reference/api-dsl/parameter/)、[goctl API](https://go-zero.dev/reference/cli-guide/api/) 和 [项目结构](https://go-zero.dev/concepts/project-structure/)。借鉴其声明接口后生成项目的工作方式:统一的类型定义、路由声明、分组、前缀和命名中间件。实现独立编写,不引入 go-zero 或 Gin 运行时依赖。
|
||||
@@ -15,9 +19,9 @@
|
||||
| 熟悉的命令别名 | `api go -api user.api -dir .` |
|
||||
| 校验契约 | `api validate --api user.api` |
|
||||
| CI 检查生成产物是否过期 | `api generate --api user.api --dir . --check` |
|
||||
| 业务逻辑目录 | `internal/service/<group>`,沿用 Omni 的 Service 分层 |
|
||||
| 依赖装配 | `internal/servicecontext` |
|
||||
| 接口文档 | 自动生成 `docs/openapi.json`,同时提供 `/openapi.json` |
|
||||
| 业务逻辑目录 | `internal/logic/<group>`,使用标准 context.Context |
|
||||
| 依赖装配 | `internal/svc` |
|
||||
| 接口文档 | 自动生成 `docs/openapi.json`,HTTP 端点由 `server.endpoints.openapi` 配置 |
|
||||
|
||||
这是明确限定的 go-zero 风格方言,**不是完整 goctl 兼容实现**。不支持的声明和校验标签会在生成前报错。
|
||||
|
||||
@@ -31,10 +35,10 @@ go run ./cmd/go-omni api new \
|
||||
|
||||
cd ../omniDemo
|
||||
go mod tidy
|
||||
go run ./cmd/server
|
||||
go run .
|
||||
```
|
||||
|
||||
访问 `http://127.0.0.1:8080/api/greet/Alice` 可得到带 `data.message` 的统一响应。`api new` 的问候示例包含可运行逻辑;自定义契约新生成的 Service 会明确返回 501,等待业务实现。
|
||||
访问 `http://127.0.0.1:8080/api/greet/Alice` 可得到带 `data.message` 的统一响应。`api new` 的问候示例包含可运行逻辑;自定义契约新生成的 Logic 会明确返回 501,等待业务实现。
|
||||
|
||||
用现成的多分组定义生成项目,在 go-omni 仓库执行:
|
||||
|
||||
@@ -57,11 +61,11 @@ go-omni new --api user.api --module example.com/userService \
|
||||
- `--storage none|local|s3|r2|oss`:默认 none,生成统一 Files / Uploads / Downloads / UploadLinks 依赖;本地签名对象端点为 `/storage/object`。
|
||||
- `--log-output stdout|stderr|file` 与 `--log-format json|text`:配置和无配置文件启动使用一致默认值。
|
||||
|
||||
初次选项记录在 `.goOmni/apiManifest.json`,以后直接 `api generate` 会继承。已有项目通过参数切换选项会报错,因为初始化代码属于业务方;可在新目录生成对照后手动迁移。`--force` 不允许覆盖 API 项目。旧式 CRUD 脚手架仍保留。
|
||||
初次选项记录在 `.goOmni/apiManifest.json`,以后直接 `api generate` 会继承。已有项目通过参数切换选项会报错,因为初始化代码属于业务方;可在新目录生成对照后手动迁移。`--force` 不允许覆盖 API 项目。new 与 api new 统一生成契约项目,不再使用旧式 CRUD 目录。
|
||||
|
||||
MySQL 密钥只从 `GO_OMNI_DB_DSN` 获取,非秘密池配置位于 YAML,环境变量优先。存储使用原有 `GO_OMNI_STORAGE_*`;API 项目不会额外生成公开的签名发放接口,鉴权和对象权限由业务实现。初始化失败会清理已打开资源,退出清理可重复调用。
|
||||
MySQL 使用官方 `database/mysql` 结构化适配器,密码从 `GO_OMNI_DB_PASSWORD` 覆盖,非秘密池配置位于 YAML,环境变量优先。存储使用原有 `GO_OMNI_STORAGE_*`;API 项目不会额外生成公开的签名发放接口,鉴权和对象权限由业务实现。初始化失败会清理已打开资源,退出清理可重复调用。
|
||||
|
||||
HTTP 请求类型不能推导数据库表结构;迁移和 Repository 由业务自己维护。完整案例见 [用户管理示例](examples/userService/README.md),覆盖真实 MySQL、事务审计、鉴权、文件导出和日志。
|
||||
HTTP 请求类型不能推导数据库表结构;迁移和 Repository 由业务自己维护。当前目录的完整示例见 [managedService](examples/managedService/README.md)。MySQL、事务审计和鉴权实现可参考 [旧目录用户管理示例](examples/userService/README.md),接入时按 [项目指南](PROJECT.md) 装配到 config/svc/logic。
|
||||
|
||||
## 接口定义
|
||||
|
||||
@@ -97,20 +101,22 @@ service user-api {
|
||||
|
||||
- 可省略的 `syntax = "v1"`;`info` 中的 title、version 和描述性元数据。
|
||||
- 单独的 `type X { ... }` 与 `type (...)`;基础标量、指针、嵌套命名类型、切片、字符串键 map。嵌套对象只使用 JSON 字段。
|
||||
- 相对 `.api` 导入及 `import (...)`,重复导入去重;拒绝循环和越过入口目录的导入。单文件 1 MiB、总计 8 MiB、最多 64 个文件,限制导入/类型深度及 schema 展开规模。
|
||||
- 相对 `.api` 导入及 `import (...)`,重复导入去重;拒绝循环和未授权的目录越界。跨服务共享须在 `omni.imports.json` 声明 roots,见 [共享契约](DEVELOPMENT.md#两个服务共享契约)。单文件 1 MiB、总计 8 MiB、最多 64 个文件,限制导入/类型深度及 schema 展开规模。
|
||||
- 同一 service 名称的多个块;`@server` 的 group、prefix、middleware、auth、errors、response,group 可以写 `admin/user`。
|
||||
- 每个路由必须有 `@handler`;可选单行/多行 `@doc`、`@auth`、`@errors`、`@response`;GET/POST/PUT/PATCH/DELETE/HEAD/OPTIONS。
|
||||
- 可省略请求或响应;有响应默认 200,无响应默认 204。Omni 扩展 `@status 201`、`@status 202`;204 不允许响应类型,HEAD 不允许响应体,GET/HEAD 不允许 JSON 请求体。
|
||||
- 每个请求字段只使用一个来源:`json`、`path`、`form`、`header`。本版 `form` 明确表示 URL query,不解析 multipart 或表单 body。query 切片用重复键;header/path 使用标量。
|
||||
- 每个请求字段只使用一个来源:`json`、`path`、`form`、`header`,或声明式上传的 `multipart`、`multipartJSON`、`file`。`form` 始终表示 URL query;query 切片用重复键,header/path 使用标量。上传示例与限额见 [声明式 multipart](DEVELOPMENT.md#声明式-multipart)。
|
||||
- 来源标签中的 `optional` / `omitempty`、标量 `default`、标量 `options=a|b`、数值 `range=[1:100]`(支持开区间和单边界)。
|
||||
- `binding` 的 `required`、`min`、`max`、`len`、`email`。长度按 Unicode 字符或切片元素数;范围边界必须有限且位于 ±2^53 内。
|
||||
|
||||
暂不支持匿名嵌入、别名、递归类型、未声明的外部 Go 类型、自定义 JSON 编解码、`[]byte`、泛型、`json:",string"`、任意 validator 标签、go-zero 的 JWT/签名/SSE 声明,以及客户端 SDK 生成。认证验证通过声明关联的命名中间件实现。未列出的语法会明确报错。
|
||||
暂不支持匿名嵌入、别名、递归类型、未声明的外部 Go 类型、未通过 scalar 显式接入的自定义编解码、`[]byte`、泛型、`json:",string"`、任意 validator 标签、go-zero 的 JWT/签名/SSE 声明,以及客户端 SDK 生成。认证验证通过声明关联的命名中间件实现。未列出的语法会明确报错。
|
||||
|
||||
业务错误、具名鉴权、多行文档及下述开发命令从 CLI `v0.7.0-preview.1` 提供。公共响应模板从 `v0.8.0-preview` 提供,CLI 与业务项目中的框架依赖需一起升级。
|
||||
|
||||
## 业务错误:声明、代码和文档共用一份定义
|
||||
|
||||
**错误目录也可以只在 Go 中维护。** 从 `v0.11.1-preview` 起,生成路由没有声明错误列表时,继承 `omni.WithErrors(catalog)` 注册的 App 目录;`.api` 继续定义接口和返回格式,无需重复登记错误。全局中间件提前返回同样适用。显式引用 `.api` 错误时才使用路由独立目录,规则见 [目录继承](errors/README.md)。CLI 不执行项目 Go 配置;可显式用 `errors.ExportCatalog` 导出 `omni.errors.json`,供文档合并同一目录,见 [导出步骤](DEVELOPMENT.md#字段文档格式化go-错误目录导出)。
|
||||
|
||||
从 `v0.10.0-preview` 起,推荐用 `errorCatalog` 同时定义逻辑标识、对外业务码、提示和 HTTP 状态:
|
||||
|
||||
```text
|
||||
@@ -130,6 +136,21 @@ errorCatalog CommonErrors (
|
||||
|
||||
同一 HTTP 200 下,OpenAPI 用 `anyOf` 保留成功 DTO 和错误体,日志与指标仍识别业务失败。完整语法、路由作用域与手写项目配置见 [统一错误目录](errors/README.md),可运行契约见 [errors.api](examples/contracts/errors.api)。
|
||||
|
||||
从 `v0.11.0-preview` 起,目录条目支持 `params` 有序列表与 `{name}` 提示模板:
|
||||
|
||||
```json
|
||||
"TextTooLong": {
|
||||
"key": "TEXT_TOO_LONG", "code": 400002,
|
||||
"message": "{label}不能超过{max}个字符",
|
||||
"params": [
|
||||
{"name": "label", "type": "string", "example": "企业名称"},
|
||||
{"name": "max", "type": "int", "example": 100}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
生成 `apierrors.TextTooLong(label string, max int)`,返回时 Message 已填好;固定提示保持无参数函数。声明顺序决定参数顺序。模板校验、运行时缺参/类型错误、转义及非 HTTP 使用规则见 [提示模板参数](errors/README.md)。OpenAPI 的错误目录保留参数定义,响应示例使用具体文案。
|
||||
|
||||
原有声明继续支持:
|
||||
|
||||
```go
|
||||
@@ -145,7 +166,7 @@ error (
|
||||
名称用 UpperCamelCase,HTTP 状态允许 200–599 中可携带响应体的最终状态(排除 204/205/304),旧声明的逻辑错误码用大写字母/数字/下划线且全局唯一,message 是可公开返回的稳定提示。`Catalog` 为生成函数保留名称。
|
||||
|
||||
`@server (errors: Unauthenticated)` 声明组级错误;路由 `@errors UserNotFound, EmailTaken` 追加到组级列表并去重。
|
||||
`@errors none` 清空继承的业务错误。未声明、重复声明或生成符号冲突会报告源码位置。
|
||||
`@errors none` 清空 `.api` 分组继承的错误列表,随后按未设置路由目录处理,继承 App 错误目录。未声明、重复声明或生成符号冲突会报告源码位置。
|
||||
|
||||
生成 `internal/apierrors/errorsGen.go`:
|
||||
|
||||
@@ -221,27 +242,29 @@ service user-api {
|
||||
```sh
|
||||
go-omni api validate # 发现 manifest 中的入口并校验
|
||||
go-omni api inspect # 方法、路径、Handler、鉴权与业务错误总览
|
||||
go-omni api generate --dry-run # 预览新增/更新/移除,完全不写文件
|
||||
go-omni api generate --dry-run # 预览文件变化、契约变更和 Logic 提示,完全不写文件
|
||||
go-omni api generate # 实际生成,保留手写业务
|
||||
go-omni api generate --check # 只读检查,可用于 CI
|
||||
go-omni api generate --check # 只读检查产物漂移及明确的 Logic 签名错误,可用于 CI
|
||||
go-omni api export > openapi.json # 独立导出纯 JSON,无需启动服务
|
||||
go test ./...
|
||||
```
|
||||
|
||||
`--dry-run` 和 `--check` 不能组合。新项目预览也不创建目录;预览不是审批或锁定,实际生成会重新检查文件状态。
|
||||
契约变化会定位到相关 Logic 文件,涵盖嵌套 DTO、原始 body 和流式返回。生成后签名不一致的问题仍可由 `--check` 检出;项目类型别名/build tags 等不确定情况提示 REVIEW,最终用 `go test ./...` 验证。详见 [契约变更提示](PROJECT.md#契约变更提示)。
|
||||
在项目外可显式传 `--api`;生成时传 `--dir` 可定位其他项目。不会猜测多个 .api 中哪个是入口。
|
||||
路由总览的 `(undeclared)` 仅表示没有声明 auth;旧式中间件仍可能做认证。
|
||||
外发文档可用 export;项目内受生成器管理的 `docs/openapi.json` 请通过 generate 更新,以同步文件哈希。
|
||||
|
||||
## 迁移扩展(v0.9.0-preview)
|
||||
## 迁移扩展
|
||||
|
||||
- 动态 JSON 字段 `any` / `interface{}`,包含切片和 map;外围契约仍严格。
|
||||
- `scalar Name (...)` 显式引用项目类型,声明 JSON 输入/输出类型及文本绑定;支持已有 JSON/Text 转换方法。
|
||||
- `@body (mode: json/raw/multipart; maxBytes: ...)`:按路由限制请求体。
|
||||
- `@binding custom` 生成保留的绑定 hook;`@handlerMode manual` 生成保留的整个 Handler,raw/multipart 使用这个入口。
|
||||
- manual 可用 `@produces "text/plain"` 等声明无 DTO 成功输出。默认状态 200,初始手写 stub 返回 501。
|
||||
- `@binding custom` 生成保留的绑定 hook;`@handlerMode manual` 生成保留的整个 Handler,仅用于自定义协议。raw 与未声明上传字段的 multipart 将受限流交给 Logic;声明了上传字段时则交给 Logic 已校验的 DTO。
|
||||
- `@produces "text/plain"` 等声明无 DTO 成功输出;自动模式的 Logic 返回 `*httpx.Stream`,Handler 负责流式发送和关闭。默认状态 200。manual 模式保留项目自己的输出实现。
|
||||
- 自动 multipart Handler 检查未读部分和结束边界;损坏/截断默认返回 400,超限为 413。公共状态、码和文案仍可用项目错误目录配置,见 [流式接入与错误处理](PROJECT.md#自动上传回调与下载)。
|
||||
|
||||
完整语法、边界和示例见 [迁移扩展指南](MIGRATION.md)。新增生成代码要求 CLI 与框架依赖一起升级到 `v0.9.0-preview`;不兼容全部 goctl 语法。
|
||||
迁移扩展起始于 v0.9.0,原有手写扩展说明见 [迁移扩展指南](MIGRATION.md)。当前根目录入口和自动流式 Handler 要求 CLI 与框架依赖一起使用 v0.14.0-preview;版本说明和新目录维护规则见 [项目指南](PROJECT.md)。不兼容全部 goctl 语法。
|
||||
|
||||
## 参数语义
|
||||
|
||||
@@ -249,25 +272,25 @@ go test ./...
|
||||
|
||||
业务需要区分“没传”和“传了零值”时使用 `*T` 加 optional。指针允许 JSON null;普通非指针字段拒绝 null(动态 `any` 允许);`binding:"required"` 拒绝指针 null。缺失与 null 都会得到 nil,本版没有三态 Nullable 类型。响应端的 `optional` 不等于 Go 编码器的 `omitempty`;需要省略零值时显式使用 `omitempty`。
|
||||
|
||||
JSON 必须是一个 object,Content-Type 为 application/json。默认最多 1 MiB,包含 chunked/未知长度和尾部空白;超限返回 413。重复 JSON key、未知字段、多个 JSON 值、标量 query/header 重复值、格式和约束错误返回 400;错误 Content-Type 返回 415。失败不会部分修改目标 DTO;错误详情只保留在内部 cause,公共响应不回显原始参数。
|
||||
普通 JSON 请求体必须是一个 object,Content-Type 为 application/json;响应根类型另支持数组、nullable 对象和 null。默认最多 1 MiB,包含 chunked/未知长度和尾部空白;超限返回 413。重复 JSON key、未知字段、多个 JSON 值、标量 query/header 重复值、格式和约束错误返回 400;错误 Content-Type 返回 415。失败不会部分修改目标 DTO;错误详情只保留在内部 cause,公共响应不回显原始参数。
|
||||
|
||||
`contract.Binder` 在服务启动时编译,随后并发复用,不会每次请求重新分析标签。CLI 用相同编译器产生文档,因此来源、必填、默认、nullable、枚举、范围和长度具有同一来源。响应类型和约束由业务实现遵守,不在每次响应上再次校验;非 nullable 集合请返回空集合而不是 nil。
|
||||
`contract.Binder` 在服务启动时编译,随后并发复用,不会每次请求重新分析标签。CLI 用相同编译器产生文档,因此来源、必填、默认、nullable、枚举、范围和长度具有同一来源。响应类型和约束由业务实现遵守,不在每次响应上再次校验;嵌套的非 nullable 集合请返回空集合而不是 nil,根 `returns ([]Item)` 会把 nil slice 规范为 `[]`。
|
||||
|
||||
## 生成目录和业务边界
|
||||
|
||||
```text
|
||||
api/ # 开发者维护的契约及导入
|
||||
cmd/server/main.go # 启动、日志、优雅退出
|
||||
main.go # 启动、日志、优雅退出
|
||||
etc/config.yaml
|
||||
internal/settings/
|
||||
internal/servicecontext/ # 依赖初始化和释放
|
||||
internal/types/typesGen.go
|
||||
internal/config/
|
||||
internal/svc/ # 依赖初始化和释放
|
||||
internal/types/*Gen.go
|
||||
internal/apierrors/errorsGen.go # 声明错误后生成
|
||||
internal/handler/routesGen.go
|
||||
internal/handler/openapiGen.go
|
||||
internal/handler/<group>/handlersGen.go
|
||||
internal/service/<group>/service.go
|
||||
internal/service/<group>/<operation>.go
|
||||
internal/logic/<group>/logic.go
|
||||
internal/logic/<group>/<operation>.go
|
||||
internal/repository/<group>/repository.go
|
||||
internal/middleware/registryGen.go
|
||||
internal/middleware/<name>.go
|
||||
@@ -275,7 +298,7 @@ docs/openapi.json
|
||||
.goOmni/apiManifest.json
|
||||
```
|
||||
|
||||
Service 方法接收标准 `context.Context` 与生成的请求 DTO;Handler 调用 Service 并完成响应包装。网络适配器仍经 Omni/httpx 接入。Repository 是业务所有的扩展位置,不自动生成虚构 CRUD 查询。
|
||||
Logic 方法接收标准 `context.Context` 与生成的请求 DTO;Handler 调用 Logic 并完成响应包装。网络适配器仍经 Omni/httpx 接入。Repository 是业务所有的扩展位置,不自动生成虚构 CRUD 查询。
|
||||
|
||||
命名中间件按声明顺序组合,第一个在最外层。新 hook 初始返回 501,必须实现后才放行;registry 自动维护,hook 文件只创建一次。缺失 hook 在注册任何路由前报错。文档通过 `x-omni-middleware` 标出命名 hook,不把名称误当成已实现的认证协议。
|
||||
|
||||
@@ -290,7 +313,7 @@ go-omni api generate --api api/user.api --dir . --check
|
||||
go test ./...
|
||||
```
|
||||
|
||||
`Gen.go` 和 OpenAPI 由生成器管理,manifest 保存其 SHA-256。手动修改后更新会报冲突。Service、Repository、main、配置、依赖装配和中间件实现按字节保留;只补缺失文件。删除接口/分组时只移除旧的生成文件,保留业务代码;已删除类型仍被旧业务引用时,需要明确迁移该业务文件,编译器会提示。
|
||||
`main.go`、`Gen.go` 和 OpenAPI 由生成器管理,manifest 保存其 SHA-256。手动修改后更新会报冲突。Logic、Repository、配置、依赖装配和中间件实现按字节保留;只补缺失文件。删除接口/分组时只移除旧的生成文件,保留业务代码;已删除类型仍被旧业务引用时,需要明确迁移该业务文件,编译器会提示。
|
||||
|
||||
`--check` 只读,检查产物是否过期,以及所需骨架是否缺失。契约复制到项目后,请编辑项目内的 API;外部定义与已存在副本不一致时会拒绝覆盖。一个项目由一个 API 入口管理,可以通过导入拆分文件。
|
||||
|
||||
@@ -302,7 +325,9 @@ go test ./...
|
||||
|
||||
```sh
|
||||
go test -race -p=2 ./contract ./openapi ./cmd/go-omni/...
|
||||
python3 scripts/verifyAPI.py
|
||||
python3 scripts/verifyManagedProject.py
|
||||
```
|
||||
|
||||
Go 测试覆盖契约拒绝规则、多源绑定、零值/缺失、默认/范围/嵌套集合、文档约束、幂等更新、只读检查、业务保留、删除路由、路径边界及生成项目编译。Python 验收会在临时目录生成项目,分别启动 nethttp/native,验证真实 HTTP 请求、中间件、日志 requestId 和优雅退出;结束后清理自己创建的进程和临时文件。结果记录在 `performance/apiGenerationLocal.json`。
|
||||
Go 测试覆盖契约拒绝规则、多源绑定、零值/缺失、默认/范围/嵌套集合、文档约束、幂等更新、只读检查、业务保留、删除路由、路径边界及生成项目编译。Python 验收会在临时目录生成项目,分别启动 nethttp/native,验证真实 HTTP 请求、中间件、日志 requestId 和优雅退出;结束后清理自己创建的进程和临时文件。结果记录在 `performance/managedProjectLocal.json`;完整记录见 [第一版验收](performance/managedProjectAcceptance.md)。
|
||||
|
||||
内置健康与 OpenAPI 路由可通过配置禁用或修改路径,`.api` 不再固定保留 `/health/live`、`/health/ready`、`/openapi.json`。启用的内置路径与业务 GET 路由重叠时,生成的 NewApp 在监听前返回配置错误;禁用后可以由 `.api` 自己维护这些路径。见 [内置接口配置](PROJECT.md#配置内置接口)。
|
||||
|
||||
26
BUSINESS_DEVELOPMENT.md
Normal file
26
BUSINESS_DEVELOPMENT.md
Normal file
@@ -0,0 +1,26 @@
|
||||
# 通用业务开发能力完善计划
|
||||
|
||||
本轮目标:业务维护契约、配置、依赖装配与业务逻辑,框架持续维护 HTTP 接入、契约校验、通用运行期与文档。按实际生成项目和可删除的业务适配点验收,不写入 eTax 业务规则。
|
||||
|
||||
## 实施与验收清单
|
||||
|
||||
- [x] 运行期:资源登记、逆序释放、幂等关闭、失败回滚、故障原因保留归框架;生成入口与项目依赖初始化边界明确。多 App 日志隔离、标准 log.Logger 适配、可信请求元数据与依赖中间件示例。
|
||||
- [x] 配置:项目完整结构体环境覆盖;默认值→文件→环境;嵌套、duration、slice、显式零值、空值策略、必填与脱敏;日志无副作用完整校验。
|
||||
- [x] 数据库:官方 MySQL 结构化配置/TLS/驱动日志适配,继续复用 OpenConnector 和唯一连接池;独立启动检查、失败清理、生成器接入;不内置版本/表结构/业务错误策略。
|
||||
- [x] 响应契约:数组、可空对象、成功 null、独立 HTTP 204;Logic/Handler/OpenAPI/漂移检查一致。
|
||||
- [x] 请求契约:未知 query、JSON UTF-8/charset、无 Body/必填空对象/可选 Body,App 默认与路由覆盖;scalar/any/数组/大小限制一致,普通 JSON 只绑定一次。
|
||||
- [x] 上传:声明文件/文本/JSON 字段,重复/未知/必填/单字段/单文件/总量限制;有界暂存与清理,调用 Logic 前完成普通表单校验;保留流式扩展与 Complete 约定。
|
||||
- [x] 文档工具:字段注释、example/枚举描述进入 DTO 与 OpenAPI;保注释、幂等格式化及 check;显式 Go 错误目录元数据导出与文档合并,不自动执行项目代码。
|
||||
- [x] 共享契约:受控 roots/摘要、循环/深度/文件数/路径保护;分源文件 DTO、多个独立 module 的工作区检查;版本提示与基础模板升级预览,保留业务文件。
|
||||
- [x] 集成:实际生成项目验证新能力、连续生成、业务文件保留、失败清理、nethttp/native 请求与退出;现有错误继承/参数/上传下载回归。
|
||||
- [x] 最终回归:全量 race(含生成器集成)、vet、build、格式化/链接检查;使用说明、迁移步骤及验收报告完整,发布后远程无 replace 验收。
|
||||
|
||||
已发布 `v0.14.0-preview`。逐项验收、可替换的业务适配点与远程报告见 [验收记录](reports/businessDevelopmentAcceptance.md);接入和升级见 [开发指南](DEVELOPMENT.md)。
|
||||
|
||||
## 核心约定
|
||||
|
||||
响应与错误码由项目配置。字段与请求策略由契约表达;基础设施采用可选适配器,保留标准 Connector。所有业务依赖在请求排空后关闭;框架不重试写操作、不自动迁移业务表、不关闭借用资源。
|
||||
|
||||
普通 multipart 为保证进入 Logic 前完成校验,以有界复制暂存到临时文件;框架清理自己创建的资源。纯流式路径明确由调用方在提交持久副作用前完成输入检查。
|
||||
|
||||
不修改 eTax/ePay 业务,不连接既有业务服务器。真实基础设施如需验证,只使用本轮隔离资源。验收报告明确区分单元测试、生成消费者测试与真实服务测试。
|
||||
35
CHANGELOG.md
35
CHANGELOG.md
@@ -1,5 +1,40 @@
|
||||
# Changelog
|
||||
|
||||
## v0.13.0-preview(2026-09-24)
|
||||
|
||||
- 新增通用 lifecycle.Controller,svc.RequestShutdown 支持依赖主动通知正常停止或致命故障,通知不阻塞,首个故障原因保留;生成入口排空 HTTP 后关闭依赖与日志,故障退出码为 1。
|
||||
- App.RunContext、ServeContext、RunWith 保留自定义 context 取消原因;正常信号取消仍正常返回。
|
||||
- 健康检查默认使用 App 响应模板和错误目录,支持 raw JSON;HTTP 状态保持 200/503,避免业务 HTTP 200 策略掩盖未就绪。内部探针原因仅供日志,失败状态继续记录到日志/指标。
|
||||
- 生成项目通过 server.endpoints 配置 liveness/readiness/openapi 的开关与路径;全局中间件继续生效,OpenAPI 保持原始规范。禁用端点不保留路径,启用路径与业务 GET/参数/通配路由冲突时在监听前报错。
|
||||
- 生成器最低框架版本更新到 v0.13.0-preview;manifest v2 和业务文件保持。v0.12 项目需要显式升级其 svc 扩展,main/Handler 由重新生成更新。
|
||||
- 增加故障退出、排空/清理顺序、初始化期间通知、子进程退出码、健康格式、损坏编码器、端点禁用/改名/冲突的回归验收。
|
||||
|
||||
## v0.12.0-preview(2026-09-24)
|
||||
|
||||
- 生成项目统一采用根目录 main.go 和 config/svc/logic/handler;new 与 api new 共用受管理生成器,manifest v2 不兼容旧目录。
|
||||
- main 和 HTTP 装配持续生成;svc 提供 AppOptions、GlobalMiddleware、OnClose 和幂等 Close,初始化失败回收资源,正常退出先排空 HTTP 再关闭项目依赖及日志。
|
||||
- 入口 @app(defaultResponse: Name) 自动接入 App 默认响应;生成路由、全局中间件及 404/405 共用策略,Go 错误目录保持单处注册。
|
||||
- raw/multipart 输入和 @produces 流式输出支持自动 Handler,Logic 接收受限请求流/返回可关闭内容流,保留显式手写扩展。
|
||||
- 新增真实生成项目生命周期、流式输入输出、文件保留和入口漂移检查,提供可运行 managedService 示例与 PROJECT.md。
|
||||
- multipart 流式读取保留解析错误,检查未消费部分与最终边界;损坏请求默认 400,超限保持 413,错误目录可定制公开响应。配置校验与实际 App 响应编码器/错误目录装配使用同一规则,冲突在构造前报错。
|
||||
- manifest 保存契约快照;生成/预览/检查提供接口类型、嵌套 DTO、scalar、body/流式模式变更提示,定位受影响的 Logic。轻量源码检查识别签名、语法和重复方法问题;--check 在重新生成后仍拒绝明确的业务签名错误,保留所有手写文件,类型别名/build tags 等不确定情况提示人工复查。
|
||||
|
||||
|
||||
## v0.11.1-preview
|
||||
|
||||
- 修复生成路由用空错误目录遮蔽 App 目录的问题;空 WithErrors 完全透传,避免 Context 与 Encoder 二次解析时丢失状态、业务码和中文提示。
|
||||
- 未声明错误列表的路由继承 App,支持错误只在 Go 中维护;显式路由目录保持独立,响应格式、RawJSON、body limit 和全局中间件提前返回保持正确策略。
|
||||
- 兼容已有 v0.11.0 生成代码;CLI 不再为空列表绑定目录。补充纯 Go 错误目录的实际生成消费者与回归验收。
|
||||
|
||||
## v0.11.0-preview
|
||||
|
||||
- 错误目录增加有序 params 和命名提示模板,生成带类型的错误函数;固定提示保持兼容,业务不再自行拼接文案。
|
||||
- 新增 WithParams、参数校验与共用解析器;复制请求参数、保留错误链,重复解析和缓冲链路保留填充结果。非法调用转系统兜底,访问日志提供不含参数值的诊断。
|
||||
- OpenAPI 错误目录同步参数类型/示例,响应示例由运行时解析器生成;JSON/YAML 配置同样支持模板。
|
||||
- 本地/共享幂等重放保存已解析公开错误快照,不保存原始参数或 cause;Redis Lua 重写记录不损失 int64 业务码精度。
|
||||
- 模板编译拒绝未声明/未使用参数、非法类型及示例;模板错误不能再维护同标识的旧 messages 映射。
|
||||
- CLI 新生成代码最低要求 v0.11.0-preview;已生成的固定错误函数和 v0.10.0 调用继续支持。
|
||||
|
||||
## v0.10.0-preview
|
||||
|
||||
- 新增 App/路由独立错误目录:`omni.WithErrors`、`errors.Catalog`、`ByCode` 与不可变 Registry,业务只选逻辑标识;对外码和提示由项目配置。
|
||||
|
||||
296
DEVELOPMENT.md
Normal file
296
DEVELOPMENT.md
Normal file
@@ -0,0 +1,296 @@
|
||||
# 通用业务开发指南(v0.18.0-preview)
|
||||
|
||||
从 [README](README.md) 创建服务后,业务主要维护 `.api`、配置、`svc.Init`、Logic、Repository 和项目中间件。本页说明通用接入能力;字段、错误码、权限、表结构和事务边界都由项目定义。
|
||||
|
||||
本版新增的公共字段组合、可选结构映射、自动中间件工厂和 Repository 开关,见 [生成能力指南](GENERATION.md)。
|
||||
|
||||
## 返回数组、可空对象和 null
|
||||
|
||||
```text
|
||||
@handler list
|
||||
get /items returns ([]Item)
|
||||
|
||||
@handler find
|
||||
get /item returns (*Item)
|
||||
|
||||
@handler save
|
||||
post /item returns (null)
|
||||
|
||||
@handler remove
|
||||
delete /item
|
||||
```
|
||||
|
||||
| 契约 | Logic 返回 | 成功数据 |
|
||||
| --- | --- | --- |
|
||||
| `returns (Item)` | `(*types.Item, error)` | 对象;nil 被视为实现错误 |
|
||||
| `returns ([]Item)` | `([]types.Item, error)` | 对象数组;nil 统一输出 `[]` |
|
||||
| `returns ([]string)` | `([]string, error)` | 字符串数组 |
|
||||
| `returns ([]int64)` / `([]bool)` | `([]int64, error)` / `([]bool, error)` | 数值 / 布尔数组 |
|
||||
| `returns ([]any)` | `([]any, error)` | 动态 JSON 数组,元素允许对象、数组、标量和 null |
|
||||
| `returns (*Item)` | `(*types.Item, error)` | 对象或 `null` |
|
||||
| `returns (null)` | `error` | HTTP 200 和空业务数据 |
|
||||
| 不写 returns | `error` | HTTP 204,无响应体 |
|
||||
|
||||
统一响应中的空数据是否显示,仍遵循项目模板的 `dataMode`;需要 `data:null` 就设置 `dataMode: always`。`@response raw` 输出业务数据本身,包括 `[]`、对象或 `null`。可用 `@status 201/202`;204 不能声明响应类型。嵌套字段继续支持既有 slice/map/pointer/scalar/any。
|
||||
|
||||
基础类型数组还支持 int/int8/int16/int32、uint/uint16/uint32/uint64、float32/float64。nil 根数组都输出 `[]`;`[]any` 在 OpenAPI 中是 `type: array, items: {}`,不限制元素形状,但元素必须可被标准 JSON 编码器序列化。`[]uint8`(Go 的 byte slice)会编码为 base64,故明确拒绝;数字数组可用 `[]int32`,二进制用下载接口。当前不支持根嵌套数组或根自定义 scalar 数组。
|
||||
|
||||
## 契约驱动的请求策略
|
||||
|
||||
入口文件可以声明默认策略,单个接口按需覆盖:
|
||||
|
||||
```text
|
||||
@app (
|
||||
defaultResponse: CommonResponse
|
||||
request: {"unknownQuery":"reject", "utf8":"reject", "charset":"utf8"}
|
||||
)
|
||||
|
||||
@handler create
|
||||
@request (body: required)
|
||||
post /items (CreateReq) returns (Item)
|
||||
|
||||
@handler touch
|
||||
@request (body: emptyObject)
|
||||
post /touch returns (null)
|
||||
```
|
||||
|
||||
- `unknownQuery: reject|ignore`:是否拒绝未声明的 URL 查询字段;重复标量仍拒绝,数组用重复的同名 query。
|
||||
- `utf8: reject|replace`:拒绝原始非法 UTF-8,或采用 JSON 的替换字符行为。不是只检查解码后的字符串。
|
||||
- `charset: utf8|any`:`utf8` 仅接受未声明 charset 或 `utf-8`;不会对其他编码做转码。
|
||||
- `body: auto|required|optional|none|emptyObject`:auto 按 JSON 字段推断;required 要求一个对象;optional 允许完全没有 body,但提供 body 后仍完整校验;none 禁止任何 body 字节;emptyObject 必须是 `{}`。
|
||||
|
||||
未设置的项继承 App 的 `config.Config.Request`,最后采用既有默认行为。`.api` 的 `@app` 显式字段优先于项目配置;路由 `@request` 优先于 App。raw/multipart 使用各自解析流程,不能套 JSON body 策略。普通 JSON 只解析一次;重复/未知 JSON 字段、约束、scalar 和 `any` 仍使用同一编译器。自定义 binder/manual Handler 的输入逻辑由项目显式负责。
|
||||
|
||||
没有 body 与提供 `{}` 不等价。optional 无 body 时 JSON 字段保留零值;需要业务区分是否出现某字段时使用 optional 指针字段。raw/multipart 不继承 App 的 JSON body 策略;GET/HEAD 如遇 App 的 required/optional/emptyObject 默认值,须明确覆盖为 auto 或 none。参数校验不会替代企业权限、记账规则或数据库约束。
|
||||
|
||||
## 声明式 multipart
|
||||
|
||||
```text
|
||||
type UploadReq {
|
||||
Title string `multipart:"title" binding:"required,max=80"`
|
||||
Metadata AttachmentMeta `multipartJSON:"metadata,maxBytes=16384"`
|
||||
Attachment File `file:"attachment,maxBytes=10485760"`
|
||||
Extras []File `file:"extras,optional,maxFiles=3,maxBytes=1048576"`
|
||||
}
|
||||
type AttachmentMeta {
|
||||
Category string `json:"category"`
|
||||
}
|
||||
|
||||
@handler upload
|
||||
@body (mode: multipart; maxBytes: 16777216; maxParts: 8; maxFieldBytes: 65536)
|
||||
post /attachments (UploadReq) returns (AttachmentResp)
|
||||
```
|
||||
|
||||
Logic 直接得到 `*types.UploadReq`,无需维护 Handler。`form` 仍表示 URL query;`multipart` 表示文本表单字段,可用基础类型、文本 scalar 和重复 scalar 数组;`multipartJSON` 是一个严格解码的 JSON 表单字段,可包含对象、数组和已声明的 scalar。
|
||||
|
||||
`File` 提供 `Name`、`ContentType`、`Size`、`Present()` 和 `Open() (io.ReadCloser, error)`。文件名和媒体类型属于客户端元数据,项目继续决定内容检测、存储权限和目标对象名。不要把客户端文件名直接用作存储路径。
|
||||
|
||||
```go
|
||||
reader, err := req.Attachment.Open()
|
||||
if err != nil { return nil, err }
|
||||
defer reader.Close()
|
||||
// 将 reader 传给项目选定的流式存储接口;不要先 io.ReadAll 整个大文件。
|
||||
```
|
||||
|
||||
所有声明字段、重复/未知字段、文件数量和各级限额都会在进入 Logic 前校验。缺少必填项、重复标量/JSON 字段、文件与文本类型混用、畸形 JSON 均失败。JSON 的类型、未知字段和重复 key 检查与普通 JSON 一致。
|
||||
|
||||
默认总量 1 MiB、最多 128 个 part、单文本/JSON 字段 64 KiB;单文件默认受总量约束,文件数组默认最多 16 个。`@body` 可设置 `maxBytes/maxParts/maxFieldBytes/maxFileBytes`;字段上的 `maxBytes/maxFiles` 再收紧对应限额。总量包括 MIME 头和结尾数据;传输编码的 part 被拒绝,避免字节含义不明确。
|
||||
|
||||
文件使用有界复制写入权限为 0600 的临时文件,文本/JSON 字段在各自限额内缓冲。生成 Handler 返回或 panic 时关闭打开的文件 reader 并删除临时文件;不能把请求的 File 留到异步任务使用。失败不会交付半个 DTO,也不会进入 Logic。临时磁盘容量、并发请求上限与 HTTP 超时由部署配置共同约束。
|
||||
|
||||
不声明 multipart 字段时,保留原来的 `body *httpx.Body` 流式入口,不创建临时文件。该方式适合特殊协议;项目应在不可撤销副作用之前完成校验并调用 `body.Complete()`。下载和原始回调继续使用 `@produces` / `@body(mode: raw)`。
|
||||
|
||||
## 完整业务配置的环境覆盖
|
||||
|
||||
生成项目默认执行:代码与 `envDefault` 默认值 → JSON/YAML 文件 → 全结构体环境覆盖 → 既有框架环境别名 → `Validate`。增加业务字段不需要另外写 `os.Getenv`。
|
||||
|
||||
```go
|
||||
type ClientConfig struct {
|
||||
Endpoint string `json:"endpoint" env:"CLIENT_ENDPOINT,required"`
|
||||
Token string `json:"-" env:"CLIENT_TOKEN,required,secret,empty=reject"`
|
||||
Timeout time.Duration `json:"timeout" envDefault:"3s"`
|
||||
Retries []int `json:"retries"`
|
||||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
```
|
||||
|
||||
把字段加入项目 `Config` 后,默认环境名取 `GO_OMNI_` 加 JSON 路径的 UPPER_SNAKE_CASE,如 `GO_OMNI_CLIENT_TIMEOUT`。显式 env 名是完整名称,可以使用自己的业务前缀。`env:"-"` 排除整个子树;`json:"-"` 字段只有显式 env 标签才参与。
|
||||
|
||||
duration 使用 `250ms`、`3s`;slice/map 使用 JSON,如 `[1,3,5]`、`{"retry":"2s"}`。支持嵌套、零/false、空数组、自定义 TextUnmarshaler。空环境值默认允许;可选择 `empty=ignore` 或 `empty=reject`。required 要求字符串非空、指针/集合非 nil;0 和 false 可以是合法业务值,范围由项目 Validate 决定。
|
||||
|
||||
手写项目可使用 `config.LoadProject(path, defaults, config.EnvOptions{Prefix:"MY_APP_"})`,或分别调用 `ApplyDefaults/DecodeFile/OverlayEnv`。读取失败不修改输入默认值;诊断不会打印输入值。不要主动打印含密钥的整个配置,或把底层解析错误展开到公开响应。Validate 不打开日志文件、数据库或存储连接;证书的可读性和握手信任在依赖初始化阶段验证。
|
||||
|
||||
### 一次覆盖完整配置与 HTTP/日志短名称
|
||||
|
||||
```go
|
||||
cfg, err = config.OverlayEnv(cfg, config.EnvOptions{
|
||||
Prefix: "MY_APP_",
|
||||
Empty: "reject", // allow / ignore / reject,项目自行选择
|
||||
Aliases: config.ServerEnvAliases("MY_APP_", "server"),
|
||||
// Lookup 可注入测试或项目配置源;默认 os.LookupEnv。
|
||||
})
|
||||
```
|
||||
|
||||
不要再紧接着调用 `ApplyEnv(cfg.Server, ...)`。上面的调用同时处理完整配置和 HTTP_ADDR、SERVICE、LOG_LEVEL 等框架短名称。例如 `MY_APP_SERVER_ADDRESS` 与 `MY_APP_HTTP_ADDR` 指向同一字段,**完整名称优先**;未设置时才读取短名称。完整名称显式设空仍优先:allow 交给该字段解析(string 可清空;数字等仍要求合法值),ignore 保留文件/默认值,reject 报错。不会因 ignore 再退回别名。字段 `env` 标签的 empty 规则优先于全局规则。
|
||||
|
||||
自定义别名用 `EnvOptions.Aliases`,键是完整环境名、值是按优先级排列的别名数组。两种名称都使用同一 Lookup、空值策略和类型转换;布尔 false、数值 0 与未设置不同。集合始终使用 JSON,包括 `MY_APP_TRUSTED_PROXIES='["127.0.0.1"]'`。
|
||||
|
||||
新项目模板一次调用 OverlayEnv,默认 `Empty: "ignore"`。旧 `ApplyEnv` 保留原有忽略空值、修剪空白及 TRUSTED_PROXIES 逗号列表行为。已有项目的 `internal/config` 属于开发者文件,升级不覆盖它;按上例合并原来的两次覆盖调用后,保留项目自己的 Validate。日志输出的显式完整名称也优先于短名称。
|
||||
|
||||
## 依赖、日志与中间件
|
||||
|
||||
`internal/svc/runtimeGen.go` 持续由生成器维护。项目的 ServiceContext 嵌入 `*lifecycle.Runtime`,只在 `Init(ctx)` 中装配额外依赖:
|
||||
|
||||
```go
|
||||
func (c *ServiceContext) Init(ctx context.Context) error {
|
||||
client, err := NewClient(c.Config.Client)
|
||||
if err != nil { return err }
|
||||
c.Client = client
|
||||
c.OnClose(client.Close)
|
||||
return nil
|
||||
}
|
||||
```
|
||||
|
||||
每个资源初始化成功就登记清理;后续初始化失败自动逆序回滚。正常退出先排空 HTTP 请求,再执行后台组件的 Stop/Drain 阶段,确认收尾后逆序关闭依赖,最后关闭日志。关闭幂等,单个清理函数 panic/返回错误不会跳过其他清理;初始化 panic 清理后继续传播。后台组件按 [分阶段关闭](lifecycle/README.md#后台组件分阶段关闭) 登记;无法确认收尾完成时保留共享依赖并异常退出,不能把取消等同于任务已经结束。
|
||||
|
||||
新项目使用中间件工厂:项目维护 `middleware.Dependencies` 小接口与 `New<Name>(deps) (httpx.Middleware, error)`,svc 提供对应访问器。生成的 runtime 在 Init 成功后自动调用工厂并注册,不再按名字替换 map;工厂失败会释放已经登记的依赖。示例与现有项目迁移步骤见 [中间件工厂](GENERATION.md#中间件工厂自动注册)。
|
||||
|
||||
v0.16 项目及 `omni.gen.json` 中选择 `middleware: handlers` 的项目继续使用原有 `Defaults()`,可以在 Init 设置 `c.Middlewares["RequireAccess"]`。更新时不会重写这部分业务代码;只有显式切换到 factories 后才使用新流程。
|
||||
|
||||
生成入口为每个 App 注入独立 Logger,不修改进程默认 Logger。业务可用 `c.Logger.InfoContext(...)`,或请求链中的 `logx.InfoContext(ctx,...)`。标准库/第三方需要 `*log.Logger` 时使用 `c.Logger.Standard(slog.LevelWarn)`;适配器借用原 Logger,不负责关闭。手写多 App 用 `omni.WithLogInstance(logger)` 显式注入。
|
||||
|
||||
`httpx.Metadata(ctx)` 在业务的标准 context 中提供可信代理规则解析后的 ClientIP、HTTP Method 和路由模板;不放入凭据、完整 URL、查询或 body。业务 userId/tenantId 等仍由项目选择,框架不预设身份模型。
|
||||
|
||||
`middleware.ContextErrors("REQUEST_CANCELED", "REQUEST_TIMEOUT")` 可将原始取消/超时错误映射到项目错误目录;显式 APIError 优先,内部 cause 链保留。状态、业务码与提示仍在项目 Catalog 配置。
|
||||
|
||||
## 字段文档、格式化、Go 错误目录导出
|
||||
|
||||
```text
|
||||
type Item {
|
||||
// 企业名称
|
||||
Name string `json:"name" example:"示例企业"`
|
||||
State int `json:"state,options=0|1" example:"1" enumDescriptions:"[\"禁用\",\"启用\"]"` // 状态
|
||||
}
|
||||
```
|
||||
|
||||
字段前/同行注释同步到 Go 注释和 OpenAPI description。显式 `description` 标签优先;example 的基础字段值也检查类型和约束,枚举说明按声明值顺序填写。字符串 example 直接写内容,其他类型写 JSON。自定义 scalar 的示例不能替代其项目解码器验证;输入输出类型不同时,不把输入示例冒充输出示例。
|
||||
|
||||
```sh
|
||||
go-omni api format --api api/service.api
|
||||
go-omni api format --api api/service.api --check
|
||||
go-omni api generate
|
||||
go-omni api generate --check
|
||||
```
|
||||
|
||||
格式化保留注释、token 和多行字面量,规范四空格缩进、空行、行首尾空白及相邻单行字段的类型/标签/行尾注释对齐;结果幂等。先校验完整契约,check 不写文件;导入的共享目录不被某个服务的格式化命令修改。DTO 按源 `.api` 输出 `dto<Source><摘要>Gen.go`,都属于 `types` 包,跨文件引用无需改包名。
|
||||
|
||||
Go Catalog 是错误的唯一来源时,优先使用 workspace 的显式导出配置,不再维护导出/复制脚本。AppOptions 仍通过 `omni.WithErrors(apierrors.Catalog())` 注册同一个 Go 函数,运行期只注册一次。单个项目也可继续使用 `errors.ExportCatalog` 导出入口目录的 `api/omni.errors.json`;此文件和下面的共享 source 二选一。
|
||||
|
||||
OpenAPI 自动把有收益的重复响应、schema 和示例移到 `components.responses/schemas/examples`,通过标准 `$ref` 引用。HTTP 200 同时含成功和业务失败时保留两种 schema、业务码、模板参数示例和 data:null;业务响应内容不会改变。消费 OpenAPI 的工具需要支持标准本地引用。
|
||||
|
||||
## 两个服务共享契约
|
||||
|
||||
```text
|
||||
workspace/
|
||||
├── contracts/common/response.api
|
||||
├── services/api/api/service.api
|
||||
├── services/api/api/omni.imports.json
|
||||
├── services/id/api/service.api
|
||||
├── services/id/api/omni.imports.json
|
||||
└── omni.workspace.json
|
||||
```
|
||||
|
||||
两个服务的 `omni.imports.json` 可以分别声明:
|
||||
|
||||
```json
|
||||
{"roots":{"common":{"path":"../../../contracts/common","version":"1.0.0"}}}
|
||||
```
|
||||
|
||||
```text
|
||||
import "@common/response.api"
|
||||
```
|
||||
|
||||
只读取入口目录和显式声明的根。保留真实路径检查、symlink 越界拒绝、循环检测、32 层、64 文件、单文件 1 MiB/总量 8 MiB 限额;最多 16 个共享根。不会下载依赖或复制共享定义进服务。移动目录时显式更新 roots。
|
||||
|
||||
`version` 是项目管理的版本标签。生成文档记录每个文件 SHA-256,以及每个共享根所导入文件集合的摘要;可把文档中的 `x-omni-contract-sources.shared.common.sha256` 填入 root 的 `sha256`,固定输入集合,内容变化后必须显式更新。摘要算法是对“该根内相对路径→文件 SHA-256”的键排序 JSON 对象再做 SHA-256,未导入的文件不参与。
|
||||
|
||||
工作区清单:
|
||||
|
||||
```json
|
||||
{"services":[{"dir":"services/api"},{"dir":"services/id"}]}
|
||||
```
|
||||
|
||||
```sh
|
||||
go-omni workspace format --file omni.workspace.json
|
||||
go-omni workspace generate --file omni.workspace.json
|
||||
go-omni workspace check --file omni.workspace.json
|
||||
```
|
||||
|
||||
各服务保持独立 module;可显式填写各服务的 `api`(相对服务目录)和 `module`。所有服务预检通过后再写;每个服务独立提交文件事务,不承诺跨项目的崩溃原子性。check 汇总格式与生成漂移;业务编译/测试仍在各 module 执行。
|
||||
|
||||
### 同一份 Go 错误目录供多个服务生成
|
||||
|
||||
在 workspace 清单显式声明提供目录的 Go 包和函数:
|
||||
|
||||
```json
|
||||
{
|
||||
"errorCatalogs": [{
|
||||
"dir": ".",
|
||||
"package": "example.com/project/internal/apierrors",
|
||||
"function": "Catalog",
|
||||
"output": "contracts/common/errors.json"
|
||||
}],
|
||||
"services": [{"dir":"services/api"}, {"dir":"services/id"}]
|
||||
}
|
||||
```
|
||||
|
||||
`dir` 相对 workspace,必须包含 go.mod;函数签名为 `func Catalog() errors.Catalog`。允许 module 内的 internal 包。先准备 output 的父目录。生成器构建一个临时导出程序,使用 Go overlay、临时 go.mod/go.sum 和 `-mod=readonly`,不会往项目增加导出命令或修改 go.mod/go.sum。最多 16 个提供方,每个有 90 秒构建/执行总时限和 1 MiB stdout 限制。错误消息不回显程序输出。
|
||||
|
||||
只有显式运行 **workspace generate/check** 会执行清单指定的 Go 包及其依赖初始化;不要指向不信任的包。提供目录的函数应只构造目录,不连接数据库、启动服务、写 stdout 或修改项目。框架不扫描其他包,也不通过 shell 执行配置字符串。普通 api validate/export/generate 和 workspace format 只读取现有元数据。
|
||||
|
||||
各服务在自己的 `api/omni.imports.json` 引用共享 JSON:
|
||||
|
||||
```json
|
||||
{
|
||||
"roots": {"common":{"path":"../../../contracts/common","version":"1.0.0"}},
|
||||
"errors": {
|
||||
"source": "@common/errors.json",
|
||||
"include": ["USERNAME_INVALID", "INTERNAL_ERROR"],
|
||||
"routes": {"GET /permissions":["INTERNAL_ERROR"]}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`include` 省略或 null 表示全部目录,`[]` 表示不列业务错误;routes 按 **大写方法 + 空格 + .api 原路径**(如 `GET /users/:id`)覆盖服务级列表。错误标识、路由拼错或重复标识会明确报错。路由已经通过 `.api @errors` 声明独立运行期目录时,不能再给它设置文档 routes 列表。
|
||||
|
||||
**这些列表只决定文档展示,不修改 App 错误目录、路由继承或运行期可返回的错误。** `.api` 不需要重复定义 Go 错误表。状态、业务码、提示和模板参数仍来自同一 Catalog;特殊自定义 Encoder 的文档由项目显式扩展。
|
||||
|
||||
```sh
|
||||
# 首次生成导出一次共享目录,所有服务预检通过才写入
|
||||
# 共享 JSON 不复制到各服务;已有 api/omni.errors.json 请显式移除后改用 source。
|
||||
go-omni workspace generate --file omni.workspace.json
|
||||
go-omni workspace format --file omni.workspace.json
|
||||
go-omni workspace generate --file omni.workspace.json
|
||||
go-omni workspace check --file omni.workspace.json
|
||||
```
|
||||
|
||||
共享 JSON 应纳入版本控制。check 在内存重新导出并比较 JSON,然后检查所有服务;缺失或过期时失败,不更新项目文件。Go 构建缓存仍可能更新;显式提供方的自有副作用不属于生成器可隔离范围。业务 CI 仍执行各 module 的编译与测试。目录函数本身有变化时重新 generate,受控共享根的固定 SHA256 如需改变仍由项目显式更新。
|
||||
|
||||
## v0.13 项目升级
|
||||
|
||||
先使用与目标框架匹配的 CLI;CLI 会提示与 go.mod 不同的版本。当前生成模板至少需要 v0.18.0-preview(或显式引用当前源码)。
|
||||
|
||||
```sh
|
||||
go-omni api upgrade --dir . --dry-run
|
||||
go-omni api upgrade --dir . --preview .goOmni/upgradePreview
|
||||
# 只有标为 safe 的原始模板才会自动替换;review 阻止全部写入。
|
||||
go-omni api upgrade --dir . --apply
|
||||
go mod tidy
|
||||
go-omni api generate --check
|
||||
go test ./...
|
||||
go build .
|
||||
```
|
||||
|
||||
自动迁移只替换与 v0.13 原始模板一致的配置/svc 基础文件、移除原始 close.go,并更新最低依赖及生成产物;保留 Logic、Repository、AppOptions、独立命令。迁移失败回滚已改的项目文件。存在业务修改时,仅输出这些基础文件的候选版本和清单,不生成整棵业务代码让人重新拼接。
|
||||
|
||||
定制项目按预览定点迁移:保留依赖字段,把初始化移到 Init,嵌入 Runtime,清理改为 OnClose 登记;移除旧 New/Close/OnClose;合并业务配置和结构化 MySQL 配置,更新 go.mod,再正常 generate。生成器会拒绝遗留构造/关闭实现,避免重复函数或悄悄绕过清理。v0.12 及更早的定制结构不能被精确自动合并,使用同一预览逐项迁移并编译验证。
|
||||
279
GENERATION.md
Normal file
279
GENERATION.md
Normal file
@@ -0,0 +1,279 @@
|
||||
# 公共字段、结构映射与生成选项
|
||||
|
||||
本页对应 `v0.18.0-preview`。CLI 与框架依赖固定到同一版本:
|
||||
|
||||
```sh
|
||||
go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.18.0-preview
|
||||
# 已有业务项目内执行:
|
||||
go get git.yuntuo.hk/k2safe/go-omni@v0.18.0-preview
|
||||
```
|
||||
|
||||
私有仓库访问设置见 [安装说明](README.md#准备环境与安装)。升级不会覆盖业务文件;已有中间件保持 handlers 模式,切换工厂模式按本页迁移步骤完成。
|
||||
|
||||
## 一份项目生成配置
|
||||
|
||||
新项目根目录生成开发者维护的 `omni.gen.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"repository": "none",
|
||||
"middleware": "factories"
|
||||
}
|
||||
```
|
||||
|
||||
该文件与 `.api` 一起提交。每次 `api generate`、`--check`、`--dry-run` 和 `workspace generate/check` 都会读取;`api validate/export/format` 只处理契约,不加载业务 Go 类型。
|
||||
|
||||
- `repository: none` 是默认值,不创建空 Repository 文件。`group` 按 `.api` 的 group 补齐 `internal/repository/<group>/repository.go`;文件始终属于业务。切回 none 后不再补骨架,已有文件不删除、不覆盖。实际 SQL、ORM 和仓储划分由项目决定。
|
||||
- `middleware: factories` 是新项目默认值,按下文自动初始化。`handlers` 保留原有 `Defaults()` 与 `func(next Handler) Handler` 接口,供既有项目逐步迁移。
|
||||
- `mappings` 可省略;只有明确列出的类型对才生成映射。未启用时不会运行 Go 类型加载命令,也不增加运行时依赖。
|
||||
- 升级首次遇到没有配置的旧 manifest 时,补一份 `middleware: handlers` 的配置,保留原有手写注册;Repository 默认 none。生成配置建立后不能直接删除,否则生成失败,避免误关闭映射或改变中间件模式。
|
||||
|
||||
### 按模块拆分映射(v0.18 新增)
|
||||
|
||||
主配置可以保留少量内联 `mappings`,也可以只列出文件:
|
||||
|
||||
```json
|
||||
{
|
||||
"repository": "none",
|
||||
"middleware": "factories",
|
||||
"mappingFiles": ["mappings/requests.json", "mappings/responses.json"]
|
||||
}
|
||||
```
|
||||
|
||||
每个子文件只包含 `{"mappings": [...]}`,其中每条映射的写法与原来一致。先加载主配置内联定义,再按文件列表顺序加载;所有定义合并后统一校验,可以跨文件、向前引用 `mapping`。输出按映射名排序,文件拆分不改变函数名或语义。
|
||||
|
||||
- 路径相对项目根目录,必须是明确列出的 `.json` 文件。不展开通配符,不允许越界路径、符号链接、重复文件或多层导入;子文件不能覆盖 repository/middleware 等项目选项。
|
||||
- 重名映射、指针辅助函数名冲突、未知映射与循环引用明确报错;文件之间不能用覆盖顺序隐藏冲突。重复定义会指出两个来源文件,字段生成错误也带来源文件。
|
||||
- 文件均由项目维护,重新生成不改写它们。原来的单文件 `mappings` 继续有效。迁移时将完整条目移到子文件并删除原位置,不能复制后保留两份同名定义。
|
||||
- 最多 64 个子文件、单文件 1 MiB、全部配置 4 MiB;合计仍最多 128 个映射。主配置和子文件一起提交 Git。
|
||||
- `api generate`、`--check`、`--dry-run` 与 `workspace generate/check` 使用同一加载逻辑。缺少子文件或不完整配置立即失败,check/预览不写入;生成期间重新核对所有配置输入,发生变化时要求重试。
|
||||
|
||||
## 公共字段组合
|
||||
|
||||
```text
|
||||
type EmployeeFields {
|
||||
// 员工姓名。
|
||||
Name string `json:"name" binding:"max=80"`
|
||||
Salary Money `json:"salary"`
|
||||
}
|
||||
|
||||
type EnterpriseRequest {
|
||||
EmployeeFields
|
||||
}
|
||||
|
||||
type AdminRequest {
|
||||
EmployeeFields
|
||||
CompanyID string `path:"companyId"`
|
||||
}
|
||||
```
|
||||
|
||||
`Money` 由项目按既有 scalar 语法声明。组合行只有已声明的对象类型名,不加指针或字段 tag。公共对象可以通过已有相对 import 或声明过的共享根 `@common/...` 引入。
|
||||
|
||||
生成器展开为普通 Go 字段。JSON 保持平铺;字段的绑定来源、校验、注释、可空性和 OpenAPI 使用同一份展开结果。不会在 JSON 中增加 `EmployeeFields` 一层。重复 Go 字段、重复来源字段名、循环组合、未知对象和过深展开均失败;即使重复字段类型相同,也不能隐式覆盖。要改变某个字段的规则,应拆出更小的公共组合。
|
||||
|
||||
允许多层组合,上限 32 层、单类型 4096 字段、全契约展开 65536 字段。Go 匿名指针嵌入并非本功能;生成 DTO 中不会使用匿名嵌入。
|
||||
|
||||
## 可选结构映射
|
||||
|
||||
`.api` 生成 DTO,业务独立维护输入/结果类型。项目明确选择需要自动赋值的类型对;不从数据库反向生成公开接口,不自动推断反向映射。
|
||||
|
||||
假设 `types.AdminRequest` 有 `Name string`、`Salary amount.Money`、`CompanyID string`,业务的 `domain.EmployeeInput` 有 `EmployeeName string` 和同类型 `Salary`:
|
||||
|
||||
```json
|
||||
{
|
||||
"repository": "none",
|
||||
"middleware": "factories",
|
||||
"mappings": [
|
||||
{
|
||||
"name": "ToEmployeeInput",
|
||||
"source": {"package": "example.com/payroll/internal/types", "type": "AdminRequest"},
|
||||
"target": {"package": "example.com/payroll/internal/domain", "type": "EmployeeInput"},
|
||||
"fields": {
|
||||
"EmployeeName": {"source": "Name"}
|
||||
},
|
||||
"ignoreSource": {
|
||||
"CompanyID": "由 Logic 单独做企业范围授权,不属于请求体输入"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
执行 `go-omni api generate` 后,产生独立的生成文件 `internal/mapping/mappingsGen.go`:
|
||||
|
||||
```go
|
||||
func ToEmployeeInput(source types.AdminRequest) (domain.EmployeeInput, error) {
|
||||
var result domain.EmployeeInput
|
||||
result.EmployeeName = source.Name
|
||||
result.Salary = source.Salary
|
||||
return result, nil
|
||||
}
|
||||
```
|
||||
|
||||
包别名由生成器决定。业务调用 `mapping.ToEmployeeInput(*req)`;反向响应映射按另一条 source/target 规则声明。两个类型完全相同时通常无需映射。
|
||||
|
||||
### 类型与覆盖检查
|
||||
|
||||
自动赋值要求同名且 **Go 类型一致**(类型别名按 Go 类型系统处理),字段名使用 Go 名称,不按 JSON 标签猜对应关系。int32 → int64、`*Money` → Money、数值 ID → string 都必须显式转换。不会暗中补零、解引用或执行数字强转。
|
||||
|
||||
每个源字段必须被使用或出现在 `ignoreSource`;每个目标字段必须被赋值或出现在 `ignoreTarget`。忽略规则必须给出非空原因,目标忽略明确意味着保留零值;规则引用不存在的字段也会失败。新字段若无法自动匹配,会在生成阶段报错。两边同时添加的同名同类型字段会自动加入生成代码,`--check` 检出未重新生成的差异。
|
||||
|
||||
根类型是项目可导入的具名结构;支持值类型匿名嵌入,规则可用 `Employee.EmployeeName` 这样的实际 Go 字段路径。同名提升歧义、未导出字段和匿名指针嵌入拒绝处理。普通具名嵌套字段使用独立映射或转换函数,不猜测内部结构。泛型声明不能直接作为根;具体字段类型按 Go 类型系统检查。
|
||||
|
||||
配置变化、遗漏映射、类型错误会在写入前失败。映射生成与其他生成产物使用同一套文件所有权机制;手改映射生成文件会被拒绝覆盖。
|
||||
|
||||
### 显式转换函数
|
||||
|
||||
```json
|
||||
"fields": {
|
||||
"Salary": {
|
||||
"source": "Salary",
|
||||
"convert": {"package": "example.com/payroll/internal/convert", "name": "SalaryValue"}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
支持这两种精确签名:
|
||||
|
||||
```go
|
||||
func SalaryValue(source *amount.Money) amount.Money
|
||||
func SalaryValue(source *amount.Money) (amount.Money, error)
|
||||
```
|
||||
|
||||
项目选择一种实现。nil、默认值、金额精度、日期格式和错误码由函数负责;若两边已经是同一个 Money 类型,无需再次解析或序列化。转换函数应仅负责值转换,不执行 SQL 或改变授权范围。
|
||||
|
||||
错误通过 `%w` 加上映射名/目标字段路径,保留 `errors.Is/As` 和框架错误链;返回零目标值,不交付部分完成的结果。包装不主动添加源值或转换后的值。错误是否公开仍由项目错误目录和响应策略决定。
|
||||
|
||||
### 嵌套对象、数组与引用
|
||||
|
||||
`mapping` 指向同配置中的另一条映射,`elements` 明确要求逐项映射:
|
||||
|
||||
```json
|
||||
"fields": {
|
||||
"Summary": {"source": "Summary", "mapping": "ToSummary"},
|
||||
"Items": {"source": "Items", "mapping": "ToItem", "elements": true}
|
||||
}
|
||||
```
|
||||
|
||||
`mapping` 与 `convert` 互斥。`elements` 可以配合其中任意一种,也可以对类型相同的元素直接赋值;支持 slice→slice、等长 array→array,禁止隐式互转。nil slice 保持 nil,非 nil 空 slice 保持非 nil,逐项失败的错误包含索引,循环映射声明被拒绝。
|
||||
|
||||
未选择 elements 的同类型指针、map、slice 使用普通 Go 赋值,**共享引用**,不做隐式深拷贝。elements 会分配新的目标 slice,但元素内的引用仍遵循其映射规则。需要深拷贝时显式提供转换函数。
|
||||
|
||||
映射不保存原 JSON 的字段存在信息。普通 `*T` 在绑定后可能已把缺失和 null 合并为 nil;无法在转换阶段恢复区分。有三态需要的项目必须先使用能携带该信息的输入类型及绑定扩展;没有声明三态时,保持当前输入类型的语义。完整表单更新和局部更新由业务决定,框架不把 nil 自动解释成“保留旧值”。
|
||||
|
||||
### 可空嵌套映射与显式 nil 策略(v0.18 新增)
|
||||
|
||||
假设已有 `ToChild` 定义 `types.Child → domain.Child`。父对象的字段规则可以直接复用它:
|
||||
|
||||
```json
|
||||
"fields": {
|
||||
"Child": {"source": "Child", "mapping": "ToChild"},
|
||||
"Children": {"source": "Children", "mapping": "ToChild", "elements": true}
|
||||
}
|
||||
```
|
||||
|
||||
| 源字段 → 目标字段 | 明确选择 mapping 后的行为 |
|
||||
| --- | --- |
|
||||
| `S → T` | 调用原值映射 |
|
||||
| `*S → *T` | 默认保留 nil;非 nil 调用值映射,并分配新的目标对象;也可配置 `nil: "error"` 拒绝 nil |
|
||||
| `S → *T` | 调用值映射,成功后返回目标对象指针 |
|
||||
| `*S → T` | 必须选择 `nil: "zero"` 或 `nil: "error"`,不猜默认值 |
|
||||
|
||||
`nil: "preserve"` 只适用于可空源和可空目标。`nil: "zero"` 只适用于可空源和非空目标:nil 直接产生目标 Go 零值,**不调用子映射或转换函数**。`nil: "error"` 在 nil 时返回带映射/字段路径的普通转换错误,HTTP 公开响应仍由项目的错误处理策略决定;需要领域错误码时使用项目 converter。
|
||||
|
||||
同一规则适用于显式 `elements: true` 下的 slice/等长数组元素,包括 `[]*S → []*T`。nil slice、非 nil 空 slice、nil 元素分别保留;元素失败保留索引与内部错误链,整个映射返回零目标值,不交付部分结果。只支持这里列出的单层指针,不自动处理多层指针或递归对象图。
|
||||
|
||||
普通同类型指针字段未指定 mapping 时仍直接赋值、共享引用;上述显式映射会创建目标子对象,但不承诺对其中所有引用深拷贝。业务不可变快照继续由项目设计。
|
||||
|
||||
简单的 `*T → T` 也可以单独指定 nil 策略,减少重复解引用函数:
|
||||
|
||||
```json
|
||||
"fields": {
|
||||
"Title": {"source": "Title", "nil": "zero"},
|
||||
"Count": {"source": "Count", "nil": "error"}
|
||||
}
|
||||
```
|
||||
|
||||
这里必须是完全相同的 Go 元素类型,`*int32 → int64`、枚举 → string、数字编号 → string 仍需显式 converter。非 nil 的零值正常保留。`convert` 与 `nil` 不能同时设置,现有 converter 继续完整控制自己的空值、格式和错误语义。
|
||||
|
||||
### 可选指针返回辅助(v0.18 新增)
|
||||
|
||||
在映射条目上增加 `"pointerResult": true`,生成器保留 `ToResponse(source S) (T, error)`,另加:
|
||||
|
||||
```go
|
||||
func ToResponsePtr(source S) (*T, error)
|
||||
```
|
||||
|
||||
Logic 在业务调用成功后可直接写 `return mapping.ToResponsePtr(result)`。转换失败时返回 `nil, err`,错误链不变;成功才返回非 nil 指针。业务调用本身的错误仍由 Logic 处理,辅助函数不执行任何业务操作。
|
||||
|
||||
辅助函数名称固定为映射名加 `Ptr`,与其他映射/辅助函数同名会在生成前报错。输入仍是原来的值类型 S,不用 nil 输入表达业务错误;关闭该选项会移除辅助函数,使用它的业务代码需同时调整。
|
||||
|
||||
### 生成顺序与 CI
|
||||
|
||||
1. 先从 `.api` 生成项目,添加业务结构、scalar 和转换函数,执行 `go mod tidy`。
|
||||
2. 在 `omni.gen.json` 列出映射,执行 `go-omni api generate`。
|
||||
3. Logic 调用生成函数。后续改字段后重复生成,不修改 `*Gen.go`。
|
||||
4. CI 同时运行 `go-omni api generate --check` 与 `go test ./...`。
|
||||
|
||||
启用映射后,CLI 通过 `go list -deps -export` 加载明确声明的包与依赖,按当前构建环境做类型检查,不执行业务代码、init 或转换函数。采用待生成 DTO 的临时 overlay,避免“新增字段还看旧 DTO”。go.mod/go.sum 的副本及 overlay 都在临时目录,check 不修改项目文件;Go 自己的构建/模块缓存可能更新。使用当前 GOOS/GOARCH/GOFLAGS、`GOWORK=off`;依赖用 go.mod 与 replace 表达,生成超时为 90 秒。
|
||||
|
||||
类型包/转换函数包不能依赖输出包 `internal/mapping`;把转换函数放在独立包中,避免导入环。若包本身无法编译,先运行 `go list -deps -export <相关包>` 排查;CLI 不回显可能含私有仓库凭据的 Go 下载输出。纯值字段转换只生成标准 Go 代码,不引入运行时反射搬运、JSON 往返、ORM 或队列依赖。
|
||||
|
||||
## 中间件工厂自动注册
|
||||
|
||||
`.api` 沿用 `middleware: RequireAccess` 或 auth 对应的中间件声明。新项目会生成可编辑的 `internal/middleware/requireAccessFactory.go`,只需实现一次:
|
||||
|
||||
```go
|
||||
// internal/middleware/dependencies.go,由项目维护。
|
||||
type AccessChecker interface {
|
||||
CheckAccess(context.Context, string) error
|
||||
}
|
||||
type Dependencies interface {
|
||||
AccessChecker() AccessChecker
|
||||
}
|
||||
|
||||
// internal/middleware/requireAccessFactory.go,由项目维护。
|
||||
func NewRequireAccess(deps Dependencies) (httpx.Middleware, error) {
|
||||
checker := deps.AccessChecker()
|
||||
if checker == nil { return nil, errors.New("access checker is required") }
|
||||
return func(next httpx.Handler) httpx.Handler {
|
||||
return func(c *httpx.Context) error {
|
||||
if err := checker.CheckAccess(c.Context(), c.Header("Authorization")); err != nil {
|
||||
return err
|
||||
}
|
||||
return next(c)
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
// internal/svc/access.go,由项目维护;ServiceContext.Init 初始化 c.Auth。
|
||||
func (c *ServiceContext) AccessChecker() middleware.AccessChecker { return c.Auth }
|
||||
```
|
||||
|
||||
编译器检查 ServiceContext 是否实现依赖接口,middleware 不导入 svc。多个工厂可以共用这些小接口。所需 imports 和具体鉴权服务仍属于项目。
|
||||
|
||||
启动顺序为:框架基础依赖 → `svc.Init` → 生成的 `middleware.New(c)` 调用各具名工厂 → 创建 App/注册路由 → 监听。中间件实例创建一次,由请求并发复用;不要把单次请求信息存在实例字段里。工厂之间没有初始化顺序依赖;路由执行顺序仍以 `.api` 声明为准。
|
||||
|
||||
工厂返回错误或 nil 中间件均阻止监听;错误链保留,已登记资源按现有生命周期清理。工厂 panic 也先清理依赖再传播。资源初始化和清理登记放在 svc.Init,工厂借用资源;HTTP 请求排空之前不会关闭这些依赖。
|
||||
|
||||
factory 模式不再在 Init 写 `Middlewares["..."]`;残留非空手写注册表会明确报错,防止生成注册静默覆盖它。未实现的工厂让启动失败;未实现的 Logic 仍返回 501。
|
||||
|
||||
### 既有项目迁移
|
||||
|
||||
1. 首次更新保持 `middleware: handlers`,现有 hook 和手写 Configure 不变。
|
||||
2. 改为 factories 并重新生成,会补 `dependencies.go` 和 `*Factory.go`,保留原来的手写文件。
|
||||
3. 将旧实现迁入工厂,提供 Dependencies 的访问器;移除 Init 中按名字覆盖 map 的代码。
|
||||
4. 旧函数不再需要时由项目删除。若新工厂符号与旧文件中的同名函数冲突,整理旧文件;生成器不覆盖业务代码。
|
||||
5. `generate --check` 与 `go test ./...` 检查后再运行。工厂没有全部实现前,服务不会启动。
|
||||
|
||||
## 文件维护边界
|
||||
|
||||
| 项目维护 | 生成器持续维护 |
|
||||
| --- | --- |
|
||||
| `.api` 公共字段与接口 | 展开后的 DTO、Handler、OpenAPI |
|
||||
| `omni.gen.json` 的映射与选项 | `internal/mapping/mappingsGen.go` |
|
||||
| Go 业务类型、scalar、转换函数 | 编译期可检查的赋值与调用代码 |
|
||||
| middleware Dependencies、具名工厂、svc 访问器 | 中间件 registry、启动调用 |
|
||||
| Logic、按需 Repository、配置、svc 初始化/清理登记 | main、runtime、路由接入 |
|
||||
|
||||
回归样例位于 `cmd/go-omni/apigen/testdata/composition`,为独立三端服务,不导入任何 eTax 代码。
|
||||
232
PROJECT.md
Normal file
232
PROJECT.md
Normal file
@@ -0,0 +1,232 @@
|
||||
# 生成项目与文件维护边界
|
||||
|
||||
v0.18.0-preview 提供受管理的生成项目。安装同版远程 CLI,生成的项目直接引用该版本,无需本地 replace。根目录入口支持 `go run .` 和 `go build .`;框架本身仍然是可直接引用的库。安装和私有仓库访问见 [README](README.md#准备环境与安装)。
|
||||
|
||||
## 创建和日常开发
|
||||
|
||||
```sh
|
||||
# 在存放业务项目的目录执行,目标目录必须为空。
|
||||
go-omni new --module example.com/hello ./hello
|
||||
cd hello
|
||||
go mod tidy
|
||||
go run .
|
||||
go build .
|
||||
```
|
||||
|
||||
在项目目录使用同版 CLI:编辑 `api/service.api`,运行 `go-omni api generate`,填写 `internal/logic` 的业务方法。增加接口、分组或具名中间件会自动更新接入代码,无需修改入口。`--dry-run` 预览;`--check` 只读检查,包括 main、HTTP 接入和 OpenAPI。API 文件通过相对 import 拆分模块。
|
||||
|
||||
## 契约变更提示
|
||||
|
||||
生成器在 manifest 中保存上次成功生成的接口快照,比较请求/返回类型、嵌套 DTO 字段与标签、自定义 scalar、路径、body 模式/限额和流式输出类型。只提示受影响的接口,并标出业务文件;删除或移动接口会提醒整理保留的旧实现。注释、文件排版、Logic 参数名与导入别名不会被当成接口变化。
|
||||
|
||||
例如把 `GreetReq` 改名为 `HelloReq`,旧业务方法尚未修改时:
|
||||
|
||||
```text
|
||||
ERROR [logicSignature] internal/logic/greet/greet.go:12 (GET /api/greet/:name): ...
|
||||
before: Greet(context.Context, *types.GreetReq) (*types.GreetResp, error)
|
||||
after: Greet(context.Context, *types.HelloReq) (*types.GreetResp, error)
|
||||
```
|
||||
|
||||
| 命令 | 变更提示与检查行为 |
|
||||
| --- | --- |
|
||||
| `api generate --dry-run` | 显示计划、契约变化和业务签名问题,不写文件;诊断本身不让预览失败 |
|
||||
| `api generate` | 更新生成文件和契约快照,保留业务代码,并显示需要处理的诊断;生成成功不代表业务已通过编译 |
|
||||
| `api generate --check` | 只读检查;生成产物过期或发现明确的 Logic 签名/语法/重复方法错误时返回非零退出码 |
|
||||
| `api validate` | 只验证 `.api`,不检查项目 Go 实现 |
|
||||
|
||||
`REVIEW` 提醒复查业务语义;`ERROR` 表示已发现源代码问题。生成后,旧签名仍会在后续 `--check` 中报错,直到你改好;DTO 字段变化的 REVIEW 与上次生成快照比较,生成后不持续要求确认,也不会替你修改业务。业务方法移到同组其他 Go 文件时仍能定位。
|
||||
|
||||
这是轻量源码分析,不运行项目 init、不执行 Go 命令、不下载依赖。项目自定义类型别名、嵌入方法或 build tags 等无法可靠判断时,会给 REVIEW 并交给 Go 编译器验证;不把这些情况强行判为签名错误。它不检查方法体是否引用已删除字段,也不证明业务逻辑正确。CI 中应同时执行 `go-omni api generate --check` 和 `go test ./...`,按项目实际构建参数测试。当前版本首次接手缺少快照的 manifest v2 时,会明确提示建立基线,不猜测旧契约;Logic 签名检查仍会执行。
|
||||
|
||||
本版新增生成选项、公共字段组合和映射,详见 [生成能力指南](GENERATION.md)。
|
||||
|
||||
## 哪些文件由用户维护
|
||||
|
||||
| 文件 | 首次生成后由项目维护 |
|
||||
| --- | --- |
|
||||
| `api/*.api` | 路由、DTO、具名中间件、响应模板、请求/响应模式 |
|
||||
| `etc/config.yaml` | 运行参数;密钥通过环境变量提供 |
|
||||
| `internal/config/config.go` | 自定义配置字段、加载、默认值与校验 |
|
||||
| `internal/svc/serviceContext.go` | 依赖字段和初始化,持有加载后的 Config |
|
||||
| `internal/svc/options.go` | AppOptions、GlobalMiddleware;注册 Go 错误目录和全局扩展 |
|
||||
| `internal/svc/databaseOptions.go` | 可选 MySQL 驱动与项目启动检查扩展 |
|
||||
| `internal/svc/storage.go` | 选用存储时生成,之后保留 |
|
||||
| `internal/logic/<group>/*.go` | 业务方法,使用标准 context.Context |
|
||||
| `internal/repository/<group>/*.go` | 数据访问;默认不补空骨架,可选 group 模式 |
|
||||
| `internal/middleware/*Factory.go`、`dependencies.go` | 中间件工厂与依赖小接口,svc 实现访问器 |
|
||||
| `omni.gen.json`、映射转换函数(支持 `mappingFiles` 引用子文件) | 生成选项、显式类型对与差异规则 |
|
||||
| 项目 Go 错误包 | 自行维护 Catalog 和常量,可跨服务共用 |
|
||||
| 自定义标量文件、`*Bind.go`、`*Manual.go` | 只有显式选择相应扩展时才生成/使用 |
|
||||
| 项目测试、迁移、部署脚本、go.mod/go.sum | 项目所有,生成器不重置其内容 |
|
||||
|
||||
以上骨架只补缺失文件,既有内容按字节保留。删除已废弃接口后,对应业务文件也不会被自动删除;由开发者整理编译器指出的失效类型或签名。
|
||||
|
||||
生成器持续管理:`internal/svc/runtimeGen.go`、`main.go`、`internal/handler/*Gen.go`、分组的 `handlersGen.go`、`internal/types/*Gen.go`、可选映射 `internal/mapping/mappingsGen.go`、中间件 `registryGen.go`、`docs/openapi.json`、`.goOmni/apiManifest.json`。如果在 `.api` 里选择声明错误,还会管理 `internal/apierrors/errorsGen.go`。文件标注 DO NOT EDIT;手改会被拒绝覆盖,需先移走自定义内容。
|
||||
|
||||
Logic 实例在注册时创建并由请求并发复用;请求数据用方法参数和局部变量传递,不存入共享 Logic/svc 字段。
|
||||
|
||||
普通 Handler 不需要维护。目录内可以同时有生成文件和单独的手写扩展文件,所有权按文件区分。没有额外 server 包或第二层 service。
|
||||
|
||||
## 依赖与启动扩展
|
||||
|
||||
`svc.New(ctx, logger, cfg)` 初始化依赖,可返回错误;Logger 由入口创建并借用。New 本身由生成器维护;新增客户端时扩展配置和 svc 的 `Init(ctx) error`:
|
||||
|
||||
```go
|
||||
client, err := newClient(c.Config.Client)
|
||||
if err != nil { return err }
|
||||
c.Client = client
|
||||
c.OnClose(client.Close) // 创建成功后立即登记,后续失败也能释放
|
||||
```
|
||||
|
||||
ServiceContext 嵌入 `*lifecycle.Runtime`;框架维护锁、清理栈和失败回滚,项目不再复制 Close/OnClose 实现。OnClose 按登记的相反顺序执行;Close 幂等。项目不要重复关闭同一资源,借用的 Logger 也不应由 svc 关闭。构造器在返回错误前若创建了尚未交付的资源,该构造器自己负责释放。
|
||||
|
||||
错误注册写在 `internal/svc/options.go`,错误表可以来自任意项目共用包:
|
||||
|
||||
```go
|
||||
func (c *ServiceContext) AppOptions() []omni.Option {
|
||||
return []omni.Option{omni.WithErrors(apierrors.Catalog())}
|
||||
}
|
||||
```
|
||||
|
||||
业务调用 `omnierrors.ByCode(apierrors.CodeUsernameInvalid)`,可接 `WithParams` / `WithCause`。无需在 `.api` 重复错误目录。未声明独立错误目录的路由继承 App 目录;独立目录不与 App 合并。普通 `.api` 编译不执行 Go;可在 workspace 显式配置 Catalog 函数,统一导出共享文档元数据,也可沿用 `api/omni.errors.json`,详见 [元数据导出](DEVELOPMENT.md#字段文档格式化go-错误目录导出)。
|
||||
|
||||
`GlobalMiddleware()` 提供全局中间件;`Middlewares` 映射提供 `.api` 声明的具名中间件。全部显式调用,不依赖项目 init 注册,也不要求 svc 导入 handler。框架请求链按原有次序运行。
|
||||
|
||||
生成入口执行:配置加载/校验 → 日志 → svc.New → 生成的 handler.NewApp(应用 AppOptions、响应策略、校验最终 HTTP 配置、全局中间件、全部路由)→ App.RunContext → 依赖关闭 → 日志关闭。初始化失败、接入配置失败和监听失败也清理资源。
|
||||
|
||||
最终配置校验包含自定义响应编码器与错误目录的组合。码类型/映射冲突、编码器已绑定另一份目录等问题在创建 App 前返回错误,进入入口的清理流程,不等到构造 App 时才 panic。
|
||||
|
||||
日志在 AppOptions 前初始化,其配置应放在 Config 中。退出信号先触发 HTTP 排空;达到 shutdownTimeout 后按框架规则强制关闭连接。请求仍依赖的后台任务/客户端不能直接绑定信号 context 提前关闭,应持有独立生命周期,使用 WorkContext 并登记成对的 OnStop/OnDrain,详见 [分阶段关闭](lifecycle/README.md#后台组件分阶段关闭)。业务必须响应请求 context 取消;忽略取消的 goroutine 无法保证在超时前结束。
|
||||
|
||||
## 依赖主动申请退出
|
||||
|
||||
从 v0.13.0-preview 起,生成的 svc 提供 `RequestShutdown(err)`。数据库客户端、节点租约、后台任务等发现不可恢复故障时,由项目决定是否退出:
|
||||
|
||||
```go
|
||||
// 在 svc 的依赖监控回调中调用;err 是实际故障原因。
|
||||
c.RequestShutdown(err)
|
||||
```
|
||||
|
||||
这只是通知,立即返回,不等待 HTTP 排空,也不直接关闭数据库。生成入口停止接收新请求,等待在途请求完成;超过 `shutdownTimeout` 后取消请求并关闭连接,再执行 svc.Close,最后关闭日志。非空故障原因保留在返回错误链中,进程退出码为 1;`RequestShutdown(nil)` 或正常 SIGINT/SIGTERM 在没有其他故障时退出码为 0。
|
||||
|
||||
多个依赖可并发通知,关闭流程仅执行一次,首个非空故障原因保留。正常信号已开始排空时收到的故障也不会被清理动作覆盖。初始化期间的通知同样有效,不依赖 HTTP 监听已经启动。后台监控任务应通过 OnStop/OnDrain 停止并等待,再进入资源清理;关闭完成以后才报告的故障无法参与已经结束的进程结果。
|
||||
|
||||
`Lifecycle *lifecycle.Controller` 是 svc 持有的通用控制器,初始化时创建,生成入口使用它的 Context。该 Context 表示服务正在停止,不代表数据库已经关闭。不能把它当作在途请求的 Context,也不要在它取消时立刻关闭请求仍依赖的资源。发号器失去租约后拒绝发号等业务安全规则仍由项目实现。
|
||||
|
||||
## 配置内置接口
|
||||
|
||||
生成项目在 `etc/config.yaml` 明确列出以下默认配置;可以独立关闭、修改路径,或让健康接口返回原始 JSON:
|
||||
|
||||
```yaml
|
||||
server:
|
||||
endpoints:
|
||||
liveness:
|
||||
enabled: true
|
||||
path: /health/live
|
||||
responseMode: app
|
||||
readiness:
|
||||
enabled: true
|
||||
path: /health/ready
|
||||
responseMode: app
|
||||
openapi:
|
||||
enabled: true
|
||||
path: /openapi.json
|
||||
```
|
||||
|
||||
`responseMode: app` 使用项目 App 的响应模板/自定义 Encoder。健康成功使用项目成功码和成功提示;未就绪的 `NOT_READY` 可在项目 Go 错误目录配置业务码和公开提示,无需在 `.api` 再声明。
|
||||
|
||||
健康探针的 HTTP 状态固定遵循探针语义:存活/就绪为 200,未就绪为 503;项目错误目录或自定义编码器不能把未就绪改为 HTTP 200。响应模板 `${status}` 同样使用探针状态。失败继续进入业务失败日志/指标,探针底层原因保留在错误链供日志使用,不直接返回给调用方。
|
||||
|
||||
`responseMode: raw` 返回 `{"status":"ready"}` 等健康数据,有依赖时包含检查名称、结果与耗时;不包装公共体。存活检查不执行依赖探针;服务开始退出后,就绪检查返回 503,存活仍可返回 200,直到监听关闭。OpenAPI 始终输出规范文档本身,不使用业务响应包装;关闭 HTTP 文档端点不影响生成 `docs/openapi.json`。
|
||||
|
||||
环境变量覆盖配置文件,例如 `GO_OMNI_ENDPOINTS_READINESS_PATH=/readyz`、`GO_OMNI_ENDPOINTS_READINESS_RESPONSE_MODE=raw`、`GO_OMNI_ENDPOINTS_OPENAPI_ENABLED=false`;liveness/readiness/openapi 都支持对应的 ENABLED 和 PATH。Config/AppOptions 的最终有效配置决定是否注册。
|
||||
|
||||
关闭或改名后,旧路径不再由框架保留,项目可以在 `.api` 自己声明。启用的内置 GET 路由与业务路由相同或被业务参数/通配路径覆盖时,在创建 App、开始监听之前报错;不会覆盖业务路由。内置接口仍经过全局中间件;项目在鉴权中间件中明确决定这些路径的访问规则,框架不自动绕过鉴权。
|
||||
|
||||
<a id="从-v012-生成项目升级"></a>
|
||||
|
||||
## 从 v0.13 或更早版本升级
|
||||
|
||||
请按 [v0.14 定点迁移说明](DEVELOPMENT.md#v013-项目升级) 使用 `api upgrade`。原始模板可以自动升级;含业务修改的基础文件必须参考局部预览合并。旧 New/Close/OnClose 移到框架 Runtime,项目依赖初始化保留在 Init。不要继续向项目里复制生命周期锁和清理栈。
|
||||
|
||||
## App 默认响应格式
|
||||
|
||||
在 **入口** `.api` 声明一次:
|
||||
|
||||
```text
|
||||
@app (
|
||||
defaultResponse: CommonResponse
|
||||
)
|
||||
response CommonResponse (
|
||||
codeType: int32
|
||||
successCode: 0
|
||||
errorCode: 9999
|
||||
messageField: msg
|
||||
dataMode: always
|
||||
)
|
||||
```
|
||||
|
||||
优先级:路由 `@response` → 分组 `@server(response: ...)` → App 默认。模板可以定义在 import 文件里,`@app` 只能在入口声明。重复声明、引用不存在的模板会报错;不会按文件顺序猜默认值。未声明 `@app` 时使用框架现有默认格式。
|
||||
|
||||
生成的 App 普通路由、404/405、未单独指定格式的生成路由共用默认模板。已匹配路由的中间件提前报错也使用路由策略。`@response raw` 的成功 JSON 不包装,错误沿用有效模板。
|
||||
|
||||
显式 `.api` 默认响应优先于 Config/AppOptions 中的响应配置,保持接口文档和实际响应一致。其他 AppOptions 继续生效;要改变格式就修改 `.api`。项目无需访问 handler 私有变量。
|
||||
|
||||
## 自动上传、回调与下载
|
||||
|
||||
以下声明生成完整 Handler,用户填写 Logic:
|
||||
|
||||
```text
|
||||
@handler upload
|
||||
@body (mode: multipart; maxBytes: 33554432)
|
||||
post /upload returns (UploadResp)
|
||||
|
||||
@handler callback
|
||||
@body (mode: raw; maxBytes: 65536)
|
||||
@produces "text/plain"
|
||||
post /callback
|
||||
|
||||
@handler download
|
||||
@produces "application/octet-stream"
|
||||
get /download (DownloadReq)
|
||||
```
|
||||
|
||||
- 以下为未声明 multipart 字段的流式用法;声明式绑定见 [上传字段](DEVELOPMENT.md#声明式-multipart)。raw/流式 multipart 的 Logic 增加 `body *httpx.Body` 参数。`body.Raw` 是受路由上限约束的原始字节流;`body.Header` 是请求头副本。
|
||||
- multipart 用 `body.Parts.NextPart()` 顺序读取,关闭每个 Part;不调用 ParseMultipartForm,不产生上传临时文件,也不把整个文件放入内存。每个字段/文件的业务要求由 Logic 校验。请求 DTO 可定义 path/form/header;`form` 仍是 URL 查询参数,不能用 json DTO 重复读取上传体。
|
||||
- Parts 是 `*httpx.MultipartReader`,返回的 `*httpx.MultipartPart` 提供 Read、Close、Header、FormName、FileName;NextRawPart 可读取未做 quoted-printable 解码的部分。NextPart 自动关闭上一个未读完的 Part;Close 会报告读取错误。不要混用 Parts 与 Raw 读取同一个 multipart 请求。
|
||||
- multipart 缺少结束边界、头部/内容损坏默认返回 `400 / INVALID_MULTIPART`;超过路由限额仍为 413,媒体类型/缺少 boundary 为 415。框架只提供通用标识和默认提示,项目可以在 Go 错误目录中配置自己的状态、业务码和文案。内部解析原因保留在错误链,不进入公开响应;Logic 自己返回的数据库等错误不会被统一改成 400。
|
||||
- raw 数据不经过 JSON 解码。签名算法、密钥及回执内容由业务提供;同一个流只消费一次。若中间件先读取验签,项目必须显式使用受限缓冲/重放,生成器不会自动缓存大文件。
|
||||
- `@produces` 使 Logic 返回 `(*httpx.Stream, error)`,其中 Content 是 `io.ReadCloser`,可选 Name 生成附件下载头。ContentType 可携带 charset,但必须与契约媒体类型匹配。Handler 负责关闭内容流,无公共体包装;写出后发生错误不会追加 JSON。
|
||||
- Handler 在成功响应前检查剩余请求流,包含未读完的 multipart 部分、最终边界和请求体上限;已经遇到的 multipart 读取错误不会因业务忽略它而变成成功。读取失败和 413 不能撤回 Logic 已产生的外部副作用;重要业务应先调用 `body.Complete()` 确认完整读取/验证,再提交事务或发布文件。Complete 可重复调用。
|
||||
- 自动 Handler 采用先完成输入、再发送输出的顺序;不要把正在读取的 body.Raw 直接当作 Stream.Content 返回。需要同时读写请求/响应的协议时使用手写扩展。
|
||||
- 大文件的耗时仍受 HTTP 读写超时约束。按接口需要配置,框架不自动无限延长。
|
||||
|
||||
默认请求体上限为 1 MiB,按路由设置 maxBytes,不能通过统一放大全局限制替代。`@produces` 仅声明媒体类型,不保证业务返回的内容符合该媒体的语义。Range、SSE 刷新、特殊协议等需要时仍可显式选 `@handlerMode manual`;它是可选扩展,不是普通上传下载的前置条件。
|
||||
|
||||
## 旧结构处理
|
||||
|
||||
新生成器使用 manifest v2,不兼容原 settings/servicecontext/service 与 cmd/server 布局。已有旧目录会报错,不能通过覆盖 main 自动迁移。试验代码需保留时,在新目录生成,移动 `.api`、Logic、配置和 svc 扩展并调整导入;独立的 `cmd/migrate` 等工具由项目维护。不会静默覆盖手写 main 或删除无关命令。
|
||||
|
||||
`go-omni new` 与 `api new` 统一使用此结构。受管理项目通过 `.api` 增加模块,旧的 `add` 手动模块装配方式不适用于该结构。直接引用 go-omni 框架包的项目不受这些目录约定约束。
|
||||
|
||||
## 已有服务接入步骤
|
||||
|
||||
每个独立服务生成一个项目,module 名称、上层目录和共享业务包由项目选择。接入时固定 CLI 与框架版本为 v0.18.0-preview:
|
||||
|
||||
1. 先保留旧服务,整理实际路由及现有响应、鉴权和超时要求。在新目录执行 `go-omni new --api /path/to/service.api --module example.com/service ./service`;不对旧入口强制覆盖。
|
||||
2. 在 `.api` 中选择 `@app(defaultResponse: ...)`,显式保留原项目的字段、成功码、错误码和 `dataMode`。需要独立回执的路由单独声明 raw/produces。普通路由、404/405 和提前返回的中间件一起验收。
|
||||
3. 把配置放入 `internal/config` 和 `etc/config.yaml`;数据库、自定义 Connector、Redis、ID Client 等依赖放入 `internal/svc`,初始化成功后立即 `OnClose`。现有事务和迁移工具继续由项目维护;数据库扩展见 [database](database/README.md)。
|
||||
4. 业务错误目录只在项目 Go 包维护,通过 `svc.AppOptions()` 返回 `omni.WithErrors(apierrors.Catalog())` 注册一次。需要继承 App 目录的路由不要重复声明独立错误目录。项目鉴权放入中间件,业务实现迁入 Logic/Repository;无需修改 main 或生成的 Handler。
|
||||
5. 上传、原始请求体验签和下载优先使用自动 Handler;只有特殊协议才选择手写扩展。保留实际验签算法、流式处理、大小限制和存储权限,按 [迁移扩展](MIGRATION.md) 处理旧 Handler 与自定义标量。
|
||||
6. 每个服务运行下面的检查,再用实际配置验证依赖连通、启动失败清理和正常退出。业务侧补充原有接口兼容、鉴权、事务回滚与实际上传下载回归;框架测试不能代替这些业务验收。
|
||||
|
||||
```sh
|
||||
go mod tidy
|
||||
go-omni api validate
|
||||
go-omni api generate --check
|
||||
go test ./...
|
||||
go build .
|
||||
go run .
|
||||
```
|
||||
|
||||
后续增加接口修改 `.api` 并重新生成;增加依赖修改配置和 svc。将契约、生成文件、manifest 与业务代码一起提交。完整流程不需要项目另外维护启动器,也不需要读取 handler 的私有变量。
|
||||
108
README.md
108
README.md
@@ -1,11 +1,25 @@
|
||||
# GoOmni
|
||||
|
||||
GoOmni 是面向多个业务项目复用的 Go 后端框架:用 `.api` 定义接口、请求和返回类型,生成项目目录、HTTP 接入代码及 OpenAPI,业务代码负责 Service 和 Repository。也可以在已有项目中直接使用 `omni.New()`,或单独引入日志、存储等模块。
|
||||
GoOmni 是面向多个业务项目复用的 Go 后端框架:用 `.api` 定义接口、请求和返回类型,生成项目目录、HTTP 接入代码及 OpenAPI,业务代码负责 Logic 和 Repository。也可以在已有项目中直接使用 `omni.New()`,或单独引入日志、存储等模块。
|
||||
|
||||
**从这份 README 开始即可接入。** 当前版本为 **`v0.10.0-preview`**,Go **1.22+**;CLI 和项目依赖使用同一固定版本。框架不依赖 Gin,默认使用标准库网络层,提供可切换的 native HTTP/1.1 和实验性 HTTP/2 适配器。
|
||||
**从这份 README 开始接入。** 当前版本 **`v0.18.0-preview`**,新生成项目采用根目录入口和 `config / svc / logic / handler`。可以固定版本开展业务接入;文件维护边界和已有服务迁移步骤见 [项目指南](PROJECT.md),版本变化与验收见 [发布说明](RELEASE.md)。
|
||||
|
||||
**v0.17 提供**:公共字段组合、可选结构映射、带依赖的中间件工厂、按需 Repository 骨架,使用与迁移见 [生成能力指南](GENERATION.md)。
|
||||
|
||||
**本版新增**:可空嵌套映射、映射配置按文件拆分、显式 nil 策略和可选指针返回辅助,见 [生成能力指南](GENERATION.md)。
|
||||
|
||||
Go **1.22+**。框架不依赖 Gin,默认使用标准库网络层,提供可切换的 native HTTP/1.1 和实验性 HTTP/2 适配器。
|
||||
|
||||
响应字段、业务码、登录方式、权限规则和数据库表结构由接入项目决定。文中的 `code/msg/data`、`200/9999` 都是示例配置,可以替换。
|
||||
|
||||
本版补齐 `returns([]string/[]int64/[]bool/[]any)`、OpenAPI 公共引用、workspace 一次导出共享 Go 错误目录,以及统一环境变量别名覆盖。详细配置与升级步骤见 [业务开发指南](DEVELOPMENT.md)。
|
||||
|
||||
已有通用开发能力见 [业务开发指南](DEVELOPMENT.md):数组/null、严格请求策略、声明式上传、完整配置环境覆盖、资源生命周期、共享契约和升级工具。MySQL 直接使用 [官方适配器](database/mysql/README.md)。
|
||||
|
||||
后台组件可在 svc 登记 [分阶段关闭](lifecycle/README.md#后台组件分阶段关闭),先停止接收工作并等待收尾,再关闭共享资源;无需绑定队列或 ORM。
|
||||
|
||||
使用 GORM 的项目可以选择 [gormx 适配器](database/gormx/README.md),复用数据库初始化和连接池,并将 SQL 日志接入 logx。
|
||||
|
||||
## 选择接入方式
|
||||
|
||||
| 你的项目 | 从哪里开始 |
|
||||
@@ -22,27 +36,26 @@ GoOmni 是面向多个业务项目复用的 Go 后端框架:用 `.api` 定义
|
||||
|
||||
module 地址是 `git.yuntuo.hk/k2safe/go-omni`。先确保本机 Go 和 Git 可用,并具有仓库读取权限;使用自己的 Git 凭据管理器,不把账号或 Token 写进 module 地址。
|
||||
|
||||
以下命令在同一个终端执行。已有 `GOPRIVATE` 时,把 `git.yuntuo.hk` 追加到原来的逗号分隔列表。
|
||||
安装固定版本的 CLI,后面的命令继续使用同一终端。私有 module 加入 GOPRIVATE,保留已有配置:
|
||||
|
||||
```sh
|
||||
export GOPRIVATE=git.yuntuo.hk
|
||||
go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.10.0-preview
|
||||
export GOPRIVATE="$(go env GOPRIVATE),git.yuntuo.hk"
|
||||
go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.18.0-preview
|
||||
export PATH="$(go env GOPATH)/bin:$PATH"
|
||||
go-omni --help
|
||||
```
|
||||
|
||||
如果配置了自定义 `GOBIN`,将那个目录加入 PATH。只引用框架包、不生成项目时可以跳过 CLI 安装,直接使用下文的 `go get`。
|
||||
如果设置过 GOBIN,将该目录加入 PATH。远程 CLI 默认引用自身版本的框架,生成的 go.mod 不需要本地 replace。开发者与 CI 使用同版 CLI;框架源码开发方式见 [CLI 文档](cmd/go-omni/README.md#版本与迁移)。
|
||||
|
||||
## 新项目:生成并运行
|
||||
|
||||
在准备存放业务项目的目录执行,`./hello` 必须是新目录或空目录;把 `example.com/hello` 换成自己的 Go module 名称。
|
||||
|
||||
```sh
|
||||
go-omni api new --framework-version v0.10.0-preview \
|
||||
--module example.com/hello ./hello
|
||||
go-omni api new --module example.com/hello ./hello
|
||||
cd hello
|
||||
go mod tidy
|
||||
go run ./cmd/server
|
||||
go run .
|
||||
```
|
||||
|
||||
另开终端验证:
|
||||
@@ -59,30 +72,31 @@ curl -sS http://127.0.0.1:8080/openapi.json
|
||||
{"code":"OK","message":"success","data":{"message":"Hello Alice"},"requestId":"本次请求编号"}
|
||||
```
|
||||
|
||||
按 Ctrl+C 停止服务。项目依赖已固定为远程版本,不需要克隆框架源码或添加本地 `replace`。
|
||||
按 Ctrl+C 停止服务。进入项目目录即可 `go build .`;该示例不需要数据库或其他外部服务。
|
||||
|
||||
### 生成的项目放什么
|
||||
|
||||
```text
|
||||
hello/
|
||||
├── api/service.api # 维护接口定义,也可 import 其他 .api
|
||||
├── cmd/server/main.go # 启动、路由注册、优雅退出
|
||||
├── main.go # 生成器维护:启动、路由注册、优雅退出
|
||||
├── etc/config.yaml # 项目配置
|
||||
├── omni.gen.json # 项目维护的生成选项与可选映射
|
||||
├── internal/
|
||||
│ ├── settings/ # 配置读取
|
||||
│ ├── servicecontext/ # 数据库、日志、外部客户端等依赖
|
||||
│ ├── types/typesGen.go # 生成的请求与业务返回类型
|
||||
│ ├── config/ # 配置读取
|
||||
│ ├── svc/ # 数据库、日志、外部客户端等依赖
|
||||
│ ├── types/*Gen.go # 按 .api 文件生成 DTO
|
||||
│ ├── handler/ # 生成的绑定、响应策略、路由及文档
|
||||
│ ├── service/greet/ # 在这里实现业务逻辑
|
||||
│ ├── repository/greet/ # 在这里实现数据访问
|
||||
│ └── middleware/ # 声明鉴权等中间件后生成对应 hook
|
||||
│ ├── logic/greet/ # 在这里实现业务逻辑
|
||||
│ ├── repository/ # 按需创建,默认不生成空骨架
|
||||
│ └── middleware/ # 声明鉴权等中间件后生成工厂
|
||||
├── docs/openapi.json # 生成的 OpenAPI 3.0.3
|
||||
└── .goOmni/apiManifest.json # 生成文件所有权与校验信息,一起提交 Git
|
||||
```
|
||||
|
||||
修改 `api/service.api` 后重新生成,业务写在 `internal/service`,数据访问写在 `internal/repository`,依赖在 `internal/servicecontext` 装配。声明业务错误后还会生成 `internal/apierrors/errorsGen.go`。
|
||||
修改 `api/service.api` 后重新生成,业务写在 `internal/logic`,数据访问写在 `internal/repository`,依赖在 `internal/svc` 装配。声明业务错误后还会生成 `internal/apierrors/errorsGen.go`。
|
||||
|
||||
`api new` 的问候接口可以直接运行;自定义接口新生成的 Service 和鉴权 hook 返回 **501**,需要实现后才可用。生成器不会根据 HTTP 字段猜数据库表或业务规则。
|
||||
`api new` 的问候接口可以直接运行;自定义接口未实现的 Logic 返回 **501**。新项目的具名中间件工厂未实现时直接阻止启动;v0.16 的原有 handler 模式仍返回 501。生成器不会根据 HTTP 字段猜数据库表或业务规则。
|
||||
|
||||
### 从自己的接口定义生成
|
||||
|
||||
@@ -91,6 +105,8 @@ hello/
|
||||
```text
|
||||
syntax = "v1"
|
||||
|
||||
@app (defaultResponse: CommonResponse)
|
||||
|
||||
response CommonResponse (
|
||||
body: {"code":"${code}", "msg":"${message}", "data":"${data}"}
|
||||
codeType: int32
|
||||
@@ -126,13 +142,13 @@ service catalog-api {
|
||||
|
||||
```sh
|
||||
go-omni api validate --api catalog.api
|
||||
go-omni new --api catalog.api --framework-version v0.10.0-preview \
|
||||
go-omni new --api catalog.api \
|
||||
--module example.com/catalog ./catalog
|
||||
cd catalog
|
||||
go mod tidy
|
||||
```
|
||||
|
||||
将生成的 `internal/service/item/getItem.go` 替换为下面的演示实现。实际业务中换成 Repository 查询,Service 返回业务数据或错误,不再手工包装公共体:
|
||||
将生成的 `internal/logic/item/getItem.go` 替换为下面的演示实现。实际业务中换成 Repository 查询,Logic 返回业务数据或错误,不再手工包装公共体:
|
||||
|
||||
```go
|
||||
package item
|
||||
@@ -144,7 +160,7 @@ import (
|
||||
"example.com/catalog/internal/types"
|
||||
)
|
||||
|
||||
func (s *Service) GetItem(ctx context.Context, req *types.GetItemReq) (*types.ItemResp, error) {
|
||||
func (s *Logic) GetItem(ctx context.Context, req *types.GetItemReq) (*types.ItemResp, error) {
|
||||
if req.ItemID != "1001" {
|
||||
return nil, apierrors.ItemNotFound()
|
||||
}
|
||||
@@ -156,7 +172,7 @@ func (s *Service) GetItem(ctx context.Context, req *types.GetItemReq) (*types.It
|
||||
|
||||
```sh
|
||||
# catalog 项目目录中启动;先停掉前面的 hello 服务,释放 8080 端口。
|
||||
go run ./cmd/server
|
||||
go run .
|
||||
# 另一个终端:
|
||||
curl -i http://127.0.0.1:8080/api/v1/items/1001
|
||||
curl -i http://127.0.0.1:8080/api/v1/items/missing
|
||||
@@ -167,24 +183,24 @@ curl -i http://127.0.0.1:8080/api/v1/items/missing
|
||||
需要 MySQL、文件存储或文件日志时,**首次生成**可以使用:
|
||||
|
||||
```sh
|
||||
go-omni new --api catalog.api --framework-version v0.10.0-preview \
|
||||
go-omni new --api catalog.api \
|
||||
--module example.com/catalogFull --database mysql --storage local \
|
||||
--log-output file --log-format json ./catalogFull
|
||||
```
|
||||
|
||||
该命令在 `catalog.api` 所在目录执行,目标仍需是新目录。数据库迁移、表结构和 Repository 由项目维护,API 模板不自动建表;MySQL DSN 通过 `GO_OMNI_DB_DSN` 提供。存储还可选 `s3`、`r2`、`oss`,按生成项目的配置说明提供桶、端点和凭据。完整装配例子见 [userService](examples/userService/README.md)。
|
||||
该命令在 `catalog.api` 所在目录执行,目标仍需是新目录。数据库迁移、表结构和 Repository 由项目维护,API 模板不自动建表;MySQL 使用结构化配置,密码通过 `GO_OMNI_DB_PASSWORD` 提供。存储还可选 `s3`、`r2`、`oss`,按生成项目的配置说明提供桶、端点和凭据。完整生成项目示例见 [managedService](examples/managedService/README.md)。
|
||||
|
||||
不采用 `.api` 时,也可以用 `go-omni new --framework-version v0.10.0-preview --module example.com/billing ./billing` 创建手写接口的 CRUD 示例项目;两种生成方式选择一种即可。
|
||||
`go-omni new` 与 `go-omni api new` 统一生成契约项目。需要手写项目时,可直接引用框架包;生成器不再提供另一套 CRUD 启动目录。
|
||||
|
||||
## 已有项目:按模块接入
|
||||
|
||||
在业务项目的 **go.mod 所在目录**执行:
|
||||
|
||||
```sh
|
||||
go get git.yuntuo.hk/k2safe/go-omni@v0.10.0-preview
|
||||
go get git.yuntuo.hk/k2safe/go-omni@v0.18.0-preview
|
||||
```
|
||||
|
||||
已有项目可直接复用现有目录、Service、Repository 和数据库连接,先选择一个接口接入。也可以只用 `logx`、`storage` 等包,暂时保留原来的 HTTP 框架。
|
||||
已有项目可直接复用现有目录、Service、Repository 和数据库连接,先选择一个接口接入。也可以只用 `logx`、`storage` 等包,暂时保留原来的 HTTP 框架。希望入口和 HTTP 接入都交给生成器的项目,按 [已有服务接入步骤](PROJECT.md#已有服务接入步骤) 在新目录生成后迁入业务代码。
|
||||
|
||||
下面是可独立运行的 `main.go` 示例;迁入现有项目时,把路由调用接到自己的业务服务即可。
|
||||
|
||||
@@ -305,11 +321,13 @@ auth UserAuth (
|
||||
)
|
||||
```
|
||||
|
||||
生成器会创建 `internal/middleware/auth.go`,由项目实现凭据校验和身份注入。声明会生成路由绑定与 OpenAPI 的认证描述,**不会自动实现登录或授权规则**;未实现的 hook 返回 501。手写路由可使用 `auth.RequireAuth(authenticator)` 或自己的中间件。JWT、Session 和 Policy 按需选择。
|
||||
新项目生成 `internal/middleware/authFactory.go`,由项目实现 `NewAuth(deps)`;生成器负责初始化和注册。v0.16 或 handler 模式继续使用 `internal/middleware/auth.go`。凭据校验和身份注入由项目实现。声明会生成路由绑定与 OpenAPI 的认证描述,**不会自动实现登录或授权规则**;未实现的工厂阻止启动,原有 hook 模式返回 501。手写路由可使用 `auth.RequireAuth(authenticator)` 或自己的中间件。JWT、Session 和 Policy 按需选择。
|
||||
|
||||
业务错误可以集中定义,业务只选择发生了哪个错误。**HTTP 状态由项目配置,业务失败可以返回 HTTP 200**;框架不根据 HTTP 状态单独判断成功与否。
|
||||
|
||||
API 项目推荐在公共 `.api` 中声明目录,其他契约通过 `import` 引用:
|
||||
错误目录支持两种维护方式,按项目选择一种:**Go 中定义并用 `omni.WithErrors` 注册一次**,或者在公共 `.api` 中声明并生成调用函数。前者不要求 `.api` 重复声明目录或逐路由列出错误;生成路由默认继承 App 目录。
|
||||
|
||||
采用 `.api` 管理时,其他契约可通过 `import` 引用:
|
||||
|
||||
```text
|
||||
errorCatalog CommonErrors (
|
||||
@@ -358,17 +376,26 @@ app := omni.New(
|
||||
// return omnierrors.ByCode("USERNAME_INVALID").WithCause(err)
|
||||
```
|
||||
|
||||
目录也可放入 `config.Config.Errors`,由 JSON/YAML 加载;生成项目的路径为 `server.errors`,但契约生成路由以 `.api` 绑定的目录为准。不同 App 独立持有配置,单路由也可通过 `response.WithErrors` 绑定专用目录与自定义 Encoder。直接输出文本、XML、下载或已提交的响应继续保留原协议。
|
||||
目录也可放入 `config.Config.Errors`,由 JSON/YAML 加载;生成项目的路径为 `server.errors`,生成路由只有显式引用错误定义时才使用 `.api` 独立目录,未声明则继承 App。不同 App 独立持有配置,单路由也可通过 `response.WithErrors` 绑定专用目录与自定义 Encoder。直接输出文本、XML、下载或已提交的响应继续保留原协议。
|
||||
|
||||
访问日志保留实际 HTTP 状态,并增加失败标记、逻辑错误标识和目录业务码;指标、链路及框架标准中间件/幂等重放也保留失败语义。未知标识使用系统错误兜底,内部 cause 不写入默认公开响应。旧 `error (...)`、`New` / `BadRequest` / `Codes` / `Messages` 继续支持;新目录与旧映射冲突会报错,避免静默覆盖。
|
||||
|
||||
动态提示也可以集中配置,在目录中声明 `message: "{label}不能超过{max}个字符"` 和有序 `params`(label/string、max/int),生成带类型的调用:
|
||||
|
||||
```go
|
||||
return apierrors.TextTooLong("企业名称", 100)
|
||||
// 最终提示:企业名称不能超过100个字符
|
||||
```
|
||||
|
||||
运行时支持 `omnierrors.ByCode("TEXT_TOO_LONG").WithParams(...)`,与生成函数共用解析器。固定错误继续无参数调用;缺参或类型错误使用系统兜底。模板、参数类型及具体响应示例同步写入 OpenAPI。完整示例见 [errorParams.api](examples/contracts/errorParams.api)。
|
||||
|
||||
更详细的配置、作用域和升级步骤见 [统一错误目录](errors/README.md)。
|
||||
|
||||
### 数据库、存储及其他模块
|
||||
|
||||
依赖在启动入口或项目自己的 `internal/servicecontext` 中创建,通过构造函数传给 Service/Repository,避免每个请求都新建连接池。只注册自己拥有的资源,例如 `app.OnShutdown(db.Close)`;关闭按注册的逆序执行。
|
||||
生成项目在 `internal/svc` 中创建依赖,提供给 Logic/Repository,并用 `c.OnClose(db.Close)` 登记自有资源。生成的 main 在 HTTP 排空后关闭 svc,再关闭日志。直接使用框架的手写项目则可以用 `app.OnShutdown(db.Close)`;不要对同一资源重复登记。
|
||||
|
||||
已有项目使用 `driver.Connector` 时,直接 `database.OpenConnector(ctx, connector, database.DefaultPoolConfig())`。MySQL 等驱动的独立 TLS、Logger、时区与拨号参数由项目构造 Connector;框架负责连接池、启动 Ping、健康检查及事务。无需转成 DSN,也无需注册全局 TLS/Logger。具体示例和资源归属见 [Connector 接入](database/README.md#connector-接入v091-preview)。
|
||||
已有项目使用 `driver.Connector` 时,直接 `database.OpenConnector(ctx, connector, database.DefaultPoolConfig())`。MySQL 可直接用 [database/mysql](database/mysql/README.md) 配置独立 TLS、Logger、时区与拨号参数;任意驱动也可由项目构造 Connector。框架负责连接池、启动 Ping、健康检查及事务。无需转成 DSN,也无需注册全局 TLS/Logger。具体示例和资源归属见 [Connector 接入](database/README.md#connector-接入v091-preview)。
|
||||
|
||||
| 能力 | 包与接入说明 |
|
||||
| --- | --- |
|
||||
@@ -395,12 +422,12 @@ API 项目的数据库迁移需要单独维护并显式执行;启动不会自
|
||||
| `GO_OMNI_SHUTDOWN_TIMEOUT` | 优雅退出等待时间,默认 `10s` |
|
||||
| `GO_OMNI_ENABLE_LOGGER` / `GO_OMNI_LOG_LEVEL` | 访问日志开关、日志级别 |
|
||||
| `GO_OMNI_TRUSTED_PROXIES` | 可解析转发地址头的可信代理 IP/CIDR,逗号分隔 |
|
||||
| `GO_OMNI_DB_DSN` | 选择 MySQL 初始化时的数据库连接配置 |
|
||||
| `GO_OMNI_DB_HOST/USER/PASSWORD/DATABASE` | 官方 MySQL 适配器的结构化连接配置 |
|
||||
|
||||
例如在生成项目中切换 native:
|
||||
|
||||
```sh
|
||||
GO_OMNI_TRANSPORT=native go run ./cmd/server
|
||||
GO_OMNI_TRANSPORT=native go run .
|
||||
```
|
||||
|
||||
手写入口可用 `omni.WithTransport(omni.TransportNative)`;TLS 配置为 `omni.WithTLS(certFile, keyFile)`。native 提供 HTTP/1.1、Keep-Alive、TLS、超时、连接/请求并发上限和对象复用;HTTP/2 是单独的实验适配器,必须配置 TLS。默认 nethttp 的 TLS 支持 HTTP/1.1 与 HTTP/2。
|
||||
@@ -421,6 +448,8 @@ server:
|
||||
|
||||
自研网络层通过 `RunWith(ctx, runner)` 接入监听与连接生命周期,通过 `NewWithRouter(router)` 替换路由;业务 Handler/Service 不需要导入具体适配器。细节与验收契约见 [网络适配层](transport/README.md)。
|
||||
|
||||
生成项目的依赖可调用 `svcCtx.RequestShutdown(err)` 主动申请退出,框架排空请求、清理依赖后保留故障原因并以非零状态退出。健康检查与 OpenAPI 的开关、路径通过 `server.endpoints` 配置,健康响应默认沿用项目公共体;未就绪保持 HTTP 503。完整配置和 v0.12 项目的升级步骤见 [项目指南](PROJECT.md#依赖主动申请退出)。
|
||||
|
||||
`Run` 处理 SIGINT/SIGTERM;`RunContext` 接收调用方控制的退出信号。服务先停止接收新请求,等待活跃请求完成,再清理资源;超时会取消请求并关闭连接,业务代码仍需响应 context 取消。
|
||||
|
||||
## 日常开发与升级
|
||||
@@ -436,9 +465,9 @@ go-omni api generate --check
|
||||
go test ./...
|
||||
```
|
||||
|
||||
`Gen.go`、`docs/openapi.json` 和 manifest 由生成器管理,手动修改会报告冲突。Service、Repository、中间件实现、main、配置和依赖装配属于项目,重复生成保留手写内容;契约改变后仍需自行调整业务签名并通过编译。将 `.api`、生成文件和 `.goOmni/apiManifest.json` 一起提交,CI 可运行 `generate --check` 检查是否同步。
|
||||
`main.go`、`Gen.go`、`docs/openapi.json` 和 manifest 由生成器管理,手动修改会报告冲突。Logic、Repository、中间件实现、配置和依赖装配属于项目,重复生成保留手写内容。契约变化会提示受影响的 Logic 文件和 DTO 字段,明确的签名错误会给出原签名与期望签名;生成后 `--check` 仍能检出未修复的问题。将 `.api`、`omni.gen.json`、生成文件和 `.goOmni/apiManifest.json` 一起提交,CI 同时运行 `generate --check` 与 `go test ./...`。详见 [契约变更提示与检查边界](PROJECT.md#契约变更提示)。
|
||||
|
||||
升级时先安装目标版本的 CLI,再在业务 module 中 `go get` **同一版本**;API 项目随后执行上述生成与测试步骤,手写项目只需依赖升级及业务回归。`v0.10.0-preview` 的变化、迁移边界和验收记录见 [发布说明](RELEASE.md)。
|
||||
CLI 与业务 module 固定使用 `v0.18.0-preview`。v0.14.x 项目升级依赖与 CLI 后重新生成,框架维护的 main/runtimeGen 自动更新;v0.13 及更早项目按 [升级预览](DEVELOPMENT.md#v013-项目升级) 处理,不直接覆盖手写入口或业务文件。升级后执行生成检查与业务回归。版本变化、迁移边界和验收记录见 [发布说明](RELEASE.md)。
|
||||
|
||||
## 按需深入
|
||||
|
||||
@@ -446,8 +475,9 @@ go test ./...
|
||||
| --- | --- |
|
||||
| `.api` 类型、参数绑定、错误、鉴权和生成规则 | [API 开发指南](API.md) |
|
||||
| 公共响应体、提示优先级、独立回调协议 | [响应参考](RESPONSE.md) |
|
||||
| CLI 完整参数、本地框架开发和模块脚手架 | [CLI](cmd/go-omni/README.md) |
|
||||
| 完整 API 业务项目 | [userService](examples/userService/README.md) |
|
||||
| CLI 完整参数、本地框架开发与迁移 | [CLI](cmd/go-omni/README.md) |
|
||||
| 当前生成项目:Go 错误目录、上传、回调和下载 | [managedService](examples/managedService/README.md) |
|
||||
| 数据库/事务业务参考(旧目录示例) | [userService](examples/userService/README.md) |
|
||||
| 手写接口分层示例 | [reference](examples/reference/README.md) |
|
||||
| 框架成熟度、历史变更 | [路线图](ROADMAP.md)、[更新记录](CHANGELOG.md) |
|
||||
| 网络性能、测试条件与适用范围 | [性能说明](PERFORMANCE.md)、[HTTP/2 验收](transport/native/HTTP2.md) |
|
||||
|
||||
269
RELEASE.md
269
RELEASE.md
@@ -1,52 +1,267 @@
|
||||
# v0.10.0-preview
|
||||
# v0.18.0-preview
|
||||
|
||||
统一错误目录:由项目集中定义逻辑标识、对外业务码、公开提示和 HTTP 状态,业务只选择错误。支持 HTTP 200 的业务失败,并贯通响应、错误函数生成、日志与 OpenAPI。
|
||||
补齐可空嵌套映射和按模块拆分配置,减少重复转换代码,业务空值规则仍由项目显式选择:
|
||||
|
||||
## 升级
|
||||
- 嵌套 mapping 支持 `*S → *T` 保留 nil、`S → *T`,以及指定 nil zero/error 后的 `*S → T`;同样适用于 slice/等长数组元素。错误保留字段路径、索引和内部错误链,失败不返回部分结果。
|
||||
- `mappingFiles` 显式引用项目内的映射文件,支持跨文件向前引用;统一检查重名、循环、类型与字段覆盖。禁止多层导入、路径越界和隐式覆盖,生成期间核对全部配置输入。
|
||||
- 同类型 `*T → T` 可显式配置 nil zero/error;现有 converter 保持完整控制,不默认放宽类型或混入领域规则。
|
||||
- `pointerResult: true` 额外生成 `<Name>Ptr(S) (*T,error)`,原值函数保留;失败返回 nil 与原错误,辅助函数名冲突在生成前拒绝。
|
||||
|
||||
```sh
|
||||
go get git.yuntuo.hk/k2safe/go-omni@v0.10.0-preview
|
||||
go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.10.0-preview
|
||||
# 使用 .api 的项目再执行:
|
||||
go-omni api generate --dry-run
|
||||
go get git.yuntuo.hk/k2safe/go-omni@v0.18.0-preview
|
||||
go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.18.0-preview
|
||||
go-omni api generate
|
||||
go-omni api generate --check
|
||||
go test ./...
|
||||
```
|
||||
|
||||
先升级项目依赖,再使用同版 CLI。新生成代码最低运行时为本版;旧的 APIError/New/BadRequest、Codes/Messages、error 声明及旧生成代码继续支持。API 错误包增加 `Catalog()`,该名称成为保留符号。项目的手写 main、Service 和 Repository 不被重生成覆盖。
|
||||
v0.17 的单文件 mappings、现有 converter 和业务文件继续保留。配置拆分时移动完整映射条目,主配置显式列出子文件;不保留重复定义。新能力均为可选生成配置,不增加运行时依赖。nil zero 表示 Go 零值并跳过子映射;业务必填、金额精度、编号格式和快照复制仍由项目负责。
|
||||
|
||||
## 接入
|
||||
使用与迁移见 [生成能力指南](GENERATION.md),回归和发布后验收见 [验收记录](reports/nullableMappingAcceptance.md)。本版仍为 preview。
|
||||
|
||||
- 手写项目:`omni.New(omni.WithErrors(catalog), omni.WithResponse(responseConfig))`,业务返回 `omnierrors.ByCode(key)`,按需 `.WithCause(err)`。
|
||||
- 契约项目:`.api errorCatalog` 定义一次,路由 `@errors` 或 `@server errors` 引用,生成 `apierrors.UsernameInvalid()` 等函数、Catalog、路由策略及 OpenAPI。
|
||||
- 项目目录的 `defaultStatus` 可以设为 200,单条错误可设置 403、409 等;默认只作用于目录条目,不改变未配置系统错误的兜底。
|
||||
- 逻辑标识必须唯一,对外业务码可以重复,支持字符串/int32/int64;整数精度与所选响应类型在加载/编译时校验。
|
||||
- HTTP 200 失败使用错误响应模板,OpenAPI 的 200 使用 anyOf 保留成功 DTO 和错误体;同码错误保留不同示例。
|
||||
- App 和路由目录隔离、初始化复制;每次生成/解析错误都返回独立对象。未知标识转系统兜底,默认公开响应不暴露 cause。
|
||||
---
|
||||
|
||||
完整接入从 [README](README.md) 开始;目录/作用域/配置边界见 [错误目录](errors/README.md);可直接生成的例子见 [errors.api](examples/contracts/errors.api)。
|
||||
# v0.17.0-preview(历史版本)
|
||||
|
||||
## 配置边界
|
||||
减少项目里的重复声明、字段赋值和中间件装配代码,业务规则继续由项目控制:
|
||||
|
||||
生成路由的目录和响应以 `.api` 为准,优先于 App 默认配置;ByCode 需要当前目录中存在该标识。使用生成函数后仍须声明实际路由的 @errors,保持文档准确。不同路由可使用不同码类型与返回格式。
|
||||
- `.api` 公共字段组合:保持 JSON 平铺,DTO、绑定、校验和 OpenAPI 同步;字段冲突、循环和超限明确报错。
|
||||
- 可选结构映射:在 `omni.gen.json` 显式声明源与目标,生成编译期检查的普通 Go 赋值。不同字段名、金额、时间和指针转换由项目指定函数;遗漏字段与类型错误在写文件前失败。不使用 JSON 往返或运行时反射。
|
||||
- 带依赖的中间件工厂:svc 初始化依赖后自动调用 `New<Name>(deps)` 并注册;错误、nil 或 panic 阻止启动,已登记资源按生命周期清理。
|
||||
- Repository 骨架默认关闭,可按 group 启用;已有业务文件始终保留。生成选项与映射纳入重新生成和 `--check`。
|
||||
|
||||
目录与响应模板分开,自定义 Encoder 继续控制最终 HTTP 状态和文本/XML 等协议。直接输出与已提交响应不会被再次包装。旧同标识 Codes/Messages 若与新目录冲突会明确拒绝。
|
||||
```sh
|
||||
go get git.yuntuo.hk/k2safe/go-omni@v0.17.0-preview
|
||||
go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.17.0-preview
|
||||
go-omni api generate
|
||||
go-omni api generate --check
|
||||
go test ./...
|
||||
```
|
||||
|
||||
访问日志记录实际提交状态,并带 failed/errorKey/可用的 businessCode。指标与链路识别 HTTP 200 业务失败;标准中间件和本地/共享幂等重放保留错误标识,旧缓存缺少标记时不补推历史状态。业务代码中的散落提示仍需业务项目按目录逐步迁移;本轮没有改写 eTax/ePay 业务逻辑。
|
||||
新项目默认使用 factories;已有项目首次升级保留 handlers 模式,需要显式迁移后再切换。映射按需开启,先准备业务类型、转换函数和依赖,再声明映射;普通引用赋值保留 Go 语义,缺失/null 语义由项目类型与转换函数定义。
|
||||
|
||||
## 验证入口
|
||||
使用方法、文件归属和升级步骤见 [生成能力指南](GENERATION.md),回归与发布验收见 [验收记录](reports/generationExperienceAcceptance.md)。本版仍为 preview;框架验收不等于业务项目完成迁移。
|
||||
|
||||
全量 44 个测试包的 race、vet、build 和实际进程验收通过;发布后的空缓存远程 CLI 与 7 组生成消费者回归也通过,同版依赖且无本地 replace。标签固定在 `8b588a24cd653dac77dd68f6777e0db367981f19`。
|
||||
|
||||
---
|
||||
|
||||
# v0.16.0-preview(历史版本)
|
||||
|
||||
补齐通用契约与生成体验,业务响应协议继续由项目定义:
|
||||
|
||||
- `returns([]string/[]int64/[]bool/[]any)` 及其他支持的基础类型数组,贯通 Logic、Handler、OpenAPI 和生成检查。nil 根数组输出 `[]`;DTO 数组、nullable、null、raw 和 204 保持原语义。字节数组明确拒绝 base64/数组歧义。
|
||||
- OpenAPI 以标准 components / `$ref` 共享重复响应、schema、示例;保留同一 HTTP 200 下的成功与失败、模板参数、数字业务码和 data:null。
|
||||
- workspace 显式配置 Go Catalog 提供方,一次导出共享 JSON,多服务通过受控根引用。支持服务/路由的文档错误列表,运行期 App 目录继承不变。check 核对导出漂移并保持项目文件不变。
|
||||
- EnvOptions.Aliases + ServerEnvAliases 统一完整配置与框架短名称的 Lookup、类型转换、空值规则。完整名称优先,显式空值不等于未设置;旧 ApplyEnv 的空值/逗号列表行为保留。
|
||||
|
||||
```sh
|
||||
go get git.yuntuo.hk/k2safe/go-omni@v0.16.0-preview
|
||||
go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.16.0-preview
|
||||
go-omni api generate
|
||||
go-omni api generate --check
|
||||
go test ./...
|
||||
```
|
||||
|
||||
旧项目 config/Logic/svc 继续由开发者维护,不会被重新生成覆盖。已有环境配置需按 [一次覆盖完整配置](DEVELOPMENT.md#一次覆盖完整配置与-http日志短名称) 合并 OverlayEnv 与 ApplyEnv;共享错误目录接入见 [workspace 配置](DEVELOPMENT.md#同一份-go-错误目录供多个服务生成)。已有手写权限 Handler 切换自动生成前,先迁出实现并显式移除 Manual 文件,生成器不会静默删除。
|
||||
|
||||
全量 race(含实际生成消费者)、vet、build、根入口/退出/再生成验证通过。eTax 只读契约验收:API 文档 20,751,149 → 847,171 字节,ID 文档 1,008,210 → 392,481 字节;70 个操作展开引用后与原文档完全一致。空缓存远程 CLI / 固定版本消费者验收也已通过,没有本地 replace;标签固定在 `64e3bd6cfced225476d58e95037249cd0a4fe695`。详见 [验收记录](reports/contractFollowupAcceptance.md)。这不代表已完成业务项目迁移。
|
||||
|
||||
---
|
||||
|
||||
# v0.15.0-preview(历史版本)
|
||||
|
||||
增加通用后台分阶段关闭:生成入口先完成 HTTP 排空,再运行全部 OnStop、全部 OnDrain,确认组件收尾后逆序释放 OnClose 资源,最后关闭日志。适用于任意后台组件;不引入 Orbit,不改变业务事务或任务一致性策略。
|
||||
|
||||
- svc 登记成对、同名的 OnStop/OnDrain,Context 用于早期退出通知,WorkContext 供已接受工作在收尾期间使用。同一阶段并发执行,存在内部先后依赖的组件组合登记。
|
||||
- server.lifecycle 配置 stopTimeout、drainTimeout、cancelTimeout。阶段超时取消工作并给未返回钩子收尾窗口;错误与阶段/组件名称可通过错误链追踪。
|
||||
- 不能确认后台已经结束时,返回 ErrResourcesRetained,保留共享资源和借用日志,生成进程非零退出。未返回的 Stop 不会与自己的 Drain 重叠;错误、panic 不阻止其他组件尝试收尾。
|
||||
- task.Runner 增加 StopAccepting/Wait,原 Shutdown/Close 继续可用。核心 lifecycle 不依赖 task 或其他适配器。
|
||||
- 初始化和监听失败复用同一流程;重复关闭只执行一次。生成器继续保留配置、svc 和业务代码。
|
||||
|
||||
```sh
|
||||
go get git.yuntuo.hk/k2safe/go-omni@v0.15.0-preview
|
||||
go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.15.0-preview
|
||||
go-omni api generate
|
||||
go-omni api generate --check
|
||||
go test ./...
|
||||
```
|
||||
|
||||
v0.14.x 无需重写 svc/config;生成器更新 main/runtimeGen,未配置时使用默认时限。也可 api upgrade --dry-run/--apply 更新依赖与生成文件,保留已有项目扩展。需要阶段保证的旧后台 OnClose 登记,应显式迁到 Stop/Drain;v0.13 及更早版本仍按原迁移指南处理。
|
||||
|
||||
全量 race(含实际生成项目)、vet、build 和隔离 MySQL/GORM 验收已通过。发布后的空缓存远程生成项目与真实 MySQL/GORM 验收也通过,CLI/框架同版且无本地 replace;标签固定在 `16deead70e860052c5b629511207578c03f9b2cb`。详见 [生命周期接入](lifecycle/README.md#后台组件分阶段关闭) 与 [验收记录](reports/phasedLifecycleAcceptance.md)。Go 无法强制结束任意 goroutine;OnClose 仍须能返回,HTTP 处理器仍须响应取消。此版不会将未知的第三方组件自动改造成优雅关闭组件。
|
||||
|
||||
---
|
||||
|
||||
# v0.14.1-preview(历史版本)
|
||||
|
||||
增加可选 `database/gormx`:GORM 复用 go-omni 已初始化的标准池、TLS、限额、健康检查和关闭流程,业务继续使用原生 GORM 模型与事务。核心 App/数据库包的编译依赖链不引入 GORM。
|
||||
|
||||
在 svc.Init 中调用 `gormx.MySQL(ctx, c.DB, gormx.WithLogger(c.Logger))`;通用 Open 工厂保留其他方言入口。适配器保护借用池不被初始化失败路径关闭,并为初始化查询设置时限。查询使用 WithContext,SQL 日志接入 logx,请求标识、慢查询和错误分类贯通;默认不输出绑定参数与驱动完整错误文本。
|
||||
|
||||
v0.14.0 项目更新 module 与 CLI 即可,不改生成目录或 main。模板最低运行时保持 v0.14.0;修正新版 CLI 把最低兼容版本误当自身版本、导致同版消费者出现版本提示的问题。其他数据库方言和管理额外池的插件未纳入本次实库验收,原生 PrepareStmt 的生命周期限制见 [接入说明](database/gormx/README.md)。
|
||||
|
||||
```sh
|
||||
go get git.yuntuo.hk/k2safe/go-omni@v0.14.1-preview
|
||||
go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.14.1-preview
|
||||
```
|
||||
|
||||
全量 race(含生成消费者)、vet、build,以及隔离 MySQL 的事务/取消/日志/生成项目接入通过。发布后的空缓存远程 MySQL/GORM 和生成项目验收均通过,无本地 replace,CLI 与框架使用同版。标签固定在 `b8ab6b35219d53d6de392c920fe7ecbe9a76dade`,详见 [GORM 验收](reports/gormAcceptance.md)。
|
||||
|
||||
---
|
||||
|
||||
# v0.14.0-preview(历史版本)
|
||||
|
||||
本版补齐契约驱动的业务开发与通用基础设施。项目继续维护接口、配置、依赖装配、Logic、SQL 和业务规则;生成器维护入口、请求绑定、路由、响应与文档。
|
||||
|
||||
## 安装与升级
|
||||
|
||||
```sh
|
||||
go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.14.0-preview
|
||||
go-omni new --module example.com/billing ./billing
|
||||
```
|
||||
|
||||
新生成器与运行时一起升级。已有 v0.13 项目先运行 `go-omni api upgrade --dry-run`,再按 [定点迁移步骤](DEVELOPMENT.md#v013-项目升级) 处理;不会静默覆盖自定义 svc/config。运行期的新构造器由生成器维护,项目初始化迁入 `svc.Init`,使用 `OnClose` 登记资源。
|
||||
|
||||
## 本版能力
|
||||
|
||||
- `.api` 支持根数组、可空对象、成功 null;真正 204 独立保留。生成 Logic、Handler、OpenAPI 和漂移检查使用相同语义。
|
||||
- 请求契约控制未知 query、原始 UTF-8、charset、必须/可选/禁止 body 和严格空对象。路由覆盖 App 策略,普通 JSON 只解析一次。
|
||||
- multipart 声明文件、文本和 JSON 表单字段。Logic 收到校验后的 DTO;文件有界暂存、流式读取、请求结束清理,保留底层 raw/multipart/下载扩展。
|
||||
- 完整项目配置支持环境覆盖、显式零值、空值策略、必填、嵌套及复杂类型。独立校验先于日志和连接创建。
|
||||
- `lifecycle.Runtime` 统一逆序清理、幂等关闭、失败回滚及故障保留;App Logger 独立,支持标准 `log.Logger` 与可信请求元数据。提供依赖中间件装配示例和可选 context 错误映射。
|
||||
- 官方 `database/mysql` 适配器提供结构化配置、每连接池独立 TLS/mTLS、驱动日志、启动检查与失败清理。继续返回同一 `database.DB`,不自动迁移表或重试业务事务。
|
||||
- 字段注释、示例和枚举说明进入 DTO/OpenAPI;`api format` 幂等格式化。Go 错误目录可显式导出供文档使用,无需复制错误定义。
|
||||
- 显式共享契约 roots、来源摘要、分文件 DTO、多服务 workspace 命令、基础模板升级预览;保留目录边界及业务文件所有权。
|
||||
|
||||
详见 [业务开发指南](DEVELOPMENT.md)、[数据库接入](database/mysql/README.md) 和 [验收记录](reports/businessDevelopmentAcceptance.md)。
|
||||
|
||||
全量 race(含生成项目)、vet、build 与本地进程验收通过。发布后的空缓存远程消费者和隔离 MySQL 验收也均通过;CLI/框架同版且无本地 replace。标签固定在 `f5478cdc7662704b164637649305361e52f7c598`,报告单独提交。
|
||||
|
||||
仍需业务维护:契约、配置、svc 依赖创建/探针/清理登记、错误目录、Logic、Repository 和权限中间件。特殊协议可使用保留的扩展点。声明式上传的暂存磁盘和并发量需配置容量;共享契约版本标签由项目管理,框架校验摘要但不下载依赖。此版仍为 preview,不代表 eTax/ePay 已完成业务迁移。
|
||||
|
||||
---
|
||||
|
||||
# v0.13.0-preview(历史版本)
|
||||
|
||||
本版补齐项目依赖主动退出与内置接口配置。ID 租约、数据库或后台任务等故障由项目判断,框架通过统一生命周期完成退出;不内置业务规则。
|
||||
|
||||
## 使用与升级
|
||||
|
||||
```sh
|
||||
go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.13.0-preview
|
||||
# 在业务 module 中执行
|
||||
go get git.yuntuo.hk/k2safe/go-omni@v0.13.0-preview
|
||||
go-omni api generate
|
||||
```
|
||||
|
||||
新项目自动获得全部扩展。v0.12 的 config/svc 属于项目文件,重新生成会保留它们;需按 [升级步骤](PROJECT.md#从-v012-生成项目升级) 给 svc 补入 Lifecycle、RequestShutdown 与清理衔接,main 和 Handler 由生成器更新。旧 v0.11 目录仍按原迁移规则在新目录生成。
|
||||
|
||||
## 行为
|
||||
|
||||
- `svc.RequestShutdown(err)` 非阻塞通知:停止接收请求、排空、关闭依赖和日志。首个非空故障保留,故障退出码为 1;正常停止在没有其他故障时退出码为 0。初始化期间通知、并发通知和排空期间报告故障均可处理。
|
||||
- App 生命周期入口保留异常 context 取消原因;符合 context.Canceled 的信号取消保持正常停止。生成入口还读取 Controller.Err,保留显式依赖故障,即使该故障包装了 context.Canceled。
|
||||
- `/health/ready` 修复绕过响应配置的问题,默认与 liveness 一起使用项目 App 的响应格式。就绪成功使用项目成功码与提示;未就绪使用 `NOT_READY` 的项目错误定义,同时固定 HTTP 503。raw 模式只返回健康数据;内部探针原因仅供日志。
|
||||
- `server.endpoints` 控制三个内置 GET 接口的开关和路径,健康端点另有 responseMode。支持环境变量覆盖。OpenAPI 保持原始规范,不包业务公共体;关闭 HTTP 端点不影响生成文档。
|
||||
- `.api` 不再固定保留三个默认路径;启用的内置路径与业务 GET/参数/通配路由重叠时,启动前返回明确错误并清理已创建的依赖。关闭或改名后允许项目接管旧路径。全局中间件继续执行。
|
||||
|
||||
## 验收
|
||||
|
||||
源码测试和实际生成项目验证记录见 [生命周期与端点验收](performance/lifecycleEndpointsAcceptance.md)。复验命令:
|
||||
|
||||
```sh
|
||||
GO_OMNI_SCAFFOLD_INTEGRATION=1 go test -race -p=1 -count=1 -timeout=300s ./...
|
||||
go vet ./...
|
||||
go build ./...
|
||||
python3 scripts/verifyErrorCatalogRelease.py --version v0.10.0-preview
|
||||
python3 scripts/verifyCompatibilityRelease.py --version v0.10.0-preview \
|
||||
--report performance/errorCatalogCompatibilityRemote.json
|
||||
python3 scripts/verifyManagedProject.py --report performance/lifecycleEndpointsLocal.json
|
||||
python3 scripts/verifyManagedRelease.py --version v0.13.0-preview --report performance/lifecycleEndpointsRemote.json
|
||||
```
|
||||
|
||||
测试覆盖多 App 隔离、并发原因隔离、精确 int64、重复/非法定义拒绝、200 失败与 403 覆盖、未知错误兜底、已提交响应不追加、文本回执、访问日志、指标/链路、标准桥接、本地与序列化共享幂等存储,以及真实生成项目的 early middleware、raw 输出和重生成保护。远程脚本从全新缓存安装 CLI,检查固定版本、模块校验和与没有本地 replace。
|
||||
发布后的远程空缓存验收已通过,耗时 26.55 秒;[机器报告](performance/lifecycleEndpointsRemote.json) 确认 CLI 和生成项目使用同版远程 module,无本地 replace。报告单独提交,发布标签保持在代码提交 `f0991d3ff248e4cb32812c368f0f8ba28740a6c7`。此版仍为 preview;具体业务的租约失效保护、鉴权与事务回归由接入项目验证。资源清理顺序不代表能强行结束忽略 context 取消的业务 goroutine;项目 Close 必须停止并等待自己启动的后台任务。
|
||||
|
||||
本版结果见 [统一错误目录验收](performance/errorCatalogAcceptance.md)。
|
||||
---
|
||||
|
||||
历史 Connector/HTTP 配置验收保留在 [v0.9.1 记录](performance/connectorAcceptance.md),历史版本变化见 [CHANGELOG](CHANGELOG.md)。
|
||||
# v0.12.0-preview(历史版本)
|
||||
|
||||
本版用于固定版本开展业务接入。生成器统一维护服务根目录 main.go、路由、HTTP Handler、DTO 和 OpenAPI,业务方维护 `.api`、config、svc、logic、repository 与项目中间件。目录不再生成 cmd/server、settings、servicecontext 或 service。
|
||||
|
||||
## 安装与接入
|
||||
|
||||
仓库权限及 PATH 配置见 [README](README.md#准备环境与安装)。CLI 与框架使用同一版本:
|
||||
|
||||
```sh
|
||||
go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.12.0-preview
|
||||
go-omni new --module example.com/hello ./hello
|
||||
cd hello
|
||||
go mod tidy
|
||||
go run .
|
||||
```
|
||||
|
||||
已存在的手写项目可直接 `go get git.yuntuo.hk/k2safe/go-omni@v0.12.0-preview`。采用生成目录的旧项目在新目录生成后迁入业务代码,详见 [接入步骤](PROJECT.md#已有服务接入步骤) 和 [文件维护边界](PROJECT.md#哪些文件由用户维护)。新 manifest v2 不兼容旧 manifest;不会静默覆盖手写 main,也不会删除独立 cmd/migrate 等工具。
|
||||
|
||||
## 本版行为
|
||||
|
||||
- 根目录支持 go run / go build;入口负责配置、日志、依赖、HTTP 启动与信号退出。svc 的 AppOptions、GlobalMiddleware 和 OnClose 是显式扩展入口;初始化/监听失败会释放资源,正常退出先排空请求再关闭依赖和日志。
|
||||
- `.api` 入口通过 `@app(defaultResponse: Name)` 选择默认响应,覆盖普通路由、404/405 与相应中间件错误;多模板不会按声明顺序猜默认值。字段、状态、业务码和文案由项目配置。
|
||||
- 保持 v0.11.1 的错误目录继承:Go 中注册一次,无独立错误目录的生成路由继承 App。带参数提示、内部错误链、raw 响应和独立目录保持各自行为。
|
||||
- raw/multipart 上传及 `@produces` 下载可直接编写 Logic,由生成 Handler 完成受限读取、响应和流关闭。损坏 multipart 默认 400,超限 413;项目可以定制公开错误。
|
||||
- 契约变更提示受影响的 Logic、DTO 字段和请求模式;明确的签名/语法/重复方法错误使 `generate --check` 失败。重新生成保留业务文件,main 和其他生成文件的漂移也会被检查。
|
||||
|
||||
## 验收与范围
|
||||
|
||||
发布前全量 race 测试、go vet、go build 和本地实际生成项目进程验收通过。发布后的空缓存远程验收也已通过,记录见 [生成项目验收](performance/managedProjectAcceptance.md) 和 [远程产物报告](performance/managedProjectRemote.json)。报告作为单独提交保存,v0.12.0-preview 标签保持在发布代码提交 `158b8002b355f09237cd12f49d9b66ee78d493f3`,没有改写。复验命令:
|
||||
|
||||
```sh
|
||||
GO_OMNI_SCAFFOLD_INTEGRATION=1 go test -race -p=1 -count=1 -timeout=300s ./...
|
||||
go vet ./...
|
||||
go build ./...
|
||||
python3 scripts/verifyManagedProject.py
|
||||
python3 scripts/verifyManagedRelease.py --version v0.12.0-preview
|
||||
```
|
||||
|
||||
远程验收使用空 module 缓存安装已发布 CLI,生成的业务项目仅依赖同版远程 module,无本地 replace;检查根目录运行/构建、重新生成、错误继承/模板参数、上传下载、nethttp/native 的真实请求与退出信号,以及初始化失败/监听失败/请求排空后的清理。
|
||||
|
||||
本版不代表具体业务项目已完成迁移验收。HTTP/2 仍为实验状态;真实数据库、云账号和业务服务器不属于本轮验收范围。轻量 Logic 检查不能代替 Go 编译器或业务测试。静态 OpenAPI 不执行 Go 代码,因此不会自动列举仅在 Go 中维护的错误目录。请求流校验不能回滚业务提前执行的副作用,关键写入应放在 `body.Complete()` 成功之后。
|
||||
|
||||
---
|
||||
|
||||
# v0.11.1-preview(历史版本)
|
||||
|
||||
修复生成路由使用空错误目录遮蔽 `omni.WithErrors(catalog)` 的问题。错误现在可以只在 Go 中注册一次,`.api` 无需重复定义错误目录或逐条列出错误。
|
||||
|
||||
## 行为
|
||||
|
||||
- 未声明路由错误列表:继承 App 目录,包括带参数提示及项目自定义 INTERNAL_ERROR 兜底。
|
||||
- 显式引用路由错误:继续使用路由独立目录,不与 App 合并。
|
||||
- `@errors none` 清空 `.api` 分组错误列表,随后继承 App。
|
||||
- 错误继承不覆盖 `.api` 的返回格式、RawJSON、手写绑定或请求体限额;全局中间件提前返回、标准桥接与克隆保持一致。
|
||||
- 空 `response.WithErrors` / `MustWithErrors` 返回原 Encoder,既不覆盖 Context 的目录,也不在编码阶段按空目录重新解析错误。
|
||||
|
||||
## 升级
|
||||
|
||||
```sh
|
||||
go get git.yuntuo.hk/k2safe/go-omni@v0.11.1-preview
|
||||
go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.11.1-preview
|
||||
go-omni api generate --dry-run
|
||||
go-omni api generate
|
||||
go test ./...
|
||||
```
|
||||
|
||||
运行时修复也适用于旧 v0.11.0 生成代码,因此升级运行时后即可解除空目录遮蔽。同步升级 CLI 重新生成时不再创建空目录包装;已生成业务文件继续受保护。代码生成最低运行时仍为 v0.11.0-preview。
|
||||
|
||||
接入见 [README](README.md)、[错误目录继承](errors/README.md) 和 [只在 Go 注册错误的契约示例](examples/contracts/errorInheritance.api)。静态 OpenAPI 不执行 Go 配置,保留契约响应结构,不自动列举运行时目录内容。
|
||||
|
||||
## 验收
|
||||
|
||||
复现用例在修复前:普通路由通过,生成/RawJSON 路由和提前返回中间件共 12 项失败,均变成 500/英文系统提示。修复后同一批用例通过。
|
||||
|
||||
```sh
|
||||
GO_OMNI_SCAFFOLD_INTEGRATION=1 go test -race -p=1 -count=1 -timeout=300s ./...
|
||||
go vet ./...
|
||||
go build ./...
|
||||
python3 scripts/verifyErrorInheritanceRelease.py --version v0.11.1-preview
|
||||
python3 scripts/verifyErrorParamsRelease.py --version v0.11.1-preview --report performance/errorInheritanceParamsRemote.json
|
||||
python3 scripts/verifyCompatibilityRelease.py --version v0.11.1-preview --report performance/errorInheritanceCompatibilityRemote.json
|
||||
```
|
||||
|
||||
结果见 [目录继承验收](performance/errorInheritanceAcceptance.md)。本轮未修改 eTax/ePay 业务代码,没有使用业务服务器。
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
# GoOmni 成熟度计划
|
||||
|
||||
当前进展:`v0.14.0-preview` 已完成契约驱动的业务开发能力、通用 Runtime/配置和官方 MySQL 适配器,并通过全量 race、实际生成服务及空缓存远程验收。新项目/升级以 [README](README.md) 和 [开发指南](DEVELOPMENT.md) 为准;下方为历史阶段记录,完整本轮验收见 [报告](reports/businessDevelopmentAcceptance.md)。具体 eTax/ePay 业务迁移与业务回归仍由接入项目完成。
|
||||
|
||||
## 1. 新项目启动与发布流程(已完成,v0.1.1)
|
||||
|
||||
- 修正脚手架固定版本引用,保留显式本地开发选项。
|
||||
|
||||
14
app/app.go
14
app/app.go
@@ -14,6 +14,7 @@ import (
|
||||
"git.yuntuo.hk/k2safe/go-omni/config"
|
||||
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
||||
"git.yuntuo.hk/k2safe/go-omni/httpx"
|
||||
"git.yuntuo.hk/k2safe/go-omni/lifecycle"
|
||||
"git.yuntuo.hk/k2safe/go-omni/middleware"
|
||||
"git.yuntuo.hk/k2safe/go-omni/response"
|
||||
netrouter "git.yuntuo.hk/k2safe/go-omni/transport/nethttp"
|
||||
@@ -183,7 +184,8 @@ func (a *App) NewServer(address string) *http.Server {
|
||||
}
|
||||
|
||||
// RunContext serves until the context is canceled, then performs a graceful shutdown.
|
||||
func (a *App) RunContext(ctx context.Context, address ...string) error {
|
||||
func (a *App) RunContext(ctx context.Context, address ...string) (runErr error) {
|
||||
defer func() { runErr = shutdownResult(ctx, runErr) }()
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
@@ -205,7 +207,8 @@ func (a *App) RunContext(ctx context.Context, address ...string) error {
|
||||
// RunWith starts a custom server runner while keeping App-owned cleanup in the
|
||||
// same lifecycle. The runner receives the transport-neutral App handler and
|
||||
// owns transport/TLS settings; built-in transport selection is bypassed.
|
||||
func (a *App) RunWith(ctx context.Context, runner Runner, address ...string) error {
|
||||
func (a *App) RunWith(ctx context.Context, runner Runner, address ...string) (runErr error) {
|
||||
defer func() { runErr = shutdownResult(ctx, runErr) }()
|
||||
if runner == nil {
|
||||
return errors.Join(errors.New("go-omni: nil runner"), a.Close())
|
||||
}
|
||||
@@ -218,6 +221,13 @@ func (a *App) RunWith(ctx context.Context, runner Runner, address ...string) err
|
||||
return errors.Join(runner.Run(ctx, a, a.listenAddress(address...)), a.Close())
|
||||
}
|
||||
|
||||
func shutdownResult(ctx context.Context, err error) error {
|
||||
if cause := lifecycle.Cause(ctx); cause != nil && !errors.Is(err, cause) {
|
||||
return errors.Join(err, cause)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (a *App) listenAddress(address ...string) string {
|
||||
listenOn := a.config.Address
|
||||
if listenOn == "" {
|
||||
|
||||
126
app/errorInheritance_test.go
Normal file
126
app/errorInheritance_test.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package app_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
omni "git.yuntuo.hk/k2safe/go-omni"
|
||||
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
||||
"git.yuntuo.hk/k2safe/go-omni/httpx"
|
||||
"git.yuntuo.hk/k2safe/go-omni/response"
|
||||
)
|
||||
|
||||
func TestRouteInheritsAppErrorCatalog(t *testing.T) {
|
||||
catalog := apierrors.Catalog{DefaultStatus: 400, Definitions: map[string]apierrors.Definition{
|
||||
"USERNAME_INVALID": {Code: 400002, Message: "用户名格式不正确"},
|
||||
"TEXT_TOO_LONG": {Status: 200, Code: 400003, Message: "{label}不能超过{max}个字符", Params: []apierrors.Parameter{{Name: "label", Type: "string"}, {Name: "max", Type: "int"}}},
|
||||
"INTERNAL_ERROR": {Status: 500, Code: 9999, Message: "系统错误"},
|
||||
}}
|
||||
format := response.Config{CodeType: "int32", SuccessCode: 0, ErrorCode: 9999, MessageField: "msg", DataMode: "always"}
|
||||
app := omni.New(omni.WithErrors(catalog), omni.WithResponse(format))
|
||||
defer app.Close()
|
||||
getError := func(c *httpx.Context) error {
|
||||
key := c.Header("X-Error")
|
||||
if key == "TEXT_TOO_LONG" {
|
||||
return apierrors.ByCode(key).WithParams(map[string]any{"label": "企业名称", "max": 100})
|
||||
}
|
||||
return apierrors.ByCode(key)
|
||||
}
|
||||
app.Use(func(next httpx.Handler) httpx.Handler {
|
||||
return func(c *httpx.Context) error {
|
||||
if c.Header("X-Early") == "true" {
|
||||
return getError(c)
|
||||
}
|
||||
return next(c)
|
||||
}
|
||||
})
|
||||
bridge := httpx.WrapMiddleware(func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { next.ServeHTTP(w, r) })
|
||||
})
|
||||
app.GET("/plain", getError)
|
||||
// This is exactly the wrapper emitted by v0.11.0, not just the new generator.
|
||||
legacy := response.MustWithErrors(apierrors.Catalog{}, response.MustNew(format))
|
||||
app.HandleWithResponse("GET", "/generated", legacy, getError, bridge)
|
||||
app.HandleWithResponse("GET", "/raw", response.RawJSON(legacy), getError, bridge)
|
||||
for _, path := range []string{"/plain", "/generated", "/raw"} {
|
||||
for _, early := range []string{"false", "true"} {
|
||||
for _, test := range []struct {
|
||||
key string
|
||||
status, code int
|
||||
message string
|
||||
}{
|
||||
{"USERNAME_INVALID", 400, 400002, "用户名格式不正确"},
|
||||
{"TEXT_TOO_LONG", 200, 400003, "企业名称不能超过100个字符"},
|
||||
{"MISSING", 500, 9999, "系统错误"},
|
||||
} {
|
||||
t.Run(path+"/"+early+"/"+test.key, func(t *testing.T) {
|
||||
r := httptest.NewRequest("GET", path, nil)
|
||||
r.Header.Set("X-Early", early)
|
||||
r.Header.Set("X-Error", test.key)
|
||||
w := httptest.NewRecorder()
|
||||
app.ServeHTTP(w, r)
|
||||
var value map[string]any
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &value); err != nil {
|
||||
t.Fatal(err, w.Body.String())
|
||||
}
|
||||
if w.Code != test.status || value["code"] != float64(test.code) || value["msg"] != test.message {
|
||||
t.Fatal(w.Code, value)
|
||||
}
|
||||
if data, ok := value["data"]; !ok || data != nil {
|
||||
t.Fatal("data:null lost", value)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExplicitRouteErrorCatalogRemainsIndependent(t *testing.T) {
|
||||
format := response.Config{CodeType: "int32", SuccessCode: 0, ErrorCode: 9999, MessageField: "msg", DataMode: "always"}
|
||||
global := apierrors.Catalog{DefaultStatus: 400, Definitions: map[string]apierrors.Definition{
|
||||
"INVALID": {Code: 400002, Message: "全局提示"}, "APP_ONLY": {Code: 400003, Message: "全局独有"},
|
||||
"INTERNAL_ERROR": {Status: 500, Code: 9999, Message: "全局系统错误"},
|
||||
}}
|
||||
local := apierrors.Catalog{DefaultStatus: 409, Definitions: map[string]apierrors.Definition{
|
||||
"INVALID": {Code: 409001, Message: "路由提示"}, "INTERNAL_ERROR": {Status: 500, Code: 9998, Message: "路由系统错误"},
|
||||
}}
|
||||
app := omni.New(omni.WithErrors(global), omni.WithResponse(format))
|
||||
defer app.Close()
|
||||
getError := func(c *httpx.Context) error { return apierrors.ByCode(c.Header("X-Error")) }
|
||||
app.Use(func(next httpx.Handler) httpx.Handler {
|
||||
return func(c *httpx.Context) error {
|
||||
if c.Header("X-Early") == "true" {
|
||||
return getError(c)
|
||||
}
|
||||
return next(c)
|
||||
}
|
||||
})
|
||||
routeFormat := format
|
||||
routeFormat.MessageField = "detail"
|
||||
localEncoder := response.MustWithErrors(local, response.MustNew(routeFormat))
|
||||
app.HandleWithResponse("GET", "/", response.RawJSON(localEncoder), getError)
|
||||
for _, early := range []string{"false", "true"} {
|
||||
for _, test := range []struct {
|
||||
key string
|
||||
status, code int
|
||||
message string
|
||||
}{
|
||||
{"INVALID", 409, 409001, "路由提示"}, {"APP_ONLY", 500, 9998, "路由系统错误"},
|
||||
} {
|
||||
r := httptest.NewRequest("GET", "/", nil)
|
||||
r.Header.Set("X-Early", early)
|
||||
r.Header.Set("X-Error", test.key)
|
||||
w := httptest.NewRecorder()
|
||||
app.ServeHTTP(w, r)
|
||||
var value map[string]any
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if w.Code != test.status || value["code"] != float64(test.code) || value["detail"] != test.message || value["msg"] != nil {
|
||||
t.Fatal(w.Code, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
186
app/errorParams_test.go
Normal file
186
app/errorParams_test.go
Normal file
@@ -0,0 +1,186 @@
|
||||
package app_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
omni "git.yuntuo.hk/k2safe/go-omni"
|
||||
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
||||
"git.yuntuo.hk/k2safe/go-omni/httpx"
|
||||
"git.yuntuo.hk/k2safe/go-omni/logx"
|
||||
"git.yuntuo.hk/k2safe/go-omni/metrics"
|
||||
"git.yuntuo.hk/k2safe/go-omni/middleware"
|
||||
"git.yuntuo.hk/k2safe/go-omni/response"
|
||||
)
|
||||
|
||||
func TestTemplateErrorsAcrossPoliciesAndReplay(t *testing.T) {
|
||||
var logs bytes.Buffer
|
||||
lc := logx.Default()
|
||||
lc.Writer = &logs
|
||||
logger, err := logx.New(lc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
logx.SetDefault(logger)
|
||||
defer logx.SetDefault(nil)
|
||||
defer logger.Close()
|
||||
cat := apierrors.Catalog{DefaultStatus: 200, Definitions: map[string]apierrors.Definition{"TOO_LONG": {
|
||||
Code: 400002, Message: "{label}不能超过{max}个字符", Params: []apierrors.Parameter{{Name: "label", Type: "string"}, {Name: "max", Type: "int"}},
|
||||
}}}
|
||||
registry := apierrors.MustRegistry(cat)
|
||||
encoder := response.MustWithErrors(cat, response.MustNew(response.Config{CodeType: "int32", SuccessCode: 0, ErrorCode: 9999, MessageField: "msg", DataMode: "always"}))
|
||||
app := omni.New(omni.WithLogger(true))
|
||||
defer app.Close()
|
||||
meter := metrics.New()
|
||||
app.Use(meter.Middleware())
|
||||
// The clone uses the route policy even though the App has a different default.
|
||||
app.Use(func(next httpx.Handler) httpx.Handler {
|
||||
return func(c *httpx.Context) error {
|
||||
err := next(c)
|
||||
if f := c.Failure(); f != nil && strings.Contains(f.Message, "{label}") {
|
||||
t.Error("unrendered failure", f)
|
||||
}
|
||||
return err
|
||||
}
|
||||
})
|
||||
bridge := httpx.WrapMiddleware(func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { next.ServeHTTP(w, r) })
|
||||
})
|
||||
calls := 0
|
||||
handler := func(c *httpx.Context) error {
|
||||
calls++
|
||||
values := map[string]any{"label": "企业名称", "max": 100}
|
||||
if c.Header("X-Label") == "remark" {
|
||||
values = map[string]any{"label": "备注", "max": 500}
|
||||
}
|
||||
switch c.Header("X-Failure") {
|
||||
case "missing":
|
||||
delete(values, "max")
|
||||
case "type":
|
||||
values["max"] = "private-value"
|
||||
}
|
||||
e := apierrors.ByCode("TOO_LONG").WithParams(values).WithCause(errors.New("private cause"))
|
||||
if c.Header("X-Resolved") == "true" {
|
||||
return registry.Resolve(e)
|
||||
}
|
||||
return e
|
||||
}
|
||||
app.HandleWithResponse("POST", "/direct", encoder, handler, bridge)
|
||||
for _, shared := range []bool{false, true} {
|
||||
cfg := middleware.IdempotencyConfig{Scope: func(*httpx.Context) (string, error) { return "fixture", nil }}
|
||||
path := "/local"
|
||||
if shared {
|
||||
path = "/shared"
|
||||
cfg.Store = &catalogStore{records: map[string]middleware.IdempotencyRecord{}}
|
||||
}
|
||||
idem, err := middleware.NewIdempotency(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
app.HandleWithResponse("POST", path, encoder, handler, idem.Middleware(), bridge)
|
||||
}
|
||||
check := func(path, label, key, kind string, status int, want string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
w := httptest.NewRecorder()
|
||||
r := httptest.NewRequest("POST", path, nil)
|
||||
r.Header.Set("Idempotency-Key", key)
|
||||
r.Header.Set("X-Label", label)
|
||||
r.Header.Set("X-Failure", kind)
|
||||
r.Header.Set("X-Resolved", "true")
|
||||
app.ServeHTTP(w, r)
|
||||
if w.Code != status || !strings.Contains(w.Body.String(), want) || strings.Contains(w.Body.String(), "private") || strings.Contains(w.Body.String(), "{label}") {
|
||||
t.Fatal(path, w.Code, w.Body.String())
|
||||
}
|
||||
if status == 200 && (!strings.Contains(w.Body.String(), `"data":null`) || !strings.Contains(w.Body.String(), `"code":400002`)) {
|
||||
t.Fatal(w.Body.String())
|
||||
}
|
||||
return w
|
||||
}
|
||||
for _, path := range []string{"/direct", "/local", "/shared"} {
|
||||
for _, label := range []string{"company", "remark"} {
|
||||
want := "企业名称不能超过100个字符"
|
||||
if label == "remark" {
|
||||
want = "备注不能超过500个字符"
|
||||
}
|
||||
first := check(path, label, label, "", 200, want)
|
||||
if path != "/direct" {
|
||||
before := calls
|
||||
second := check(path, label, label, "", 200, want)
|
||||
if calls != before || first.Body.String() != second.Body.String() || second.Header().Get("Idempotency-Replayed") != "true" {
|
||||
t.Fatal("bad replay")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
check("/direct", "company", "", "missing", 500, "internal server error")
|
||||
check("/direct", "company", "", "type", 500, "internal server error")
|
||||
stats := meter.Snapshot()
|
||||
if stats.Errors != 12 || stats.StatusCounts["200"] != 10 || stats.StatusCounts["500"] != 2 {
|
||||
t.Fatal(stats)
|
||||
}
|
||||
seen, diagnostics := 0, 0
|
||||
for _, line := range strings.Split(strings.TrimSpace(logs.String()), "\n") {
|
||||
var entry map[string]any
|
||||
if err := json.Unmarshal([]byte(line), &entry); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if entry["msg"] != "http.request" {
|
||||
continue
|
||||
}
|
||||
seen++
|
||||
if entry["failed"] != true {
|
||||
t.Fatal(entry)
|
||||
}
|
||||
if entry["status"] == float64(200) && (entry["errorKey"] != "TOO_LONG" || entry["businessCode"] != float64(400002)) {
|
||||
t.Fatal(entry)
|
||||
}
|
||||
if entry["errorParameter"] != nil {
|
||||
diagnostics++
|
||||
if strings.Contains(entry["errorParameter"].(string), "private-value") {
|
||||
t.Fatal(entry)
|
||||
}
|
||||
}
|
||||
}
|
||||
if seen != 12 || diagnostics != 2 {
|
||||
t.Fatal(seen, diagnostics)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolvedFailureReplaySurvivesCatalogChanges(t *testing.T) {
|
||||
store := &catalogStore{records: map[string]middleware.IdempotencyRecord{}}
|
||||
var observed []*apierrors.APIError
|
||||
makeApp := func(message string) *omni.App {
|
||||
cat := apierrors.Catalog{DefaultStatus: 200, Definitions: map[string]apierrors.Definition{"T": {Code: "code", Message: message, Params: []apierrors.Parameter{{Name: "label", Type: "string"}}}}}
|
||||
app := omni.New(omni.WithErrors(cat))
|
||||
app.Use(func(next httpx.Handler) httpx.Handler {
|
||||
return func(c *httpx.Context) error { err := next(c); observed = append(observed, c.Failure()); return err }
|
||||
})
|
||||
idem, err := middleware.NewIdempotency(middleware.IdempotencyConfig{Store: store, Scope: func(*httpx.Context) (string, error) { return "fixture", nil }})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
app.POST("/", func(c *httpx.Context) error {
|
||||
return apierrors.ByCode("T").WithParams(map[string]any{"label": "企业"})
|
||||
}, idem.Middleware())
|
||||
return app
|
||||
}
|
||||
for _, message := range []string{"{label}原文案", "{label}新文案"} {
|
||||
app := makeApp(message)
|
||||
w := httptest.NewRecorder()
|
||||
r := httptest.NewRequest("POST", "/", nil)
|
||||
r.Header.Set("Idempotency-Key", "same")
|
||||
app.ServeHTTP(w, r)
|
||||
app.Close()
|
||||
if !strings.Contains(w.Body.String(), "企业原文案") {
|
||||
t.Fatal(w.Body.String())
|
||||
}
|
||||
}
|
||||
if len(observed) != 2 || observed[0].Message != "企业原文案" || observed[1].Message != "企业原文案" || observed[1].Code != "T" {
|
||||
t.Fatal(observed)
|
||||
}
|
||||
}
|
||||
119
app/fatalShutdown_test.go
Normal file
119
app/fatalShutdown_test.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.yuntuo.hk/k2safe/go-omni/config"
|
||||
"git.yuntuo.hk/k2safe/go-omni/httpx"
|
||||
"git.yuntuo.hk/k2safe/go-omni/lifecycle"
|
||||
)
|
||||
|
||||
func TestFatalShutdownDrainsAndPreservesCause(t *testing.T) {
|
||||
for _, transport := range []string{config.TransportNetHTTP, config.TransportNative} {
|
||||
t.Run(transport, func(t *testing.T) {
|
||||
app := New(config.WithTransport(transport), config.WithShutdownTimeout(2*time.Second))
|
||||
control := lifecycle.New(context.Background())
|
||||
defer control.RequestShutdown(nil)
|
||||
entered, release := make(chan struct{}), make(chan struct{})
|
||||
var releaseOnce sync.Once
|
||||
unblock := func() { releaseOnce.Do(func() { close(release) }) }
|
||||
defer unblock()
|
||||
var closed atomic.Int32
|
||||
closeErr := errors.New("cleanup diagnostic")
|
||||
app.OnShutdown(func() error { closed.Add(1); return closeErr })
|
||||
app.GET("/work", func(c *httpx.Context) error {
|
||||
close(entered)
|
||||
<-release
|
||||
if closed.Load() != 0 {
|
||||
return errors.New("closed during request")
|
||||
}
|
||||
return c.OK("done")
|
||||
})
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- app.ServeContext(control.Context(), listener) }()
|
||||
requestDone := make(chan error, 1)
|
||||
client := http.Client{Timeout: 3 * time.Second}
|
||||
go func() {
|
||||
res, err := client.Get("http://" + listener.Addr().String() + "/work")
|
||||
if err == nil {
|
||||
_, err = io.Copy(io.Discard, res.Body)
|
||||
_ = res.Body.Close()
|
||||
if res.StatusCode != 200 {
|
||||
err = errors.New("request failed")
|
||||
}
|
||||
}
|
||||
requestDone <- err
|
||||
}()
|
||||
select {
|
||||
case <-entered:
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("request never started")
|
||||
}
|
||||
fatal := errors.New("lease lost")
|
||||
control.RequestShutdown(fatal)
|
||||
select {
|
||||
case err := <-done:
|
||||
t.Fatal("did not drain", err)
|
||||
case <-time.After(30 * time.Millisecond):
|
||||
}
|
||||
if closed.Load() != 0 {
|
||||
t.Fatal("closed dependency before drain")
|
||||
}
|
||||
unblock()
|
||||
select {
|
||||
case err := <-requestDone:
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("request stuck")
|
||||
}
|
||||
select {
|
||||
case err := <-done:
|
||||
if !errors.Is(err, fatal) || !errors.Is(err, closeErr) {
|
||||
t.Fatal("lost cause", err)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("shutdown stuck")
|
||||
}
|
||||
_ = app.Close()
|
||||
if closed.Load() != 1 {
|
||||
t.Fatal("cleanup repeated")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCanceledStartupPreservesFault(t *testing.T) {
|
||||
fatal := errors.New("dependency failed during startup")
|
||||
ctx, cancel := context.WithCancelCause(context.Background())
|
||||
cancel(fatal)
|
||||
app := New()
|
||||
if err := app.RunContext(ctx); !errors.Is(err, fatal) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = New().ServeContext(ctx, listener); !errors.Is(err, fatal) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
normal, stop := context.WithCancel(context.Background())
|
||||
stop()
|
||||
if err = New().RunContext(normal); err != nil {
|
||||
t.Fatal("normal stop failed", err)
|
||||
}
|
||||
}
|
||||
58
app/logIsolation_test.go
Normal file
58
app/logIsolation_test.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package app_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
omni "git.yuntuo.hk/k2safe/go-omni"
|
||||
"git.yuntuo.hk/k2safe/go-omni/httpx"
|
||||
"git.yuntuo.hk/k2safe/go-omni/logx"
|
||||
)
|
||||
|
||||
func TestAppLogAndRequestMetadataIsolation(t *testing.T) {
|
||||
var a, b bytes.Buffer
|
||||
la, _ := logx.New(logx.Config{Writer: &a})
|
||||
lb, _ := logx.New(logx.Config{Writer: &b})
|
||||
apps := []*omni.App{
|
||||
omni.New(omni.WithLogger(true), omni.WithLogInstance(la), omni.WithTrustedProxies("127.0.0.1/32")),
|
||||
omni.New(omni.WithLogger(true), omni.WithLogInstance(lb)),
|
||||
}
|
||||
for i, app := range apps {
|
||||
app.GET("/probe/:id", func(c *httpx.Context) error {
|
||||
ctx := c.Context()
|
||||
meta := httpx.Metadata(ctx)
|
||||
expected := "127.0.0.1"
|
||||
if i == 0 {
|
||||
expected = "203.0.113.7"
|
||||
}
|
||||
if meta.ClientIP != expected || meta.Route != "/probe/:id" || meta.Method != "GET" {
|
||||
t.Error(meta)
|
||||
}
|
||||
logx.InfoContext(ctx, "business", "app", i)
|
||||
clone := c.Clone(context.WithValue(ctx, struct{}{}, true), httptest.NewRecorder())
|
||||
if httpx.Metadata(clone.Context()) != meta {
|
||||
t.Error("metadata lost")
|
||||
}
|
||||
return c.OK(nil)
|
||||
})
|
||||
}
|
||||
for _, app := range apps {
|
||||
r := httptest.NewRequest("GET", "/probe/42", nil)
|
||||
r.RemoteAddr = "127.0.0.1:1234"
|
||||
r.Header.Set("X-Forwarded-For", "203.0.113.7")
|
||||
app.ServeHTTP(httptest.NewRecorder(), r)
|
||||
}
|
||||
for i, out := range []string{a.String(), b.String()} {
|
||||
if !strings.Contains(out, "http.request") || !strings.Contains(out, "business") || strings.Contains(out, `"app":`+string(rune('0'+(1-i)))) {
|
||||
t.Fatal(out)
|
||||
}
|
||||
}
|
||||
_ = apps[0].Close()
|
||||
lb.Info("still open")
|
||||
if !strings.Contains(b.String(), "still open") {
|
||||
t.Fatal("borrowed logger closed")
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,9 @@ package app
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.yuntuo.hk/k2safe/go-omni/contract"
|
||||
"git.yuntuo.hk/k2safe/go-omni/httpx"
|
||||
"git.yuntuo.hk/k2safe/go-omni/logx"
|
||||
"git.yuntuo.hk/k2safe/go-omni/response"
|
||||
)
|
||||
|
||||
@@ -26,10 +28,14 @@ func (a *App) adaptResponse(encoder response.Encoder, handler httpx.Handler, mid
|
||||
}
|
||||
}
|
||||
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
request = request.WithContext(logx.WithLogger(contract.WithRequestPolicy(request.Context(), a.config.Request), a.config.LogInstance))
|
||||
if len(a.config.TrustedProxies) > 0 {
|
||||
request = request.WithContext(httpx.WithClientIPResolver(request.Context(), a.clientIPResolver))
|
||||
}
|
||||
requestContext := httpx.NewContext(request, writer, httpx.RouteParams(request.Context()))
|
||||
requestContext.WithContext(httpx.WithRequestMetadata(request.Context(), httpx.RequestMetadata{
|
||||
ClientIP: requestContext.ClientIP(), Method: request.Method, Route: httpx.RoutePattern(request.Context()),
|
||||
}))
|
||||
if provider, ok := a.responseEncoder.(response.ErrorRegistryProvider); ok {
|
||||
requestContext.SetErrorRegistry(provider.ErrorRegistry())
|
||||
}
|
||||
|
||||
@@ -17,7 +17,8 @@ import (
|
||||
// ServeContext serves a caller-created listener using the configured transport
|
||||
// and TLS. App takes ownership of the listener, including on startup failure,
|
||||
// and runs resource cleanup after the server has stopped.
|
||||
func (a *App) ServeContext(ctx context.Context, listener net.Listener) error {
|
||||
func (a *App) ServeContext(ctx context.Context, listener net.Listener) (runErr error) {
|
||||
defer func() { runErr = shutdownResult(ctx, runErr) }()
|
||||
if listener == nil {
|
||||
return errors.Join(errors.New("go-omni: nil listener"), a.Close())
|
||||
}
|
||||
|
||||
@@ -1,158 +1,71 @@
|
||||
# go-omni CLI
|
||||
|
||||
首次接入见 [README](../../README.md),无需克隆框架源码。本页说明命令参数;下方 `go run ./cmd/go-omni` 示例在框架仓库中执行,并显式引用本地源码。
|
||||
v0.18 新增:可空嵌套映射、`mappingFiles` 配置拆分、显式 nil 策略和可选 `pointerResult`。原 v0.17 配置继续有效,见 [生成能力指南](../../GENERATION.md)。
|
||||
|
||||
## API 契约驱动项目
|
||||
v0.17 增加公共字段组合、可选结构映射、自动中间件工厂和按需 Repository。配置和迁移见 [生成能力指南](../../GENERATION.md)。
|
||||
|
||||
v0.16 增加基础类型/动态 JSON 数组返回、OpenAPI 公共引用和 workspace Go Catalog 统一导出。见 [接入与筛选规则](../../DEVELOPMENT.md#同一份-go-错误目录供多个服务生成)。
|
||||
|
||||
v0.14 新增 `api format [--check]`、`api upgrade [--dry-run|--preview <空目录>|--apply]`,以及 `workspace format|generate|check --file omni.workspace.json`。详见 [通用开发指南](../../DEVELOPMENT.md)。
|
||||
|
||||
新项目采用根目录 `main.go` 与 `internal/config、svc、logic、handler、types`。完整职责、启动扩展、流式接口和生成文件边界见 [项目指南](../../PROJECT.md)。
|
||||
|
||||
## 创建项目
|
||||
|
||||
```sh
|
||||
# 创建包含可运行问候接口的新项目
|
||||
go run ./cmd/go-omni api new --module example.com/demo --framework-dir . ../demo
|
||||
# 仓库读取权限与 PATH 配置见根目录 README
|
||||
go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.18.0-preview
|
||||
go-omni new --module example.com/hello ./hello
|
||||
|
||||
# 从已有定义生成对应目录、类型、路由和业务骨架
|
||||
go run ./cmd/go-omni api generate --api examples/contracts/user.api \
|
||||
--dir ../userService --module example.com/userService --framework-dir .
|
||||
|
||||
# 校验定义;也接受 goctl 风格的 api go -api ... -dir ...
|
||||
go run ./cmd/go-omni api validate --api examples/contracts/user.api
|
||||
# 从自己的契约生成;service.api 及其相对 import 文件需要在本地存在
|
||||
go-omni new --api service.api --module example.com/example ./example
|
||||
```
|
||||
|
||||
`new --api <file>` 是契约项目的另一个入口。可以组合 `--database mysql --storage local --log-output file`,生成配置、依赖初始化与资源关闭。API 默认无数据库、无对象存储;`--database memory` 等同于不打开连接。`--force` 不允许覆盖 API 项目。初始配置选择写入 manifest,重新生成自动沿用。
|
||||
`new` 与 `api new` 统一生成契约项目;不再提供另一套 CRUD 启动目录。未实现的 Logic 返回 501;中间件工厂未实现时阻止启动,既有 handler 模式仍返回 501。进入项目执行 `go mod tidy`、`go run .`、`go build .`。
|
||||
|
||||
后续在生成项目内执行 `go-omni api generate --api api/user.api --dir .`。
|
||||
项目内可省略 `--api` / `--dir`。`api inspect` 展示路由和鉴权/业务错误,`api export` 向 stdout 导出 OpenAPI,`api generate --dry-run` 预览文件变化。`--check` 只读检查生成产物是否过期。生成器记录所有权,保留 Service / Repository / 配置 / main 的手写内容,遇到生成文件被手动修改则报冲突。新 Service 与中间件占位实现返回 501。支持 `error (...)`、具名 `auth`、路由 `@errors` / `@auth` 和多行 `@doc`;错误目录同步生成 `internal/apierrors` 调用函数。
|
||||
参数:`--database none|memory|mysql`(默认 none;memory 等同 none),`--storage none|local|s3|r2|oss`,`--log-output stdout|stderr|file`,`--log-format json|text`。基础设施按需生成,连接凭据由环境提供,不自动创建数据库、表或云资源。选择 MySQL 不会凭空生成业务表或 Repository 查询。
|
||||
|
||||
`v0.10.0-preview` 新增 `errorCatalog`:集中配置业务码、提示和 HTTP 状态,支持默认 HTTP 200 与单条覆盖,生成函数、路由策略和 OpenAPI 共用一份定义。语法及配置方式见 [统一错误目录](../../errors/README.md)。
|
||||
存储配置和客户端位于 config/svc,业务使用统一接口。local 存储签名端点由生成器接入;上传、下载等业务路由由 `.api` 定义。认证与存储路径由项目实现。
|
||||
|
||||
支持范围、参数语义、目录及安全更新策略见 [API 开发指南](../../API.md)。本版借鉴 go-zero 的工作流,不宣称兼容全部 goctl 语法;仍通过 Omni/httpx 保留网络层替换能力。
|
||||
|
||||
## 日志与对象存储按需装配
|
||||
|
||||
新增 `--log-output stdout|stderr|file`、`--log-format json|text`、`--storage none|local|s3|r2|oss`。
|
||||
指定任意一个新选项后,生成严格的配置文件、初始化代码和项目自身的测试;不指定则保留原有轻量骨架。
|
||||
对象存储独立于 `--database memory|mysql`。只有选择文件存储才会生成文件 Handler / Service 和对应适配器。
|
||||
|
||||
开发阶段可以使用本地源码;固定远程版本的安装和验收见 [发布说明](../../RELEASE.md):
|
||||
## 日常生成
|
||||
|
||||
```sh
|
||||
# 在 go-omni 仓库中运行;目标目录须为空。
|
||||
go run ./cmd/go-omni new --framework-dir . \
|
||||
--module example.com/fileService --log-output file --log-format json \
|
||||
--storage local ../fileService
|
||||
go-omni api validate
|
||||
go-omni api inspect
|
||||
go-omni api generate --dry-run
|
||||
go-omni api generate
|
||||
go-omni api generate --check
|
||||
go-omni api export > openapi.json
|
||||
go test ./...
|
||||
```
|
||||
|
||||
生成项目包含 `etc/config.yaml`、`internal/settings`、`internal/servicecontext/storage.go`、
|
||||
文件 Service/Handler、测试和 `MODULES.md`。按生成的 MODULES.md 配置示例 Token 和本地签名密钥后,
|
||||
`go mod tidy && go run ./cmd/server` 即可体验。云存储分别选择 `--storage s3`、`r2`、`oss`,
|
||||
再设置已有桶/凭据等环境变量;不自动创建桶或修改云资源。
|
||||
项目内自动发现 manifest 中的 API 入口,也可显式 `--api ... --dir ...`。`api go -api ... -dir ...` 为命令别名,不代表兼容全部 goctl 语法。
|
||||
|
||||
配置优先级为默认值 → YAML/JSON → 环境变量;密钥仅允许环境变量。Service 依赖统一存储接口,
|
||||
组合处负责创建客户端与有界连接池,退出时关闭自己创建的资源。文件接口提供服务端上传、直传签名、
|
||||
临时下载链接及统一错误处理;日志示例携带 requestId,文件输出默认带轮转。没有固定业务身份上下文。
|
||||
main、Gen.go、OpenAPI 和 manifest 由生成器维护;业务文件按字节保留。手改生成文件会报冲突,`--check` 检查入口与所有生成产物。没有 `--force` 覆盖业务的入口。初始基础设施 profile 在重新生成时沿用,后续项目依赖由用户修改 config/svc。
|
||||
|
||||
新模板要求 `v0.7.0-preview` 或更新版本;明确指定旧版本并启用新模块会被拒绝,
|
||||
避免模板引用旧框架不存在的接口。本地 `--framework-dir` 验证不受此限制。`--force` 可重新生成兼容配置,
|
||||
但如果切换配置会遗留引用已移除依赖的旧文件,则拒绝操作,要求使用新目录;不会自动删除修改过的代码。
|
||||
生成/预览会比较上次契约快照,提示受影响的 Logic 文件、DTO 字段和 body/流式模式变化。明确的 Logic 签名错误显示文件行号、原签名和期望签名;生成后未调整业务时,`--check` 仍返回失败。REVIEW 不强制确认,类型别名/build tags 等不确定情况交给 `go test ./...` 验证。完整行为见 [契约变更提示](../../PROJECT.md#契约变更提示)。
|
||||
|
||||
验证当前生成器:
|
||||
`@app(defaultResponse: TemplateName)` 选择 App 默认响应,路由/分组可以覆盖。错误表可以只在 Go 中维护,通过 svc.AppOptions 注册一次。上传和原始 body 的自动 Logic 参数、下载输出见 [项目指南](../../PROJECT.md)。
|
||||
|
||||
## 版本与迁移
|
||||
|
||||
CLI 与框架依赖固定使用 v0.18.0-preview。远程安装的 CLI 默认把自身版本写入 go.mod,不添加本地 replace;`--framework-version` 可显式指定已发布的兼容版本。仅在开发框架源码时使用本地替换:
|
||||
|
||||
```sh
|
||||
GO_OMNI_SCAFFOLD_INTEGRATION=1 go test -race ./cmd/go-omni/...
|
||||
python3 scripts/verifyScaffoldModules.py
|
||||
# 在 go-omni 源码仓库中执行
|
||||
go run ./cmd/go-omni new --module example.com/hello --framework-dir . ../hello
|
||||
```
|
||||
|
||||
前者覆盖两种数据库与五种文件存储选择、日志方式/格式,以及生成项目的配置、资源关闭和接口测试。
|
||||
后者实际编译启动服务,验证日志、上传/下载/直传链接、错误状态、nethttp/native 下的 SIGINT/SIGTERM 和监听端口释放。
|
||||
云端选项的测试使用本地替身,不代表 OSS/R2 实网验收;本批不访问业务服务器。报告见
|
||||
[脚手架模块验收](../../performance/scaffoldModulesLocal.json)。
|
||||
当前模板与依赖以 v0.18.0-preview 为基线,继续使用 manifest v2;GORM 可选适配器从 v0.14.1-preview 提供。已有项目使用 [定点升级与预览](../../DEVELOPMENT.md#v013-项目升级):原始模板可自动替换,含业务修改的基础文件按预览合并;通用清理由 Runtime 负责。独立工具命令不会被删除。受管理项目通过 `.api` 模块导入增加业务,不使用旧 `add` 命令的手动路由装配。
|
||||
|
||||
## 其他命令与远程安装
|
||||
## 数据库迁移命令
|
||||
|
||||
main 分支还提供 `go-omni add orderItems --dir /path/to/project`,向已有 GoOmni 项目增加
|
||||
Handler / Service / Repository / Model 与模块注册入口。模块名为 lowerCamelCase,
|
||||
目标目录存在时拒绝覆盖,已有启动代码保持原样。生成模块带可替换的内存 CRUD Repository,
|
||||
导入后使用 `orderItems.Register(application, repository, authMiddleware)` 注册。
|
||||
该命令从 `v0.7.0-preview` 提供;源码可用 `go build -o /tmp/go-omni ./cmd/go-omni` 构建。
|
||||
边界和验证方式见 [业务基础模块](../../MODULES.md)。
|
||||
|
||||
从远程安装,生成一个带 Handler / Service / Repository / Model 分层的服务骨架:
|
||||
|
||||
```sh
|
||||
GOPRIVATE=git.yuntuo.hk go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.10.0-preview
|
||||
go-omni new --framework-version v0.10.0-preview --module example.com/billing ./billing
|
||||
cd billing
|
||||
go mod tidy
|
||||
go run ./cmd/server
|
||||
```
|
||||
|
||||
通过 `go install ...@版本` 安装的 CLI 从构建信息中读取版本,并把它固定到生成项目的
|
||||
`go.mod`。不会使用不存在的 `v0.0.0`,也不会在普通业务目录中添加本地 `replace`。
|
||||
|
||||
需要选择其他已发布版本时,使用完整版本号:
|
||||
|
||||
```sh
|
||||
go-omni new --framework-version v0.6.0-preview --module example.com/billing ./billing
|
||||
```
|
||||
|
||||
`--framework-version` 禁用自动本地替换;`main`、`latest` 等浮动引用不接受为固定版本。
|
||||
源码构建的 CLI 使用代码中声明的目标发布版本;该版本未发布时请显式选择已发布版本,或用 `--framework-dir` 验证本地源码。
|
||||
|
||||
在 GoOmni 源码目录执行时,命令会自动在生成项目的 `go.mod` 中加入本地
|
||||
`replace git.yuntuo.hk/k2safe/go-omni => ...`,便于立即运行和测试。目标目录非空时命令会停止,明确传入
|
||||
`--force` 才会覆盖脚手架文件。
|
||||
|
||||
也可以在任意目录显式指定源码路径:
|
||||
|
||||
```sh
|
||||
go-omni new --framework-dir /path/to/go-omni --module example.com/billing ./billing
|
||||
```
|
||||
|
||||
默认生成内存存储版本;选择 MySQL 时:
|
||||
|
||||
```sh
|
||||
go-omni new --database mysql --module example.com/billing ./billing
|
||||
```
|
||||
|
||||
MySQL 项目额外包含 `schema.sql`、MySQL Repository 和可选的数据库集成测试。
|
||||
先应用建表脚本,再设置 `GO_OMNI_DB_DSN` 启动。ServiceContext 负责连接池初始化、表结构检查、
|
||||
就绪探针注册和关闭。两种模式都提供用户 CRUD、分页、输入校验和统一错误响应。
|
||||
|
||||
生成服务默认包含 `/health/live`、`/health/ready` 和示例 `/users` CRUD。
|
||||
退出信号通过 `signal.NotifyContext` 传入 `RunContext`,Logger 的关闭函数注册到应用生命周期。
|
||||
|
||||
发布验收使用 Python 3 标准库脚本,不需要额外 Python 依赖:
|
||||
|
||||
```sh
|
||||
python3 scripts/verifyScaffold.py --version v0.6.0-preview
|
||||
python3 scripts/verifyDatabase.py --version v0.6.0-preview
|
||||
```
|
||||
|
||||
脚本在源码目录外安装远程 CLI、生成项目,确认固定版本且没有 `replace`,再下载依赖、
|
||||
编译、请求接口,并对真实二进制分别发送 `SIGINT` 和 `SIGTERM`。同时验证框架 `Run` 的
|
||||
信号处理和清理回调。每个进程都必须退出码为 0、释放监听端口;临时项目和测试进程会被清理。
|
||||
|
||||
发布前验证当前源码:
|
||||
|
||||
```sh
|
||||
go build -o /tmp/go-omni ./cmd/go-omni
|
||||
python3 scripts/verifyScaffold.py --cli /tmp/go-omni --framework-dir .
|
||||
python3 scripts/verifyDatabase.py --cli /tmp/go-omni --framework-dir .
|
||||
```
|
||||
|
||||
数据库验收需要本地 Docker,使用独立 MySQL 8.4 容器;不会连接已有业务数据库,结束后清理测试容器与数据卷。
|
||||
|
||||
## 显式迁移(main)
|
||||
|
||||
SQL 命名为 `0001_createUsers.up.sql`。先预览版本和 SHA-256 清单:
|
||||
保留独立迁移工具,应用启动不自动迁移:
|
||||
|
||||
```sh
|
||||
go-omni migrate --dir migrations
|
||||
```
|
||||
|
||||
部署步骤配置 `GO_OMNI_DB_DSN`,显式执行:
|
||||
|
||||
```sh
|
||||
# 检查预览,配置 GO_OMNI_DB_DSN 后显式应用
|
||||
go-omni migrate --dir migrations --apply
|
||||
```
|
||||
|
||||
预览不连接数据库;应用启动不自动迁移。失败保留 dirty 状态,不提供自动 down/force。
|
||||
脚手架原有 `schema.sql` 不会自动变成有版本的迁移,已有库需要先确定基线。详见 [扩展模块](../../ADVANCED.md)。
|
||||
SQL 命名示例 `0001_createUsers.up.sql`;预览只显示版本和 SHA-256,不连接数据库。失败保留 dirty 状态,不自动 down/force。细节见 [扩展模块](../../ADVANCED.md)。
|
||||
|
||||
@@ -15,16 +15,19 @@ func runAPI(args []string) error {
|
||||
return nil
|
||||
}
|
||||
command := args[0]
|
||||
if command == "upgrade" {
|
||||
return runUpgrade(args[1:])
|
||||
}
|
||||
if command == "go" {
|
||||
command = "generate"
|
||||
}
|
||||
if command == "check" {
|
||||
command = "validate"
|
||||
}
|
||||
if command != "new" && command != "generate" && command != "validate" && command != "inspect" && command != "export" {
|
||||
if command != "new" && command != "generate" && command != "validate" && command != "inspect" && command != "export" && command != "format" {
|
||||
return fmt.Errorf("unknown api command %q", command)
|
||||
}
|
||||
readOnly := command == "validate" || command == "inspect" || command == "export"
|
||||
readOnly := command == "validate" || command == "inspect" || command == "export" || command == "format"
|
||||
o := apigen.Options{}
|
||||
for i := 1; i < len(args); i++ {
|
||||
arg := args[i]
|
||||
@@ -33,7 +36,7 @@ func runAPI(args []string) error {
|
||||
return nil
|
||||
}
|
||||
if arg == "--check" || arg == "--dry-run" {
|
||||
if command != "generate" {
|
||||
if command != "generate" && !(command == "format" && arg == "--check") {
|
||||
return fmt.Errorf("%s belongs to api generate", arg)
|
||||
}
|
||||
if arg == "--check" {
|
||||
@@ -108,6 +111,14 @@ func runAPI(args []string) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if command == "format" {
|
||||
changes, err := apigen.FormatProject(o.API, o.Check)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("API format: %d files updated\n", len(changes))
|
||||
return nil
|
||||
}
|
||||
if readOnly {
|
||||
spec, err := apigen.Parse(o.API)
|
||||
if err != nil {
|
||||
@@ -159,6 +170,7 @@ func runAPI(args []string) error {
|
||||
}
|
||||
out, err = apigen.Generate(o)
|
||||
}
|
||||
printAPIDiagnostics(out.Diagnostics)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -168,9 +180,32 @@ func runAPI(args []string) error {
|
||||
}
|
||||
fmt.Printf("Preview: %d changes; %d existing developer-owned files preserved; no files written\n", len(out.Changes), len(out.Preserved))
|
||||
} else if o.Check {
|
||||
fmt.Println("API generated files are current")
|
||||
fmt.Println("API generated files are current; no definite Logic signature errors found (run go test ./... for full verification)")
|
||||
} else {
|
||||
fmt.Printf("API project ready: %d generated, %d created, %d preserved, %d removed\n", len(out.Generated), len(out.Created), len(out.Preserved), len(out.Removed))
|
||||
fmt.Printf("API project generated: %d generated, %d created, %d preserved, %d removed\n", len(out.Generated), len(out.Created), len(out.Preserved), len(out.Removed))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func printAPIDiagnostics(diagnostics []apigen.Diagnostic) {
|
||||
for _, d := range diagnostics {
|
||||
location := d.Path
|
||||
if d.Line > 0 {
|
||||
location += fmt.Sprintf(":%d", d.Line)
|
||||
}
|
||||
fmt.Printf("%s [%s] %s", strings.ToUpper(d.Severity), d.Code, location)
|
||||
if d.Operation != "" {
|
||||
fmt.Printf(" (%s)", d.Operation)
|
||||
}
|
||||
fmt.Printf(": %s\n", d.Message)
|
||||
if d.Before != "" {
|
||||
fmt.Printf(" before: %s\n", d.Before)
|
||||
}
|
||||
if d.After != "" {
|
||||
fmt.Printf(" after: %s\n", d.After)
|
||||
}
|
||||
for _, detail := range d.Details {
|
||||
fmt.Printf(" - %s\n", detail)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ func TestAPICommands(t *testing.T) {
|
||||
if err = runNew([]string{"--api", api, "--module", "example.com/other", "--framework-dir", root, target}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = os.Stat(filepath.Join(target, "internal/service/greet/greet.go")); err != nil {
|
||||
if _, err = os.Stat(filepath.Join(target, "internal/logic/greet/greet.go")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, args := range [][]string{{"validate"}, {"generate", "--api"}, {"new", "--api=x", "somewhere"}, {"generate", "--dir="}, {"generate", "--force"}, {"validate", "--api", api, "--dir", project}, {"validate", "--check"}} {
|
||||
@@ -40,7 +40,7 @@ func TestAPICommands(t *testing.T) {
|
||||
t.Fatalf("invalid args accepted: %v", args)
|
||||
}
|
||||
}
|
||||
if err = runNew([]string{"--api", api, "--force", target}); err == nil || !strings.Contains(err.Error(), "does not accept --force") {
|
||||
if err = runNew([]string{"--api", api, "--force", target}); err == nil || !strings.Contains(err.Error(), "do not accept --force") {
|
||||
t.Fatalf("incompatible profile: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -86,6 +86,15 @@ func TestAPIDeveloperCommands(t *testing.T) {
|
||||
}
|
||||
}
|
||||
func captureAPIOutput(t *testing.T, args []string) string {
|
||||
t.Helper()
|
||||
data, err := captureAPIResult(t, args)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func captureAPIResult(t *testing.T, args []string) (string, error) {
|
||||
t.Helper()
|
||||
r, w, err := os.Pipe()
|
||||
if err != nil {
|
||||
@@ -100,8 +109,41 @@ func captureAPIOutput(t *testing.T, args []string) string {
|
||||
w.Close()
|
||||
os.Stdout = previous
|
||||
data := <-result
|
||||
if callErr != nil {
|
||||
t.Fatal(callErr)
|
||||
}
|
||||
return string(data)
|
||||
return string(data), callErr
|
||||
}
|
||||
|
||||
func TestAPIChangeDiagnosticsVisibleOnFailure(t *testing.T) {
|
||||
root, err := filepath.Abs("../..")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dir := filepath.Join(t.TempDir(), "app")
|
||||
captureAPIOutput(t, []string{"new", dir, "--module", "example.com/diagnostics", "--framework-dir", root})
|
||||
api := filepath.Join(dir, "api/service.api")
|
||||
data, err := os.ReadFile(api)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = os.WriteFile(api, []byte(strings.ReplaceAll(string(data), "GreetReq", "HelloReq")), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, flag := range []string{"--dry-run", "--check", ""} {
|
||||
args := []string{"generate", "--dir", dir}
|
||||
if flag != "" {
|
||||
args = append(args, flag)
|
||||
}
|
||||
text, err := captureAPIResult(t, args)
|
||||
if (flag == "--check") != (err != nil) {
|
||||
t.Fatalf("%s: %v", flag, err)
|
||||
}
|
||||
for _, want := range []string{"REVIEW [contractChanged]", "ERROR [logicSignature]", "internal/logic/greet/greet.go:", "types.HelloReq", "types.GreetReq"} {
|
||||
if !strings.Contains(text, want) {
|
||||
t.Fatalf("%s hides %q:\n%s", flag, want, text)
|
||||
}
|
||||
}
|
||||
}
|
||||
text, err := captureAPIResult(t, []string{"generate", "--dir", dir, "--check"})
|
||||
if err == nil || !strings.Contains(text, "ERROR [logicSignature]") || strings.Contains(text, "files are current") {
|
||||
t.Fatalf("check claimed success: %v\n%s", err, text)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,7 +87,6 @@ func TestInvalidDefinitions(t *testing.T) {
|
||||
"service user { @handler go\n @status 201\n post /go\n}",
|
||||
"type A { Name string `header:\"Name\"` }\nservice user { @handler go\n get /go returns (A)\n}",
|
||||
"@server (group: ../../bad)\nservice user { @handler go\n get /go\n}",
|
||||
"service user { @handler go\n get /health/ready\n}",
|
||||
"type A { Name string `json:\"name\"`\n Name int `json:\"other\"` }\nservice user { @handler go\n post /go (A)\n}",
|
||||
"service user { @handler go\n get /go\n @handler Go\n get /other\n}",
|
||||
"type A { Value []byte `json:\"value\"` }\nservice user { @handler go\n post /go (A)\n}",
|
||||
@@ -168,10 +167,11 @@ func TestRegenerationAndConflicts(t *testing.T) {
|
||||
if _, err := Generate(o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
servicePath := filepath.Join(dir, "internal/service/greet/greet.go")
|
||||
servicePath := filepath.Join(dir, "internal/logic/greet/greet.go")
|
||||
service := readFile(t, servicePath) + "\n// user edit must survive\n"
|
||||
writeFile(t, servicePath, service)
|
||||
for _, file := range []string{"cmd/server/main.go", "etc/config.yaml", "internal/servicecontext/context.go", "internal/repository/greet/repository.go"} {
|
||||
writeFile(t, filepath.Join(dir, "internal/repository/greet/repository.go"), "package greet\n")
|
||||
for _, file := range []string{"etc/config.yaml", "internal/svc/serviceContext.go", "internal/repository/greet/repository.go"} {
|
||||
body := readFile(t, filepath.Join(dir, file))
|
||||
if strings.HasSuffix(file, ".go") {
|
||||
body += "\n// handwritten extension\n"
|
||||
@@ -204,7 +204,7 @@ func TestRegenerationAndConflicts(t *testing.T) {
|
||||
if _, err := os.Stat(filepath.Join(dir, "internal/handler/extra/handlersGen.go")); !os.IsNotExist(err) {
|
||||
t.Fatal("stale handler remains")
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "internal/service/extra/ping.go")); err != nil {
|
||||
if _, err := os.Stat(filepath.Join(dir, "internal/logic/extra/ping.go")); err != nil {
|
||||
t.Fatal("business stub removed")
|
||||
}
|
||||
writeFile(t, filepath.Join(dir, "internal/types/typesGen.go"), typesBefore+"\n// edited generated file\n")
|
||||
@@ -302,6 +302,7 @@ func TestGeneratedProjectsBuildAndRun(t *testing.T) {
|
||||
if starter {
|
||||
_, err = New(o)
|
||||
} else {
|
||||
writeFile(t, filepath.Join(dir, generatorConfigFile), `{"middleware":"handlers"}`)
|
||||
o.API = filepath.Join(root, "examples/contracts/user.api")
|
||||
_, err = Generate(o)
|
||||
}
|
||||
@@ -312,9 +313,9 @@ func TestGeneratedProjectsBuildAndRun(t *testing.T) {
|
||||
if starter {
|
||||
source = consumerStarterTest
|
||||
} else {
|
||||
writeFile(t, filepath.Join(dir, "internal/service/user/updateUser.go"), `package user
|
||||
writeFile(t, filepath.Join(dir, "internal/logic/user/updateUser.go"), `package user
|
||||
import("context";"example.com/consumer/internal/types")
|
||||
func(s *Service) UpdateUser(ctx context.Context,req *types.UpdateUserReq)(*types.UserResp,error){return &types.UserResp{UserID:req.UserID,Name:req.Name,Active:req.Active},nil}
|
||||
func(s *Logic) UpdateUser(ctx context.Context,req *types.UpdateUserReq)(*types.UserResp,error){return &types.UserResp{UserID:req.UserID,Name:req.Name,Active:req.Active},nil}
|
||||
`)
|
||||
}
|
||||
writeFile(t, filepath.Join(dir, "contract_test.go"), source)
|
||||
@@ -339,13 +340,13 @@ func(s *Service) UpdateUser(ctx context.Context,req *types.UpdateUserReq)(*types
|
||||
}
|
||||
}
|
||||
|
||||
const consumerStarterTest = `package consumer_test
|
||||
import("context";"io";"net/http/httptest";"strings";"testing";omni "git.yuntuo.hk/k2safe/go-omni";"git.yuntuo.hk/k2safe/go-omni/logx";"example.com/consumer/internal/handler";"example.com/consumer/internal/servicecontext";"example.com/consumer/internal/settings")
|
||||
func TestGreeting(t *testing.T){cfg,err:=settings.Load("etc/config.yaml");if err!=nil{t.Fatal(err)};cfg.Server.Log.Writer=io.Discard;cfg.Server.Log.File="";cfg.Server.Log.Rotation=logx.RotationConfig{};logger,err:=logx.New(cfg.Server.Log);if err!=nil{t.Fatal(err)};defer logger.Close();deps,err:=servicecontext.New(context.Background(),logger);if err!=nil{t.Fatal(err)};defer deps.Close();app:=omni.New();defer app.Close();if err=handler.RegisterRoutes(app,deps);err!=nil{t.Fatal(err)};w:=httptest.NewRecorder();app.Engine().ServeHTTP(w,httptest.NewRequest("GET","/api/greet/Alice",nil));if w.Code!=200||!strings.Contains(w.Body.String(),"Hello Alice")||w.Header().Get("X-Request-ID")==""{t.Fatalf("%d %s",w.Code,w.Body.String())}}
|
||||
const consumerStarterTest = `package main_test
|
||||
import("context";"io";"net/http/httptest";"strings";"testing";omni "git.yuntuo.hk/k2safe/go-omni";"git.yuntuo.hk/k2safe/go-omni/logx";"example.com/consumer/internal/handler";"example.com/consumer/internal/svc";"example.com/consumer/internal/config")
|
||||
func TestGreeting(t *testing.T){cfg,err:=config.Load("etc/config.yaml");if err!=nil{t.Fatal(err)};cfg.Server.Log.Writer=io.Discard;cfg.Server.Log.File="";cfg.Server.Log.Rotation=logx.RotationConfig{};logger,err:=logx.New(cfg.Server.Log);if err!=nil{t.Fatal(err)};defer logger.Close();deps,err:=svc.New(context.Background(),logger);if err!=nil{t.Fatal(err)};defer deps.Close();app:=omni.New();defer app.Close();if err=handler.RegisterRoutes(app,deps);err!=nil{t.Fatal(err)};w:=httptest.NewRecorder();app.Engine().ServeHTTP(w,httptest.NewRequest("GET","/api/greet/Alice",nil));if w.Code!=200||!strings.Contains(w.Body.String(),"Hello Alice")||w.Header().Get("X-Request-ID")==""{t.Fatalf("%d %s",w.Code,w.Body.String())}}
|
||||
`
|
||||
const consumerContractTest = `package consumer_test
|
||||
import("context";"encoding/json";"io";"net/http/httptest";"strings";"testing";omni "git.yuntuo.hk/k2safe/go-omni";"git.yuntuo.hk/k2safe/go-omni/logx";"example.com/consumer/internal/handler";"example.com/consumer/internal/servicecontext")
|
||||
func TestContract(t *testing.T){cfg:=logx.Default();cfg.Writer=io.Discard;logger,err:=logx.New(cfg);if err!=nil{t.Fatal(err)};defer logger.Close();deps,err:=servicecontext.New(context.Background(),logger);if err!=nil{t.Fatal(err)};defer deps.Close();app:=omni.New();defer app.Close();if err=handler.RegisterRoutes(app,deps);err!=nil{t.Fatal(err)}
|
||||
const consumerContractTest = `package main_test
|
||||
import("context";"encoding/json";"io";"net/http/httptest";"strings";"testing";omni "git.yuntuo.hk/k2safe/go-omni";"git.yuntuo.hk/k2safe/go-omni/logx";"example.com/consumer/internal/handler";"example.com/consumer/internal/svc")
|
||||
func TestContract(t *testing.T){cfg:=logx.Default();cfg.Writer=io.Discard;logger,err:=logx.New(cfg);if err!=nil{t.Fatal(err)};defer logger.Close();deps,err:=svc.New(context.Background(),logger);if err!=nil{t.Fatal(err)};defer deps.Close();app:=omni.New();defer app.Close();if err=handler.RegisterRoutes(app,deps);err!=nil{t.Fatal(err)}
|
||||
for _,tc:=range []struct{method,path,body string;status int}{
|
||||
{"PUT","/api/v1/users/7?notify=true",` + "`" + `{"name":"Alice","active":false,"age":0}` + "`" + `,200},
|
||||
{"PUT","/api/v1/users/7",` + "`" + `{"name":"Alice"}` + "`" + `,400},
|
||||
|
||||
104
cmd/go-omni/apigen/arrayResponse_test.go
Normal file
104
cmd/go-omni/apigen/arrayResponse_test.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPrimitiveArrayContracts(t *testing.T) {
|
||||
for _, name := range []string{"string", "bool", "int", "int8", "int16", "int32", "int64", "uint", "uint16", "uint32", "uint64", "float32", "float64", "any"} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
spec, err := parseText(t, fmt.Sprintf("service arrays{\n@handler list\nget /list returns ([]%s)\n}", name))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c, err := Compile(spec)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
schema := operation(t, document(t, c), "/list", "get")["responses"].(map[string]any)["200"].(map[string]any)["content"].(map[string]any)["application/json"].(map[string]any)["schema"].(map[string]any)
|
||||
data := schema["properties"].(map[string]any)["data"].(map[string]any)
|
||||
if data["type"] != "array" || data["items"] == nil {
|
||||
t.Fatal(data)
|
||||
}
|
||||
if name == "any" && len(data["items"].(map[string]any)) != 0 {
|
||||
t.Fatal("dynamic JSON was constrained")
|
||||
}
|
||||
if responseGoType("[]"+name) != "[]"+name || responseNeedsTypes("[]"+name) {
|
||||
t.Fatal("wrong Go signature/import")
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, name := range []string{"uint8", "File", "Unknown", "[]string"} {
|
||||
spec, err := parseText(t, fmt.Sprintf("service arrays{\n@handler list\nget /list returns ([]%s)\n}", name))
|
||||
if err == nil {
|
||||
_, err = Compile(spec)
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatal("unsupported root accepted", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrimitiveArrayConsumer(t *testing.T) {
|
||||
if os.Getenv("GO_OMNI_SCAFFOLD_INTEGRATION") != "1" {
|
||||
t.Skip("set GO_OMNI_SCAFFOLD_INTEGRATION=1")
|
||||
}
|
||||
dir := filepath.Join(t.TempDir(), "arrays")
|
||||
o := Options{Dir: dir, API: filepath.Join(frameworkRoot(t), "examples/contracts/arrays.api"), Module: "example.com/arrays", FrameworkDir: frameworkRoot(t)}
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, name := range []string{"strings", "integers", "dynamic", "empty", "raw"} {
|
||||
writeFile(t, filepath.Join(dir, "internal/logic/arrays/"+name+".go"), "package arrays\n")
|
||||
}
|
||||
writeFile(t, filepath.Join(dir, "internal/logic/arrays/values.go"), readFile(t, "testdata/arrays/values.go"))
|
||||
writeFile(t, filepath.Join(dir, "consumer_test.go"), readFile(t, "testdata/arrays/consumer_test.go"))
|
||||
o.API = filepath.Join(dir, "api/arrays.api")
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(readFile(t, filepath.Join(dir, "internal/logic/arrays/values.go")), "企业") {
|
||||
t.Fatal("logic overwritten")
|
||||
}
|
||||
o.Check = true
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "go", "test", "-race", "-mod=mod", "-count=1", "./...")
|
||||
cmd.Dir = dir
|
||||
cmd.Env = append(os.Environ(), "GOWORK=off")
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("%v\n%s", err, out)
|
||||
}
|
||||
if os.Getenv("GO_OMNI_RELEASE_CLI") != "" {
|
||||
cmd = exec.CommandContext(ctx, "go", "list", "-m", "-json", frameworkModule)
|
||||
cmd.Dir = dir
|
||||
cmd.Env = append(os.Environ(), "GOWORK=off")
|
||||
raw, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatal(err, string(raw))
|
||||
}
|
||||
var module struct {
|
||||
Version, Sum string
|
||||
Replace any
|
||||
}
|
||||
if err = json.Unmarshal(raw, &module); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if module.Version != os.Getenv("GO_OMNI_RELEASE_VERSION") || module.Replace != nil || module.Sum == "" {
|
||||
t.Fatal("not a fixed remote consumer")
|
||||
}
|
||||
t.Logf("remote framework %s checksum %s; no local replace", module.Version, module.Sum)
|
||||
}
|
||||
|
||||
}
|
||||
92
cmd/go-omni/apigen/business_test.go
Normal file
92
cmd/go-omni/apigen/business_test.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestBusinessContract(t *testing.T) {
|
||||
spec, err := Parse(filepath.Join(frameworkRoot(t), "examples/contracts/business.api"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c, err := Compile(spec)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var doc map[string]any
|
||||
if err = json.Unmarshal(c.Document, &doc); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
paths := doc["paths"].(map[string]any)
|
||||
body := paths["/optional"].(map[string]any)["post"].(map[string]any)["requestBody"].(map[string]any)
|
||||
if body["required"] != false {
|
||||
t.Fatal("optional body documented as required")
|
||||
}
|
||||
for _, path := range []string{"/empty", "/absent"} {
|
||||
if paths[path].(map[string]any)["post"].(map[string]any)["x-omni-request-policy"] == nil {
|
||||
t.Fatal("missing policy metadata")
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestBusinessGeneratedProject(t *testing.T) {
|
||||
if os.Getenv("GO_OMNI_SCAFFOLD_INTEGRATION") != "1" {
|
||||
t.Skip("set GO_OMNI_SCAFFOLD_INTEGRATION=1")
|
||||
}
|
||||
dir := filepath.Join(t.TempDir(), "business")
|
||||
writeFile(t, filepath.Join(dir, generatorConfigFile), `{"middleware":"handlers"}`)
|
||||
o := Options{Dir: dir, API: filepath.Join(frameworkRoot(t), "examples/contracts/business.api"), Module: "example.com/business", FrameworkDir: frameworkRoot(t)}
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, name := range []string{"upload", "nullable", "nothing", "rawNull", "rawArray", "empty", "absent", "optional", "echo", "tolerant", "noContent"} {
|
||||
writeFile(t, filepath.Join(dir, "internal/logic/demo/"+name+".go"), "package demo\n")
|
||||
}
|
||||
writeFile(t, filepath.Join(dir, "internal/logic/demo/list.go"), readFile(t, "testdata/business/operations.go"))
|
||||
writeFile(t, filepath.Join(dir, "consumer_test.go"), readFile(t, "testdata/business/consumer_test.go"))
|
||||
writeFile(t, filepath.Join(dir, "internal/middleware/accessFactory.go"), readFile(t, "testdata/business/accessFactory.go"))
|
||||
writeFile(t, filepath.Join(dir, "internal/svc/client.go"), readFile(t, "testdata/business/client.go"))
|
||||
svcFile := filepath.Join(dir, "internal/svc/serviceContext.go")
|
||||
source := strings.Replace(readFile(t, svcFile), "type ServiceContext struct {", "type ServiceContext struct {\n Client *accessClient", 1)
|
||||
source = strings.Replace(source, "return nil", "return c.initAccess(ctx)", 1)
|
||||
writeFile(t, svcFile, source)
|
||||
o.API = filepath.Join(dir, "api/business.api")
|
||||
o.Check = true
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "go", "test", "-race", "-mod=mod", "-count=1", "./...")
|
||||
cmd.Dir = dir
|
||||
cmd.Env = append(os.Environ(), "GOWORK=off")
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("%v\n%s", err, out)
|
||||
}
|
||||
if os.Getenv("GO_OMNI_RELEASE_CLI") != "" {
|
||||
cmd = exec.CommandContext(ctx, "go", "list", "-m", "-json", frameworkModule)
|
||||
cmd.Dir = dir
|
||||
cmd.Env = append(os.Environ(), "GOWORK=off")
|
||||
raw, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatal(err, string(raw))
|
||||
}
|
||||
var module struct {
|
||||
Version, Sum string
|
||||
Replace any
|
||||
}
|
||||
if err = json.Unmarshal(raw, &module); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if module.Version != os.Getenv("GO_OMNI_RELEASE_VERSION") || module.Replace != nil || module.Sum == "" {
|
||||
t.Fatal("not a fixed remote consumer")
|
||||
}
|
||||
t.Logf("remote framework %s checksum %s; no local replace", module.Version, module.Sum)
|
||||
}
|
||||
}
|
||||
161
cmd/go-omni/apigen/catalogDocument.go
Normal file
161
cmd/go-omni/apigen/catalogDocument.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
||||
"git.yuntuo.hk/k2safe/go-omni/response"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const catalogDocumentFile = "omni.errors.json"
|
||||
|
||||
// DocumentErrors selects documentation only. Runtime App inheritance and
|
||||
// route-local @errors declarations are never changed by this metadata.
|
||||
// A nil Include lists all errors; [] lists none. Routes override Include.
|
||||
type DocumentErrors struct {
|
||||
Source string `json:"source"`
|
||||
Include []string `json:"include"`
|
||||
Routes map[string][]string `json:"routes,omitempty"`
|
||||
}
|
||||
|
||||
func readDocumentCatalog(root string, spec *Spec, overrides map[string][]byte) error {
|
||||
name := catalogDocumentFile
|
||||
path := filepath.Join(root, name)
|
||||
// Shared metadata uses the same explicitly authorized roots as .api imports.
|
||||
|
||||
if selection := spec.DocumentErrors; selection != nil {
|
||||
if _, err := os.Lstat(path); !errors.Is(err, os.ErrNotExist) {
|
||||
return errors.New("api: use either local omni.errors.json or shared errors.source, not both")
|
||||
}
|
||||
name = selection.Source
|
||||
alias, sub, ok := strings.Cut(strings.TrimPrefix(name, "@"), "/")
|
||||
declared, found := spec.SharedRoots[alias]
|
||||
if !strings.HasPrefix(name, "@") || !ok || !found || sub == "" || filepath.Ext(sub) != ".json" {
|
||||
return errors.New("api: errors.source must name a JSON file in a declared @root")
|
||||
}
|
||||
if err := safePath(declared.Path, sub); err != nil {
|
||||
return err
|
||||
}
|
||||
path = filepath.Join(declared.Path, filepath.FromSlash(sub))
|
||||
}
|
||||
raw, ok := overrides[path]
|
||||
if !ok {
|
||||
stat, err := os.Lstat(path)
|
||||
if errors.Is(err, os.ErrNotExist) && spec.DocumentErrors == nil {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !stat.Mode().IsRegular() {
|
||||
return errors.New("api: error metadata must be a regular file")
|
||||
}
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
raw, err = io.ReadAll(io.LimitReader(f, (1<<20)+1))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if len(raw) > 1<<20 {
|
||||
return errors.New("api: error metadata exceeds 1 MiB")
|
||||
}
|
||||
var catalog apierrors.Catalog
|
||||
if err := apierrors.DecodeCatalogJSON(raw, &catalog); err != nil {
|
||||
return err
|
||||
}
|
||||
registry, err := apierrors.NewRegistry(catalog)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
normalized := registry.Catalog()
|
||||
spec.DocumentCatalog = &normalized
|
||||
spec.Sources[name] = raw
|
||||
spec.SourcePaths[name] = path
|
||||
return validateDocumentSelection(spec)
|
||||
}
|
||||
|
||||
func documentRouteCatalog(spec *Spec, route Route) apierrors.Catalog {
|
||||
catalog := spec.DocumentCatalog.Clone()
|
||||
if spec.DocumentErrors == nil {
|
||||
return catalog
|
||||
}
|
||||
include := spec.DocumentErrors.Include
|
||||
if selected, ok := spec.DocumentErrors.Routes[strings.ToUpper(route.Method)+" "+route.Path]; ok {
|
||||
include = selected
|
||||
}
|
||||
if include != nil {
|
||||
selected := map[string]apierrors.Definition{}
|
||||
for _, key := range include {
|
||||
selected[key] = catalog.Definitions[key]
|
||||
}
|
||||
catalog.Definitions = selected
|
||||
}
|
||||
return catalog
|
||||
}
|
||||
|
||||
func validateDocumentSelection(spec *Spec) error {
|
||||
cfg := spec.DocumentErrors
|
||||
if cfg == nil {
|
||||
return nil
|
||||
}
|
||||
validate := func(keys []string) error {
|
||||
seen := map[string]bool{}
|
||||
for _, key := range keys {
|
||||
if _, ok := spec.DocumentCatalog.Definitions[key]; !ok {
|
||||
return fmt.Errorf("api: unknown documentation error %q", key)
|
||||
}
|
||||
if seen[key] {
|
||||
return fmt.Errorf("api: duplicate documentation error %q", key)
|
||||
}
|
||||
seen[key] = true
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if err := validate(cfg.Include); err != nil {
|
||||
return err
|
||||
}
|
||||
routes := map[string]Route{}
|
||||
for _, r := range spec.Routes {
|
||||
routes[strings.ToUpper(r.Method)+" "+r.Path] = r
|
||||
}
|
||||
for key, keys := range cfg.Routes {
|
||||
r, ok := routes[key]
|
||||
if !ok {
|
||||
return fmt.Errorf("api: unknown documentation route %q", key)
|
||||
}
|
||||
if len(r.Errors) > 0 {
|
||||
return fmt.Errorf("api: %s already declares route-local errors", key)
|
||||
}
|
||||
if err := validate(keys); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateDocumentCatalog(spec *Spec) error {
|
||||
if spec.DocumentCatalog == nil {
|
||||
return nil
|
||||
}
|
||||
for _, r := range spec.Routes {
|
||||
if len(r.Errors) > 0 {
|
||||
continue
|
||||
}
|
||||
policy, err := response.New(responseConfig(spec, r))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = policy.ValidateErrors(documentRouteCatalog(spec, r)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -69,14 +69,14 @@ func TestCompatibilityConsumer(t *testing.T) {
|
||||
}
|
||||
generate(false)
|
||||
files := map[string]string{
|
||||
"internal/service/demo/echo.go": "echo.go",
|
||||
"internal/logic/demo/echo.go": "echo.go",
|
||||
"internal/handler/demo/customBind.go": "hooks.go",
|
||||
"internal/handler/files/downloadManual.go": "download.go",
|
||||
"consumer_test.go": "consumer_test.go",
|
||||
}
|
||||
// The fixture implements related methods together; remove only its generated
|
||||
// one-time stubs, then fill every stub so regeneration can preserve all files.
|
||||
for _, name := range []string{"internal/service/demo/item.go", "internal/service/demo/custom.go"} {
|
||||
for _, name := range []string{"internal/logic/demo/item.go", "internal/logic/demo/custom.go"} {
|
||||
writeFile(t, filepath.Join(dir, name), "package demo\n")
|
||||
}
|
||||
for _, name := range []string{"internal/handler/demo/uploadManual.go", "internal/handler/demo/callbackManual.go"} {
|
||||
|
||||
@@ -40,8 +40,20 @@ func Compile(spec *Spec) (*Compiled, error) {
|
||||
if prepareErr != nil {
|
||||
return nil, prepareErr
|
||||
}
|
||||
if err := expandFields(spec); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := validateDocumentCatalog(spec); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := spec.RequestPolicy.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
definitions := map[string]Type{}
|
||||
for _, t := range spec.Types {
|
||||
if t.Name == "File" {
|
||||
return nil, errors.New("File is reserved for multipart uploads")
|
||||
}
|
||||
if t.Name == "ContractScalars" && len(spec.Scalars) > 0 {
|
||||
return nil, errors.New("ContractScalars is reserved when declaring scalars")
|
||||
}
|
||||
@@ -61,7 +73,7 @@ func Compile(spec *Spec) (*Compiled, error) {
|
||||
resolving := map[string]bool{}
|
||||
var resolve func(string, int) (reflect.Type, error)
|
||||
var expression func(ast.Expr, int) (reflect.Type, error)
|
||||
primitives := map[string]reflect.Type{"any": reflect.TypeOf((*any)(nil)).Elem(), "string": reflect.TypeOf(""), "bool": reflect.TypeOf(false), "int": reflect.TypeOf(int(0)), "int8": reflect.TypeOf(int8(0)), "int16": reflect.TypeOf(int16(0)), "int32": reflect.TypeOf(int32(0)), "int64": reflect.TypeOf(int64(0)), "uint": reflect.TypeOf(uint(0)), "uint8": reflect.TypeOf(uint8(0)), "uint16": reflect.TypeOf(uint16(0)), "uint32": reflect.TypeOf(uint32(0)), "uint64": reflect.TypeOf(uint64(0)), "float32": reflect.TypeOf(float32(0)), "float64": reflect.TypeOf(float64(0))}
|
||||
|
||||
expression = func(e ast.Expr, depth int) (reflect.Type, error) {
|
||||
if depth > 32 {
|
||||
return nil, errors.New("type nesting exceeds 32")
|
||||
@@ -71,7 +83,7 @@ func Compile(spec *Spec) (*Compiled, error) {
|
||||
if t, ok := scalarTypes[e.Name]; ok {
|
||||
return t, nil
|
||||
}
|
||||
if t, ok := primitives[e.Name]; ok {
|
||||
if t, ok := primitiveTypes[e.Name]; ok {
|
||||
return t, nil
|
||||
}
|
||||
return resolve(e.Name, depth+1)
|
||||
@@ -138,7 +150,7 @@ func Compile(spec *Spec) (*Compiled, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s.%s: %w", name, f.Name, err)
|
||||
}
|
||||
fields = append(fields, reflect.StructField{Name: f.Name, Type: t, Tag: reflect.StructTag(f.Tag)})
|
||||
fields = append(fields, reflect.StructField{Name: f.Name, Type: t, Tag: reflect.StructTag(fieldTag(f))})
|
||||
}
|
||||
t := reflect.StructOf(fields)
|
||||
resolved[name] = t
|
||||
@@ -162,12 +174,12 @@ func Compile(spec *Spec) (*Compiled, error) {
|
||||
}
|
||||
handlers, paths, shapes, groups := map[string]bool{}, map[string]bool{}, map[string]string{}, map[string]bool{}
|
||||
middlewareSymbols := map[string]string{}
|
||||
for _, r := range spec.Routes {
|
||||
for routeIndex, r := range spec.Routes {
|
||||
if !goName.MatchString(r.Handler) {
|
||||
return nil, fmt.Errorf("%s: invalid handler name", r.Position)
|
||||
}
|
||||
if strings.EqualFold(r.Handler, "service") {
|
||||
return nil, errors.New("handler name service conflicts with the service constructor file")
|
||||
if strings.EqualFold(r.Handler, "service") || strings.EqualFold(r.Handler, "logic") {
|
||||
return nil, errors.New("handler name conflicts with the Logic constructor file")
|
||||
}
|
||||
if r.Group == "" {
|
||||
return nil, errors.New("group required")
|
||||
@@ -218,16 +230,13 @@ func Compile(spec *Spec) (*Compiled, error) {
|
||||
return nil, fmt.Errorf("duplicate route %s", pathKey)
|
||||
}
|
||||
paths[pathKey] = true
|
||||
if r.Path == "/health/live" || r.Path == "/health/ready" || r.Path == "/openapi.json" {
|
||||
return nil, fmt.Errorf("route %s is reserved by the generated server", r.Path)
|
||||
}
|
||||
if r.Method != strings.ToLower(r.Method) {
|
||||
return nil, errors.New("HTTP methods must be lowercase")
|
||||
}
|
||||
if r.Status != 200 && r.Status != 201 && r.Status != 202 && r.Status != 204 {
|
||||
return nil, errors.New("@status supports 200, 201, 202 or 204")
|
||||
}
|
||||
if !r.Manual && (r.Status == 204) != (r.Response == "") {
|
||||
if !r.Manual && r.Produces == "" && (r.Status == 204) != (r.Response == "") {
|
||||
return nil, errors.New("204 requires no response; other success statuses require a response type")
|
||||
}
|
||||
if r.Method == "head" && r.Response != "" {
|
||||
@@ -236,16 +245,28 @@ func Compile(spec *Spec) (*Compiled, error) {
|
||||
if err := validateRouteExtensions(r); err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", r.Position, err)
|
||||
}
|
||||
effectivePolicy := contract.MergePolicy(spec.RequestPolicy, r.RequestPolicy)
|
||||
if (r.Method == "get" || r.Method == "head") && effectivePolicy.Body != "" && effectivePolicy.Body != "auto" && effectivePolicy.Body != "none" {
|
||||
return nil, errors.New("GET/HEAD cannot inherit a JSON body policy; override body with auto or none")
|
||||
}
|
||||
op := openapi.Operation{Method: r.Method, Path: r.Path, OperationID: operationID(r), Summary: r.Summary, Responses: map[int]openapi.Response{}}
|
||||
if r.Request != "" {
|
||||
b, ok := c.Bindings[r.Request]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unknown request %s", r.Request)
|
||||
}
|
||||
r.BoundMultipart = b.HasMultipart() && !r.CustomBinding && !r.Manual
|
||||
spec.Routes[routeIndex].BoundMultipart = r.BoundMultipart
|
||||
if b.HasMultipart() && r.BodyMode != "multipart" {
|
||||
return nil, errors.New("multipart fields require @body (mode: multipart)")
|
||||
}
|
||||
op.Parameters = b.Parameters()
|
||||
op.Request = b.BodySchema()
|
||||
if op.Request != nil && (effectivePolicy.Body == "none" || effectivePolicy.Body == "emptyObject") {
|
||||
return nil, errors.New("request policy conflicts with JSON fields")
|
||||
}
|
||||
if op.Request != nil && (r.BodyMode == "raw" || r.BodyMode == "multipart") {
|
||||
return nil, errors.New("raw/multipart request DTOs may declare only path/form/header fields; body binding is manual")
|
||||
return nil, errors.New("raw/multipart request DTOs may declare only path/form/header fields; use the generated body stream for content")
|
||||
}
|
||||
if op.Request != nil && (r.Method == "get" || r.Method == "head") {
|
||||
return nil, errors.New("GET/HEAD requests cannot declare JSON bodies; use form/path/header tags")
|
||||
@@ -253,11 +274,7 @@ func Compile(spec *Spec) (*Compiled, error) {
|
||||
}
|
||||
var response *openapi.Schema
|
||||
if r.Response != "" {
|
||||
b, ok := c.Bindings[r.Response]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unknown response %s", r.Response)
|
||||
}
|
||||
s, err := b.ResponseSchema()
|
||||
s, err := responseDataSchema(r.Response, c.Bindings)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -298,7 +315,8 @@ func Compile(spec *Spec) (*Compiled, error) {
|
||||
if err = decoder.Decode(&doc); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
decorateDocument(doc, spec)
|
||||
decorateDocument(doc, spec, c.Bindings)
|
||||
compactDocument(doc)
|
||||
c.Document, err = json.MarshalIndent(doc, "", " ")
|
||||
return c, err
|
||||
}
|
||||
|
||||
102
cmd/go-omni/apigen/composition.go
Normal file
102
cmd/go-omni/apigen/composition.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// expandFields copies definitions rather than embedding Go structs. Binding,
|
||||
// validation, DTOs, snapshots and OpenAPI all consume the same flattened fields.
|
||||
func expandFields(spec *Spec) error {
|
||||
definitions := map[string]Type{}
|
||||
for _, t := range spec.Types {
|
||||
if _, exists := definitions[t.Name]; exists {
|
||||
return fmt.Errorf("%s: duplicate type %s", t.Position, t.Name)
|
||||
}
|
||||
definitions[t.Name] = t
|
||||
}
|
||||
expanded := map[string]Type{}
|
||||
heights := map[string]int{}
|
||||
visiting := map[string]bool{}
|
||||
total := 0
|
||||
var expand func(string, int) (Type, error)
|
||||
expand = func(name string, depth int) (Type, error) {
|
||||
if t, ok := expanded[name]; ok {
|
||||
if depth+heights[name] > 32 {
|
||||
return Type{}, fmt.Errorf("field composition exceeds 32 levels at %s", name)
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
if visiting[name] {
|
||||
return Type{}, fmt.Errorf("field composition cycle at %s", name)
|
||||
}
|
||||
if depth > 32 {
|
||||
return Type{}, fmt.Errorf("field composition exceeds 32 levels at %s", name)
|
||||
}
|
||||
t, ok := definitions[name]
|
||||
if !ok {
|
||||
return Type{}, fmt.Errorf("unknown composed object type %s", name)
|
||||
}
|
||||
visiting[name] = true
|
||||
defer delete(visiting, name)
|
||||
fields := []Field{}
|
||||
height := 0
|
||||
seen, wire := map[string]bool{}, map[string]bool{}
|
||||
for _, f := range t.Fields {
|
||||
add := []Field{f}
|
||||
if f.Type == "" {
|
||||
child, err := expand(f.Name, depth+1)
|
||||
if err != nil {
|
||||
return Type{}, fmt.Errorf("%s.%s: %w", name, f.Name, err)
|
||||
}
|
||||
add = child.Fields
|
||||
if heights[f.Name]+1 > height {
|
||||
height = heights[f.Name] + 1
|
||||
}
|
||||
if height > 32 {
|
||||
return Type{}, fmt.Errorf("field composition exceeds 32 levels at %s", name)
|
||||
}
|
||||
}
|
||||
for _, field := range add {
|
||||
if seen[field.Name] {
|
||||
return Type{}, fmt.Errorf("%s: conflicting composed field %s", name, field.Name)
|
||||
}
|
||||
seen[field.Name] = true
|
||||
for _, source := range []string{"json", "form", "path", "header", "multipart", "multipartJSON", "file"} {
|
||||
tag := strings.Split(reflect.StructTag(field.Tag).Get(source), ",")[0]
|
||||
if tag == "" || tag == "-" {
|
||||
continue
|
||||
}
|
||||
if source == "header" {
|
||||
tag = strings.ToLower(tag)
|
||||
}
|
||||
key := source + ":" + tag
|
||||
if wire[key] {
|
||||
return Type{}, fmt.Errorf("%s: conflicting composed field tag %s", name, key)
|
||||
}
|
||||
wire[key] = true
|
||||
}
|
||||
fields = append(fields, field)
|
||||
total++
|
||||
if len(fields) > 4096 || total > 65536 {
|
||||
return Type{}, fmt.Errorf("field composition expansion limit exceeded")
|
||||
}
|
||||
}
|
||||
}
|
||||
t.Fields = fields
|
||||
expanded[name] = t
|
||||
heights[name] = height
|
||||
return t, nil
|
||||
}
|
||||
result := make([]Type, 0, len(spec.Types))
|
||||
for _, t := range spec.Types {
|
||||
out, err := expand(t.Name, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
result = append(result, out)
|
||||
}
|
||||
spec.Types = result
|
||||
return nil
|
||||
}
|
||||
163
cmd/go-omni/apigen/composition_test.go
Normal file
163
cmd/go-omni/apigen/composition_test.go
Normal file
@@ -0,0 +1,163 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFieldComposition(t *testing.T) {
|
||||
source := readFile(t, "testdata/composition/service.api")
|
||||
spec, err := parseText(t, source)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
first, err := Compile(spec)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := Compile(spec)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(first.Document, second.Document) || len(spec.Types[1].Fields) != 1 {
|
||||
t.Fatal("composition mutates input")
|
||||
}
|
||||
for _, name := range []string{"EnterpriseRequest", "TenantRequest", "AdminRequest"} {
|
||||
schema := first.Bindings[name].BodySchema()
|
||||
raw, _ := json.Marshal(schema)
|
||||
if !bytes.Contains(raw, []byte("Employee display name.")) || bytes.Contains(raw, []byte("SalaryFields")) {
|
||||
t.Fatalf("flattened schema/comment missing: %s", raw)
|
||||
}
|
||||
}
|
||||
cases := []struct{ name, decl, want string }{
|
||||
{"unknown", "type Req {\n Missing\n}", "unknown composed"},
|
||||
{"cycle", "type Req {\n Other\n}\ntype Other {\n Req\n}", "cycle"},
|
||||
{"conflict", "type Base {\n Name string `json:\"name\"`\n}\ntype Req {\n Base\n Name string `json:\"other\"`\n}", "conflicting"},
|
||||
{"wireConflict", "type Base {\n Name string `json:\"name\"`\n}\ntype Req {\n Base\n Other string `json:\"name\"`\n}", "conflicting"},
|
||||
{"tagged", "type Req {\n Other `json:\"other\"`\n}", "cannot carry"},
|
||||
{"diamond", "type Base {\n Name string `json:\"name\"`\n}\ntype Left {\n Base\n}\ntype Right {\n Base\n}\ntype Req {\n Left\n Right\n}", "conflicting"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
s, e := parseText(t, tc.decl+"\nservice demo{\n@handler post\npost / (Req)\n}")
|
||||
if e == nil {
|
||||
_, e = Compile(s)
|
||||
}
|
||||
if e == nil || !strings.Contains(e.Error(), tc.want) {
|
||||
t.Fatal(e)
|
||||
}
|
||||
})
|
||||
}
|
||||
// Imports can contain reusable definitions and retain the existing root policy.
|
||||
dir := t.TempDir()
|
||||
writeFile(t, filepath.Join(dir, "base.api"), "type Base {\n Name string `json:\"name\"`\n}\n")
|
||||
writeFile(t, filepath.Join(dir, "service.api"), "import \"base.api\"\ntype Req {\n Base\n}\nservice demo{\n@handler post\npost / (Req)\n}")
|
||||
if _, err := FormatProject(filepath.Join(dir, "service.api"), false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := FormatProject(filepath.Join(dir, "service.api"), true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepositoryGenerationOptions(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "app")
|
||||
if _, err := New(Options{Dir: dir, Module: "example.com/repository", FrameworkDir: frameworkRoot(t)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "internal/repository")); !os.IsNotExist(err) {
|
||||
t.Fatal("default creates repository")
|
||||
}
|
||||
o := Options{Dir: dir, API: filepath.Join(dir, "api/service.api")}
|
||||
writeFile(t, filepath.Join(dir, generatorConfigFile), `{"repository":"group","middleware":"factories"}`)
|
||||
if _, err := Generate(o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repo := filepath.Join(dir, "internal/repository/greet/repository.go")
|
||||
original := readFile(t, repo) + "\n// business SQL belongs here\n"
|
||||
writeFile(t, repo, original)
|
||||
writeFile(t, filepath.Join(dir, generatorConfigFile), `{"repository":"none","middleware":"factories"}`)
|
||||
if _, err := Generate(o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
o.Check = true
|
||||
if _, err := Generate(o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if readFile(t, repo) != original {
|
||||
t.Fatal("repository removed or overwritten")
|
||||
}
|
||||
for _, raw := range []string{`{"repository":"oops"}`, `{"middleware":"oops"}`, `{"unknown":true}`, `{"repository":"none","repository":"group"}`} {
|
||||
writeFile(t, filepath.Join(dir, generatorConfigFile), raw)
|
||||
before := snapshot(t, dir)
|
||||
if _, err := Generate(o); err == nil {
|
||||
t.Fatal("accepted", raw)
|
||||
}
|
||||
if !sameFiles(before, snapshot(t, dir)) {
|
||||
t.Fatal("invalid config wrote files")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompositionExpansionBound(t *testing.T) {
|
||||
var source strings.Builder
|
||||
source.WriteString("type Base {\n Value string `json:\"value\"`\n}\n")
|
||||
last := "Base"
|
||||
for i := 0; i < 35; i++ {
|
||||
name := fmt.Sprintf("Level%d", i)
|
||||
fmt.Fprintf(&source, "type %s {\n %s\n}\n", name, last)
|
||||
last = name
|
||||
}
|
||||
// Reverse order forces traversal instead of hitting pre-expanded definitions.
|
||||
spec, err := parseText(t, source.String()+"service demo{\n@handler post\npost / ("+last+")\n}")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i, j := 0, len(spec.Types)-1; i < j; i, j = i+1, j-1 {
|
||||
spec.Types[i], spec.Types[j] = spec.Types[j], spec.Types[i]
|
||||
}
|
||||
if _, err = Compile(spec); err == nil {
|
||||
t.Fatal("unbounded composition accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerationConfigMissingAndLegacy(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "app")
|
||||
if _, err := New(Options{Dir: dir, Module: "example.com/config", FrameworkDir: frameworkRoot(t)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
o := Options{Dir: dir, API: filepath.Join(dir, "api/service.api")}
|
||||
if err := os.Remove(filepath.Join(dir, generatorConfigFile)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before := snapshot(t, dir)
|
||||
if _, err := Generate(o); err == nil || !strings.Contains(err.Error(), "restore") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !sameFiles(before, snapshot(t, dir)) {
|
||||
t.Fatal("missing config changed project")
|
||||
}
|
||||
// A genuine old manifest has never tracked a generator config.
|
||||
var old manifest
|
||||
if err := json.Unmarshal([]byte(readFile(t, filepath.Join(dir, manifestPath))), &old); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
old.GeneratorConfig = false
|
||||
raw, _ := json.Marshal(old)
|
||||
writeFile(t, filepath.Join(dir, manifestPath), string(raw))
|
||||
if _, err := Generate(o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg, exists, err := readGeneratorConfig(dir)
|
||||
if err != nil || !exists || cfg.Middleware != "handlers" || cfg.Repository != "none" {
|
||||
t.Fatal(cfg, exists, err)
|
||||
}
|
||||
if !strings.Contains(readFile(t, filepath.Join(dir, "internal/svc/runtimeGen.go")), "middleware.Defaults()") {
|
||||
t.Fatal("legacy registration replaced")
|
||||
}
|
||||
}
|
||||
245
cmd/go-omni/apigen/contractChanges.go
Normal file
245
cmd/go-omni/apigen/contractChanges.go
Normal file
@@ -0,0 +1,245 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
goparser "go/parser"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Diagnostic points to preserved project code that needs attention. Review
|
||||
// notices describe contract changes; errors describe definite source problems.
|
||||
type Diagnostic struct {
|
||||
Severity, Code, Path, Operation, Message string
|
||||
Line int
|
||||
Before, After string
|
||||
Details []string
|
||||
}
|
||||
|
||||
// Snapshots describe the last successfully generated contract, never business
|
||||
// source or absolute paths. They are persisted in the ownership transaction.
|
||||
type contractSnapshot struct {
|
||||
Routes map[string]routeContract `json:"routes"`
|
||||
}
|
||||
|
||||
type routeContract struct {
|
||||
Method string `json:"method"`
|
||||
Path string `json:"path"`
|
||||
Group string `json:"group"`
|
||||
Handler string `json:"handler"`
|
||||
Signature string `json:"signature"`
|
||||
Request string `json:"request,omitempty"`
|
||||
Response string `json:"response,omitempty"`
|
||||
BodyMode string `json:"bodyMode,omitempty"`
|
||||
Produces string `json:"produces,omitempty"`
|
||||
MaxBodyBytes int64 `json:"maxBodyBytes,omitempty"`
|
||||
Manual bool `json:"manual,omitempty"`
|
||||
CustomBinding bool `json:"customBinding,omitempty"`
|
||||
Types map[string]typeContract `json:"types,omitempty"`
|
||||
}
|
||||
|
||||
type typeContract struct {
|
||||
Fields []contractField `json:"fields,omitempty"`
|
||||
Scalar string `json:"scalar,omitempty"`
|
||||
}
|
||||
|
||||
type contractField struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Tag string `json:"tag"`
|
||||
}
|
||||
|
||||
func snapshotContract(spec *Spec) *contractSnapshot {
|
||||
definitions := map[string]typeContract{}
|
||||
for _, t := range spec.Types {
|
||||
d := typeContract{}
|
||||
for _, f := range t.Fields {
|
||||
d.Fields = append(d.Fields, contractField{f.Name, f.Type, f.Tag})
|
||||
}
|
||||
definitions[t.Name] = d
|
||||
}
|
||||
for _, s := range spec.Scalars {
|
||||
definitions[s.Name] = typeContract{Scalar: fmt.Sprintf("%s.%s (jsonInput=%s, jsonOutput=%s, text=%t)", s.GoImport, s.GoType, s.JSONInput, s.JSONOutput, s.Text)}
|
||||
}
|
||||
snapshot := &contractSnapshot{Routes: map[string]routeContract{}}
|
||||
for _, r := range spec.Routes {
|
||||
dependencies := map[string]typeContract{}
|
||||
var visit func(string)
|
||||
visit = func(name string) {
|
||||
d, ok := definitions[name]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if _, exists := dependencies[name]; exists {
|
||||
return
|
||||
}
|
||||
dependencies[name] = d
|
||||
for _, f := range d.Fields {
|
||||
expr, err := goparser.ParseExpr(f.Type)
|
||||
if err != nil {
|
||||
continue
|
||||
} // Compile already validates expressions.
|
||||
ast.Inspect(expr, func(n ast.Node) bool {
|
||||
if id, ok := n.(*ast.Ident); ok {
|
||||
visit(id.Name)
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
}
|
||||
visit(r.Request)
|
||||
_, responseName := responseShape(r.Response)
|
||||
visit(responseName)
|
||||
op := routeContract{
|
||||
Method: strings.ToUpper(r.Method), Path: r.Path, Group: r.Group, Handler: r.Handler,
|
||||
Request: r.Request, Response: r.Response, BodyMode: r.BodyMode, Produces: r.Produces,
|
||||
MaxBodyBytes: r.MaxBodyBytes, Manual: r.Manual, CustomBinding: r.CustomBinding, Types: dependencies,
|
||||
}
|
||||
if !r.Manual {
|
||||
op.Signature = expectedLogicSignature(r)
|
||||
}
|
||||
snapshot.Routes[r.Group+"/"+lower(r.Handler)] = op
|
||||
}
|
||||
return snapshot
|
||||
}
|
||||
|
||||
func (r routeContract) file() string {
|
||||
if r.Manual {
|
||||
return "internal/handler/" + r.Group + "/" + lower(r.Handler) + "Manual.go"
|
||||
}
|
||||
return "internal/logic/" + r.Group + "/" + lower(r.Handler) + ".go"
|
||||
}
|
||||
|
||||
func (r routeContract) operation() string { return r.Method + " " + r.Path }
|
||||
|
||||
func compareContracts(old, next *contractSnapshot) []Diagnostic {
|
||||
if old == nil {
|
||||
return nil
|
||||
}
|
||||
var diagnostics []Diagnostic
|
||||
for key, previous := range old.Routes {
|
||||
current, exists := next.Routes[key]
|
||||
if !exists {
|
||||
diagnostics = append(diagnostics, Diagnostic{Severity: "review", Code: "routeRemoved", Path: previous.file(), Operation: previous.operation(), Message: "route removed or moved; its handwritten implementation is preserved; review obsolete code and types"})
|
||||
continue
|
||||
}
|
||||
d := Diagnostic{Severity: "review", Code: "contractChanged", Path: current.file(), Operation: current.operation(), Message: "contract changed; review the preserved implementation"}
|
||||
if previous.Signature != current.Signature {
|
||||
d.Before, d.After = previous.Signature, current.Signature
|
||||
d.Details = append(d.Details, "Logic call signature changed")
|
||||
}
|
||||
if previous.Method != current.Method || previous.Path != current.Path {
|
||||
d.Details = append(d.Details, previous.operation()+" -> "+current.operation())
|
||||
}
|
||||
if previous.Manual != current.Manual || previous.CustomBinding != current.CustomBinding || previous.BodyMode != current.BodyMode || previous.MaxBodyBytes != current.MaxBodyBytes || previous.Produces != current.Produces {
|
||||
d.Details = append(d.Details, fmt.Sprintf("HTTP access: manual=%t customBinding=%t body=%s maxBytes=%d produces=%q -> manual=%t customBinding=%t body=%s maxBytes=%d produces=%q", previous.Manual, previous.CustomBinding, previous.BodyMode, previous.MaxBodyBytes, previous.Produces, current.Manual, current.CustomBinding, current.BodyMode, current.MaxBodyBytes, current.Produces))
|
||||
}
|
||||
d.Details = append(d.Details, changedTypes(previous.Types, current.Types)...)
|
||||
if len(d.Details) > 0 {
|
||||
diagnostics = append(diagnostics, d)
|
||||
}
|
||||
}
|
||||
return diagnostics
|
||||
}
|
||||
|
||||
func changedTypes(old, next map[string]typeContract) []string {
|
||||
names := map[string]bool{}
|
||||
for name := range old {
|
||||
names[name] = true
|
||||
}
|
||||
for name := range next {
|
||||
names[name] = true
|
||||
}
|
||||
var details []string
|
||||
for name := range names {
|
||||
a, existed := old[name]
|
||||
b, exists := next[name]
|
||||
switch {
|
||||
case !existed:
|
||||
details = append(details, "DTO dependency added: "+name)
|
||||
case !exists:
|
||||
details = append(details, "DTO dependency removed: "+name)
|
||||
case a.Scalar != b.Scalar:
|
||||
details = append(details, fmt.Sprintf("scalar %s: %s -> %s", name, a.Scalar, b.Scalar))
|
||||
case !reflect.DeepEqual(a.Fields, b.Fields):
|
||||
oldFields, newFields := map[string]contractField{}, map[string]contractField{}
|
||||
for _, f := range a.Fields {
|
||||
oldFields[f.Name] = f
|
||||
}
|
||||
for _, f := range b.Fields {
|
||||
newFields[f.Name] = f
|
||||
}
|
||||
before := len(details)
|
||||
for field, f := range oldFields {
|
||||
if n, ok := newFields[field]; !ok {
|
||||
details = append(details, fmt.Sprintf("DTO %s.%s removed", name, field))
|
||||
} else if f != n {
|
||||
details = append(details, fmt.Sprintf("DTO %s.%s: %s `%s` -> %s `%s`", name, field, f.Type, f.Tag, n.Type, n.Tag))
|
||||
}
|
||||
}
|
||||
for field := range newFields {
|
||||
if _, ok := oldFields[field]; !ok {
|
||||
details = append(details, fmt.Sprintf("DTO %s.%s added", name, field))
|
||||
}
|
||||
}
|
||||
if len(details) == before {
|
||||
details = append(details, "DTO "+name+" field order changed; review positional struct literals")
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Strings(details)
|
||||
return details
|
||||
}
|
||||
|
||||
func sortDiagnostics(diagnostics []Diagnostic) {
|
||||
sort.Slice(diagnostics, func(i, j int) bool {
|
||||
a, b := diagnostics[i], diagnostics[j]
|
||||
if a.Path != b.Path {
|
||||
return a.Path < b.Path
|
||||
}
|
||||
if a.Line != b.Line {
|
||||
return a.Line < b.Line
|
||||
}
|
||||
if a.Code != b.Code {
|
||||
return a.Code < b.Code
|
||||
}
|
||||
return a.Operation < b.Operation
|
||||
})
|
||||
}
|
||||
|
||||
func diagnosticErrors(diagnostics []Diagnostic) int {
|
||||
count := 0
|
||||
for _, d := range diagnostics {
|
||||
if d.Severity == "error" {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func expectedLogicSignature(r Route) string {
|
||||
params := []string{"context.Context"}
|
||||
if r.Request != "" {
|
||||
params = append(params, "*types."+r.Request)
|
||||
}
|
||||
if streamsBody(r) {
|
||||
params = append(params, "*httpx.Body")
|
||||
}
|
||||
results := []string{"error"}
|
||||
if r.Produces != "" {
|
||||
results = []string{"*httpx.Stream", "error"}
|
||||
} else if result := responseGoType(r.Response); result != "" {
|
||||
results = []string{result, "error"}
|
||||
}
|
||||
return displaySignature(upper(r.Handler), params, results)
|
||||
}
|
||||
|
||||
func displaySignature(name string, params, results []string) string {
|
||||
result := strings.Join(results, ", ")
|
||||
if len(results) != 1 {
|
||||
result = "(" + result + ")"
|
||||
}
|
||||
return name + "(" + strings.Join(params, ", ") + ") " + result
|
||||
}
|
||||
308
cmd/go-omni/apigen/contractChanges_test.go
Normal file
308
cmd/go-omni/apigen/contractChanges_test.go
Normal file
@@ -0,0 +1,308 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func findDiagnostic(t *testing.T, out Output, code string) Diagnostic {
|
||||
t.Helper()
|
||||
for _, d := range out.Diagnostics {
|
||||
if d.Code == code {
|
||||
return d
|
||||
}
|
||||
}
|
||||
t.Fatalf("missing %s diagnostic: %+v", code, out.Diagnostics)
|
||||
return Diagnostic{}
|
||||
}
|
||||
|
||||
func TestContractChangesPersistUntilLogicFixed(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "app")
|
||||
out, err := New(Options{Dir: dir, Module: "example.com/changes", FrameworkDir: frameworkRoot(t)})
|
||||
if err != nil || len(out.Diagnostics) > 0 {
|
||||
t.Fatalf("initial generation: %v %+v", err, out.Diagnostics)
|
||||
}
|
||||
o := Options{Dir: dir, API: filepath.Join(dir, "api/service.api")}
|
||||
logicPath := filepath.Join(dir, "internal/logic/greet/greet.go")
|
||||
logicBefore := readFile(t, logicPath)
|
||||
writeFile(t, o.API, strings.ReplaceAll(readFile(t, o.API), "GreetReq", "HelloReq"))
|
||||
before := snapshot(t, dir)
|
||||
for _, dryRun := range []bool{true, false} {
|
||||
o.DryRun, o.Check = dryRun, !dryRun
|
||||
out, err = Generate(o)
|
||||
if dryRun && err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !dryRun && err == nil {
|
||||
t.Fatal("check accepted stale project")
|
||||
}
|
||||
d := findDiagnostic(t, out, "logicSignature")
|
||||
if d.Line <= 0 || d.Path != "internal/logic/greet/greet.go" || !strings.Contains(d.After, "*types.HelloReq") || !strings.Contains(d.Before, "*types.GreetReq") {
|
||||
t.Fatalf("imprecise diagnosis: %+v", d)
|
||||
}
|
||||
findDiagnostic(t, out, "contractChanged")
|
||||
if !sameFiles(before, snapshot(t, dir)) {
|
||||
t.Fatal("preview/check changed files")
|
||||
}
|
||||
}
|
||||
o.DryRun, o.Check = false, false
|
||||
out, err = Generate(o)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
findDiagnostic(t, out, "logicSignature")
|
||||
if readFile(t, logicPath) != logicBefore {
|
||||
t.Fatal("business source overwritten")
|
||||
}
|
||||
o.Check = true
|
||||
out, err = Generate(o)
|
||||
if err == nil || strings.Contains(err.Error(), "stale") || len(out.Changes) != 0 {
|
||||
t.Fatalf("signature drift must remain after generation: %v %+v", err, out.Changes)
|
||||
}
|
||||
findDiagnostic(t, out, "logicSignature")
|
||||
// Method location and import/parameter names are project-owned. Find the
|
||||
// method in a different file, honoring aliases and named result variables.
|
||||
writeFile(t, logicPath, "package greet\n// business split into visitor.go\n")
|
||||
writeFile(t, filepath.Join(dir, "internal/logic/greet/visitor.go"), `package greet
|
||||
import (ctx "context"; dto "example.com/changes/internal/types")
|
||||
func(s *Logic)Greet(input ctx.Context, request *dto.HelloReq)(output *dto.GreetResp, problem error){
|
||||
return &dto.GreetResp{Message:request.Name},nil
|
||||
}
|
||||
`)
|
||||
out, err = Generate(o)
|
||||
if err != nil || len(out.Diagnostics) > 0 {
|
||||
t.Fatalf("compatible business method rejected: %v %+v", err, out.Diagnostics)
|
||||
}
|
||||
before = snapshot(t, dir)
|
||||
o.Check = false
|
||||
if _, err = Generate(o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !sameFiles(before, snapshot(t, dir)) {
|
||||
t.Fatal("stable generation changed files")
|
||||
}
|
||||
// Field review notices should point to the actual moved business method.
|
||||
writeFile(t, o.API, strings.Replace(readFile(t, o.API), `max=80`, `max=90`, 1))
|
||||
o.DryRun = true
|
||||
out, err = Generate(o)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d := findDiagnostic(t, out, "contractChanged")
|
||||
if d.Path != "internal/logic/greet/visitor.go" || d.Line == 0 {
|
||||
t.Fatalf("wrong implementation location: %+v", d)
|
||||
}
|
||||
o.DryRun = false
|
||||
if _, err = Generate(o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "go", "test", "-mod=mod", "./...")
|
||||
cmd.Dir = dir
|
||||
cmd.Env = append(os.Environ(), "GOWORK=off")
|
||||
if data, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("fixed generated consumer: %v\n%s", err, data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContractChangesReachNestedDTOs(t *testing.T) {
|
||||
source := `syntax = "v1"
|
||||
type Address { Street string ` + "`json:\"street\"`" + ` }
|
||||
type Request { Address Address ` + "`json:\"address\"`" + ` }
|
||||
type Reply { Accepted bool ` + "`json:\"accepted\"`" + ` }
|
||||
@server(group: orders)
|
||||
service orders-api {
|
||||
@handler create
|
||||
post /orders (Request) returns (Reply)
|
||||
@handler ping
|
||||
get /ping
|
||||
}
|
||||
`
|
||||
root := t.TempDir()
|
||||
api := filepath.Join(root, "source/service.api")
|
||||
writeFile(t, api, source)
|
||||
dir := filepath.Join(root, "app")
|
||||
if _, err := Generate(Options{API: api, Dir: dir, Module: "example.com/nested", FrameworkDir: frameworkRoot(t)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
o := Options{API: filepath.Join(dir, "api/service.api"), Dir: dir, DryRun: true}
|
||||
writeFile(t, o.API, strings.Replace(source, "Street string", "Street int64", 1))
|
||||
out, err := Generate(o)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(out.Diagnostics) != 1 {
|
||||
t.Fatalf("unrelated routes should not be warned: %+v", out.Diagnostics)
|
||||
}
|
||||
d := findDiagnostic(t, out, "contractChanged")
|
||||
if d.Path != "internal/logic/orders/create.go" || !strings.Contains(strings.Join(d.Details, "\n"), "Address.Street: string") {
|
||||
t.Fatalf("nested field not explained: %+v", d)
|
||||
}
|
||||
// Import/file/comment/formatting changes are not semantic DTO changes.
|
||||
writeFile(t, o.API, source+"\n// a harmless documentation edit\n")
|
||||
out, err = Generate(o)
|
||||
if err != nil || len(out.Diagnostics) > 0 {
|
||||
t.Fatalf("comment caused false diagnostic: %v %+v", err, out.Diagnostics)
|
||||
}
|
||||
// Removal preserves the old implementation and explicitly points it out.
|
||||
writeFile(t, o.API, strings.Replace(source, " @handler create\n post /orders (Request) returns (Reply)\n", "", 1))
|
||||
out, err = Generate(o)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d = findDiagnostic(t, out, "routeRemoved")
|
||||
if d.Path != "internal/logic/orders/create.go" {
|
||||
t.Fatal(d)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContractSnapshotSemanticDetails(t *testing.T) {
|
||||
base := &Spec{Types: []Type{{Name: "Req", Fields: []Field{{Name: "ID", Type: "ID", Tag: `form:"id"`}}}}, Scalars: []ScalarDefinition{{Name: "ID", GoImport: "example.com/id", GoType: "Value", JSONInput: "string", JSONOutput: "string"}}, Routes: []Route{{Group: "files", Handler: "send", Method: "post", Path: "/file", Request: "Req", BodyMode: "raw", MaxBodyBytes: 64, Produces: "text/plain"}}}
|
||||
old := snapshotContract(base)
|
||||
base.Scalars[0].Text = true
|
||||
base.Routes[0].BodyMode = "multipart"
|
||||
base.Routes[0].Produces = "application/octet-stream"
|
||||
base.Routes[0].MaxBodyBytes = 128
|
||||
base.Routes[0].Path = "/files"
|
||||
diagnostics := compareContracts(old, snapshotContract(base))
|
||||
if len(diagnostics) != 1 {
|
||||
t.Fatal(diagnostics)
|
||||
}
|
||||
details := strings.Join(diagnostics[0].Details, "\n")
|
||||
for _, want := range []string{"scalar ID", "body=raw", "body=multipart", "POST /file -> POST /files", "maxBytes=128"} {
|
||||
if !strings.Contains(details, want) {
|
||||
t.Fatal("missing", want, details)
|
||||
}
|
||||
}
|
||||
base.Routes[0].Produces = ""
|
||||
diagnostics = compareContracts(old, snapshotContract(base))
|
||||
if !strings.Contains(diagnostics[0].Before, "*httpx.Stream") || !strings.HasSuffix(diagnostics[0].After, ") error") {
|
||||
t.Fatal(diagnostics)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogicDiagnosticsSourceBoundaries(t *testing.T) {
|
||||
for _, tc := range []struct{ name, source, code, severity string }{
|
||||
{"wrongResult", `package greet; import "context"; func(s *Logic)Greet(context.Context) error{return nil}`, "logicSignature", "error"},
|
||||
{"syntax", `package greet; func(s *Logic)Greet( {`, "logicSyntax", "error"},
|
||||
{"alias", `package greet; import("context";"example.com/test/internal/types");type Input = types.GreetReq; func(s *Logic)Greet(context.Context,*Input)(*types.GreetResp,error){return nil,nil}`, "logicUnchecked", "review"},
|
||||
{"conditional", "//go:build projectTag\n\npackage greet\n", "logicUnchecked", "review"},
|
||||
{"missing", "package greet\n", "logicNotFound", "review"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "app")
|
||||
if _, err := New(Options{Dir: dir, Module: "example.com/test", FrameworkDir: frameworkRoot(t)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeFile(t, filepath.Join(dir, "internal/logic/greet/greet.go"), tc.source)
|
||||
before := snapshot(t, dir)
|
||||
out, err := Generate(Options{Dir: dir, API: filepath.Join(dir, "api/service.api"), Check: true})
|
||||
d := findDiagnostic(t, out, tc.code)
|
||||
if d.Severity != tc.severity || (tc.severity == "error") != (err != nil) {
|
||||
t.Fatalf("unexpected check result: %v %+v", err, d)
|
||||
}
|
||||
if !sameFiles(before, snapshot(t, dir)) {
|
||||
t.Fatal("analysis modified business source")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestContractSnapshotBootstrapAndDeterminism(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "app")
|
||||
if _, err := New(Options{Dir: dir, Module: "example.com/test", FrameworkDir: frameworkRoot(t)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
path := filepath.Join(dir, manifestPath)
|
||||
var previous manifest
|
||||
if err := json.Unmarshal([]byte(readFile(t, path)), &previous); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
previous.Contract = nil
|
||||
data, _ := json.MarshalIndent(previous, "", " ")
|
||||
writeFile(t, path, string(data)+"\n")
|
||||
o := Options{Dir: dir, API: filepath.Join(dir, "api/service.api"), DryRun: true}
|
||||
a, err := Generate(o)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b, err := Generate(o)
|
||||
if err != nil || !reflect.DeepEqual(a, b) {
|
||||
t.Fatal("non-deterministic preview", err)
|
||||
}
|
||||
findDiagnostic(t, a, "contractBaseline")
|
||||
o.DryRun = false
|
||||
if _, err = Generate(o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
o.Check = true
|
||||
out, err := Generate(o)
|
||||
if err != nil || len(out.Diagnostics) > 0 {
|
||||
t.Fatal(err, out.Diagnostics)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogicResponseAndBodySignatureChanges(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, after string
|
||||
change func(string) string
|
||||
}{
|
||||
{"responseType", "*types.HelloResp", func(s string) string { return strings.ReplaceAll(s, "GreetResp", "HelloResp") }},
|
||||
{"bodyParameter", "*httpx.Body", func(s string) string {
|
||||
return strings.Replace(s, " get /greet", " @body (mode: raw; maxBytes: 64)\n post /greet", 1)
|
||||
}},
|
||||
{"streamResult", "*httpx.Stream", func(s string) string {
|
||||
return strings.Replace(strings.Replace(s, " returns (GreetResp)", "", 1), " get /greet", " @produces \"text/plain\"\n get /greet", 1)
|
||||
}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "app")
|
||||
if _, err := New(Options{Dir: dir, Module: "example.com/test", FrameworkDir: frameworkRoot(t)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
o := Options{Dir: dir, API: filepath.Join(dir, "api/service.api"), DryRun: true}
|
||||
writeFile(t, o.API, tc.change(readFile(t, o.API)))
|
||||
out, err := Generate(o)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d := findDiagnostic(t, out, "logicSignature")
|
||||
if !strings.Contains(d.After, tc.after) {
|
||||
t.Fatal(d)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogicDuplicateAndTestFiles(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "app")
|
||||
if _, err := New(Options{Dir: dir, Module: "example.com/test", FrameworkDir: frameworkRoot(t)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
source := readFile(t, filepath.Join(dir, "internal/logic/greet/greet.go"))
|
||||
// String literals mentioning build constraints do not make a file conditional.
|
||||
writeFile(t, filepath.Join(dir, "internal/logic/greet/greet.go"), source+"\nconst diagnosticText=\"//go:build special\"\n")
|
||||
writeFile(t, filepath.Join(dir, "internal/logic/greet/fixture_test.go"), source)
|
||||
o := Options{Dir: dir, API: filepath.Join(dir, "api/service.api"), Check: true}
|
||||
out, err := Generate(o)
|
||||
if err != nil || len(out.Diagnostics) > 0 {
|
||||
t.Fatal(err, out.Diagnostics)
|
||||
}
|
||||
writeFile(t, filepath.Join(dir, "internal/logic/greet/duplicate.go"), source)
|
||||
out, err = Generate(o)
|
||||
if err == nil {
|
||||
t.Fatal("duplicate method passed check")
|
||||
}
|
||||
d := findDiagnostic(t, out, "logicDuplicate")
|
||||
if len(d.Details) != 2 || !strings.Contains(d.Details[0], "duplicate.go:") {
|
||||
t.Fatal(d)
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package apigen
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.yuntuo.hk/k2safe/go-omni/contract"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"sort"
|
||||
@@ -53,6 +54,7 @@ func prepareMetadata(original *Spec) (*Spec, error) {
|
||||
normalized := registry.Catalog()
|
||||
for i := range spec.Errors {
|
||||
spec.Errors[i].BusinessCode = normalized.Definitions[spec.Errors[i].Code].Code
|
||||
spec.Errors[i].Params = normalized.Definitions[spec.Errors[i].Code].Params
|
||||
}
|
||||
if err := validateErrorPolicies(&spec); err != nil {
|
||||
return nil, err
|
||||
@@ -118,7 +120,11 @@ func prepareMetadata(original *Spec) (*Spec, error) {
|
||||
return &spec, nil
|
||||
}
|
||||
|
||||
func decorateDocument(doc map[string]any, spec *Spec) {
|
||||
func decorateDocument(doc map[string]any, spec *Spec, bindings map[string]*contract.Binder) {
|
||||
doc["x-omni-contract-sources"] = sourceMetadata(spec)
|
||||
if spec.DefaultResponse != "" {
|
||||
doc["x-omni-default-response"] = spec.DefaultResponse
|
||||
}
|
||||
if spec.Description != "" {
|
||||
doc["info"].(map[string]any)["description"] = spec.Description
|
||||
}
|
||||
@@ -140,6 +146,12 @@ func decorateDocument(doc map[string]any, spec *Spec) {
|
||||
}
|
||||
doc["components"].(map[string]any)["securitySchemes"] = schemes
|
||||
definitions := map[string]ErrorDefinition{}
|
||||
declaredRegistry := apierrors.MustRegistry(errorCatalog(spec, nil))
|
||||
var documentRegistry *apierrors.Registry
|
||||
if spec.DocumentCatalog != nil {
|
||||
documentRegistry = apierrors.MustRegistry(*spec.DocumentCatalog)
|
||||
doc["x-omni-app-error-catalog"] = documentRegistry.Catalog()
|
||||
}
|
||||
for _, e := range spec.Errors {
|
||||
definitions[e.Name] = e
|
||||
}
|
||||
@@ -147,6 +159,7 @@ func decorateDocument(doc map[string]any, spec *Spec) {
|
||||
doc["x-omni-errors"] = spec.Errors
|
||||
}
|
||||
for _, r := range spec.Routes {
|
||||
registry := declaredRegistry
|
||||
policy := response.MustNew(responseConfig(spec, r))
|
||||
cfg := policy.Config()
|
||||
parts := strings.Split(r.Path, "/")
|
||||
@@ -156,7 +169,16 @@ func decorateDocument(doc map[string]any, spec *Spec) {
|
||||
}
|
||||
}
|
||||
op := doc["paths"].(map[string]any)[strings.Join(parts, "/")].(map[string]any)[r.Method].(map[string]any)
|
||||
decorateRequestOptions(op, r)
|
||||
requestRoute := r
|
||||
requestRoute.RequestPolicy = contract.MergePolicy(spec.RequestPolicy, r.RequestPolicy)
|
||||
decorateRequestOptions(op, requestRoute)
|
||||
if b := bindings[r.Request]; b != nil && b.HasMultipart() {
|
||||
encoding := map[string]any{}
|
||||
for _, name := range b.MultipartJSONFields() {
|
||||
encoding[name] = map[string]any{"contentType": "application/json"}
|
||||
}
|
||||
op["requestBody"] = map[string]any{"required": true, "content": map[string]any{"multipart/form-data": map[string]any{"schema": b.MultipartSchema(), "encoding": encoding}}}
|
||||
}
|
||||
if r.ResponseFormat != "" {
|
||||
op["x-omni-response"] = r.ResponseFormat
|
||||
}
|
||||
@@ -188,6 +210,13 @@ func decorateDocument(doc map[string]any, spec *Spec) {
|
||||
e := definitions[name]
|
||||
grouped[e.Status] = append(grouped[e.Status], e)
|
||||
}
|
||||
if len(r.Errors) == 0 && documentRegistry != nil {
|
||||
registry = documentRegistry
|
||||
op["x-omni-error-catalog-scope"] = "app"
|
||||
for key, definition := range documentRouteCatalog(spec, r).Definitions {
|
||||
grouped[definition.Status] = append(grouped[definition.Status], ErrorDefinition{Name: key, Code: key, Status: definition.Status, Message: definition.Message, BusinessCode: definition.Code, Params: definition.Params})
|
||||
}
|
||||
}
|
||||
if len(r.Errors) > 0 {
|
||||
op["x-omni-errors"] = r.Errors
|
||||
}
|
||||
@@ -225,8 +254,10 @@ func decorateDocument(doc map[string]any, spec *Spec) {
|
||||
}
|
||||
sort.Slice(errs, func(i, j int) bool { return errs[i].Name < errs[j].Name })
|
||||
for _, e := range errs {
|
||||
value := policy.Value(responseResult(e))
|
||||
examples[e.Name] = map[string]any{"summary": e.Message, "value": value}
|
||||
result := responseResult(e)
|
||||
result.Message = registry.Example(e.Code).Message
|
||||
value := policy.Value(result)
|
||||
examples[e.Name] = map[string]any{"summary": result.Message, "value": value}
|
||||
codeKey := fmt.Sprint(value[cfg.CodeField])
|
||||
if !seenCodes[codeKey] {
|
||||
codes = append(codes, value[cfg.CodeField])
|
||||
|
||||
@@ -4,18 +4,21 @@ import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
||||
)
|
||||
|
||||
// ErrorDefinition declares a public, stable error. Generated constructors return
|
||||
// a fresh error each time so WithCause cannot mutate a shared singleton.
|
||||
type ErrorDefinition struct {
|
||||
Name string `json:"name"`
|
||||
BusinessCode any `json:"businessCode,omitempty"`
|
||||
Catalog string `json:"catalog,omitempty"`
|
||||
Status int `json:"status"`
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Position string `json:"-"`
|
||||
Name string `json:"name"`
|
||||
BusinessCode any `json:"businessCode,omitempty"`
|
||||
Params []apierrors.Parameter `json:"params,omitempty"`
|
||||
Catalog string `json:"catalog,omitempty"`
|
||||
Status int `json:"status"`
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Position string `json:"-"`
|
||||
}
|
||||
type AuthDefinition struct {
|
||||
Name, Type, In, KeyName, Middleware, Description, BearerFormat, Position string
|
||||
|
||||
86
cmd/go-omni/apigen/documentRefs.go
Normal file
86
cmd/go-omni/apigen/documentRefs.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package apigen
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
// compactDocument only replaces standard OpenAPI Reference Object positions.
|
||||
// Payloads (including examples containing a literal "$ref") are never walked.
|
||||
// Mixed HTTP 200 responses retain both success and error alternatives.
|
||||
func compactDocument(doc map[string]any) {
|
||||
type slot struct {
|
||||
parent map[string]any
|
||||
key string
|
||||
}
|
||||
components := doc["components"].(map[string]any)
|
||||
var responses, schemas, examples []slot
|
||||
for _, path := range doc["paths"].(map[string]any) {
|
||||
for _, value := range path.(map[string]any) {
|
||||
op, ok := value.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
rs, _ := op["responses"].(map[string]any)
|
||||
for status := range rs {
|
||||
responses = append(responses, slot{rs, status})
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
factor := func(kind string, slots []slot) {
|
||||
counts := map[string]int{}
|
||||
for _, s := range slots {
|
||||
raw, _ := json.Marshal(s.parent[s.key])
|
||||
counts[string(raw)]++
|
||||
}
|
||||
shared, _ := components[kind].(map[string]any)
|
||||
if shared == nil {
|
||||
shared = map[string]any{}
|
||||
}
|
||||
for _, s := range slots {
|
||||
raw, _ := json.Marshal(s.parent[s.key])
|
||||
count := counts[string(raw)]
|
||||
if count < 2 {
|
||||
continue
|
||||
}
|
||||
name := "Omni_" + digest(raw)
|
||||
ref := map[string]any{"$ref": "#/components/" + kind + "/" + name}
|
||||
refJSON, _ := json.Marshal(ref)
|
||||
// Keep small objects inline if components would increase size.
|
||||
if (count-1)*len(raw) <= count*len(refJSON)+len(name)+4 {
|
||||
continue
|
||||
}
|
||||
shared[name] = s.parent[s.key]
|
||||
s.parent[s.key] = map[string]any{"$ref": "#/components/" + kind + "/" + name}
|
||||
}
|
||||
if len(shared) > 0 {
|
||||
components[kind] = shared
|
||||
}
|
||||
}
|
||||
|
||||
// Share whole responses before counting inner objects. Otherwise a catalog
|
||||
// copied across 100 routes can create hundreds of unnecessary components
|
||||
// that only one shared response ultimately uses.
|
||||
factor("responses", responses)
|
||||
collectMedia := func(value any) {
|
||||
resp := value.(map[string]any)
|
||||
content, _ := resp["content"].(map[string]any)
|
||||
for _, value := range content {
|
||||
media := value.(map[string]any)
|
||||
if media["schema"] != nil {
|
||||
schemas = append(schemas, slot{media, "schema"})
|
||||
}
|
||||
es, _ := media["examples"].(map[string]any)
|
||||
for name := range es {
|
||||
examples = append(examples, slot{es, name})
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, s := range responses {
|
||||
collectMedia(s.parent[s.key])
|
||||
}
|
||||
sharedResponses, _ := components["responses"].(map[string]any)
|
||||
for _, resp := range sharedResponses {
|
||||
collectMedia(resp)
|
||||
}
|
||||
factor("examples", examples)
|
||||
factor("schemas", schemas)
|
||||
}
|
||||
107
cmd/go-omni/apigen/documentRefs_test.go
Normal file
107
cmd/go-omni/apigen/documentRefs_test.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSharedErrorDocumentReferences(t *testing.T) {
|
||||
source := `@app(defaultResponse: Common)
|
||||
response Common(codeType:int32;successCode:0;errorCode:9999;messageField:msg;dataMode:always)
|
||||
service demo {
|
||||
`
|
||||
for i := 0; i < 16; i++ {
|
||||
kind := "string"
|
||||
if i%2 == 0 {
|
||||
kind = "int64"
|
||||
}
|
||||
source += fmt.Sprintf("@handler list%d\nget /list%d returns ([]%s)\n", i, i, kind)
|
||||
}
|
||||
source += "}\n"
|
||||
spec, err := parseText(t, source)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
catalog := apierrors.Catalog{DefaultStatus: 400, Definitions: map[string]apierrors.Definition{}}
|
||||
for i := 0; i < 80; i++ {
|
||||
status := 400
|
||||
if i%2 == 0 {
|
||||
status = 200
|
||||
}
|
||||
catalog.Definitions[fmt.Sprintf("ERROR_%d", i)] = apierrors.Definition{Status: status, Code: 400000 + i, Message: strings.Repeat("说明", 40) + "{label}不能超过{max}个字符", Params: []apierrors.Parameter{{Name: "label", Type: "string", Example: "企业名称"}, {Name: "max", Type: "int", Example: 100}}}
|
||||
}
|
||||
spec.DocumentCatalog = &catalog
|
||||
compiled, err := Compile(spec)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var raw map[string]any
|
||||
if err = json.Unmarshal(compiled.Document, &raw); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
components := raw["components"].(map[string]any)
|
||||
for _, kind := range []string{"responses", "examples"} {
|
||||
if len(components[kind].(map[string]any)) == 0 {
|
||||
t.Fatal("missing shared", kind)
|
||||
}
|
||||
}
|
||||
expanded := document(t, compiled)
|
||||
for i := 0; i < 16; i++ {
|
||||
responses := operation(t, expanded, fmt.Sprintf("/list%d", i), "get")["responses"].(map[string]any)
|
||||
for _, status := range []string{"200", "400"} {
|
||||
media := responses[status].(map[string]any)["content"].(map[string]any)["application/json"].(map[string]any)
|
||||
if len(media["examples"].(map[string]any)) != 40 {
|
||||
t.Fatal("examples lost")
|
||||
}
|
||||
if status == "200" {
|
||||
choices := media["schema"].(map[string]any)["anyOf"].([]any)
|
||||
if len(choices) != 2 || choices[0].(map[string]any)["properties"].(map[string]any)["data"].(map[string]any)["type"] != "array" {
|
||||
t.Fatal("200 success shape lost")
|
||||
}
|
||||
}
|
||||
for _, example := range media["examples"].(map[string]any) {
|
||||
value := example.(map[string]any)["value"].(map[string]any)
|
||||
if _, ok := value["data"]; !ok || value["data"] != nil || !strings.Contains(value["msg"].(string), "企业名称不能超过100个字符") {
|
||||
t.Fatal(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
expandedBytes, _ := json.MarshalIndent(expanded, "", " ")
|
||||
if len(compiled.Document)*5 > len(expandedBytes) {
|
||||
t.Fatalf("poor dedup: %d vs %d", len(compiled.Document), len(expandedBytes))
|
||||
}
|
||||
again, err := Compile(spec)
|
||||
if err != nil || string(again.Document) != string(compiled.Document) {
|
||||
t.Fatal("non-deterministic document", err)
|
||||
}
|
||||
before, _ := json.Marshal(raw)
|
||||
compactDocument(raw)
|
||||
data, _ := json.Marshal(raw)
|
||||
if string(data) != string(before) {
|
||||
t.Fatal("compaction not idempotent")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSharedSchemasAndLiteralExampleReference(t *testing.T) {
|
||||
doc := map[string]any{"components": map[string]any{}, "paths": map[string]any{}}
|
||||
paths := doc["paths"].(map[string]any)
|
||||
for _, name := range []string{"first", "second", "third"} {
|
||||
schema := map[string]any{"type": "object", "description": strings.Repeat("schema description ", 40)}
|
||||
example := map[string]any{"summary": strings.Repeat("example ", 50), "value": map[string]any{"$ref": "literal business field"}}
|
||||
paths["/"+name] = map[string]any{"get": map[string]any{"responses": map[string]any{"200": map[string]any{"description": name, "content": map[string]any{"application/json": map[string]any{"schema": schema, "examples": map[string]any{"same": example}}}}}}}
|
||||
}
|
||||
compactDocument(doc)
|
||||
components := doc["components"].(map[string]any)
|
||||
if len(components["schemas"].(map[string]any)) != 1 {
|
||||
t.Fatal("schema not shared")
|
||||
}
|
||||
for _, example := range components["examples"].(map[string]any) {
|
||||
if example.(map[string]any)["value"].(map[string]any)["$ref"] != "literal business field" {
|
||||
t.Fatal("payload reference interpreted")
|
||||
}
|
||||
}
|
||||
}
|
||||
127
cmd/go-omni/apigen/documentation_test.go
Normal file
127
cmd/go-omni/apigen/documentation_test.go
Normal file
@@ -0,0 +1,127 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFieldDocumentationAndFormat(t *testing.T) {
|
||||
source := "// 项目说明\n type Req{\n// 状态\n State int `json:\"state,options=0|1\" example:\"1\" enumDescriptions:\"[\\\"禁用\\\",\\\"启用\\\"]\"` // 状态值\n /* 第一行\n 第二行 */\nName string `json:\"name\" example:\"企业名称\"`\n}\nservice demo{\n @handler echo\npost /echo (Req) returns (Req)\n}\n"
|
||||
formatted, err := Format([]byte(source))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
again, err := Format(formatted)
|
||||
if err != nil || !bytes.Equal(formatted, again) {
|
||||
t.Fatal("format is not idempotent", err, string(again))
|
||||
}
|
||||
if !bytes.Contains(formatted, []byte(" // 状态\n State")) {
|
||||
t.Fatal(string(formatted))
|
||||
}
|
||||
spec, err := parseText(t, string(formatted))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c, err := Compile(spec)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if spec.Types[0].Fields[0].Description != "状态\n状态值" || spec.Types[0].Fields[1].Description != "第一行\n第二行" {
|
||||
t.Fatalf("%+v", spec.Types[0].Fields)
|
||||
}
|
||||
schema, err := c.Bindings["Req"].ResponseSchema()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
state := schema.Properties["state"]
|
||||
if state.Description != "状态\n状态值" || len(state.EnumDescriptions) != 2 || state.Example != json.Number("1") {
|
||||
t.Fatalf("%+v", state)
|
||||
}
|
||||
dir := t.TempDir()
|
||||
entry := filepath.Join(dir, "demo.api")
|
||||
writeFile(t, entry, source)
|
||||
if _, err = FormatProject(entry, true); err == nil {
|
||||
t.Fatal("check accepted drift")
|
||||
}
|
||||
if readFile(t, entry) != source {
|
||||
t.Fatal("check wrote source")
|
||||
}
|
||||
if _, err = FormatProject(entry, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = FormatProject(entry, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
project := filepath.Join(t.TempDir(), "demo")
|
||||
if _, err = Generate(Options{API: entry, Dir: project, Module: "example.com/docs", FrameworkDir: frameworkRoot(t)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dto := readFile(t, filepath.Join(project, "internal/types/"+dtoFile("demo.api:1:1")))
|
||||
if !strings.Contains(dto, "// 状态值") || !strings.Contains(dto, `description:"状态\n状态值"`) {
|
||||
t.Fatal(dto)
|
||||
}
|
||||
// Invalid examples fail before project output is created.
|
||||
bad := strings.Replace(source, "example:\"1\"", "example:\"9\"", 1)
|
||||
writeFile(t, entry, bad)
|
||||
if _, err = FormatProject(entry, false); err == nil {
|
||||
t.Fatal("invalid enum example accepted")
|
||||
}
|
||||
if data, _ := os.ReadFile(entry); string(data) != bad {
|
||||
t.Fatal("invalid source changed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatAlignsFieldsAndPreservesTokens(t *testing.T) {
|
||||
source := []byte("type Req {\n Name string `json:\"name\"` // 名称\n Sequence int64 `json:\"sequence\"` // 编号\n}\n")
|
||||
out, err := Format(source)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
again, err := Format(out)
|
||||
if err != nil || !bytes.Equal(out, again) {
|
||||
t.Fatal("alignment not idempotent", err)
|
||||
}
|
||||
lines := strings.Split(string(out), "\n")
|
||||
if strings.Index(lines[1], "`") != strings.Index(lines[2], "`") || strings.Index(lines[1], "//") != strings.Index(lines[2], "//") {
|
||||
t.Fatal("fields not aligned", string(out))
|
||||
}
|
||||
for _, literal := range []string{"`json:\"name\"`", "`json:\"sequence\"`", "// 名称", "// 编号"} {
|
||||
if !bytes.Contains(out, []byte(literal)) {
|
||||
t.Fatal("literal changed", literal)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequestPolicyCannotCreateGETBody(t *testing.T) {
|
||||
for _, source := range []string{
|
||||
"service demo { @handler get\n @request(body: required)\n get /x }",
|
||||
"@app(request: {\"body\":\"optional\"})\n service demo { @handler get\n get /x }",
|
||||
} {
|
||||
spec, err := parseText(t, source)
|
||||
if err == nil {
|
||||
_, err = Compile(spec)
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatal("GET body policy accepted")
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestFormatLiteralPreservation(t *testing.T) {
|
||||
source := []byte("type Demo {\n Body string `json:\"body\"\ndescription:\"literal space\"`\n}\n")
|
||||
out, err := Format(source)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Contains(out, []byte("`json:\"body\"\ndescription:\"literal space\"`")) {
|
||||
t.Fatal(string(out))
|
||||
}
|
||||
for _, src := range []string{"type X {", "}", "info (title:\"unterminated)"} {
|
||||
if _, err := Format([]byte(src)); err == nil {
|
||||
t.Fatal("invalid input accepted")
|
||||
}
|
||||
}
|
||||
}
|
||||
60
cmd/go-omni/apigen/endpoints_test.go
Normal file
60
cmd/go-omni/apigen/endpoints_test.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestConfigurableBuiltinRoutesConsumer(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "endpoints")
|
||||
api := filepath.Join(t.TempDir(), "service.api")
|
||||
writeFile(t, api, "service healthApi {\n @handler customReady\n get /health/ready\n @handler customDoc\n get /openapi.json\n}\n")
|
||||
_, err := Generate(Options{Dir: dir, API: api, Module: "example.com/endpoints", FrameworkDir: frameworkRoot(t)})
|
||||
if err != nil {
|
||||
t.Fatal("built-in paths must be permitted in API contracts", err)
|
||||
}
|
||||
writeFile(t, filepath.Join(dir, "endpoints_test.go"), `package main
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"strings"
|
||||
"example.com/endpoints/internal/config"
|
||||
"example.com/endpoints/internal/svc"
|
||||
"example.com/endpoints/internal/handler"
|
||||
"git.yuntuo.hk/k2safe/go-omni/logx"
|
||||
)
|
||||
func TestProjectOwnsDisabledBuiltinPaths(t *testing.T){
|
||||
cfg:=config.Default();cfg.Server.Log.Writer=io.Discard
|
||||
logger,_:=logx.New(cfg.Server.Log);defer logger.Close()
|
||||
deps,err:=svc.New(context.Background(),logger,cfg);if err!=nil{t.Fatal(err)};defer deps.Close()
|
||||
if _,err=handler.NewApp(deps);err==nil || !strings.Contains(err.Error(),"overlaps business"){t.Fatal("expected preflight collision",err)}
|
||||
deps.Config.Server.Endpoints.Readiness.Enabled=false
|
||||
deps.Config.Server.Endpoints.OpenAPI.Enabled=false
|
||||
app,err:=handler.NewApp(deps);if err!=nil{t.Fatal(err)};defer app.Close()
|
||||
for _,path:=range []string{"/health/ready","/openapi.json"}{
|
||||
out:=httptest.NewRecorder();app.ServeHTTP(out,httptest.NewRequest("GET",path,nil))
|
||||
if out.Code!=501{t.Fatal("expected generated business Logic stub",path,out.Code,out.Body.String())}
|
||||
}
|
||||
deps.Config.Server.Endpoints.Readiness.Enabled=true
|
||||
deps.Config.Server.Endpoints.Readiness.Path="/readyz"
|
||||
deps.Config.Server.Endpoints.OpenAPI.Enabled=true
|
||||
deps.Config.Server.Endpoints.OpenAPI.Path="/spec.json"
|
||||
app2,err:=handler.NewApp(deps);if err!=nil{t.Fatal(err)};defer app2.Close()
|
||||
for _,path:=range []string{"/readyz","/spec.json"}{out:=httptest.NewRecorder();app2.ServeHTTP(out,httptest.NewRequest("GET",path,nil));if out.Code!=200{t.Fatal(path,out.Code)}}
|
||||
}
|
||||
`)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "go", "test", "-race", "-mod=mod", "-count=1", "./...")
|
||||
cmd.Dir = dir
|
||||
cmd.Env = append(os.Environ(), "GOWORK=off")
|
||||
if output, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatal(err, string(output))
|
||||
}
|
||||
}
|
||||
@@ -49,10 +49,11 @@ func (p *parser) parseErrorCatalog() error {
|
||||
}
|
||||
}
|
||||
type entry struct {
|
||||
Key string `json:"key"`
|
||||
Code any `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Status int `json:"status"`
|
||||
Key string `json:"key"`
|
||||
Code any `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Status int `json:"status"`
|
||||
Params []apierrors.Parameter `json:"params"`
|
||||
}
|
||||
var entries map[string]entry
|
||||
if err := apierrors.DecodeCatalogJSON([]byte(props["errors"]), &entries); err != nil {
|
||||
@@ -74,7 +75,7 @@ func (p *parser) parseErrorCatalog() error {
|
||||
if e.Status == 0 {
|
||||
e.Status = defaultStatus
|
||||
}
|
||||
p.spec.Errors = append(p.spec.Errors, ErrorDefinition{Name: fn, Status: e.Status, Code: e.Key, Message: e.Message, BusinessCode: e.Code, Catalog: name, Position: pos})
|
||||
p.spec.Errors = append(p.spec.Errors, ErrorDefinition{Name: fn, Status: e.Status, Code: e.Key, Message: e.Message, BusinessCode: e.Code, Params: e.Params, Catalog: name, Position: pos})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -89,7 +90,7 @@ func errorCatalog(spec *Spec, names []string) apierrors.Catalog {
|
||||
if names != nil && !selected[e.Name] {
|
||||
continue
|
||||
}
|
||||
c.Definitions[e.Code] = apierrors.Definition{Status: e.Status, Code: e.BusinessCode, Message: e.Message}
|
||||
c.Definitions[e.Code] = apierrors.Definition{Status: e.Status, Code: e.BusinessCode, Message: e.Message, Params: e.Params}
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -84,12 +84,17 @@ func TestErrorCatalogDocumentAndValidation(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestErrorCatalogConsumer(t *testing.T) {
|
||||
testCatalogConsumer(t, "errors.api", "errorCatalog")
|
||||
}
|
||||
|
||||
func testCatalogConsumer(t *testing.T, apiFile, fixtureName string, extraFiles ...map[string]string) {
|
||||
t.Helper()
|
||||
if os.Getenv("GO_OMNI_SCAFFOLD_INTEGRATION") == "" {
|
||||
t.Skip("consumer integration")
|
||||
}
|
||||
root := frameworkRoot(t)
|
||||
dir := filepath.Join(t.TempDir(), "consumer")
|
||||
opts := Options{API: filepath.Join(root, "examples/contracts/errors.api"), Dir: dir, Module: "example.com/errorCatalog", FrameworkDir: root}
|
||||
opts := Options{API: filepath.Join(root, "examples/contracts", apiFile), Dir: dir, Module: "example.com/errorCatalog", FrameworkDir: root}
|
||||
cli := os.Getenv("GO_OMNI_RELEASE_CLI")
|
||||
generate := func(check bool) {
|
||||
t.Helper()
|
||||
@@ -113,12 +118,24 @@ func TestErrorCatalogConsumer(t *testing.T) {
|
||||
}
|
||||
generate(false)
|
||||
|
||||
fixture := readFile(t, "testdata/errorCatalog/consumer_test.go")
|
||||
writeFile(t, filepath.Join(dir, "consumer_test.go"), fixture)
|
||||
files := map[string]string{"consumer_test.go": "consumer_test.go"}
|
||||
for _, extra := range extraFiles {
|
||||
for target, source := range extra {
|
||||
files[target] = source
|
||||
}
|
||||
}
|
||||
before := map[string]string{}
|
||||
for target, source := range files {
|
||||
fixture := readFile(t, filepath.Join("testdata", fixtureName, source))
|
||||
writeFile(t, filepath.Join(dir, target), fixture)
|
||||
before[target] = fixture
|
||||
}
|
||||
generate(false)
|
||||
generate(true)
|
||||
if readFile(t, filepath.Join(dir, "consumer_test.go")) != fixture {
|
||||
t.Fatal("regeneration overwrote manual code")
|
||||
for target, fixture := range before {
|
||||
if readFile(t, filepath.Join(dir, target)) != fixture {
|
||||
t.Fatalf("regeneration overwrote %s", target)
|
||||
}
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
|
||||
defer cancel()
|
||||
|
||||
12
cmd/go-omni/apigen/errorInheritance_test.go
Normal file
12
cmd/go-omni/apigen/errorInheritance_test.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package apigen
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestErrorInheritanceConsumer(t *testing.T) {
|
||||
testCatalogConsumer(t, "errorInheritance.api", "errorInheritance", map[string]string{
|
||||
"internal/logic/account/plain.go": "service.go",
|
||||
"internal/logic/account/raw.go": "emptyService.go",
|
||||
"internal/logic/account/cleared.go": "emptyService.go",
|
||||
"internal/handler/account/boundedManual.go": "bounded.go",
|
||||
})
|
||||
}
|
||||
86
cmd/go-omni/apigen/errorParams_test.go
Normal file
86
cmd/go-omni/apigen/errorParams_test.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestErrorParamsConsumer(t *testing.T) { testCatalogConsumer(t, "errorParams.api", "errorParams") }
|
||||
|
||||
func TestErrorParamsDocumentAndValidation(t *testing.T) {
|
||||
path := filepath.Join(frameworkRoot(t), "examples/contracts/errorParams.api")
|
||||
source := readFile(t, path)
|
||||
spec, err := Parse(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
compiled, err := Compile(spec)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
doc := document(t, compiled)
|
||||
definitions := doc["x-omni-errors"].([]any)
|
||||
found := false
|
||||
for _, value := range definitions {
|
||||
e := value.(map[string]any)
|
||||
if e["name"] == "TextTooLong" {
|
||||
found = true
|
||||
params := e["params"].([]any)
|
||||
if len(params) != 2 || params[0].(map[string]any)["name"] != "label" || params[1].(map[string]any)["type"] != "int" || e["message"] != "{label}不能超过{max}个字符" {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("missing template definition")
|
||||
}
|
||||
for _, path := range []string{"/token", "/raw"} {
|
||||
method := "post"
|
||||
if path == "/raw" {
|
||||
method = "get"
|
||||
}
|
||||
response := operation(t, doc, path, method)["responses"].(map[string]any)["200"].(map[string]any)
|
||||
media := response["content"].(map[string]any)["application/json"].(map[string]any)
|
||||
value := media["examples"].(map[string]any)["TextTooLong"].(map[string]any)["value"].(map[string]any)
|
||||
if value["msg"] != "企业名称不能超过100个字符" || value["code"] != float64(400002) {
|
||||
t.Fatal(value)
|
||||
}
|
||||
if _, ok := value["data"]; !ok {
|
||||
t.Fatal("data null omitted", value)
|
||||
}
|
||||
if len(media["schema"].(map[string]any)["anyOf"].([]any)) != 2 {
|
||||
t.Fatal("success schema lost")
|
||||
}
|
||||
}
|
||||
for _, change := range [][2]string{
|
||||
{`"{label}不能超过{max}个字符"`, `"{label}不能超过{missing}个字符"`},
|
||||
{`"type": "int"`, `"type": "unknown"`},
|
||||
{`"example": 100`, `"example": "100"`},
|
||||
{`"name": "max"`, `"name": "label"`},
|
||||
{`"name": "max"`, `"name": "type"`},
|
||||
{`successCode: 0`, `successCode: 0\n messages: {"TEXT_TOO_LONG":"{label}不能超过{max}个字符"}`},
|
||||
} {
|
||||
changed := strings.Replace(source, change[0], strings.ReplaceAll(change[1], `\n`, "\n"), 1)
|
||||
s, err := parseText(t, changed)
|
||||
if err == nil {
|
||||
_, err = Compile(s)
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatal("accepted", change)
|
||||
}
|
||||
}
|
||||
// Removing examples still produces a concrete, deterministic example.
|
||||
noExamples := strings.ReplaceAll(strings.ReplaceAll(source, `, "example": "企业名称"`, ""), `, "example": 100`, "")
|
||||
s, err := parseText(t, noExamples)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c, err := Compile(s)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(c.Document), "label不能超过1个字符") {
|
||||
t.Fatal("missing generated example")
|
||||
}
|
||||
}
|
||||
207
cmd/go-omni/apigen/format.go
Normal file
207
cmd/go-omni/apigen/format.go
Normal file
@@ -0,0 +1,207 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"text/scanner"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// Format normalizes indentation and blank lines without rewriting tokens,
|
||||
// comments, quoted metadata, struct tags or multiline literal contents.
|
||||
func Format(source []byte) ([]byte, error) {
|
||||
if len(source) > 1<<20 {
|
||||
return nil, errors.New("api: source size limit exceeded")
|
||||
}
|
||||
var scan scanner.Scanner
|
||||
scan.Init(bytes.NewReader(source))
|
||||
scan.Mode = scanner.ScanIdents | scanner.ScanInts | scanner.ScanStrings | scanner.ScanRawStrings | scanner.ScanComments
|
||||
var scanErr error
|
||||
scan.Error = func(s *scanner.Scanner, msg string) {
|
||||
if scanErr == nil {
|
||||
scanErr = fmt.Errorf("%s: %s", s.Position, msg)
|
||||
}
|
||||
}
|
||||
depths := map[int]int{}
|
||||
protected := map[int]bool{}
|
||||
depth := 0
|
||||
for k := scan.Scan(); k != scanner.EOF; k = scan.Scan() {
|
||||
line := scan.Position.Line
|
||||
value := scan.TokenText()
|
||||
if k == '}' || k == ')' {
|
||||
depth--
|
||||
if depth < 0 {
|
||||
return nil, errors.New("api: unmatched closing delimiter")
|
||||
}
|
||||
}
|
||||
if _, ok := depths[line]; !ok {
|
||||
depths[line] = depth
|
||||
}
|
||||
// Preserve interior lines exactly, including the closing line of a literal.
|
||||
count := strings.Count(value, "\n")
|
||||
for n := 1; n <= count; n++ {
|
||||
protected[line+n] = true
|
||||
}
|
||||
if k == '{' || k == '(' {
|
||||
depth++
|
||||
}
|
||||
}
|
||||
if scanErr != nil {
|
||||
return nil, scanErr
|
||||
}
|
||||
if depth != 0 {
|
||||
return nil, errors.New("api: unclosed delimiter")
|
||||
}
|
||||
lines := strings.Split(string(source), "\n")
|
||||
out := make([]string, 0, len(lines))
|
||||
outProtected := map[int]bool{}
|
||||
blank := true
|
||||
lastDepth := 0
|
||||
for i, line := range lines {
|
||||
if protected[i+1] {
|
||||
out = append(out, line)
|
||||
outProtected[len(out)] = true
|
||||
blank = false
|
||||
continue
|
||||
}
|
||||
trimmed := strings.TrimSpace(line)
|
||||
if trimmed == "" {
|
||||
if !blank {
|
||||
out = append(out, "")
|
||||
blank = true
|
||||
}
|
||||
continue
|
||||
}
|
||||
d, ok := depths[i+1]
|
||||
if ok {
|
||||
lastDepth = d
|
||||
}
|
||||
out = append(out, strings.Repeat(" ", lastDepth)+trimmed)
|
||||
blank = false
|
||||
}
|
||||
for len(out) > 0 && out[len(out)-1] == "" {
|
||||
out = out[:len(out)-1]
|
||||
}
|
||||
alignFieldLines(out, outProtected)
|
||||
return []byte(strings.Join(out, "\n") + "\n"), nil
|
||||
}
|
||||
|
||||
// Only complete, single-line fields are aligned. Literal bytes and multiline
|
||||
// comment contents are never candidates, even when they resemble a field.
|
||||
var singleLineField = regexp.MustCompile("^([ \\t]*)([A-Z][A-Za-z0-9_]*)[ \\t]+([^ \\t`]+)[ \\t]+(`[^`]*`)[ \\t]*(//.*)?$")
|
||||
|
||||
func alignFieldLines(lines []string, protected map[int]bool) {
|
||||
for start := 0; start < len(lines); {
|
||||
var fields [][]string
|
||||
widths := [3]int{}
|
||||
end := start
|
||||
for end < len(lines) && !protected[end+1] {
|
||||
parts := singleLineField.FindStringSubmatch(lines[end])
|
||||
if parts == nil || (len(fields) > 0 && parts[1] != fields[0][1]) {
|
||||
break
|
||||
}
|
||||
for col := range widths {
|
||||
if n := utf8.RuneCountInString(parts[col+2]); n > widths[col] {
|
||||
widths[col] = n
|
||||
}
|
||||
}
|
||||
fields = append(fields, parts)
|
||||
end++
|
||||
}
|
||||
for i, parts := range fields {
|
||||
line := parts[1]
|
||||
for col := range widths {
|
||||
line += parts[col+2]
|
||||
if col < 2 || parts[5] != "" {
|
||||
line += strings.Repeat(" ", widths[col]-utf8.RuneCountInString(parts[col+2])+1)
|
||||
}
|
||||
}
|
||||
lines[start+i] = line + parts[5]
|
||||
}
|
||||
if end == start {
|
||||
end++
|
||||
}
|
||||
start = end
|
||||
}
|
||||
}
|
||||
|
||||
// FormatProject validates the full contract before writing any local source.
|
||||
// Check mode is read-only and reports every file needing formatting.
|
||||
func FormatProject(entry string, check bool) ([]string, error) {
|
||||
spec, err := Parse(entry)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err = Compile(spec); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
real, err := filepath.EvalSymlinks(entry)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
root := filepath.Dir(real)
|
||||
names := make([]string, 0, len(spec.Sources))
|
||||
for name := range spec.Sources {
|
||||
if strings.HasPrefix(name, "@") || filepath.Ext(name) != ".api" {
|
||||
continue
|
||||
}
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
next := map[string][]byte{}
|
||||
var changed []string
|
||||
for _, name := range names {
|
||||
data, err := Format(spec.Sources[name])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", name, err)
|
||||
}
|
||||
if !bytes.Equal(data, spec.Sources[name]) {
|
||||
next[name] = data
|
||||
changed = append(changed, name)
|
||||
}
|
||||
}
|
||||
if check {
|
||||
if len(changed) > 0 {
|
||||
return changed, fmt.Errorf("API format drift: %s", strings.Join(changed, ", "))
|
||||
}
|
||||
return changed, nil
|
||||
}
|
||||
for _, name := range changed {
|
||||
path := filepath.Join(root, filepath.FromSlash(name))
|
||||
old, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return changed, err
|
||||
}
|
||||
if !bytes.Equal(old, spec.Sources[name]) {
|
||||
return changed, fmt.Errorf("API source changed during formatting: %s", name)
|
||||
}
|
||||
stat, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return changed, err
|
||||
}
|
||||
f, err := os.CreateTemp(filepath.Dir(path), ".omni-format-*")
|
||||
if err != nil {
|
||||
return changed, err
|
||||
}
|
||||
tmp := f.Name()
|
||||
err = f.Chmod(stat.Mode().Perm())
|
||||
if err == nil {
|
||||
_, err = f.Write(next[name])
|
||||
}
|
||||
err = errors.Join(err, f.Close())
|
||||
if err == nil {
|
||||
err = os.Rename(tmp, path)
|
||||
}
|
||||
_ = os.Remove(tmp)
|
||||
if err != nil {
|
||||
return changed, err
|
||||
}
|
||||
}
|
||||
return changed, nil
|
||||
}
|
||||
@@ -19,10 +19,13 @@ import (
|
||||
)
|
||||
|
||||
const frameworkModule = "git.yuntuo.hk/k2safe/go-omni"
|
||||
const minimumVersion = "v0.10.0-preview"
|
||||
const minimumVersion = "v0.18.0-preview"
|
||||
const generatedHeader = "// Code generated by go-omni api. DO NOT EDIT.\n"
|
||||
|
||||
type Options struct {
|
||||
generation GeneratorConfig
|
||||
lockHeld bool
|
||||
documentCatalogs map[string][]byte
|
||||
Database, Storage string
|
||||
API, Dir, Module, FrameworkDir, FrameworkVersion, LogOutput, LogFormat string
|
||||
Check bool
|
||||
@@ -35,6 +38,7 @@ type FileChange struct {
|
||||
type Output struct {
|
||||
Generated, Created, Preserved, Removed []string
|
||||
Changes []FileChange
|
||||
Diagnostics []Diagnostic
|
||||
}
|
||||
|
||||
// Generate creates or safely updates one contract-driven project. It renders
|
||||
@@ -46,7 +50,7 @@ func Generate(o Options) (Output, error) {
|
||||
if o.API == "" || o.Dir == "" {
|
||||
return Output{}, errors.New("--api and --dir are required")
|
||||
}
|
||||
spec, err := Parse(o.API)
|
||||
spec, err := parseWithCatalogs(o.API, o.documentCatalogs)
|
||||
if err != nil {
|
||||
return Output{}, err
|
||||
}
|
||||
@@ -94,6 +98,14 @@ func generate(o Options, c *Compiled, starter bool) (Output, error) {
|
||||
return Output{}, err
|
||||
}
|
||||
o.Dir = directory
|
||||
genConfig, hasGenConfig, err := readGeneratorConfig(directory)
|
||||
if err != nil {
|
||||
return Output{}, err
|
||||
}
|
||||
o.generation = genConfig
|
||||
if err = checkRuntimeExtension(directory); err != nil {
|
||||
return Output{}, err
|
||||
}
|
||||
for _, r := range c.Spec.Routes {
|
||||
if !r.Manual {
|
||||
path := filepath.Join(directory, "internal/handler", r.Group, lower(r.Handler)+"Manual.go")
|
||||
@@ -108,6 +120,7 @@ func generate(o Options, c *Compiled, starter bool) (Output, error) {
|
||||
return Output{}, err
|
||||
}
|
||||
seeds := map[string][]byte{}
|
||||
requiredVersion := ""
|
||||
moduleData, readErr := os.ReadFile(filepath.Join(directory, "go.mod"))
|
||||
if readErr == nil {
|
||||
mf, err := modfile.Parse("go.mod", moduleData, nil)
|
||||
@@ -119,7 +132,6 @@ func generate(o Options, c *Compiled, starter bool) (Output, error) {
|
||||
}
|
||||
o.Module = mf.Module.Mod.Path
|
||||
found := false
|
||||
requiredVersion := ""
|
||||
for _, r := range mf.Require {
|
||||
if r.Mod.Path == frameworkModule {
|
||||
found = true
|
||||
@@ -140,13 +152,13 @@ func generate(o Options, c *Compiled, starter bool) (Output, error) {
|
||||
if !filepath.IsAbs(path) {
|
||||
path = filepath.Join(directory, path)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(path, "errors", "catalog.go")); err == nil {
|
||||
if source, err := os.ReadFile(filepath.Join(path, "config", "env.go")); err == nil && strings.Contains(string(source), "func ServerEnvAliases(") {
|
||||
localCurrent = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if !localCurrent {
|
||||
return Output{}, fmt.Errorf("existing framework dependency predates error catalog support; use %s or a current local replacement", minimumVersion)
|
||||
return Output{}, fmt.Errorf("existing framework dependency predates the generated configuration helpers; use %s or a current local replacement", minimumVersion)
|
||||
}
|
||||
}
|
||||
if o.FrameworkDir != "" {
|
||||
@@ -186,11 +198,24 @@ func generate(o Options, c *Compiled, starter bool) (Output, error) {
|
||||
for name, data := range base {
|
||||
seeds[name] = data
|
||||
}
|
||||
if !hasGenConfig {
|
||||
raw, _ := json.MarshalIndent(genConfig, "", " ")
|
||||
seeds[generatorConfigFile] = append(raw, '\n')
|
||||
}
|
||||
// The API source remains developer-owned. A relocated project regenerates
|
||||
// from its own api/ tree, including all imports.
|
||||
entry := "api/" + c.Spec.Entry
|
||||
for name, data := range c.Spec.Sources {
|
||||
seeds["api/"+name] = data
|
||||
if !strings.HasPrefix(name, "@") {
|
||||
seeds["api/"+name] = data
|
||||
}
|
||||
}
|
||||
if len(c.Spec.SharedRoots) > 0 {
|
||||
raw, e := sharedSeeds(c.Spec, directory)
|
||||
if e != nil {
|
||||
return Output{}, e
|
||||
}
|
||||
seeds["api/"+importsFile] = raw
|
||||
}
|
||||
if o.API != "" {
|
||||
source, _ := filepath.Abs(o.API)
|
||||
@@ -199,6 +224,7 @@ func generate(o Options, c *Compiled, starter bool) (Output, error) {
|
||||
rel, e := filepath.Rel(targetReal, sourceReal)
|
||||
if e == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
||||
entry = filepath.ToSlash(rel)
|
||||
delete(seeds, "api/"+importsFile)
|
||||
for name := range c.Spec.Sources {
|
||||
delete(seeds, "api/"+name)
|
||||
}
|
||||
@@ -207,7 +233,7 @@ func generate(o Options, c *Compiled, starter bool) (Output, error) {
|
||||
// Source collisions must never leave a copied contract different from the
|
||||
// generated code. On updates, use the developer-owned API in the project.
|
||||
for name, data := range seeds {
|
||||
if strings.HasSuffix(name, ".api") {
|
||||
if strings.HasSuffix(name, ".api") || filepath.Base(name) == catalogDocumentFile || filepath.Base(name) == importsFile {
|
||||
current, e := os.ReadFile(filepath.Join(directory, filepath.FromSlash(name)))
|
||||
if e == nil && string(current) != string(data) {
|
||||
return Output{}, fmt.Errorf("%s already differs; regenerate from that project API instead", name)
|
||||
@@ -226,7 +252,28 @@ func generate(o Options, c *Compiled, starter bool) (Output, error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return apply(directory, o.Module, entry, generated, seeds, o.Check, o.DryRun, o.profile())
|
||||
if err := renderMappings(o, generated, seeds); err != nil {
|
||||
return Output{}, err
|
||||
}
|
||||
currentConfig, stillExists, err := readGeneratorConfig(directory)
|
||||
if err != nil {
|
||||
return Output{}, err
|
||||
}
|
||||
beforeConfig, _ := json.Marshal(genConfig)
|
||||
afterConfig, _ := json.Marshal(currentConfig)
|
||||
if hasGenConfig != stillExists || genConfig.inputDigest != currentConfig.inputDigest || string(beforeConfig) != string(afterConfig) {
|
||||
return Output{}, errors.New("generation configuration changed during generation; retry")
|
||||
}
|
||||
out, err := apply(directory, o.Module, entry, generated, seeds, o.Check, o.DryRun, o.profile(), snapshotContract(c.Spec), o.lockHeld)
|
||||
expectedVersion := minimumVersion
|
||||
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Path == frameworkModule && info.Main.Sum != "" && semver.IsValid(info.Main.Version) {
|
||||
expectedVersion = info.Main.Version
|
||||
}
|
||||
if requiredVersion != "" && requiredVersion != expectedVersion {
|
||||
out.Diagnostics = append(out.Diagnostics, Diagnostic{Severity: "review", Code: "frameworkVersion", Path: "go.mod", Before: requiredVersion, After: expectedVersion, Message: "project dependency differs from this CLI version; use matching CLI/framework versions, or verify the explicit local replacement"})
|
||||
sortDiagnostics(out.Diagnostics)
|
||||
}
|
||||
return out, err
|
||||
}
|
||||
|
||||
func moduleFile(o Options) ([]byte, error) {
|
||||
@@ -315,12 +362,33 @@ func renderProject(o Options, c *Compiled, starter bool) (map[string][]byte, map
|
||||
content := "package apierrors\nimport(\"encoding/json\"; omnierrors " + strconv.Quote(frameworkModule+"/errors") + ")\n"
|
||||
content += fmt.Sprintf("func Catalog()omnierrors.Catalog{var c omnierrors.Catalog;if err:=json.Unmarshal([]byte(%q),&c);err!=nil{panic(err)};return c}\nvar errorRegistry=omnierrors.MustRegistry(Catalog())\n", errorCatalogJSON(c.Spec, nil))
|
||||
for _, e := range c.Spec.Errors {
|
||||
content += fmt.Sprintf("const Code%s=%q\nfunc %s()*omnierrors.APIError{return errorRegistry.Resolve(omnierrors.ByCode(Code%s))}\n", e.Name, e.Code, e.Name, e.Name)
|
||||
var args, values []string
|
||||
for _, p := range e.Params {
|
||||
name := p.Name
|
||||
switch name {
|
||||
case "string", "any", "omnierrors", "errorRegistry", "int", "int32", "int64", "float64", "bool":
|
||||
name = "_" + name
|
||||
}
|
||||
args = append(args, name+" "+p.Type)
|
||||
values = append(values, strconv.Quote(p.Name)+":"+name)
|
||||
}
|
||||
ref := fmt.Sprintf("omnierrors.ByCode(Code%s)", e.Name)
|
||||
if len(args) > 0 {
|
||||
ref += ".WithParams(map[string]any{" + strings.Join(values, ",") + "})"
|
||||
}
|
||||
content += fmt.Sprintf("const Code%s=%q\n// %s returns a fresh, resolved error declared in the API contract.\nfunc %s(%s)*omnierrors.APIError{return errorRegistry.Resolve(%s)}\n", e.Name, e.Code, e.Name, e.Name, strings.Join(args, ","), ref)
|
||||
}
|
||||
put(generated, "internal/apierrors/errorsGen.go", generatedHeader+content)
|
||||
}
|
||||
|
||||
types := "package types\n\n"
|
||||
needsUpload := false
|
||||
for _, b := range c.Bindings {
|
||||
needsUpload = needsUpload || b.HasMultipart()
|
||||
}
|
||||
if needsUpload {
|
||||
types += fmt.Sprintf("import %q\n", frameworkModule+"/contract")
|
||||
}
|
||||
if len(c.Spec.Scalars) > 0 {
|
||||
types += "import("
|
||||
for i, d := range c.Spec.Scalars {
|
||||
@@ -335,12 +403,28 @@ func renderProject(o Options, c *Compiled, starter bool) (map[string][]byte, map
|
||||
registry += "}}\n"
|
||||
put(generated, "internal/types/scalarsGen.go", generatedHeader+registry)
|
||||
}
|
||||
if needsUpload {
|
||||
types += "type File = contract.File\n"
|
||||
}
|
||||
bySource := map[string]string{}
|
||||
for _, t := range c.Spec.Types {
|
||||
types += "type " + t.Name + " struct {\n"
|
||||
for _, f := range t.Fields {
|
||||
types += fmt.Sprintf("%s %s `%s`\n", f.Name, f.Type, f.Tag)
|
||||
name := dtoFile(t.Position)
|
||||
if bySource[name] == "" {
|
||||
bySource[name] = "package types\n\n"
|
||||
}
|
||||
types += "}\n"
|
||||
text := "type " + t.Name + " struct {\n"
|
||||
for _, f := range t.Fields {
|
||||
if f.Description != "" {
|
||||
for _, line := range strings.Split(f.Description, "\n") {
|
||||
text += "// " + line + "\n"
|
||||
}
|
||||
}
|
||||
text += fmt.Sprintf("%s %s `%s`\n", f.Name, f.Type, fieldTag(f))
|
||||
}
|
||||
bySource[name] += text + "}\n"
|
||||
}
|
||||
for name, text := range bySource {
|
||||
put(generated, "internal/types/"+name, generatedHeader+text)
|
||||
}
|
||||
put(generated, "internal/types/typesGen.go", generatedHeader+types)
|
||||
put(generated, "docs/openapi.json", string(c.Document)+"\n")
|
||||
@@ -353,11 +437,11 @@ func renderProject(o Options, c *Compiled, starter bool) (map[string][]byte, map
|
||||
}
|
||||
policySource += fmt.Sprintf("%q: newResponseTemplate(%q),\n", definition.Name, string(data))
|
||||
}
|
||||
policySource += "}\nfunc newResponseTemplate(raw string)response.Encoder{var cfg response.Config;decoder:=json.NewDecoder(strings.NewReader(raw));decoder.UseNumber();if err:=decoder.Decode(&cfg);err!=nil{panic(err)};return response.MustNew(cfg)}\n"
|
||||
policySource += "func responseEncoder(name string,raw bool,catalogJSON string)response.Encoder{encoder:=responseTemplates[name];if encoder==nil{panic(\"unknown response template\")};var catalog omnierrors.Catalog;if err:=json.Unmarshal([]byte(catalogJSON),&catalog);err!=nil{panic(err)};encoder=response.MustWithErrors(catalog,encoder);if raw{encoder=response.RawJSON(encoder)};return encoder}\n"
|
||||
policySource += "}\nfunc newResponseTemplate(raw string)*response.Formatter{var cfg response.Config;decoder:=json.NewDecoder(strings.NewReader(raw));decoder.UseNumber();if err:=decoder.Decode(&cfg);err!=nil{panic(err)};return response.MustNew(cfg)}\n"
|
||||
policySource += "func responseEncoder(name string,raw bool,catalogJSON string)response.Encoder{encoder:=responseTemplates[name];if encoder==nil{panic(\"unknown response template\")};var catalog omnierrors.Catalog;if err:=json.Unmarshal([]byte(catalogJSON),&catalog);err!=nil{panic(err)};if len(catalog.Definitions)>0{encoder=response.MustWithErrors(catalog,encoder)};if raw{encoder=response.RawJSON(encoder)};return encoder}\n"
|
||||
put(generated, "internal/handler/responseGen.go", generatedHeader+policySource)
|
||||
imports := fmt.Sprintf("import (\"fmt\"; %q; %q; %q;", frameworkModule+"/httpx", frameworkModule+"/openapi", o.Module+"/internal/servicecontext")
|
||||
routes := "func RegisterRoutes(router openapi.Registrar, deps *servicecontext.Context) error {\n if router==nil || deps==nil {return fmt.Errorf(\"router and dependencies required\")}\n"
|
||||
imports := fmt.Sprintf("import (\"fmt\"; %q; %q; %q;", frameworkModule+"/httpx", frameworkModule+"/openapi", o.Module+"/internal/svc")
|
||||
routes := "func RegisterRoutes(router openapi.Registrar, deps *svc.ServiceContext) error {\n if router==nil || deps==nil {return fmt.Errorf(\"router and dependencies required\")}\n"
|
||||
if len(c.Spec.Scalars) > 0 {
|
||||
imports += fmt.Sprintf("%q;%q;", frameworkModule+"/contract", o.Module+"/internal/types")
|
||||
routes += "if err:=contract.ValidateScalars(types.ContractScalars()...);err!=nil{return err}\n"
|
||||
@@ -377,7 +461,7 @@ func renderProject(o Options, c *Compiled, starter bool) (map[string][]byte, map
|
||||
routes += fmt.Sprintf("if deps.Middlewares[%q]==nil {return fmt.Errorf(%q)}\n", m, "missing middleware "+m)
|
||||
}
|
||||
for i, group := range c.Groups {
|
||||
imports += fmt.Sprintf("h%d %q; s%d %q;", i, o.Module+"/internal/handler/"+group, i, o.Module+"/internal/service/"+group)
|
||||
imports += fmt.Sprintf("h%d %q; s%d %q;", i, o.Module+"/internal/handler/"+group, i, o.Module+"/internal/logic/"+group)
|
||||
routes += fmt.Sprintf("h%d := h%d.New(s%d.New(deps),deps)\n", i, i, i)
|
||||
groupRoutes := []Route{}
|
||||
for _, r := range c.Spec.Routes {
|
||||
@@ -393,10 +477,10 @@ func renderProject(o Options, c *Compiled, starter bool) (map[string][]byte, map
|
||||
continue
|
||||
}
|
||||
hasAutomatic = true
|
||||
needsTypes = needsTypes || r.Request != "" || r.Response != ""
|
||||
needsBinder = needsBinder || (r.Request != "" && !r.CustomBinding)
|
||||
needsTypes = needsTypes || r.Request != "" || responseNeedsTypes(r.Response)
|
||||
needsBinder = needsBinder || !r.CustomBinding
|
||||
}
|
||||
text += fmt.Sprintf("%q;%q;", frameworkModule+"/httpx", o.Module+"/internal/servicecontext")
|
||||
text += fmt.Sprintf("%q;%q;", frameworkModule+"/httpx", o.Module+"/internal/svc")
|
||||
if needsTypes {
|
||||
text += fmt.Sprintf("%q;", o.Module+"/internal/types")
|
||||
}
|
||||
@@ -404,13 +488,13 @@ func renderProject(o Options, c *Compiled, starter bool) (map[string][]byte, map
|
||||
text += fmt.Sprintf("%q;", frameworkModule+"/contract")
|
||||
}
|
||||
text += ")\n"
|
||||
text += "type Service interface {\n"
|
||||
text += "type Logic interface {\n"
|
||||
for _, r := range groupRoutes {
|
||||
if !r.Manual {
|
||||
text += signature(r) + "\n"
|
||||
}
|
||||
}
|
||||
text += "}\ntype Handler struct {service Service;deps *servicecontext.Context}\nfunc New(s Service,deps ...*servicecontext.Context)*Handler {h:=&Handler{service:s};if len(deps)>0{h.deps=deps[0]};return h}\n"
|
||||
text += "}\ntype Handler struct {logic Logic;deps *svc.ServiceContext}\nfunc New(s Logic,deps ...*svc.ServiceContext)*Handler {h:=&Handler{logic:s};if len(deps)>0{h.deps=deps[0]};return h}\n"
|
||||
for _, r := range groupRoutes {
|
||||
name := upper(r.Handler)
|
||||
// The response policy and body reader cap are installed before App middleware.
|
||||
@@ -427,12 +511,18 @@ func renderProject(o Options, c *Compiled, starter bool) (map[string][]byte, map
|
||||
put(seeds, "internal/handler/"+group+"/"+lower(r.Handler)+"Manual.go", fmt.Sprintf("package %s\nimport(%q;apierrors %q)\n// %s owns raw/multipart binding and response output. h.deps holds project dependencies.\n// This file is preserved on regeneration.\nfunc(h *Handler)%s(c *httpx.Context)error{return apierrors.New(501,\"NOT_IMPLEMENTED\",\"manual handler is not implemented\")}\n", packageName, frameworkModule+"/httpx", frameworkModule+"/errors", name, name))
|
||||
continue
|
||||
}
|
||||
if r.Request != "" && !r.CustomBinding {
|
||||
if !r.CustomBinding {
|
||||
scalarArgs := ""
|
||||
if len(c.Spec.Scalars) > 0 {
|
||||
scalarArgs = "types.ContractScalars()..."
|
||||
}
|
||||
text += fmt.Sprintf("var %sRequest = contract.MustNew[types.%s](%s)\n", lower(r.Handler), r.Request, scalarArgs)
|
||||
requestType := "struct{}"
|
||||
if r.Request != "" {
|
||||
requestType = "types." + r.Request
|
||||
} else {
|
||||
scalarArgs = ""
|
||||
}
|
||||
text += fmt.Sprintf("var %sRequest = contract.MustNew[%s](%s)\n", lower(r.Handler), requestType, scalarArgs)
|
||||
}
|
||||
text += fmt.Sprintf("func(h *Handler) %s(c *httpx.Context)error {\n", name)
|
||||
call := "c.Context()"
|
||||
@@ -440,41 +530,90 @@ func renderProject(o Options, c *Compiled, starter bool) (map[string][]byte, map
|
||||
if r.CustomBinding {
|
||||
text += fmt.Sprintf("var req types.%s\nif err:=h.bind%s(c,&req);err!=nil{return err}\n", r.Request, name)
|
||||
put(seeds, "internal/handler/"+group+"/"+lower(r.Handler)+"Bind.go", fmt.Sprintf("package %s\nimport(%q;%q;apierrors %q)\n// bind%s is project-owned; repeated generation preserves it.\nfunc(h *Handler)bind%s(c *httpx.Context,req *types.%s)error{return apierrors.New(501,\"BINDING_NOT_IMPLEMENTED\",\"custom binding is not implemented\")}\n", packageName, frameworkModule+"/httpx", o.Module+"/internal/types", frameworkModule+"/errors", name, name, r.Request))
|
||||
} else if r.BoundMultipart {
|
||||
text += fmt.Sprintf("var req types.%s\ncleanup,err:=%sRequest.BindMultipart(c.Request(),httpx.RouteParams(c.Context()),&req,contract.MultipartOptions{MaxBodyBytes:%d,MaxParts:%d,MaxFieldBytes:%d,MaxFileBytes:%d,Policy:%s});if err!=nil{return err};defer cleanup()\n", r.Request, lower(r.Handler), r.MaxBodyBytes, r.MaxParts, r.MaxFieldBytes, r.MaxFileBytes, policyLiteral(r.RequestPolicy))
|
||||
} else {
|
||||
limit := ""
|
||||
if r.MaxBodyBytes > 0 {
|
||||
limit = fmt.Sprintf(",%d", r.MaxBodyBytes)
|
||||
}
|
||||
text += fmt.Sprintf("var req types.%s\nif err:=%sRequest.Bind(c.Request(),httpx.RouteParams(c.Context()),&req%s);err!=nil{return err}\n", r.Request, lower(r.Handler), limit)
|
||||
policy := r.RequestPolicy
|
||||
if r.BodyMode == "raw" || r.BodyMode == "multipart" {
|
||||
policy.Body = "auto"
|
||||
}
|
||||
text += fmt.Sprintf("var req types.%s\nif err:=%sRequest.BindWithPolicy(c.Request(),httpx.RouteParams(c.Context()),&req,%s%s);err!=nil{return err}\n", r.Request, lower(r.Handler), policyLiteral(policy), limit)
|
||||
}
|
||||
call += ", &req"
|
||||
} else {
|
||||
limit := ""
|
||||
if r.MaxBodyBytes > 0 {
|
||||
limit = fmt.Sprintf(",%d", r.MaxBodyBytes)
|
||||
}
|
||||
policy := r.RequestPolicy
|
||||
if r.BodyMode == "raw" || r.BodyMode == "multipart" {
|
||||
policy.Body = "auto"
|
||||
}
|
||||
text += fmt.Sprintf("if err:=%sRequest.BindWithPolicy(c.Request(),httpx.RouteParams(c.Context()),&struct{}{},%s%s);err!=nil{return err}\n", lower(r.Handler), policyLiteral(policy), limit)
|
||||
}
|
||||
if r.Response != "" {
|
||||
text += fmt.Sprintf("resp,err:=h.service.%s(%s);if err!=nil{return err};if resp==nil{return fmt.Errorf(\"%s returned a nil response\")}\n", name, call, name)
|
||||
streamingBody := streamsBody(r)
|
||||
if streamingBody {
|
||||
text += fmt.Sprintf("body,err:=httpx.NewBody(c.Request(),%q);if err!=nil{return err}\n", r.BodyMode)
|
||||
call += ",body"
|
||||
}
|
||||
if r.Produces != "" {
|
||||
text += fmt.Sprintf("resp,err:=h.logic.%s(%s);if err!=nil{if resp!=nil{_ = resp.Close()};return err}\n", name, call)
|
||||
if streamingBody {
|
||||
text += "if err:=body.Complete();err!=nil{if resp!=nil{_ =resp.Close()};return err}\n"
|
||||
}
|
||||
text += fmt.Sprintf("return c.WriteStream(%d,%q,resp)\n}\n", r.Status, r.Produces)
|
||||
} else if responseGoType(r.Response) != "" {
|
||||
text += fmt.Sprintf("resp,err:=h.logic.%s(%s);if err!=nil{return err};\n", name, call)
|
||||
kind, _ := responseShape(r.Response)
|
||||
if kind == "object" {
|
||||
text += fmt.Sprintf("if resp==nil{return fmt.Errorf(%q)}\n", name+" returned a nil response")
|
||||
}
|
||||
if kind == "array" {
|
||||
text += "if resp==nil{resp=make(" + responseGoType(r.Response) + ",0)}\n"
|
||||
}
|
||||
if streamingBody {
|
||||
text += "if err:=body.Complete();err!=nil{return err}\n"
|
||||
}
|
||||
method := map[int]string{200: "OK", 201: "Created", 202: "Accepted"}[r.Status]
|
||||
text += "return c." + method + "(resp)\n}\n"
|
||||
} else {
|
||||
text += fmt.Sprintf("if err:=h.service.%s(%s);err!=nil{return err};return c.NoContent()\n}\n", name, call)
|
||||
text += fmt.Sprintf("if err:=h.logic.%s(%s);err!=nil{return err};", name, call)
|
||||
if streamingBody {
|
||||
text += "if err:=body.Complete();err!=nil{return err};"
|
||||
}
|
||||
if r.Response == "null" {
|
||||
method := map[int]string{200: "OK", 201: "Created", 202: "Accepted"}[r.Status]
|
||||
text += "return c." + method + "(nil)\n}\n"
|
||||
} else {
|
||||
text += "return c.NoContent()\n}\n"
|
||||
}
|
||||
}
|
||||
service := fmt.Sprintf("package %s\nimport(\"context\"; apierrors %q;", packageName, frameworkModule+"/errors")
|
||||
if r.Request != "" || r.Response != "" {
|
||||
if r.Request != "" || responseNeedsTypes(r.Response) {
|
||||
service += fmt.Sprintf("%q;", o.Module+"/internal/types")
|
||||
}
|
||||
if streamingBody || r.Produces != "" {
|
||||
service += fmt.Sprintf("%q;", frameworkModule+"/httpx")
|
||||
}
|
||||
service += ")\n"
|
||||
service += "// " + name + " implements the business operation. This file is never overwritten.\nfunc(s *Service) " + signature(r) + " {\n"
|
||||
service += "// " + name + " implements the business operation. This file is never overwritten.\nfunc(s *Logic) " + signature(r) + " {\n"
|
||||
if starter && r.Handler == "greet" {
|
||||
service = strings.Replace(service, "apierrors "+strconv.Quote(frameworkModule+"/errors")+";", "\"fmt\";", 1)
|
||||
service += "s.deps.Logger.InfoContext(ctx,\"greet.called\")\nreturn &types.GreetResp{Message:fmt.Sprintf(\"Hello %s\",req.Name)},nil\n}"
|
||||
} else {
|
||||
service += "// TODO: implement business behavior and use s.deps for dependencies.\n"
|
||||
if r.Response != "" {
|
||||
if responseGoType(r.Response) != "" || r.Produces != "" {
|
||||
service += "return nil, "
|
||||
} else {
|
||||
service += "return "
|
||||
}
|
||||
service += "apierrors.New(501,\"NOT_IMPLEMENTED\",\"operation is not implemented\")\n}"
|
||||
}
|
||||
put(seeds, "internal/service/"+group+"/"+lower(r.Handler)+".go", service)
|
||||
put(seeds, "internal/logic/"+group+"/"+lower(r.Handler)+".go", service)
|
||||
}
|
||||
if !hasAutomatic {
|
||||
text = strings.Replace(text, strconv.Quote(frameworkModule+"/httpx")+";", "", 1)
|
||||
@@ -484,8 +623,10 @@ func renderProject(o Options, c *Compiled, starter bool) (map[string][]byte, map
|
||||
text = strings.Replace(text, "\"fmt\";", "", 1)
|
||||
}
|
||||
put(generated, "internal/handler/"+group+"/handlersGen.go", generatedHeader+text)
|
||||
put(seeds, "internal/service/"+group+"/service.go", fmt.Sprintf("package %s\nimport %q\ntype Service struct {deps *servicecontext.Context}\nfunc New(deps *servicecontext.Context)*Service{return &Service{deps:deps}}\n", packageName, o.Module+"/internal/servicecontext"))
|
||||
put(seeds, "internal/repository/"+group+"/repository.go", "// Package "+packageName+" owns persistence contracts for this business group.\n// Define repository interfaces and implementations here; HTTP DTOs do not\n// determine a database schema. This file is never overwritten.\npackage "+packageName+"\n")
|
||||
put(seeds, "internal/logic/"+group+"/logic.go", fmt.Sprintf("package %s\nimport %q\ntype Logic struct {deps *svc.ServiceContext}\nfunc New(deps *svc.ServiceContext)*Logic{return &Logic{deps:deps}}\n", packageName, o.Module+"/internal/svc"))
|
||||
if o.generation.Repository == "group" {
|
||||
put(seeds, "internal/repository/"+group+"/repository.go", "// Package "+packageName+" owns persistence contracts for this business group.\n// Define repository interfaces and implementations here; HTTP DTOs do not\n// determine a database schema. This file is never overwritten.\npackage "+packageName+"\n")
|
||||
}
|
||||
}
|
||||
put(generated, "internal/handler/routesGen.go", generatedHeader+"package handler\n"+imports+")\n"+routes+"return nil\n}\n")
|
||||
registry := "package middleware\nimport " + strconv.Quote(frameworkModule+"/httpx") + "\nfunc Defaults()map[string]httpx.Middleware{return map[string]httpx.Middleware{\n"
|
||||
@@ -494,6 +635,9 @@ func renderProject(o Options, c *Compiled, starter bool) (map[string][]byte, map
|
||||
put(seeds, "internal/middleware/"+lower(m)+".go", fmt.Sprintf("package middleware\nimport(%q; apierrors %q)\n// %s is a developer-owned hook. Implement it before enabling this route.\nfunc %s(next httpx.Handler)httpx.Handler{return func(c *httpx.Context)error {return apierrors.New(501,\"MIDDLEWARE_NOT_IMPLEMENTED\",\"middleware is not implemented\")}}\n", frameworkModule+"/httpx", frameworkModule+"/errors", upper(m), upper(m)))
|
||||
}
|
||||
put(generated, "internal/middleware/registryGen.go", generatedHeader+registry+"}}\n")
|
||||
if o.generation.Middleware == "factories" {
|
||||
renderMiddlewareFactories(middlewareNames, generated, seeds)
|
||||
}
|
||||
for name, source := range baseTemplates {
|
||||
content := strings.ReplaceAll(source, "MODULE", o.Module)
|
||||
content = strings.ReplaceAll(content, "LOGFORMAT", o.LogFormat)
|
||||
@@ -501,7 +645,11 @@ func renderProject(o Options, c *Compiled, starter bool) (map[string][]byte, map
|
||||
if name == "etc/config.yaml" && o.LogOutput == "file" {
|
||||
content = strings.Replace(content, "output: file", "output: stderr\n file: ./var/log/server.log\n rotation:\n enabled: true\n maxSizeMb: 100\n maxBackups: 10\n maxAgeDays: 7\n compress: true", 1)
|
||||
}
|
||||
put(seeds, name, content)
|
||||
if name == "main.go" {
|
||||
put(generated, name, generatedHeader+content)
|
||||
} else {
|
||||
put(seeds, name, content)
|
||||
}
|
||||
}
|
||||
|
||||
infra, err := scaffold.APIInfrastructure(o.Module, o.Database, o.Storage, o.LogOutput, o.LogFormat)
|
||||
@@ -509,7 +657,21 @@ func renderProject(o Options, c *Compiled, starter bool) (map[string][]byte, map
|
||||
return nil, nil, err
|
||||
}
|
||||
for name, data := range infra {
|
||||
seeds[name] = data
|
||||
if o.generation.Middleware == "factories" && name == "internal/svc/runtimeGen.go" {
|
||||
formatted, err := format.Source(data)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
source := string(formatted)
|
||||
source = strings.Replace(source, "Middlewares: middleware.Defaults(),", "", 1)
|
||||
source = strings.Replace(source, "return c, nil", "if len(c.Middlewares) != 0 { return nil, errors.New(\"factory middleware is registered automatically; remove manual Middlewares assignments from Init\") }\nc.Middlewares, err = middleware.New(c)\nif err != nil { return nil, err }\nreturn c, nil", 1)
|
||||
data = []byte(source)
|
||||
}
|
||||
if strings.HasSuffix(name, "Gen.go") {
|
||||
generated[name] = append([]byte(generatedHeader), data...)
|
||||
} else {
|
||||
seeds[name] = data
|
||||
}
|
||||
}
|
||||
if o.Storage == "local" {
|
||||
for _, r := range c.Spec.Routes {
|
||||
@@ -517,17 +679,14 @@ func renderProject(o Options, c *Compiled, starter bool) (map[string][]byte, map
|
||||
return nil, nil, errors.New("/storage/object is reserved by local storage")
|
||||
}
|
||||
}
|
||||
main := string(seeds["cmd/server/main.go"])
|
||||
main = strings.Replace(main, " if err=handler.RegisterRoutes", ` for _,method:=range []string{"GET","HEAD","PUT"}{app.Handle(method,"/storage/object",func(c *httpx.Context)error{deps.FileHTTP.ServeHTTP(c.Raw(),c.Request());return nil})}
|
||||
if err=handler.RegisterRoutes`, 1)
|
||||
main = strings.Replace(main, `"git.yuntuo.hk/k2safe/go-omni/logx"`, `"git.yuntuo.hk/k2safe/go-omni/logx"; "git.yuntuo.hk/k2safe/go-omni/httpx"`, 1)
|
||||
seeds["cmd/server/main.go"] = []byte(main)
|
||||
}
|
||||
put(generated, "internal/handler/appGen.go", generatedHeader+appSource(o, c.Spec))
|
||||
|
||||
return generated, seeds, nil
|
||||
}
|
||||
func hasResponse(routes []Route) bool {
|
||||
for _, r := range routes {
|
||||
if r.Response != "" && !r.Manual {
|
||||
if kind, _ := responseShape(r.Response); r.Response != "" && kind == "object" && !r.Manual && r.Produces == "" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -538,11 +697,41 @@ func signature(r Route) string {
|
||||
if r.Request != "" {
|
||||
s += ",req *types." + r.Request
|
||||
}
|
||||
if streamsBody(r) {
|
||||
s += ",body *httpx.Body"
|
||||
}
|
||||
s += ") "
|
||||
if r.Response != "" {
|
||||
s += "(*types." + r.Response + ",error)"
|
||||
if r.Produces != "" {
|
||||
return s + "(*httpx.Stream,error)"
|
||||
}
|
||||
if result := responseGoType(r.Response); result != "" {
|
||||
s += "(" + result + ",error)"
|
||||
} else {
|
||||
s += "error"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func streamsBody(r Route) bool {
|
||||
return r.BodyMode == "raw" || (r.BodyMode == "multipart" && !r.BoundMultipart)
|
||||
}
|
||||
|
||||
func dtoFile(position string) string {
|
||||
source := position
|
||||
for i := 0; i < 2; i++ {
|
||||
if idx := strings.LastIndex(source, ":"); idx >= 0 {
|
||||
source = source[:idx]
|
||||
}
|
||||
}
|
||||
stem := strings.TrimSuffix(filepath.Base(source), ".api")
|
||||
var b strings.Builder
|
||||
for _, r := range stem {
|
||||
if r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9' {
|
||||
b.WriteRune(r)
|
||||
}
|
||||
}
|
||||
if b.Len() == 0 {
|
||||
b.WriteString("contract")
|
||||
}
|
||||
return "dto" + upper(b.String()) + digest([]byte(source))[:12] + "Gen.go"
|
||||
}
|
||||
|
||||
149
cmd/go-omni/apigen/generatorConfig.go
Normal file
149
cmd/go-omni/apigen/generatorConfig.go
Normal file
@@ -0,0 +1,149 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
||||
)
|
||||
|
||||
const generatorConfigFile = "omni.gen.json"
|
||||
|
||||
// GeneratorConfig is developer-owned and checked on every generation. Mapping
|
||||
// rules never change the public API or infer a persistence model.
|
||||
type GeneratorConfig struct {
|
||||
Repository string `json:"repository,omitempty"`
|
||||
Middleware string `json:"middleware,omitempty"`
|
||||
Mappings []Mapping `json:"mappings,omitempty"`
|
||||
MappingFiles []string `json:"mappingFiles,omitempty"`
|
||||
inputDigest string
|
||||
}
|
||||
|
||||
func readGeneratorConfig(root string) (GeneratorConfig, bool, error) {
|
||||
cfg := GeneratorConfig{Repository: "none", Middleware: "factories"}
|
||||
if err := safePath(root, generatorConfigFile); err != nil {
|
||||
return cfg, false, err
|
||||
}
|
||||
stat, err := os.Lstat(filepath.Join(root, generatorConfigFile))
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
// Existing projects keep their handwritten middleware signature until they
|
||||
// explicitly select factories; do not rewrite or adopt developer files.
|
||||
if raw, e := os.ReadFile(filepath.Join(root, manifestPath)); e == nil {
|
||||
var previous manifest
|
||||
if err := json.Unmarshal(raw, &previous); err != nil {
|
||||
return cfg, false, err
|
||||
}
|
||||
if previous.GeneratorConfig {
|
||||
return cfg, false, errors.New("omni.gen.json is missing; restore the project-owned generation options and mappings before regenerating")
|
||||
}
|
||||
cfg.Middleware = "handlers"
|
||||
} else if !errors.Is(e, os.ErrNotExist) {
|
||||
return cfg, false, e
|
||||
}
|
||||
return cfg, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return cfg, false, err
|
||||
}
|
||||
if !stat.Mode().IsRegular() || stat.Size() > 1<<20 {
|
||||
return cfg, false, errors.New("omni.gen.json must be a regular file of at most 1 MiB")
|
||||
}
|
||||
raw, err := readMappingConfigFile(root, generatorConfigFile)
|
||||
if err != nil {
|
||||
return cfg, false, err
|
||||
}
|
||||
if err = apierrors.DecodeCatalogJSON(raw, &cfg); err != nil {
|
||||
return cfg, false, fmt.Errorf("omni.gen.json: %w", err)
|
||||
}
|
||||
// Child files only declare mappings. No recursive imports, globs, implicit
|
||||
// directory scans or access outside the project are supported.
|
||||
if len(cfg.MappingFiles) > 64 {
|
||||
return cfg, false, errors.New("omni.gen.json: at most 64 mappingFiles supported")
|
||||
}
|
||||
digest := sha256.New()
|
||||
fmt.Fprintf(digest, "%s:%d:", generatorConfigFile, len(raw))
|
||||
digest.Write(raw)
|
||||
total := len(raw)
|
||||
for i := range cfg.Mappings {
|
||||
cfg.Mappings[i].origin = generatorConfigFile
|
||||
}
|
||||
seen := map[string]bool{generatorConfigFile: true}
|
||||
for _, name := range cfg.MappingFiles {
|
||||
key := strings.ToLower(name)
|
||||
if seen[key] {
|
||||
return cfg, false, fmt.Errorf("omni.gen.json: duplicate mapping file %q", name)
|
||||
}
|
||||
seen[key] = true
|
||||
if !strings.HasSuffix(name, ".json") {
|
||||
return cfg, false, fmt.Errorf("mapping file %q must have a .json extension", name)
|
||||
}
|
||||
data, err := readMappingConfigFile(root, name)
|
||||
if err != nil {
|
||||
return cfg, false, err
|
||||
}
|
||||
total += len(data)
|
||||
if total > 4<<20 {
|
||||
return cfg, false, errors.New("mapping configuration exceeds 4 MiB in total")
|
||||
}
|
||||
var file struct {
|
||||
Mappings []Mapping `json:"mappings"`
|
||||
}
|
||||
if err = apierrors.DecodeCatalogJSON(data, &file); err != nil {
|
||||
return cfg, false, fmt.Errorf("%s: %w (mapping files only support mappings; nested imports are not supported)", name, err)
|
||||
}
|
||||
for i := range file.Mappings {
|
||||
file.Mappings[i].origin = name
|
||||
}
|
||||
cfg.Mappings = append(cfg.Mappings, file.Mappings...)
|
||||
fmt.Fprintf(digest, "%s:%d:", name, len(data))
|
||||
digest.Write(data)
|
||||
}
|
||||
cfg.inputDigest = fmt.Sprintf("%x", digest.Sum(nil))
|
||||
if cfg.Repository != "none" && cfg.Repository != "group" {
|
||||
return cfg, false, errors.New("omni.gen.json: repository must be none or group")
|
||||
}
|
||||
if cfg.Middleware != "factories" && cfg.Middleware != "handlers" {
|
||||
return cfg, false, errors.New("omni.gen.json: middleware must be factories or handlers")
|
||||
}
|
||||
if len(cfg.Mappings) > 128 {
|
||||
return cfg, false, errors.New("omni.gen.json: at most 128 mappings supported")
|
||||
}
|
||||
return cfg, true, nil
|
||||
}
|
||||
|
||||
// Read through a bounded reader as well as checking stat: a growing file cannot
|
||||
// make configuration loading unbounded. safePath also rejects symlink escapes.
|
||||
func readMappingConfigFile(root, name string) ([]byte, error) {
|
||||
if err := safePath(root, name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f, err := os.Open(filepath.Join(root, filepath.FromSlash(name)))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", name, err)
|
||||
}
|
||||
defer f.Close()
|
||||
stat, err := f.Stat()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !stat.Mode().IsRegular() || stat.Size() > 1<<20 {
|
||||
return nil, fmt.Errorf("%s must be a regular file of at most 1 MiB", name)
|
||||
}
|
||||
raw, err := io.ReadAll(io.LimitReader(f, (1<<20)+1))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(raw) > 1<<20 {
|
||||
return nil, fmt.Errorf("%s exceeds 1 MiB", name)
|
||||
}
|
||||
if trimmed := strings.TrimSpace(string(raw)); !strings.HasPrefix(trimmed, "{") {
|
||||
return nil, fmt.Errorf("%s must contain a JSON object", name)
|
||||
}
|
||||
return raw, nil
|
||||
}
|
||||
141
cmd/go-omni/apigen/generatorConfig_test.go
Normal file
141
cmd/go-omni/apigen/generatorConfig_test.go
Normal file
@@ -0,0 +1,141 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMappingFileConfiguration(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
defs := nullableMappings()
|
||||
writeGeneratorConfig(t, dir, GeneratorConfig{Repository: "none", Middleware: "factories", Mappings: defs[:1], MappingFiles: []string{"mappings/a.json", "mappings/b.json"}})
|
||||
writeMappingFile(t, dir, "mappings/a.json", defs[1:5])
|
||||
writeMappingFile(t, dir, "mappings/b.json", defs[5:])
|
||||
first, exists, err := readGeneratorConfig(dir)
|
||||
if err != nil || !exists || len(first.Mappings) != len(defs) {
|
||||
t.Fatal(first, err)
|
||||
}
|
||||
for i, m := range first.Mappings {
|
||||
if m.Name != defs[i].Name {
|
||||
t.Fatal("non-deterministic load order")
|
||||
}
|
||||
}
|
||||
second, _, err := readGeneratorConfig(dir)
|
||||
if err != nil || first.inputDigest != second.inputDigest {
|
||||
t.Fatal("unstable config digest", err)
|
||||
}
|
||||
path := filepath.Join(dir, "mappings/a.json")
|
||||
writeFile(t, path, readFile(t, path)+"\n")
|
||||
changed, _, err := readGeneratorConfig(dir)
|
||||
if err != nil || changed.inputDigest == first.inputDigest {
|
||||
t.Fatal("child file change invisible to generation guard", err)
|
||||
}
|
||||
// Duplicate definitions name both files instead of silently overriding.
|
||||
writeMappingFile(t, dir, "mappings/b.json", defs[1:2])
|
||||
cfg, _, err := readGeneratorConfig(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = validateMappings(cfg.Mappings, "example.com/nullable")
|
||||
if err == nil || !strings.Contains(err.Error(), "mappings/a.json") || !strings.Contains(err.Error(), "mappings/b.json") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMappingFileRejection(t *testing.T) {
|
||||
for _, tc := range []struct{ name, root, child, want string }{
|
||||
{"missing", `{"mappingFiles":["maps.json"]}`, "", "maps.json"},
|
||||
{"escape", `{"mappingFiles":["../maps.json"]}`, "", "unsafe"},
|
||||
{"absolute", `{"mappingFiles":["/maps.json"]}`, "", "unsafe"},
|
||||
{"nonCanonical", `{"mappingFiles":["sub/../maps.json"]}`, "", "unsafe"},
|
||||
{"duplicate", `{"mappingFiles":["maps.json","maps.json"]}`, `{"mappings":[]}`, "duplicate mapping file"},
|
||||
{"caseDuplicate", `{"mappingFiles":["maps.json","MAPS.json"]}`, `{"mappings":[]}`, "duplicate mapping file"},
|
||||
{"self", `{"mappingFiles":["omni.gen.json"]}`, "", "duplicate mapping file"},
|
||||
{"recursive", `{"mappingFiles":["maps.json"]}`, `{"mappingFiles":["omni.gen.json"]}`, "nested imports are not supported"},
|
||||
{"childSettings", `{"mappingFiles":["maps.json"]}`, `{"repository":"none","mappings":[]}`, "maps.json"},
|
||||
{"duplicateKeys", `{"mappingFiles":["maps.json"]}`, `{"mappings":[],"mappings":[]}`, "duplicate key"},
|
||||
{"rootNull", `null`, "", "JSON object"},
|
||||
{"childNull", `{"mappingFiles":["maps.json"]}`, `null`, "JSON object"},
|
||||
{"unknownPolicy", `{"mappingFiles":["maps.json"]}`, `{"mappings":[{"unknown":true}]}`, "unknown field"},
|
||||
{"trailingValue", `{"mappingFiles":["maps.json"]}`, `{"mappings":[]} {}`, "maps.json"},
|
||||
{"tooLarge", `{"mappingFiles":["maps.json"]}`, strings.Repeat(" ", 1<<20) + `{}`, "1 MiB"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
writeFile(t, filepath.Join(dir, generatorConfigFile), tc.root)
|
||||
if tc.child != "" {
|
||||
writeFile(t, filepath.Join(dir, "maps.json"), tc.child)
|
||||
}
|
||||
before := snapshot(t, dir)
|
||||
_, _, err := readGeneratorConfig(dir)
|
||||
if err == nil || !strings.Contains(err.Error(), tc.want) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !sameFiles(before, snapshot(t, dir)) {
|
||||
t.Fatal("read changed configuration")
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, directory := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("symlinkDirectory%t", directory), func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
outside := t.TempDir()
|
||||
writeMappingFile(t, outside, "maps.json", nil)
|
||||
name := "maps.json"
|
||||
target := filepath.Join(outside, name)
|
||||
link := filepath.Join(dir, name)
|
||||
if directory {
|
||||
name = "linked/maps.json"
|
||||
target = outside
|
||||
link = filepath.Join(dir, "linked")
|
||||
}
|
||||
if err := os.Symlink(target, link); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeGeneratorConfig(t, dir, GeneratorConfig{Repository: "none", Middleware: "factories", MappingFiles: []string{name}})
|
||||
if _, _, err := readGeneratorConfig(dir); err == nil || !strings.Contains(err.Error(), "symlink") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMappingFileBounds(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
files := make([]string, 65)
|
||||
for i := range files {
|
||||
files[i] = fmt.Sprintf("maps%d.json", i)
|
||||
}
|
||||
writeGeneratorConfig(t, dir, GeneratorConfig{Repository: "none", Middleware: "factories", MappingFiles: files})
|
||||
if _, _, err := readGeneratorConfig(dir); err == nil || !strings.Contains(err.Error(), "64") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeGeneratorConfig(t, dir, GeneratorConfig{Repository: "none", Middleware: "factories", MappingFiles: files[:4]})
|
||||
for _, name := range files[:4] {
|
||||
writeFile(t, filepath.Join(dir, name), `{"mappings":[]}`+strings.Repeat(" ", (1<<20)-len(`{"mappings":[]}`)))
|
||||
}
|
||||
if _, _, err := readGeneratorConfig(dir); err == nil || !strings.Contains(err.Error(), "4 MiB") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// The definition limit applies after merging, not separately to each file.
|
||||
defs := make([]Mapping, 129)
|
||||
for i := range defs {
|
||||
defs[i] = nullableMappings()[0]
|
||||
defs[i].Name = fmt.Sprintf("Map%d", i)
|
||||
}
|
||||
writeGeneratorConfig(t, dir, GeneratorConfig{Repository: "none", Middleware: "factories", MappingFiles: files[:2]})
|
||||
writeMappingFile(t, dir, files[0], defs[:65])
|
||||
writeMappingFile(t, dir, files[1], defs[65:])
|
||||
if _, _, err := readGeneratorConfig(dir); err == nil || !strings.Contains(err.Error(), "128") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Origins/digests are loader metadata, never serialized into user configuration.
|
||||
raw, err := json.Marshal(GeneratorConfig{inputDigest: "private"})
|
||||
if err != nil || strings.Contains(string(raw), "private") {
|
||||
t.Fatal(string(raw), err)
|
||||
}
|
||||
}
|
||||
240
cmd/go-omni/apigen/logicCheck.go
Normal file
240
cmd/go-omni/apigen/logicCheck.go
Normal file
@@ -0,0 +1,240 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/build"
|
||||
"go/format"
|
||||
goparser "go/parser"
|
||||
"go/scanner"
|
||||
gotoken "go/token"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type logicMethod struct {
|
||||
path, signature string
|
||||
line int
|
||||
known bool
|
||||
}
|
||||
|
||||
// checkLogic inspects source only: no project code, init hooks, Go commands or
|
||||
// dependency downloads are executed. Seeds participate only when files do not
|
||||
// already exist, matching apply's ownership rules.
|
||||
func checkLogic(root, module string, contract *contractSnapshot, seeds map[string][]byte) ([]Diagnostic, map[string]logicMethod) {
|
||||
groups := map[string][]routeContract{}
|
||||
knownTypes := map[string]bool{}
|
||||
for _, r := range contract.Routes {
|
||||
if !r.Manual {
|
||||
groups[r.Group] = append(groups[r.Group], r)
|
||||
}
|
||||
for name := range r.Types {
|
||||
knownTypes[name] = true
|
||||
}
|
||||
}
|
||||
// Project-owned aliases may make differently named DTOs identical. Let
|
||||
// the compiler handle such cases rather than rejecting valid Go aliases.
|
||||
typeExtensions := false
|
||||
if entries, err := os.ReadDir(filepath.Join(root, "internal/types")); err == nil {
|
||||
for _, e := range entries {
|
||||
if sourceFile(e.Name()) && !ownedPath("internal/types/"+e.Name()) {
|
||||
typeExtensions = true
|
||||
}
|
||||
}
|
||||
}
|
||||
var diagnostics []Diagnostic
|
||||
locations := map[string]logicMethod{}
|
||||
for group, routes := range groups {
|
||||
directory := "internal/logic/" + group
|
||||
files := map[string][]byte{}
|
||||
uncertain := false
|
||||
entries, err := os.ReadDir(filepath.Join(root, filepath.FromSlash(directory)))
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
diagnostics = append(diagnostics, Diagnostic{Severity: "error", Code: "logicUnreadable", Path: directory, Message: err.Error()})
|
||||
continue
|
||||
}
|
||||
for _, e := range entries {
|
||||
if e.IsDir() || !sourceFile(e.Name()) {
|
||||
continue
|
||||
}
|
||||
name := directory + "/" + e.Name()
|
||||
if err := safePath(root, name); err != nil {
|
||||
diagnostics = append(diagnostics, Diagnostic{Severity: "error", Code: "logicUnreadable", Path: name, Message: err.Error()})
|
||||
uncertain = true
|
||||
continue
|
||||
}
|
||||
data, err := os.ReadFile(filepath.Join(root, filepath.FromSlash(name)))
|
||||
if err != nil {
|
||||
diagnostics = append(diagnostics, Diagnostic{Severity: "error", Code: "logicUnreadable", Path: name, Message: err.Error()})
|
||||
uncertain = true
|
||||
continue
|
||||
}
|
||||
files[name] = data
|
||||
}
|
||||
for name, data := range seeds {
|
||||
if filepath.ToSlash(filepath.Dir(name)) == directory && sourceFile(filepath.Base(name)) {
|
||||
if _, exists := files[name]; !exists {
|
||||
files[name] = data
|
||||
}
|
||||
}
|
||||
}
|
||||
methods := map[string][]logicMethod{}
|
||||
var names []string
|
||||
for name := range files {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
for _, name := range names {
|
||||
data := files[name]
|
||||
// Do not guess the user's GOFLAGS/build-tag combination. Flag the
|
||||
// group for compiler verification if conditional files are present.
|
||||
if _, err := os.Stat(filepath.Join(root, filepath.FromSlash(name))); err == nil {
|
||||
match, err := build.Default.MatchFile(filepath.Join(root, filepath.FromSlash(directory)), filepath.Base(name))
|
||||
if err != nil || !match {
|
||||
uncertain = true
|
||||
continue
|
||||
}
|
||||
}
|
||||
positions := gotoken.NewFileSet()
|
||||
file, err := goparser.ParseFile(positions, name, data, goparser.SkipObjectResolution|goparser.ParseComments)
|
||||
if err != nil {
|
||||
d := Diagnostic{Severity: "error", Code: "logicSyntax", Path: name, Message: "cannot parse Logic source: " + err.Error()}
|
||||
if list, ok := err.(scanner.ErrorList); ok && len(list) > 0 {
|
||||
d.Line = list[0].Pos.Line
|
||||
}
|
||||
diagnostics = append(diagnostics, d)
|
||||
uncertain = true
|
||||
continue
|
||||
}
|
||||
conditional := false
|
||||
for _, group := range file.Comments {
|
||||
for _, comment := range group.List {
|
||||
if comment.Pos() < file.Package && (strings.HasPrefix(comment.Text, "//go:build ") || strings.HasPrefix(comment.Text, "// +build ")) {
|
||||
conditional = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if conditional {
|
||||
uncertain = true
|
||||
continue
|
||||
}
|
||||
imports := map[string]string{}
|
||||
for _, imp := range file.Imports {
|
||||
path, err := strconv.Unquote(imp.Path.Value)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
alias := filepath.Base(path)
|
||||
if imp.Name != nil {
|
||||
alias = imp.Name.Name
|
||||
}
|
||||
imports[alias] = path
|
||||
}
|
||||
for _, decl := range file.Decls {
|
||||
fn, ok := decl.(*ast.FuncDecl)
|
||||
if !ok || fn.Recv == nil || len(fn.Recv.List) != 1 {
|
||||
continue
|
||||
}
|
||||
receiver := fn.Recv.List[0].Type
|
||||
if pointer, ok := receiver.(*ast.StarExpr); ok {
|
||||
receiver = pointer.X
|
||||
}
|
||||
id, ok := receiver.(*ast.Ident)
|
||||
if !ok || id.Name != "Logic" {
|
||||
continue
|
||||
}
|
||||
params, a := canonicalFields(fn.Type.Params, imports, module, knownTypes, typeExtensions)
|
||||
results, b := canonicalFields(fn.Type.Results, imports, module, knownTypes, typeExtensions)
|
||||
methods[fn.Name.Name] = append(methods[fn.Name.Name], logicMethod{name, displaySignature(fn.Name.Name, params, results), positions.Position(fn.Pos()).Line, a && b})
|
||||
}
|
||||
}
|
||||
for _, r := range routes {
|
||||
found := methods[upper(r.Handler)]
|
||||
if len(found) == 1 && !uncertain {
|
||||
locations[r.file()] = found[0]
|
||||
}
|
||||
d := Diagnostic{Path: r.file(), Operation: r.operation(), After: r.Signature}
|
||||
switch {
|
||||
case uncertain:
|
||||
d.Severity, d.Code, d.Message = "review", "logicUnchecked", "conditional or unreadable source prevents a complete signature check; run go test ./... with your build settings"
|
||||
case len(found) == 0:
|
||||
d.Severity, d.Code, d.Message = "review", "logicNotFound", "no direct Logic method found; check embedded/aliased implementations with go test ./..."
|
||||
case len(found) > 1:
|
||||
d.Severity, d.Code, d.Message = "error", "logicDuplicate", "multiple Logic methods have this name"
|
||||
for _, m := range found {
|
||||
d.Details = append(d.Details, fmt.Sprintf("%s:%d", m.path, m.line))
|
||||
}
|
||||
case !found[0].known:
|
||||
d.Severity, d.Code, d.Message = "review", "logicUnchecked", "signature uses project-specific type names; verify aliases with go test ./..."
|
||||
d.Path, d.Line, d.Before = found[0].path, found[0].line, found[0].signature
|
||||
case found[0].signature != r.Signature:
|
||||
d.Severity, d.Code, d.Message = "error", "logicSignature", "preserved Logic signature differs from the generated call; update this method"
|
||||
d.Path, d.Line, d.Before = found[0].path, found[0].line, found[0].signature
|
||||
default:
|
||||
continue
|
||||
}
|
||||
diagnostics = append(diagnostics, d)
|
||||
}
|
||||
}
|
||||
return diagnostics, locations
|
||||
}
|
||||
|
||||
func sourceFile(name string) bool {
|
||||
return strings.HasSuffix(name, ".go") && !strings.HasSuffix(name, "_test.go") && !strings.HasPrefix(name, ".") && !strings.HasPrefix(name, "_")
|
||||
}
|
||||
|
||||
func canonicalFields(fields *ast.FieldList, imports map[string]string, module string, knownTypes map[string]bool, extensions bool) ([]string, bool) {
|
||||
var values []string
|
||||
known := true
|
||||
if fields != nil {
|
||||
for _, field := range fields.List {
|
||||
value, ok := canonicalType(field.Type, imports, module, knownTypes, extensions)
|
||||
known = known && ok
|
||||
count := len(field.Names)
|
||||
if count == 0 {
|
||||
count = 1
|
||||
}
|
||||
for i := 0; i < count; i++ {
|
||||
values = append(values, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
return values, known
|
||||
}
|
||||
|
||||
func canonicalType(expr ast.Expr, imports map[string]string, module string, knownTypes map[string]bool, extensions bool) (string, bool) {
|
||||
switch e := expr.(type) {
|
||||
case *ast.ParenExpr:
|
||||
return canonicalType(e.X, imports, module, knownTypes, extensions)
|
||||
case *ast.StarExpr:
|
||||
value, ok := canonicalType(e.X, imports, module, knownTypes, extensions)
|
||||
return "*" + value, ok
|
||||
case *ast.Ellipsis:
|
||||
value, ok := canonicalType(e.Elt, imports, module, knownTypes, extensions)
|
||||
return "..." + value, ok
|
||||
case *ast.Ident:
|
||||
switch e.Name {
|
||||
case "error", "bool", "string", "int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64", "uintptr", "float32", "float64", "complex64", "complex128", "any":
|
||||
return e.Name, true
|
||||
}
|
||||
case *ast.SelectorExpr:
|
||||
if pkg, ok := e.X.(*ast.Ident); ok {
|
||||
switch imports[pkg.Name] {
|
||||
case "context":
|
||||
return "context." + e.Sel.Name, true
|
||||
case frameworkModule + "/httpx":
|
||||
return "httpx." + e.Sel.Name, true
|
||||
case module + "/internal/types":
|
||||
return "types." + e.Sel.Name, knownTypes[e.Sel.Name] || !extensions
|
||||
}
|
||||
}
|
||||
}
|
||||
var b bytes.Buffer
|
||||
_ = format.Node(&b, gotoken.NewFileSet(), expr)
|
||||
return b.String(), false
|
||||
}
|
||||
218
cmd/go-omni/apigen/managed_test.go
Normal file
218
cmd/go-omni/apigen/managed_test.go
Normal file
@@ -0,0 +1,218 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestManagedDefaultValidation(t *testing.T) {
|
||||
source := readFile(t, filepath.Join(frameworkRoot(t), "examples/contracts/managed.api"))
|
||||
spec, err := parseText(t, source)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c, err := Compile(spec)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if spec.Routes[0].ResponseFormat != "" {
|
||||
t.Fatal("compile mutated source")
|
||||
}
|
||||
var doc map[string]any
|
||||
if err = json.Unmarshal(c.Document, &doc); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if doc["x-omni-default-response"] != "CommonResponse" {
|
||||
t.Fatal("missing default metadata")
|
||||
}
|
||||
for _, bad := range []string{
|
||||
strings.Replace(source, "defaultResponse: CommonResponse", "defaultResponse: Missing", 1),
|
||||
source + "\n@app (defaultResponse: CommonResponse)\n",
|
||||
strings.Replace(source, "maxBytes: 2048", "maxBytes: 0", 1),
|
||||
strings.Replace(source, "@produces \"text/plain\"", "@produces \"text/plain; charset=utf-8\"", 1),
|
||||
} {
|
||||
s, err := parseText(t, bad)
|
||||
if err == nil {
|
||||
_, err = Compile(s)
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatal("invalid contract accepted")
|
||||
}
|
||||
}
|
||||
root := t.TempDir()
|
||||
writeFile(t, filepath.Join(root, "service.api"), "import \"child.api\"\nservice demo {\n@handler hello\nget /hello\n}")
|
||||
writeFile(t, filepath.Join(root, "child.api"), "@app (defaultResponse: Shared)\nresponse Shared ()")
|
||||
if _, err = Parse(filepath.Join(root, "service.api")); err == nil {
|
||||
t.Fatal("import owns App defaults")
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagedGeneratedProject(t *testing.T) {
|
||||
root := frameworkRoot(t)
|
||||
dir := filepath.Join(t.TempDir(), "managed")
|
||||
o := Options{Dir: dir, API: filepath.Join(root, "examples/contracts/managed.api"), Module: "example.com/managed", FrameworkDir: root}
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
o.API = filepath.Join(dir, "api/managed.api")
|
||||
for _, path := range []string{"cmd/server/main.go", "internal/settings", "internal/servicecontext", "internal/service"} {
|
||||
if _, err := os.Stat(filepath.Join(dir, path)); !os.IsNotExist(err) {
|
||||
t.Fatalf("old layout remains: %s", path)
|
||||
}
|
||||
}
|
||||
mainBefore := readFile(t, filepath.Join(dir, "main.go"))
|
||||
files := map[string]string{
|
||||
"consumer_test.go": "consumer_test.go", "internal/svc/options.go": "options.go", "internal/svc/client.go": "client.go", "internal/logic/demo/check.go": "operations.go",
|
||||
"shutdown_test.go": "shutdown_test.go",
|
||||
"phases_test.go": "phases_test.go", "internal/svc/background.go": "background.go",
|
||||
}
|
||||
for path, fixture := range files {
|
||||
writeFile(t, filepath.Join(dir, path), readFile(t, filepath.Join("testdata/managed", fixture)))
|
||||
}
|
||||
for _, name := range []string{"other", "rawJSON", "upload", "download", "callback", "slow"} {
|
||||
writeFile(t, filepath.Join(dir, "internal/logic/demo", name+".go"), "package demo\n")
|
||||
}
|
||||
configPath := filepath.Join(dir, "internal/config/config.go")
|
||||
writeFile(t, configPath, strings.Replace(readFile(t, configPath), "type Config struct {", "type Config struct {\n BackgroundMode string `json:\"backgroundMode\"`\n FailInit bool `json:\"failInit\"`\n ShutdownInit bool `json:\"shutdownInit\"`", 1))
|
||||
svcPath := filepath.Join(dir, "internal/svc/serviceContext.go")
|
||||
source := strings.Replace(readFile(t, svcPath), "type ServiceContext struct {", "type ServiceContext struct {\n Client *Client", 1)
|
||||
source = strings.Replace(source, "return nil", "return initClient(ctx,c)", 1)
|
||||
writeFile(t, svcPath, source)
|
||||
// Independent tooling stays outside generator ownership.
|
||||
writeFile(t, filepath.Join(dir, "cmd/migrate/main.go"), "package main\nfunc main(){}\n")
|
||||
before := snapshot(t, dir)
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !sameFiles(before, snapshot(t, dir)) {
|
||||
t.Fatal("regeneration changed handwritten files")
|
||||
}
|
||||
o.Check = true
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
o.Check = false
|
||||
// Add an API endpoint: handler and Logic stub appear without touching main or business code.
|
||||
api := readFile(t, o.API)
|
||||
writeFile(t, o.API, api+"\n@server (group: extra)\nservice managed-api {\n@handler extra\nget /extra\n}\n")
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if readFile(t, filepath.Join(dir, "main.go")) != mainBefore {
|
||||
t.Fatal("main requires business-specific edits")
|
||||
}
|
||||
for path := range files {
|
||||
if readFile(t, filepath.Join(dir, path)) != string(before[path]) {
|
||||
t.Fatal("business file changed", path)
|
||||
}
|
||||
}
|
||||
// Main is owned, checked, and restored when missing. Hand edits are a conflict.
|
||||
writeFile(t, filepath.Join(dir, "main.go"), mainBefore+"\n// drift\n")
|
||||
o.Check = true
|
||||
if err := generateManagedTestProject(t, o); err == nil {
|
||||
t.Fatal("main drift accepted")
|
||||
}
|
||||
o.Check = false
|
||||
if err := os.Remove(filepath.Join(dir, "main.go")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
o.Check = true
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
|
||||
defer cancel()
|
||||
for _, args := range [][]string{{"test", "-race", "-mod=mod", "-count=1", "./..."}, {"build", "-mod=mod", "."}} {
|
||||
cmd := exec.CommandContext(ctx, "go", args...)
|
||||
cmd.Dir = dir
|
||||
cmd.Env = append(os.Environ(), "GOWORK=off")
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("%v: %v\n%s", args, err, out)
|
||||
}
|
||||
}
|
||||
if os.Getenv("GO_OMNI_RELEASE_CLI") != "" {
|
||||
cmd := exec.CommandContext(ctx, "go", "list", "-m", "-json", frameworkModule)
|
||||
cmd.Dir = dir
|
||||
cmd.Env = append(os.Environ(), "GOWORK=off")
|
||||
data, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatal(err, string(data))
|
||||
}
|
||||
var dependency struct {
|
||||
Version, Sum string
|
||||
Replace any
|
||||
}
|
||||
if err = json.Unmarshal(data, &dependency); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if dependency.Version != os.Getenv("GO_OMNI_RELEASE_VERSION") || dependency.Replace != nil || dependency.Sum == "" {
|
||||
t.Fatal("not a fixed remote consumer")
|
||||
}
|
||||
t.Logf("remote framework %s checksum %s; no local replace", dependency.Version, dependency.Sum)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func generateManagedTestProject(t *testing.T, o Options) error {
|
||||
t.Helper()
|
||||
cli := os.Getenv("GO_OMNI_RELEASE_CLI")
|
||||
if cli == "" {
|
||||
_, err := Generate(o)
|
||||
return err
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
||||
defer cancel()
|
||||
args := []string{"api", "generate", "--api", o.API, "--dir", o.Dir, "--module", o.Module}
|
||||
if o.Check {
|
||||
args = append(args, "--check")
|
||||
}
|
||||
if o.DryRun {
|
||||
args = append(args, "--dry-run")
|
||||
}
|
||||
cmd := exec.CommandContext(ctx, cli, args...)
|
||||
cmd.Dir = filepath.Dir(o.Dir)
|
||||
if data, err := cmd.CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("released CLI: %w\n%s", err, data)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestManagedOwnershipRejectsOldOrHandwrittenEntry(t *testing.T) {
|
||||
for _, oldLayout := range []bool{false, true} {
|
||||
t.Run(map[bool]string{false: "handwrittenMain", true: "oldManifest"}[oldLayout], func(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "app")
|
||||
if _, err := New(Options{Dir: dir, Module: "example.com/entry", FrameworkDir: frameworkRoot(t)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var m manifest
|
||||
if err := json.Unmarshal([]byte(readFile(t, filepath.Join(dir, manifestPath))), &m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if oldLayout {
|
||||
m.Version = 1
|
||||
} else {
|
||||
delete(m.Generated, "main.go")
|
||||
writeFile(t, filepath.Join(dir, "main.go"), "package main\nfunc main(){}\n")
|
||||
}
|
||||
data, _ := json.Marshal(m)
|
||||
writeFile(t, filepath.Join(dir, manifestPath), string(data))
|
||||
before := snapshot(t, dir)
|
||||
if _, err := Generate(Options{Dir: dir, API: filepath.Join(dir, "api/service.api")}); err == nil {
|
||||
t.Fatal("unowned layout accepted")
|
||||
}
|
||||
if !sameFiles(before, snapshot(t, dir)) {
|
||||
t.Fatal("rejected migration modified project")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
453
cmd/go-omni/apigen/mapping.go
Normal file
453
cmd/go-omni/apigen/mapping.go
Normal file
@@ -0,0 +1,453 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/format"
|
||||
goparser "go/parser"
|
||||
gotoken "go/token"
|
||||
"go/types"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/mod/module"
|
||||
)
|
||||
|
||||
// GoTypeRef names an existing, exported, non-generic Go struct.
|
||||
type GoTypeRef struct {
|
||||
Package string `json:"package"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
type GoFunctionRef struct {
|
||||
Package string `json:"package"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
type MappingField struct {
|
||||
Source string `json:"source"`
|
||||
Convert *GoFunctionRef `json:"convert,omitempty"`
|
||||
Mapping string `json:"mapping,omitempty"`
|
||||
Elements bool `json:"elements,omitempty"`
|
||||
Nil string `json:"nil,omitempty"`
|
||||
}
|
||||
type Mapping struct {
|
||||
Name string `json:"name"`
|
||||
PointerResult bool `json:"pointerResult,omitempty"`
|
||||
origin string
|
||||
Source GoTypeRef `json:"source"`
|
||||
Target GoTypeRef `json:"target"`
|
||||
Fields map[string]MappingField `json:"fields,omitempty"`
|
||||
IgnoreSource map[string]string `json:"ignoreSource,omitempty"`
|
||||
IgnoreTarget map[string]string `json:"ignoreTarget,omitempty"`
|
||||
}
|
||||
type mappingField struct {
|
||||
path, name string
|
||||
typ types.Type
|
||||
}
|
||||
type mappingTypes struct{ source, target types.Type }
|
||||
type mappingRenderer struct {
|
||||
importer types.Importer
|
||||
imports map[string]string
|
||||
definitions map[string]Mapping
|
||||
types map[string]mappingTypes
|
||||
}
|
||||
|
||||
func renderMappings(o Options, generated, seeds map[string][]byte) error {
|
||||
if len(o.generation.Mappings) == 0 {
|
||||
return nil
|
||||
}
|
||||
refs, err := validateMappings(o.generation.Mappings, o.Module)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
importer, close, err := loadMappingTypes(o, refs, generated, seeds)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer close()
|
||||
r := mappingRenderer{importer: importer, imports: map[string]string{}, definitions: map[string]Mapping{}, types: map[string]mappingTypes{}}
|
||||
for _, m := range o.generation.Mappings {
|
||||
source, err := r.lookup(m.Source)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
target, err := r.lookup(m.Target)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.definitions[m.Name] = m
|
||||
r.types[m.Name] = mappingTypes{source, target}
|
||||
}
|
||||
names := make([]string, 0, len(r.definitions))
|
||||
for name := range r.definitions {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
var body strings.Builder
|
||||
for _, name := range names {
|
||||
source, err := r.render(r.definitions[name])
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: mapping %s: %w", mappingOrigin(r.definitions[name]), name, err)
|
||||
}
|
||||
body.WriteString(source)
|
||||
}
|
||||
imports := make([]string, 0, len(r.imports))
|
||||
for path := range r.imports {
|
||||
imports = append(imports, path)
|
||||
}
|
||||
sort.Strings(imports)
|
||||
text := generatedHeader + "package mapping\nimport(\n"
|
||||
for _, path := range imports {
|
||||
text += r.imports[path] + " " + strconv.Quote(path) + "\n"
|
||||
}
|
||||
text += ")\n" + body.String()
|
||||
code, err := format.Source([]byte(text))
|
||||
if err != nil {
|
||||
return fmt.Errorf("format mappings: %w", err)
|
||||
}
|
||||
// The generated package is type-checked before it is written. No application
|
||||
// code (including init or converters) is executed during generation.
|
||||
fset := gotoken.NewFileSet()
|
||||
file, err := goparser.ParseFile(fset, "mappingsGen.go", code, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
check := types.Config{Importer: importer}
|
||||
if _, err = check.Check(o.Module+"/internal/mapping", fset, []*ast.File{file}, nil); err != nil {
|
||||
return fmt.Errorf("generated mapping type check: %w", err)
|
||||
}
|
||||
generated["internal/mapping/mappingsGen.go"] = code
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateMappings(defs []Mapping, project string) ([]string, error) {
|
||||
refs := map[string]bool{}
|
||||
names := map[string]string{}
|
||||
validPackage := func(path string) bool {
|
||||
if module.CheckImportPath(path) != nil || path == project+"/internal/mapping" {
|
||||
return false
|
||||
}
|
||||
// go/types does not enforce the compiler's internal import visibility.
|
||||
if strings.HasPrefix(path, "internal/") {
|
||||
return false
|
||||
}
|
||||
if i := strings.LastIndex(path, "/internal/"); i >= 0 {
|
||||
return strings.HasPrefix(project+"/internal/mapping", path[:i]+"/")
|
||||
}
|
||||
return true
|
||||
}
|
||||
for _, m := range defs {
|
||||
if !gotoken.IsIdentifier(m.Name) || !gotoken.IsExported(m.Name) {
|
||||
return nil, fmt.Errorf("mapping name %q must be unique and exported", m.Name)
|
||||
}
|
||||
functions := []string{m.Name}
|
||||
if m.PointerResult {
|
||||
functions = append(functions, m.Name+"Ptr")
|
||||
}
|
||||
for _, name := range functions {
|
||||
if prior, exists := names[name]; exists {
|
||||
return nil, fmt.Errorf("mapping function %q must be unique: %s conflicts with %s", name, mappingOrigin(m), prior)
|
||||
}
|
||||
names[name] = mappingOrigin(m)
|
||||
}
|
||||
for _, ref := range []GoTypeRef{m.Source, m.Target} {
|
||||
if !validPackage(ref.Package) || !gotoken.IsIdentifier(ref.Type) || !gotoken.IsExported(ref.Type) {
|
||||
return nil, fmt.Errorf("mapping %s: invalid Go type reference", m.Name)
|
||||
}
|
||||
refs[ref.Package] = true
|
||||
}
|
||||
if len(m.Fields) > 4096 || len(m.IgnoreSource) > 4096 || len(m.IgnoreTarget) > 4096 {
|
||||
return nil, fmt.Errorf("mapping %s: field rule limit exceeded", m.Name)
|
||||
}
|
||||
for target, f := range m.Fields {
|
||||
if target == "" || f.Source == "" {
|
||||
return nil, fmt.Errorf("mapping %s: explicit field rule requires target and source paths", m.Name)
|
||||
}
|
||||
if f.Nil != "" && f.Nil != "preserve" && f.Nil != "zero" && f.Nil != "error" {
|
||||
return nil, fmt.Errorf("mapping %s.%s: nil must be preserve, zero or error", m.Name, target)
|
||||
}
|
||||
if f.Convert != nil && f.Nil != "" {
|
||||
return nil, fmt.Errorf("mapping %s.%s: nil policy cannot be combined with a converter; the converter owns nil behavior", m.Name, target)
|
||||
}
|
||||
if f.Convert != nil {
|
||||
if f.Mapping != "" || !validPackage(f.Convert.Package) || !gotoken.IsIdentifier(f.Convert.Name) || !gotoken.IsExported(f.Convert.Name) {
|
||||
return nil, fmt.Errorf("mapping %s.%s: invalid or conflicting converter", m.Name, target)
|
||||
}
|
||||
refs[f.Convert.Package] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
visiting, done := map[string]bool{}, map[string]bool{}
|
||||
byName := map[string]Mapping{}
|
||||
for _, m := range defs {
|
||||
byName[m.Name] = m
|
||||
}
|
||||
var visit func(string) error
|
||||
visit = func(name string) error {
|
||||
if done[name] {
|
||||
return nil
|
||||
}
|
||||
if visiting[name] {
|
||||
return fmt.Errorf("mapping cycle at %s", name)
|
||||
}
|
||||
m, ok := byName[name]
|
||||
if !ok {
|
||||
return fmt.Errorf("unknown mapping %s", name)
|
||||
}
|
||||
visiting[name] = true
|
||||
for _, f := range m.Fields {
|
||||
if f.Mapping != "" {
|
||||
if err := visit(f.Mapping); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
delete(visiting, name)
|
||||
done[name] = true
|
||||
return nil
|
||||
}
|
||||
for _, m := range defs {
|
||||
if err := visit(m.Name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
out := make([]string, 0, len(refs))
|
||||
for path := range refs {
|
||||
out = append(out, path)
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (r *mappingRenderer) qualifier(p *types.Package) string {
|
||||
if alias, ok := r.imports[p.Path()]; ok {
|
||||
return alias
|
||||
}
|
||||
alias := fmt.Sprintf("pkg%d", len(r.imports))
|
||||
r.imports[p.Path()] = alias
|
||||
return alias
|
||||
}
|
||||
func (r *mappingRenderer) typeName(t types.Type) string { return types.TypeString(t, r.qualifier) }
|
||||
func (r *mappingRenderer) lookup(ref GoTypeRef) (types.Type, error) {
|
||||
pkg, err := r.importer.Import(ref.Package)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("mapping type %s.%s: %w", ref.Package, ref.Type, err)
|
||||
}
|
||||
obj, ok := pkg.Scope().Lookup(ref.Type).(*types.TypeName)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mapping type %s.%s not found", ref.Package, ref.Type)
|
||||
}
|
||||
typ := obj.Type()
|
||||
if n, ok := typ.(*types.Named); ok && n.TypeParams() != nil && n.TypeParams().Len() > 0 {
|
||||
return nil, fmt.Errorf("mapping root %s must be a non-generic struct", ref.Type)
|
||||
}
|
||||
if _, ok := typ.Underlying().(*types.Struct); !ok {
|
||||
return nil, fmt.Errorf("mapping root %s must be a struct", ref.Type)
|
||||
}
|
||||
return typ, nil
|
||||
}
|
||||
|
||||
func mappingFields(typ types.Type) (map[string]mappingField, error) {
|
||||
out := map[string]mappingField{}
|
||||
leaves := map[string]string{}
|
||||
var walk func(types.Type, string, int) error
|
||||
walk = func(typ types.Type, prefix string, depth int) error {
|
||||
if depth > 32 {
|
||||
return fmt.Errorf("embedded field nesting exceeds 32")
|
||||
}
|
||||
st, ok := typ.Underlying().(*types.Struct)
|
||||
if !ok {
|
||||
return fmt.Errorf("embedded field %s must be a value struct", prefix)
|
||||
}
|
||||
for i := 0; i < st.NumFields(); i++ {
|
||||
f := st.Field(i)
|
||||
path := prefix + f.Name()
|
||||
if !f.Exported() {
|
||||
return fmt.Errorf("unexported field %s cannot be mapped; use an explicit converter on the containing value", path)
|
||||
}
|
||||
if f.Embedded() {
|
||||
if err := walk(f.Type(), path+".", depth+1); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
if previous, exists := leaves[f.Name()]; exists {
|
||||
return fmt.Errorf("ambiguous embedded fields %s and %s", previous, path)
|
||||
}
|
||||
leaves[f.Name()] = path
|
||||
out[path] = mappingField{path, f.Name(), f.Type()}
|
||||
if len(out) > 4096 {
|
||||
return fmt.Errorf("mapping field limit exceeded")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if err := walk(typ, "", 0); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (r *mappingRenderer) render(m Mapping) (string, error) {
|
||||
mt := r.types[m.Name]
|
||||
source, err := mappingFields(mt.source)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
target, err := mappingFields(mt.target)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for key, reason := range m.IgnoreSource {
|
||||
if _, ok := source[key]; !ok || strings.TrimSpace(reason) == "" {
|
||||
return "", fmt.Errorf("ignoreSource %s requires an existing field and reason", key)
|
||||
}
|
||||
}
|
||||
for key, reason := range m.IgnoreTarget {
|
||||
if _, ok := target[key]; !ok || strings.TrimSpace(reason) == "" {
|
||||
return "", fmt.Errorf("ignoreTarget %s requires an existing field and reason", key)
|
||||
}
|
||||
}
|
||||
for key := range m.Fields {
|
||||
if _, ok := target[key]; !ok {
|
||||
return "", fmt.Errorf("unknown target field %s", key)
|
||||
}
|
||||
if _, ignored := m.IgnoreTarget[key]; ignored {
|
||||
return "", fmt.Errorf("target %s is both mapped and ignored", key)
|
||||
}
|
||||
}
|
||||
names := make([]string, 0, len(target))
|
||||
for name := range target {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
used := map[string]bool{}
|
||||
text := fmt.Sprintf("// %s maps one value. Pointer/map/slice assignments retain Go reference semantics.\nfunc %s(source %s)(%s,error){\nvar result %s\n", m.Name, m.Name, r.typeName(mt.source), r.typeName(mt.target), r.typeName(mt.target))
|
||||
for _, path := range names {
|
||||
dst := target[path]
|
||||
if reason, ignored := m.IgnoreTarget[path]; ignored {
|
||||
text += "// Target " + path + " is intentionally left at zero; see omni.gen.json.\n"
|
||||
_ = reason
|
||||
continue
|
||||
}
|
||||
rule, explicit := m.Fields[path]
|
||||
if !explicit {
|
||||
for key, f := range source {
|
||||
if f.name == dst.name {
|
||||
rule.Source = key
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
src, ok := source[rule.Source]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("target %s has no source; add a field rule or ignoreTarget reason", path)
|
||||
}
|
||||
if _, ignored := m.IgnoreSource[src.path]; ignored {
|
||||
return "", fmt.Errorf("source %s is both mapped and ignored", src.path)
|
||||
}
|
||||
used[src.path] = true
|
||||
from, to := src.typ, dst.typ
|
||||
srcExpr, dstExpr := "source."+src.path, "result."+path
|
||||
if rule.Elements {
|
||||
switch d := to.Underlying().(type) {
|
||||
case *types.Slice:
|
||||
s, ok := from.Underlying().(*types.Slice)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("%s: elements requires slice to slice or equal-length array to array", path)
|
||||
}
|
||||
text += fmt.Sprintf("if %s!=nil {\n%s=make(%s,len(%s))\nfor index:=range %s {\n", srcExpr, dstExpr, r.typeName(to), srcExpr, srcExpr)
|
||||
from, to = s.Elem(), d.Elem()
|
||||
case *types.Array:
|
||||
s, ok := from.Underlying().(*types.Array)
|
||||
if !ok || s.Len() != d.Len() {
|
||||
return "", fmt.Errorf("%s: array lengths must match", path)
|
||||
}
|
||||
text += fmt.Sprintf("for index:=range %s {\n", srcExpr)
|
||||
from, to = s.Elem(), d.Elem()
|
||||
default:
|
||||
return "", fmt.Errorf("%s: elements requires a slice or array", path)
|
||||
}
|
||||
srcExpr += "[index]"
|
||||
dstExpr += "[index]"
|
||||
}
|
||||
expr, fallible, err := r.expression(rule, from, to, srcExpr)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%s -> %s: %w", src.path, path, err)
|
||||
}
|
||||
if fallible {
|
||||
fmtPkg, err := r.importer.Import("fmt")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
alias := r.qualifier(fmtPkg)
|
||||
message := m.Name + "." + path + ": %w"
|
||||
args := "err"
|
||||
if rule.Elements {
|
||||
message = m.Name + "." + path + "[%d]: %w"
|
||||
args = "index,err"
|
||||
}
|
||||
text += fmt.Sprintf("{ value,err:=%s\nif err!=nil{var zero %s;return zero,%s.Errorf(%q,%s)}\n%s=value\n}\n", expr, r.typeName(mt.target), alias, message, args, dstExpr)
|
||||
} else {
|
||||
text += dstExpr + "=" + expr + "\n"
|
||||
}
|
||||
if rule.Elements {
|
||||
text += "}\n"
|
||||
if _, ok := dst.typ.Underlying().(*types.Slice); ok {
|
||||
text += "}\n"
|
||||
}
|
||||
}
|
||||
}
|
||||
keys := make([]string, 0, len(source))
|
||||
for key := range source {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, key := range keys {
|
||||
if !used[key] {
|
||||
if _, ignored := m.IgnoreSource[key]; !ignored {
|
||||
return "", fmt.Errorf("unmapped source field %s; map it or provide an ignoreSource reason", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
text += "return result,nil\n}\n"
|
||||
if m.PointerResult {
|
||||
text += fmt.Sprintf("// %sPtr returns nil on conversion failure and otherwise a pointer to the result.\nfunc %sPtr(source %s)(*%s,error){\nvalue,err:=%s(source)\nif err!=nil{return nil,err}\nreturn &value,nil\n}\n", m.Name, m.Name, r.typeName(mt.source), r.typeName(mt.target), m.Name)
|
||||
}
|
||||
return text, nil
|
||||
}
|
||||
|
||||
func (r *mappingRenderer) expression(rule MappingField, from, to types.Type, source string) (string, bool, error) {
|
||||
if rule.Mapping != "" {
|
||||
return r.nestedExpression(rule, from, to, source)
|
||||
}
|
||||
if rule.Nil != "" {
|
||||
return r.dereferenceExpression(rule, from, to, source)
|
||||
}
|
||||
if rule.Convert != nil {
|
||||
pkg, err := r.importer.Import(rule.Convert.Package)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
fn, ok := pkg.Scope().Lookup(rule.Convert.Name).(*types.Func)
|
||||
if !ok {
|
||||
return "", false, fmt.Errorf("converter function %s not found", rule.Convert.Name)
|
||||
}
|
||||
sig := fn.Type().(*types.Signature)
|
||||
if sig.Variadic() || (sig.TypeParams() != nil && sig.TypeParams().Len() > 0) || sig.Params().Len() != 1 || sig.Results().Len() < 1 || sig.Results().Len() > 2 {
|
||||
return "", false, fmt.Errorf("converter must have signature func(Source) Target or func(Source) (Target,error)")
|
||||
}
|
||||
if !types.Identical(sig.Params().At(0).Type(), from) || !types.Identical(sig.Results().At(0).Type(), to) {
|
||||
return "", false, fmt.Errorf("converter signature does not match field types")
|
||||
}
|
||||
fallible := sig.Results().Len() == 2
|
||||
if fallible && !types.Identical(sig.Results().At(1).Type(), types.Universe.Lookup("error").Type()) {
|
||||
return "", false, fmt.Errorf("converter second result must be error")
|
||||
}
|
||||
return r.qualifier(pkg) + "." + rule.Convert.Name + "(" + source + ")", fallible, nil
|
||||
}
|
||||
if !types.Identical(from, to) {
|
||||
return "", false, fmt.Errorf("types %s and %s differ; provide an explicit converter", types.TypeString(from, nil), types.TypeString(to, nil))
|
||||
}
|
||||
return source, false, nil
|
||||
}
|
||||
175
cmd/go-omni/apigen/mappingLoad.go
Normal file
175
cmd/go-omni/apigen/mappingLoad.go
Normal file
@@ -0,0 +1,175 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"go/importer"
|
||||
gotoken "go/token"
|
||||
"go/types"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/mod/modfile"
|
||||
)
|
||||
|
||||
// loadMappingTypes uses the Go compiler's export data, including the pending
|
||||
// DTOs via an overlay. Module files and sum updates remain in a temporary dir.
|
||||
// It compiles only explicitly selected packages and their dependencies; never
|
||||
// runs project code. Build tags/GOOS follow the caller; GOWORK is disabled.
|
||||
func loadMappingTypes(o Options, refs []string, generated, seeds map[string][]byte) (types.Importer, func(), error) {
|
||||
// go list resolves its working directory (notably /var -> /private/var on
|
||||
// macOS). Overlay keys must name that same physical path or stale DTOs win.
|
||||
o.Dir = canonical(o.Dir)
|
||||
temp, err := os.MkdirTemp("", "go-omni-mapping-*")
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
cleanup := func() { _ = os.RemoveAll(temp) }
|
||||
fail := func(err error) (types.Importer, func(), error) { cleanup(); return nil, nil, err }
|
||||
raw, err := os.ReadFile(filepath.Join(o.Dir, "go.mod"))
|
||||
if err != nil {
|
||||
return fail(errors.New("mapping generation requires an existing Go module; scaffold first and resolve dependencies"))
|
||||
}
|
||||
mf, err := modfile.Parse("go.mod", raw, nil)
|
||||
if err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
replacements := append([]*modfile.Replace(nil), mf.Replace...)
|
||||
for _, r := range replacements {
|
||||
if r.New.Version == "" && !filepath.IsAbs(r.New.Path) {
|
||||
if err = mf.AddReplace(r.Old.Path, r.Old.Version, filepath.Join(o.Dir, r.New.Path), ""); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
raw, err = mf.Format()
|
||||
if err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
modPath := filepath.Join(temp, "go.mod")
|
||||
if err = os.WriteFile(modPath, raw, 0600); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
if sums, e := os.ReadFile(filepath.Join(o.Dir, "go.sum")); e == nil {
|
||||
if err = os.WriteFile(filepath.Join(temp, "go.sum"), sums, 0600); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
} else if !errors.Is(e, os.ErrNotExist) {
|
||||
return fail(e)
|
||||
}
|
||||
overlay := map[string]string{}
|
||||
// Hide stale owned Go files, which may otherwise redeclare a renamed DTO.
|
||||
var previous manifest
|
||||
if data, e := os.ReadFile(filepath.Join(o.Dir, manifestPath)); e == nil {
|
||||
if err = json.Unmarshal(data, &previous); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
for name := range previous.Generated {
|
||||
if strings.HasSuffix(name, ".go") {
|
||||
if !ownedPath(name) {
|
||||
return fail(fmt.Errorf("invalid owned path %s", name))
|
||||
}
|
||||
if err = safePath(o.Dir, name); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
if _, ok := generated[name]; !ok {
|
||||
overlay[filepath.Join(o.Dir, filepath.FromSlash(name))] = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if !errors.Is(e, os.ErrNotExist) {
|
||||
return fail(e)
|
||||
}
|
||||
for i, files := range []map[string][]byte{generated, seeds} {
|
||||
names := make([]string, 0, len(files))
|
||||
for name := range files {
|
||||
if strings.HasSuffix(name, ".go") {
|
||||
names = append(names, name)
|
||||
}
|
||||
}
|
||||
sort.Strings(names)
|
||||
for _, name := range names {
|
||||
if err = safePath(o.Dir, name); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
path := filepath.Join(o.Dir, filepath.FromSlash(name))
|
||||
if i == 1 {
|
||||
if _, e := os.Stat(path); e == nil {
|
||||
continue
|
||||
} else if !errors.Is(e, os.ErrNotExist) {
|
||||
return fail(e)
|
||||
}
|
||||
}
|
||||
backing := filepath.Join(temp, fmt.Sprintf("overlay%d.go", len(overlay)))
|
||||
if err = os.WriteFile(backing, files[name], 0600); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
overlay[path] = backing
|
||||
}
|
||||
}
|
||||
raw, err = json.Marshal(map[string]any{"Replace": overlay})
|
||||
if err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
overlayPath := filepath.Join(temp, "overlay.json")
|
||||
if err = os.WriteFile(overlayPath, raw, 0600); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
args := []string{"list", "-deps", "-export", "-json", "-mod=readonly", "-modfile", modPath, "-overlay", overlayPath}
|
||||
// fmt is used for fallible conversions even if neither struct imports it.
|
||||
args = append(args, "fmt")
|
||||
args = append(args, refs...)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "go", args...)
|
||||
cmd.Dir = o.Dir
|
||||
cmd.Env = append(os.Environ(), "GOWORK=off")
|
||||
cmd.WaitDelay = 2 * time.Second
|
||||
stdout := &limitedCatalogBuffer{limit: 32 << 20}
|
||||
stderr := &limitedCatalogBuffer{limit: 64 << 10}
|
||||
cmd.Stdout = stdout
|
||||
cmd.Stderr = stderr
|
||||
if err = cmd.Run(); err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return fail(fmt.Errorf("mapping type loading: %w", ctx.Err()))
|
||||
}
|
||||
// Go download errors may contain authenticated remote URLs. Keep arbitrary
|
||||
// command output out of diagnostics; exact mapping errors follow type loading.
|
||||
return fail(fmt.Errorf("mapping type loading failed (%v); run go list -deps -export for the configured source/target/converter packages; check dependencies, build tags and import cycles", err))
|
||||
}
|
||||
exports := map[string]string{}
|
||||
dec := json.NewDecoder(bytes.NewReader(stdout.Bytes()))
|
||||
for {
|
||||
var p struct {
|
||||
ImportPath, Export string
|
||||
Error *struct{ Err string }
|
||||
}
|
||||
if err = dec.Decode(&p); err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
if p.Error != nil {
|
||||
return fail(fmt.Errorf("mapping package %s could not be compiled", p.ImportPath))
|
||||
}
|
||||
if p.Export != "" {
|
||||
exports[p.ImportPath] = p.Export
|
||||
}
|
||||
}
|
||||
imp := importer.ForCompiler(gotoken.NewFileSet(), "gc", func(path string) (io.ReadCloser, error) {
|
||||
file, ok := exports[path]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("no export data for mapping dependency %s", path)
|
||||
}
|
||||
return os.Open(file)
|
||||
})
|
||||
return imp, cleanup, nil
|
||||
}
|
||||
250
cmd/go-omni/apigen/mappingNullable_test.go
Normal file
250
cmd/go-omni/apigen/mappingNullable_test.go
Normal file
@@ -0,0 +1,250 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func nullableMappings() []Mapping {
|
||||
pkg := "example.com/nullable/internal/"
|
||||
ref := func(p, name string) GoTypeRef { return GoTypeRef{pkg + p, name} }
|
||||
child := Mapping{Name: "ToChild", Source: ref("types", "Child"), Target: ref("domain", "Child"), Fields: map[string]MappingField{
|
||||
"Name": {Source: "Name", Convert: &GoFunctionRef{pkg + "convert", "Name"}},
|
||||
}}
|
||||
parent := Mapping{Name: "ToParent", PointerResult: true, Source: ref("types", "Parent"), Target: ref("domain", "Parent"), Fields: map[string]MappingField{
|
||||
"Child": {Source: "Child", Mapping: "ToChild"}, "Children": {Source: "Children", Mapping: "ToChild", Elements: true},
|
||||
}}
|
||||
defs := []Mapping{child, parent,
|
||||
{Name: "ToWireChild", Source: ref("domain", "Child"), Target: ref("types", "Child")},
|
||||
{Name: "ToWire", PointerResult: true, Source: ref("domain", "Parent"), Target: ref("types", "Parent"), Fields: map[string]MappingField{
|
||||
"Child": {Source: "Child", Mapping: "ToWireChild"}, "Children": {Source: "Children", Mapping: "ToWireChild", Elements: true},
|
||||
}},
|
||||
{Name: "ToChildDTO", Source: ref("domain", "Child"), Target: ref("domain", "ChildDTO"), Fields: map[string]MappingField{"Name": {Source: "Name", Convert: &GoFunctionRef{pkg + "convert", "Name"}}}},
|
||||
{Name: "Lift", Source: ref("domain", "ValueHolder"), Target: ref("domain", "PointerHolder"), Fields: map[string]MappingField{"Child": {Source: "Child", Mapping: "ToChildDTO"}}},
|
||||
{Name: "Batch", Source: ref("domain", "InputBatch"), Target: ref("domain", "OutputBatch"), Fields: map[string]MappingField{"Fixed": {Source: "Fixed", Mapping: "ToChildDTO", Elements: true}}},
|
||||
{Name: "ShallowCopy", Source: ref("domain", "Parent"), Target: ref("domain", "Parent")},
|
||||
{Name: "RequirePointer", Source: ref("types", "Parent"), Target: ref("domain", "Parent"), Fields: map[string]MappingField{
|
||||
"Child": {Source: "Child", Mapping: "ToChild", Nil: "error"}, "Children": {Source: "Children", Mapping: "ToChild", Elements: true},
|
||||
}},
|
||||
}
|
||||
for _, p := range []struct{ name, policy string }{{"RequiredZero", "zero"}, {"RequiredError", "error"}} {
|
||||
defs = append(defs, Mapping{Name: p.name, Source: ref("types", "Parent"), Target: ref("domain", "Required"), Fields: map[string]MappingField{
|
||||
"Child": {Source: "Child", Mapping: "ToChild", Nil: p.policy},
|
||||
}, IgnoreSource: map[string]string{"Children": "this explicit projection only includes the child"}})
|
||||
}
|
||||
for _, p := range []struct{ name, policy string }{{"ScalarZero", "zero"}, {"ScalarError", "error"}} {
|
||||
m := Mapping{Name: p.name, Source: ref("domain", "ScalarInput"), Target: ref("domain", "ScalarOutput"), Fields: map[string]MappingField{}}
|
||||
for _, name := range []string{"Text", "Count", "Enabled"} {
|
||||
m.Fields[name] = MappingField{Source: name, Nil: p.policy}
|
||||
}
|
||||
defs = append(defs, m)
|
||||
}
|
||||
return defs
|
||||
}
|
||||
|
||||
func writeMappingFile(t *testing.T, root, name string, defs []Mapping) {
|
||||
t.Helper()
|
||||
raw, err := json.MarshalIndent(struct {
|
||||
Mappings []Mapping `json:"mappings"`
|
||||
}{defs}, "", " ")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeFile(t, filepath.Join(root, name), string(raw)+"\n")
|
||||
}
|
||||
|
||||
func TestNullableMappingConsumer(t *testing.T) {
|
||||
if os.Getenv("GO_OMNI_SCAFFOLD_INTEGRATION") != "1" {
|
||||
t.Skip("set GO_OMNI_SCAFFOLD_INTEGRATION=1")
|
||||
}
|
||||
root := t.TempDir()
|
||||
dir := filepath.Join(root, "app")
|
||||
o := Options{API: filepath.Join(frameworkRoot(t), "cmd/go-omni/apigen/testdata/nullable/service.api"), Dir: dir, Module: "example.com/nullable", FrameworkDir: frameworkRoot(t)}
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for path, fixture := range map[string]string{"internal/domain/model.go": "domain.go", "internal/convert/value.go": "convert.go"} {
|
||||
writeFile(t, filepath.Join(dir, path), readFile(t, "testdata/nullable/"+fixture))
|
||||
}
|
||||
runCompositionGo(t, dir, "mod", "tidy")
|
||||
defs := nullableMappings()
|
||||
// References go across files, including forward references to ToChild.
|
||||
writeMappingFile(t, dir, "mappings/parents.json", defs[1:])
|
||||
writeMappingFile(t, dir, "mappings/children.json", defs[:1])
|
||||
cfg := GeneratorConfig{Repository: "none", Middleware: "factories", MappingFiles: []string{"mappings/parents.json", "mappings/children.json"}}
|
||||
writeGeneratorConfig(t, dir, cfg)
|
||||
o.API = filepath.Join(dir, "api/service.api")
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeFile(t, filepath.Join(dir, "internal/logic/probe/roundTrip.go"), `package probe
|
||||
import("context";"example.com/nullable/internal/types";"example.com/nullable/internal/mapping")
|
||||
func(s *Logic)RoundTrip(ctx context.Context,req *types.Parent)(*types.Parent,error){
|
||||
value,err:=mapping.ToParent(*req);if err!=nil{return nil,err}
|
||||
return mapping.ToWirePtr(value)
|
||||
}
|
||||
`)
|
||||
writeFile(t, filepath.Join(dir, "consumer_test.go"), readFile(t, "testdata/nullable/consumer_test.go"))
|
||||
before := snapshot(t, dir)
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !sameFiles(before, snapshot(t, dir)) {
|
||||
t.Fatal("regeneration changed business/config files")
|
||||
}
|
||||
o.Check = true
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !sameFiles(before, snapshot(t, dir)) {
|
||||
t.Fatal("check wrote files")
|
||||
}
|
||||
runCompositionGo(t, dir, "test", "-race", "-count=1", "./...")
|
||||
runCompositionGo(t, dir, "build", ".")
|
||||
// Workspace preflight and check use the same split configuration loader.
|
||||
workspace := filepath.Join(root, "omni.workspace.json")
|
||||
writeFile(t, workspace, `{"services":[{"dir":"app"}]}`)
|
||||
for _, command := range []string{"format", "generate", "check"} {
|
||||
if err := nullableWorkspace(t, workspace, command); err != nil {
|
||||
t.Fatal(command, err)
|
||||
}
|
||||
}
|
||||
// All invalid configurations fail before either writing or previewing output.
|
||||
for _, tc := range []struct {
|
||||
name, want string
|
||||
edit func([]Mapping) []Mapping
|
||||
}{
|
||||
{"missingNilPolicy", "requires explicit nil", func(d []Mapping) []Mapping {
|
||||
d[9].Fields["Child"] = MappingField{Source: "Child", Mapping: "ToChild"}
|
||||
return d
|
||||
}},
|
||||
{"preserveValue", "requires explicit nil", func(d []Mapping) []Mapping {
|
||||
d[9].Fields["Child"] = MappingField{Source: "Child", Mapping: "ToChild", Nil: "preserve"}
|
||||
return d
|
||||
}},
|
||||
{"zeroPointer", "pointer target", func(d []Mapping) []Mapping {
|
||||
d[1].Fields["Child"] = MappingField{Source: "Child", Mapping: "ToChild", Nil: "zero"}
|
||||
return d
|
||||
}},
|
||||
{"scalarMismatch", "identical *T to T", func(d []Mapping) []Mapping {
|
||||
d[11].Fields["Text"] = MappingField{Source: "Count", Nil: "zero"}
|
||||
return d
|
||||
}},
|
||||
{"valueNil", "requires a pointer source", func(d []Mapping) []Mapping {
|
||||
d[5].Fields["Child"] = MappingField{Source: "Child", Mapping: "ToChildDTO", Nil: "zero"}
|
||||
return d
|
||||
}},
|
||||
{"converterNil", "cannot be combined", func(d []Mapping) []Mapping {
|
||||
f := d[0].Fields["Name"]
|
||||
f.Nil = "error"
|
||||
d[0].Fields["Name"] = f
|
||||
return d
|
||||
}},
|
||||
{"pointerNameCollision", "must be unique", func(d []Mapping) []Mapping { m := d[0]; m.Name = "ToParentPtr"; return append(d, m) }},
|
||||
{"mappingCycle", "cycle", func(d []Mapping) []Mapping {
|
||||
d[0].Fields["Name"] = MappingField{Source: "Name", Mapping: "ToParent"}
|
||||
return d
|
||||
}},
|
||||
{"unknownMapping", "unknown mapping", func(d []Mapping) []Mapping {
|
||||
d[1].Fields["Child"] = MappingField{Source: "Child", Mapping: "Missing"}
|
||||
return d
|
||||
}},
|
||||
{"unhandledField", "unmapped source", func(d []Mapping) []Mapping { d[9].IgnoreSource = nil; return d }},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
changed := tc.edit(nullableMappings())
|
||||
writeMappingFile(t, dir, "mappings/parents.json", changed[1:])
|
||||
writeMappingFile(t, dir, "mappings/children.json", changed[:1])
|
||||
defer writeMappingFile(t, dir, "mappings/parents.json", defs[1:])
|
||||
defer writeMappingFile(t, dir, "mappings/children.json", defs[:1])
|
||||
before := snapshot(t, dir)
|
||||
for _, mode := range []string{"check", "preview", "generate"} {
|
||||
current := o
|
||||
current.Check = mode == "check"
|
||||
current.DryRun = mode == "preview"
|
||||
err := generateManagedTestProject(t, current)
|
||||
if err == nil || !strings.Contains(err.Error(), tc.want) {
|
||||
t.Fatal(mode, err)
|
||||
}
|
||||
if !sameFiles(before, snapshot(t, dir)) {
|
||||
t.Fatal("invalid mapping wrote files")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
// An edit inside a child file is picked up by --check; the config is never rewritten.
|
||||
changed := nullableMappings()
|
||||
changed[0].PointerResult = true
|
||||
writeMappingFile(t, dir, "mappings/children.json", changed[:1])
|
||||
before = snapshot(t, dir)
|
||||
if err := generateManagedTestProject(t, o); err == nil {
|
||||
t.Fatal("check missed child mapping drift")
|
||||
}
|
||||
if err := nullableWorkspace(t, workspace, "check"); err == nil {
|
||||
t.Fatal("workspace missed child mapping drift")
|
||||
}
|
||||
if !sameFiles(before, snapshot(t, dir)) {
|
||||
t.Fatal("drift check wrote files")
|
||||
}
|
||||
preview := o
|
||||
preview.Check = false
|
||||
preview.DryRun = true
|
||||
if err := generateManagedTestProject(t, preview); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !sameFiles(before, snapshot(t, dir)) {
|
||||
t.Fatal("preview wrote files")
|
||||
}
|
||||
if err := nullableWorkspace(t, workspace, "generate"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := nullableWorkspace(t, workspace, "check"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if os.Getenv("GO_OMNI_RELEASE_CLI") != "" {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "go", "list", "-m", "-json", frameworkModule)
|
||||
cmd.Dir = dir
|
||||
cmd.Env = append(os.Environ(), "GOWORK=off")
|
||||
data, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatal(err, string(data))
|
||||
}
|
||||
var dependency struct {
|
||||
Version, Sum string
|
||||
Replace any
|
||||
}
|
||||
if err = json.Unmarshal(data, &dependency); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if dependency.Version != os.Getenv("GO_OMNI_RELEASE_VERSION") || dependency.Replace != nil || dependency.Sum == "" {
|
||||
t.Fatal("not a fixed remote consumer")
|
||||
}
|
||||
t.Logf("remote framework %s checksum %s; no local replace", dependency.Version, dependency.Sum)
|
||||
}
|
||||
}
|
||||
|
||||
func nullableWorkspace(t *testing.T, path, command string) error {
|
||||
t.Helper()
|
||||
cli := os.Getenv("GO_OMNI_RELEASE_CLI")
|
||||
if cli == "" {
|
||||
return Workspace(path, command, io.Discard)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, cli, "workspace", command, "--file", path)
|
||||
cmd.Dir = filepath.Dir(path)
|
||||
if data, err := cmd.CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("released workspace CLI: %w\n%s", err, data)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
97
cmd/go-omni/apigen/mappingPointer.go
Normal file
97
cmd/go-omni/apigen/mappingPointer.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/types"
|
||||
)
|
||||
|
||||
func mappingOrigin(m Mapping) string {
|
||||
if m.origin != "" {
|
||||
return m.origin
|
||||
}
|
||||
return generatorConfigFile
|
||||
}
|
||||
|
||||
// Pointer adaptation is only selected by an explicit nested mapping. Ordinary
|
||||
// same-type pointer assignments retain their existing reference semantics.
|
||||
func (r *mappingRenderer) nestedExpression(rule MappingField, from, to types.Type, source string) (string, bool, error) {
|
||||
m := r.types[rule.Mapping]
|
||||
fromValue, toValue := from, to
|
||||
src, sourcePointer := from.Underlying().(*types.Pointer)
|
||||
dst, targetPointer := to.Underlying().(*types.Pointer)
|
||||
if sourcePointer {
|
||||
fromValue = src.Elem()
|
||||
}
|
||||
if targetPointer {
|
||||
toValue = dst.Elem()
|
||||
}
|
||||
if !types.Identical(fromValue, m.source) || !types.Identical(toValue, m.target) {
|
||||
return "", false, fmt.Errorf("nested mapping %s has incompatible types", rule.Mapping)
|
||||
}
|
||||
if !sourcePointer && rule.Nil != "" {
|
||||
return "", false, fmt.Errorf("nil policy requires a pointer source")
|
||||
}
|
||||
if !sourcePointer && !targetPointer {
|
||||
return rule.Mapping + "(" + source + ")", true, nil
|
||||
}
|
||||
policy := rule.Nil
|
||||
if sourcePointer {
|
||||
if targetPointer {
|
||||
if policy == "" {
|
||||
policy = "preserve"
|
||||
}
|
||||
if policy != "preserve" && policy != "error" {
|
||||
return "", false, fmt.Errorf("pointer target requires nil preserve or error")
|
||||
}
|
||||
} else if policy != "zero" && policy != "error" {
|
||||
return "", false, fmt.Errorf("pointer to value mapping requires explicit nil policy zero or error")
|
||||
}
|
||||
}
|
||||
// Keep a typed local input so the source is evaluated once. The surrounding
|
||||
// renderer adds the mapping/field/index error path and returns a zero parent.
|
||||
body := "var zero " + r.typeName(to) + "\n"
|
||||
input := "input"
|
||||
if sourcePointer {
|
||||
nilResult, err := r.nilResult(policy)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
body += "if input==nil {" + nilResult + "}\n"
|
||||
input = "*input"
|
||||
}
|
||||
body += "value,err:=" + rule.Mapping + "(" + input + ")\nif err!=nil{return zero,err}\n"
|
||||
if targetPointer {
|
||||
body += "return &value,nil\n"
|
||||
} else {
|
||||
body += "return value,nil\n"
|
||||
}
|
||||
return "func(input " + r.typeName(from) + ")(" + r.typeName(to) + ",error){\n" + body + "}(" + source + ")", true, nil
|
||||
}
|
||||
|
||||
// A nil policy without a converter/mapping is deliberately limited to *T -> T.
|
||||
// It never casts numbers, changes named types, or invents business defaults.
|
||||
func (r *mappingRenderer) dereferenceExpression(rule MappingField, from, to types.Type, source string) (string, bool, error) {
|
||||
pointer, ok := from.Underlying().(*types.Pointer)
|
||||
if !ok || !types.Identical(pointer.Elem(), to) {
|
||||
return "", false, fmt.Errorf("nil policy without a mapping requires identical *T to T types")
|
||||
}
|
||||
if rule.Nil != "zero" && rule.Nil != "error" {
|
||||
return "", false, fmt.Errorf("pointer to value conversion requires nil zero or error")
|
||||
}
|
||||
nilResult, err := r.nilResult(rule.Nil)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
return "func(input " + r.typeName(from) + ")(" + r.typeName(to) + ",error){\nvar zero " + r.typeName(to) + "\nif input==nil{" + nilResult + "}\nreturn *input,nil\n}(" + source + ")", true, nil
|
||||
}
|
||||
|
||||
func (r *mappingRenderer) nilResult(policy string) (string, error) {
|
||||
if policy != "error" {
|
||||
return "return zero,nil", nil
|
||||
}
|
||||
pkg, err := r.importer.Import("fmt")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "return zero," + r.qualifier(pkg) + ".Errorf(\"nil source is not allowed\")", nil
|
||||
}
|
||||
266
cmd/go-omni/apigen/mapping_test.go
Normal file
266
cmd/go-omni/apigen/mapping_test.go
Normal file
@@ -0,0 +1,266 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func compositionConfig() GeneratorConfig {
|
||||
pkg := "example.com/composition/internal/"
|
||||
cfg := GeneratorConfig{Repository: "none", Middleware: "factories"}
|
||||
for _, name := range []string{"Enterprise", "Tenant", "Admin"} {
|
||||
fields := map[string]MappingField{
|
||||
"EmployeeName": {Source: "Name", Convert: &GoFunctionRef{pkg + "convert", "Name"}},
|
||||
"Salary": {Source: "Salary", Convert: &GoFunctionRef{pkg + "convert", "Money"}},
|
||||
"Enabled": {Source: "Enabled", Convert: &GoFunctionRef{pkg + "convert", "Bool"}},
|
||||
}
|
||||
m := Mapping{Name: "From" + name, Source: GoTypeRef{pkg + "types", name + "Request"}, Target: GoTypeRef{pkg + "domain", "Employee"}, Fields: fields}
|
||||
if name != "Enterprise" {
|
||||
m.IgnoreSource = map[string]string{"CompanyID": "Authorized separately by Logic; never copy path scope into body"}
|
||||
}
|
||||
cfg.Mappings = append(cfg.Mappings, m)
|
||||
}
|
||||
cfg.Mappings = append(cfg.Mappings, Mapping{Name: "ToResponse", Source: GoTypeRef{pkg + "domain", "Result"}, Target: GoTypeRef{pkg + "types", "EmployeeResponse"}, Fields: map[string]MappingField{"ID": {Source: "ID", Convert: &GoFunctionRef{pkg + "convert", "ID"}}}})
|
||||
cfg.Mappings = append(cfg.Mappings, Mapping{Name: "ToRow", Source: GoTypeRef{pkg + "domain", "Row"}, Target: GoTypeRef{pkg + "domain", "RowDTO"}})
|
||||
cfg.Mappings = append(cfg.Mappings, Mapping{Name: "ToBatch", Source: GoTypeRef{pkg + "domain", "Batch"}, Target: GoTypeRef{pkg + "domain", "BatchDTO"}, Fields: map[string]MappingField{
|
||||
"Rows": {Source: "Rows", Mapping: "ToRow", Elements: true}, "Fixed": {Source: "Fixed", Mapping: "ToRow", Elements: true},
|
||||
}})
|
||||
return cfg
|
||||
}
|
||||
func writeGeneratorConfig(t *testing.T, dir string, cfg GeneratorConfig) {
|
||||
t.Helper()
|
||||
raw, err := json.MarshalIndent(cfg, "", " ")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeFile(t, filepath.Join(dir, generatorConfigFile), string(raw)+"\n")
|
||||
}
|
||||
func runCompositionGo(t *testing.T, dir string, args ...string) {
|
||||
t.Helper()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "go", args...)
|
||||
cmd.Dir = dir
|
||||
cmd.Env = append(os.Environ(), "GOWORK=off")
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("go %v: %v\n%s", args, err, out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompositionMappingConsumer(t *testing.T) {
|
||||
if os.Getenv("GO_OMNI_SCAFFOLD_INTEGRATION") != "1" {
|
||||
t.Skip("set GO_OMNI_SCAFFOLD_INTEGRATION=1")
|
||||
}
|
||||
dir := filepath.Join(t.TempDir(), "app")
|
||||
o := Options{API: filepath.Join(frameworkRoot(t), "cmd/go-omni/apigen/testdata/composition/service.api"), Dir: dir, Module: "example.com/composition", FrameworkDir: frameworkRoot(t)}
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fixtures := map[string]string{
|
||||
"internal/amount/money.go": "amount.go", "internal/domain/employee.go": "domain.go", "internal/convert/fields.go": "converters.go",
|
||||
"internal/middleware/dependencies.go": "dependencies.go", "internal/middleware/accessFactory.go": "accessFactory.go", "internal/svc/client.go": "client.go", "consumer_test.go": "consumer_test.go",
|
||||
}
|
||||
for path, fixture := range fixtures {
|
||||
writeFile(t, filepath.Join(dir, path), readFile(t, "testdata/composition/"+fixture))
|
||||
}
|
||||
svcFile := filepath.Join(dir, "internal/svc/serviceContext.go")
|
||||
source := strings.Replace(readFile(t, svcFile), "type ServiceContext struct {", "type ServiceContext struct {\n client *Client", 1)
|
||||
source = strings.Replace(source, "return nil", "return c.initClient(ctx)", 1)
|
||||
writeFile(t, svcFile, source)
|
||||
configFile := filepath.Join(dir, "internal/config/config.go")
|
||||
writeFile(t, configFile, strings.Replace(readFile(t, configFile), "type Config struct {", "type Config struct {\n FactoryMode string `json:\"factoryMode\"`", 1))
|
||||
// Resolve dependencies before opting into compile-time mapping. The first
|
||||
// generation creates DTOs; the second sees current DTOs through an overlay.
|
||||
// Logic does not yet import the generated mapping package.
|
||||
testSource := readFile(t, filepath.Join(dir, "consumer_test.go"))
|
||||
if err := os.Remove(filepath.Join(dir, "consumer_test.go")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runCompositionGo(t, dir, "mod", "tidy")
|
||||
cfg := compositionConfig()
|
||||
writeGeneratorConfig(t, dir, cfg)
|
||||
o.API = filepath.Join(dir, "api/service.api")
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeFile(t, filepath.Join(dir, "consumer_test.go"), testSource)
|
||||
for _, group := range []string{"Enterprise", "Tenant", "Admin"} {
|
||||
text := fmt.Sprintf(`package %s
|
||||
import("context";"example.com/composition/internal/types";"example.com/composition/internal/mapping";"example.com/composition/internal/domain")
|
||||
func(s *Logic)Save(ctx context.Context,req *types.%sRequest)(*types.EmployeeResponse,error){
|
||||
input,err:=mapping.From%s(*req);if err!=nil{return nil,err}
|
||||
result,err:=mapping.ToResponse(domain.Result{Employee:input,ID:9007199254740993});if err!=nil{return nil,err};return &result,nil
|
||||
}
|
||||
`, strings.ToLower(group), group, group)
|
||||
writeFile(t, filepath.Join(dir, "internal/logic", strings.ToLower(group), "save.go"), text)
|
||||
}
|
||||
before := snapshot(t, dir)
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !sameFiles(before, snapshot(t, dir)) {
|
||||
t.Fatal("regeneration changed project")
|
||||
}
|
||||
o.Check = true
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !sameFiles(before, snapshot(t, dir)) {
|
||||
t.Fatal("check changed project")
|
||||
}
|
||||
runCompositionGo(t, dir, "test", "-race", "-count=1", "./...")
|
||||
runCompositionGo(t, dir, "build", ".")
|
||||
generated := readFile(t, filepath.Join(dir, "internal/mapping/mappingsGen.go"))
|
||||
if strings.Contains(generated, "encoding/json") || strings.Contains(generated, "reflect") || !strings.Contains(generated, "result.EmployeeName = value") {
|
||||
t.Fatal("mapping does not emit typed assignments")
|
||||
}
|
||||
// Adding a source or target field is detected before any files are written.
|
||||
for _, tc := range []struct{ name, path, before, after, want string }{
|
||||
{"source", o.API, "Name *string", "NewField *string `json:\"newField,optional\"`\n Name *string", "unmapped source field NewField"},
|
||||
{"target", filepath.Join(dir, "internal/domain/employee.go"), "EmployeeName string", "Unmapped string\n EmployeeName string", "target Unmapped has no source"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
original := readFile(t, tc.path)
|
||||
writeFile(t, tc.path, strings.Replace(original, tc.before, tc.after, 1))
|
||||
defer writeFile(t, tc.path, original)
|
||||
snap := snapshot(t, dir)
|
||||
if err := generateManagedTestProject(t, o); err == nil || !strings.Contains(err.Error(), tc.want) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !sameFiles(snap, snapshot(t, dir)) {
|
||||
t.Fatal("failed check wrote files")
|
||||
}
|
||||
o.Check = false
|
||||
defer func() { o.Check = true }()
|
||||
if err := generateManagedTestProject(t, o); err == nil {
|
||||
t.Fatal("generate accepted missing mapping")
|
||||
}
|
||||
if !sameFiles(snap, snapshot(t, dir)) {
|
||||
t.Fatal("failed generate wrote files")
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
edit func(*GeneratorConfig)
|
||||
want string
|
||||
}{
|
||||
{"pointerPolicy", func(c *GeneratorConfig) { delete(c.Mappings[0].Fields, "Salary") }, "provide an explicit converter"},
|
||||
{"unknownSource", func(c *GeneratorConfig) { c.Mappings[0].Fields["Salary"] = MappingField{Source: "Missing"} }, "has no source"},
|
||||
{"wrongConverter", func(c *GeneratorConfig) {
|
||||
f := c.Mappings[0].Fields["Salary"]
|
||||
f.Convert.Name = "ID"
|
||||
c.Mappings[0].Fields["Salary"] = f
|
||||
}, "signature does not match"},
|
||||
{"unhandledScope", func(c *GeneratorConfig) { c.Mappings[1].IgnoreSource = nil }, "unmapped source field CompanyID"},
|
||||
{"badIgnore", func(c *GeneratorConfig) { c.Mappings[1].IgnoreSource["Missing"] = "stale rule" }, "ignoreSource Missing"},
|
||||
{"arrayLength", func(c *GeneratorConfig) {
|
||||
f := c.Mappings[5].Fields["Fixed"]
|
||||
f.Source = "Rows"
|
||||
c.Mappings[5].Fields["Fixed"] = f
|
||||
}, "array lengths must match"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
changed := compositionConfig()
|
||||
tc.edit(&changed)
|
||||
writeGeneratorConfig(t, dir, changed)
|
||||
defer writeGeneratorConfig(t, dir, cfg)
|
||||
snap := snapshot(t, dir)
|
||||
if err := generateManagedTestProject(t, o); err == nil || !strings.Contains(err.Error(), tc.want) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !sameFiles(snap, snapshot(t, dir)) {
|
||||
t.Fatal("invalid rule wrote files")
|
||||
}
|
||||
})
|
||||
}
|
||||
// All diagnostics exercise current DTOs, not stale exported generated types.
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Exercise the repository switch with the same actual CLI as the mapper.
|
||||
if _, err := os.Stat(filepath.Join(dir, "internal/repository")); !os.IsNotExist(err) {
|
||||
t.Fatalf("default repository skeleton: %v", err)
|
||||
}
|
||||
o.Check = false
|
||||
cfg.Repository = "group"
|
||||
writeGeneratorConfig(t, dir, cfg)
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repo := filepath.Join(dir, "internal/repository/enterprise/repository.go")
|
||||
writeFile(t, repo, readFile(t, repo)+"\n// Project-owned persistence.\n")
|
||||
cfg.Repository = "none"
|
||||
writeGeneratorConfig(t, dir, cfg)
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(readFile(t, repo), "Project-owned persistence.") {
|
||||
t.Fatal("disabling skeleton overwrote business code")
|
||||
}
|
||||
o.Check = true
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if os.Getenv("GO_OMNI_RELEASE_CLI") != "" {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "go", "list", "-m", "-json", frameworkModule)
|
||||
cmd.Dir = dir
|
||||
cmd.Env = append(os.Environ(), "GOWORK=off")
|
||||
data, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatal(err, string(data))
|
||||
}
|
||||
var dependency struct {
|
||||
Version, Sum string
|
||||
Replace any
|
||||
}
|
||||
if err = json.Unmarshal(data, &dependency); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if dependency.Version != os.Getenv("GO_OMNI_RELEASE_VERSION") || dependency.Replace != nil || dependency.Sum == "" {
|
||||
t.Fatal("not a fixed remote consumer")
|
||||
}
|
||||
t.Logf("remote framework %s checksum %s; no local replace", dependency.Version, dependency.Sum)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMappingConfigurationRejection(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
edit func(*GeneratorConfig)
|
||||
want string
|
||||
}{
|
||||
{"cycle", func(c *GeneratorConfig) {
|
||||
c.Mappings[4].Fields = map[string]MappingField{"Value": {Source: "Value", Mapping: "ToRow"}}
|
||||
}, "cycle"},
|
||||
{"unknown", func(c *GeneratorConfig) {
|
||||
c.Mappings[5].Fields["Rows"] = MappingField{Source: "Rows", Mapping: "Missing", Elements: true}
|
||||
}, "unknown mapping"},
|
||||
{"conflict", func(c *GeneratorConfig) {
|
||||
f := c.Mappings[0].Fields["Salary"]
|
||||
f.Mapping = "ToRow"
|
||||
c.Mappings[0].Fields["Salary"] = f
|
||||
}, "conflicting converter"},
|
||||
{"internal", func(c *GeneratorConfig) { c.Mappings[0].Source.Package = "example.net/private/internal/types" }, "invalid Go type"},
|
||||
{"outputCycle", func(c *GeneratorConfig) { c.Mappings[0].Source.Package = "example.com/composition/internal/mapping" }, "invalid Go type"},
|
||||
{"duplicates", func(c *GeneratorConfig) { c.Mappings[1].Name = c.Mappings[0].Name }, "must be unique"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
cfg := compositionConfig()
|
||||
tc.edit(&cfg)
|
||||
if _, err := validateMappings(cfg.Mappings, "example.com/composition"); err == nil || !strings.Contains(err.Error(), tc.want) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -74,7 +74,7 @@ func document(t *testing.T, c *Compiled) map[string]any {
|
||||
if err := json.Unmarshal(c.Document, &doc); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return doc
|
||||
return expandDocumentRefs(t, doc, doc).(map[string]any)
|
||||
}
|
||||
func operation(t *testing.T, doc map[string]any, path, method string) map[string]any {
|
||||
t.Helper()
|
||||
@@ -238,6 +238,7 @@ func TestDeclaredContractConsumer(t *testing.T) {
|
||||
source := filepath.Join(root, "input/service.api")
|
||||
writeFile(t, source, declaredAPI)
|
||||
dir := filepath.Join(root, "project")
|
||||
writeFile(t, filepath.Join(dir, generatorConfigFile), `{"middleware":"handlers"}`)
|
||||
if _, err := Generate(Options{API: source, Dir: dir, Module: "example.com/dx", FrameworkDir: frameworkRoot(t)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -252,7 +253,7 @@ func TestDeclaredContractConsumer(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
const declaredConsumerTest = `package dx_test
|
||||
const declaredConsumerTest = `package main_test
|
||||
import (
|
||||
"context";"errors";"io";"net/http/httptest";"strings";"testing"
|
||||
omni "git.yuntuo.hk/k2safe/go-omni"
|
||||
@@ -260,13 +261,13 @@ import (
|
||||
"git.yuntuo.hk/k2safe/go-omni/logx"
|
||||
"example.com/dx/internal/apierrors"
|
||||
"example.com/dx/internal/handler"
|
||||
"example.com/dx/internal/servicecontext"
|
||||
"example.com/dx/internal/svc"
|
||||
)
|
||||
func TestRuntimeAuthAndFreshErrors(t *testing.T){
|
||||
first:=apierrors.EmailTaken().WithCause(errors.New("private"));second:=apierrors.EmailTaken()
|
||||
if first==second||second.Cause!=nil||second.Code!=apierrors.CodeEmailTaken||second.Status!=409{t.Fatal("shared error instance")}
|
||||
cfg:=logx.Default();cfg.Writer=io.Discard;logger,err:=logx.New(cfg);if err!=nil{t.Fatal(err)};defer logger.Close()
|
||||
deps,err:=servicecontext.New(context.Background(),logger);if err!=nil{t.Fatal(err)};defer deps.Close()
|
||||
deps,err:=svc.New(context.Background(),logger);if err!=nil{t.Fatal(err)};defer deps.Close()
|
||||
app:=omni.New();defer app.Close();if err=handler.RegisterRoutes(app,deps);err!=nil{t.Fatal(err)}
|
||||
w:=httptest.NewRecorder();app.Engine().ServeHTTP(w,httptest.NewRequest("POST","/users",nil))
|
||||
if w.Code!=501||!strings.Contains(w.Body.String(),"MIDDLEWARE_NOT_IMPLEMENTED"){t.Fatal("unimplemented auth did not fail closed")}
|
||||
@@ -303,3 +304,21 @@ func TestAuthMetadataWithParameterNamePrefixes(t *testing.T) {
|
||||
t.Fatal("auth metadata lost")
|
||||
}
|
||||
}
|
||||
|
||||
// Existing semantic assertions resolve the standard local references emitted
|
||||
// by the compiler. documentRefs_test additionally checks the actual references.
|
||||
func expandDocumentRefs(t *testing.T, doc map[string]any, v any) any {
|
||||
t.Helper()
|
||||
switch v:=v.(type){
|
||||
case map[string]any:
|
||||
if ref,ok:=v["$ref"].(string);ok && strings.HasPrefix(ref,"#/components/") && len(v)==1 {
|
||||
var target any=doc
|
||||
for _,part:=range strings.Split(strings.TrimPrefix(ref,"#/"),"/"){m,ok:=target.(map[string]any);if !ok{t.Fatal("invalid reference",ref)};target=m[part]}
|
||||
if target==nil{t.Fatal("dangling reference",ref)};return expandDocumentRefs(t,doc,target)
|
||||
}
|
||||
out:=map[string]any{};for k,x:=range v{out[k]=expandDocumentRefs(t,doc,x)};return out
|
||||
case []any:
|
||||
out:=make([]any,len(v));for i,x:=range v{out[i]=expandDocumentRefs(t,doc,x)};return out
|
||||
default:return v
|
||||
}
|
||||
}
|
||||
|
||||
19
cmd/go-omni/apigen/middlewareFactory.go
Normal file
19
cmd/go-omni/apigen/middlewareFactory.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package apigen
|
||||
|
||||
import "fmt"
|
||||
|
||||
func renderMiddlewareFactories(names []string, generated, seeds map[string][]byte) {
|
||||
// All project-specific types live behind this project-owned interface. svc
|
||||
// implements it; middleware never imports svc (and cannot form that cycle).
|
||||
seeds["internal/middleware/dependencies.go"] = []byte("package middleware\n// Dependencies is implemented by *svc.ServiceContext. Add typed accessors here\n// and implement them on ServiceContext. Factories borrow dependencies; register\n// their cleanup in ServiceContext.Init, not in a middleware factory.\ntype Dependencies interface {}\n")
|
||||
text := fmt.Sprintf("package middleware\nimport(\"fmt\";%q)\n// New constructs declared middleware after ServiceContext.Init succeeds.\nfunc New(deps Dependencies)(map[string]httpx.Middleware,error){\nresult:=map[string]httpx.Middleware{}\n", frameworkModule+"/httpx")
|
||||
if len(names) == 0 {
|
||||
text = fmt.Sprintf("package middleware\nimport %q\nfunc New(deps Dependencies)(map[string]httpx.Middleware,error){\nresult:=map[string]httpx.Middleware{}\n", frameworkModule+"/httpx")
|
||||
}
|
||||
for i, name := range names {
|
||||
delete(seeds, "internal/middleware/"+lower(name)+".go")
|
||||
text += fmt.Sprintf("m%d,err:=New%s(deps)\nif err!=nil{return nil,fmt.Errorf(%q,err)}\nif m%d==nil{return nil,fmt.Errorf(%q)}\nresult[%q]=m%d\n", i, upper(name), "middleware "+name+": %w", i, "middleware "+name+": factory returned nil", name, i)
|
||||
seeds["internal/middleware/"+lower(name)+"Factory.go"] = []byte(fmt.Sprintf("package middleware\nimport(\"errors\";%q)\n// New%s is project-owned. Implement this factory once; registration is generated.\n// Dependencies have already been initialized. Do not acquire owned resources here.\nfunc New%s(deps Dependencies)(httpx.Middleware,error){\nreturn nil,errors.New(%q)\n}\n", frameworkModule+"/httpx", upper(name), upper(name), "implement "+name+" middleware factory"))
|
||||
}
|
||||
generated["internal/middleware/registryGen.go"] = []byte(generatedHeader + text + "return result,nil\n}\n")
|
||||
}
|
||||
@@ -5,6 +5,8 @@ package apigen
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.yuntuo.hk/k2safe/go-omni/contract"
|
||||
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -14,6 +16,13 @@ import (
|
||||
)
|
||||
|
||||
type Spec struct {
|
||||
DocumentCatalog *apierrors.Catalog
|
||||
DocumentErrors *DocumentErrors
|
||||
SharedRoots map[string]SharedRoot
|
||||
SourcePaths map[string]string
|
||||
RequestPolicy contract.RequestPolicy
|
||||
DefaultResponse string
|
||||
appDeclared bool
|
||||
Title, Version, Service, Description string
|
||||
Errors []ErrorDefinition
|
||||
ErrorCatalogs []string
|
||||
@@ -30,8 +39,14 @@ type Type struct {
|
||||
Fields []Field
|
||||
Position string
|
||||
}
|
||||
type Field struct{ Name, Type, Tag string }
|
||||
|
||||
// An empty Type denotes a named field composition, expanded before compilation.
|
||||
type Field struct{ Name, Type, Tag, Description string }
|
||||
type Route struct {
|
||||
BoundMultipart bool
|
||||
MaxParts int
|
||||
MaxFieldBytes, MaxFileBytes int64
|
||||
RequestPolicy contract.RequestPolicy
|
||||
Manual, CustomBinding bool
|
||||
BodyMode, Produces string
|
||||
MaxBodyBytes int64
|
||||
@@ -49,6 +64,7 @@ type Route struct {
|
||||
}
|
||||
type token struct {
|
||||
text, position string
|
||||
comment string
|
||||
kind rune
|
||||
space bool
|
||||
}
|
||||
@@ -59,7 +75,9 @@ type parser struct {
|
||||
imports []string
|
||||
}
|
||||
|
||||
func Parse(path string) (*Spec, error) {
|
||||
func Parse(path string) (*Spec, error) { return parseWithCatalogs(path, nil) }
|
||||
|
||||
func parseWithCatalogs(path string, catalogs map[string][]byte) (*Spec, error) {
|
||||
entry, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -69,11 +87,16 @@ func Parse(path string) (*Spec, error) {
|
||||
return nil, err
|
||||
}
|
||||
root := filepath.Dir(entry)
|
||||
spec := &Spec{Version: "1.0.0", Sources: map[string][]byte{}, Entry: filepath.Base(entry)}
|
||||
shared, err := readSharedConfig(root)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
roots := shared.Roots
|
||||
spec := &Spec{Version: "1.0.0", DocumentErrors: shared.Errors, SharedRoots: roots, SourcePaths: map[string]string{}, Sources: map[string][]byte{}, Entry: filepath.Base(entry)}
|
||||
visiting, visited := map[string]bool{}, map[string]bool{}
|
||||
total := 0
|
||||
var visit func(string, int) error
|
||||
visit = func(name string, depth int) error {
|
||||
var visit func(string, string, int) error
|
||||
visit = func(name, alias string, depth int) error {
|
||||
if depth > 32 || len(visited)+len(visiting) >= 64 {
|
||||
return errors.New("api: import limit exceeded")
|
||||
}
|
||||
@@ -81,9 +104,16 @@ func Parse(path string) (*Spec, error) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rel, err := filepath.Rel(root, real)
|
||||
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
||||
return errors.New("api: imports must stay within the entry API directory")
|
||||
sourceRoot := root
|
||||
if alias != "" {
|
||||
sourceRoot = roots[alias].Path
|
||||
}
|
||||
rel, err := sourceWithin(sourceRoot, real)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if alias != "" {
|
||||
rel = "@" + alias + "/" + rel
|
||||
}
|
||||
if filepath.Ext(real) != ".api" {
|
||||
return errors.New("api: expected .api file")
|
||||
@@ -118,18 +148,37 @@ func Parse(path string) (*Spec, error) {
|
||||
return err
|
||||
}
|
||||
spec.Sources[filepath.ToSlash(rel)] = data
|
||||
spec.SourcePaths[filepath.ToSlash(rel)] = real
|
||||
for _, imp := range p.imports {
|
||||
if filepath.IsAbs(imp) || strings.Contains(imp, "\\") {
|
||||
return errors.New("api: imports must use relative slash paths")
|
||||
}
|
||||
if err = visit(filepath.Join(filepath.Dir(real), filepath.FromSlash(imp)), depth+1); err != nil {
|
||||
nextAlias := alias
|
||||
next := filepath.Join(filepath.Dir(real), filepath.FromSlash(imp))
|
||||
if strings.HasPrefix(imp, "@") {
|
||||
var sub string
|
||||
var ok bool
|
||||
nextAlias, sub, ok = strings.Cut(strings.TrimPrefix(imp, "@"), "/")
|
||||
declared, found := roots[nextAlias]
|
||||
if !ok || !found || sub == "" || strings.HasPrefix(sub, "/") || strings.HasPrefix(sub, "../") || filepath.ToSlash(filepath.Clean(sub)) != sub {
|
||||
return errors.New("api: import needs a declared @root and a clean relative path")
|
||||
}
|
||||
next = filepath.Join(declared.Path, filepath.FromSlash(sub))
|
||||
}
|
||||
if err = visit(next, nextAlias, depth+1); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
visited[real] = true
|
||||
return nil
|
||||
}
|
||||
if err = visit(entry, 0); err != nil {
|
||||
if err = visit(entry, "", 0); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = readDocumentCatalog(root, spec, catalogs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = validateSharedPins(spec); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if spec.Service == "" || len(spec.Routes) == 0 {
|
||||
@@ -144,7 +193,7 @@ func lex(name, source string) ([]token, error) {
|
||||
var s scanner.Scanner
|
||||
s.Init(strings.NewReader(source))
|
||||
s.Filename = name
|
||||
s.Mode = scanner.ScanIdents | scanner.ScanInts | scanner.ScanStrings | scanner.ScanRawStrings | scanner.ScanComments | scanner.SkipComments
|
||||
s.Mode = scanner.ScanIdents | scanner.ScanInts | scanner.ScanStrings | scanner.ScanRawStrings | scanner.ScanComments
|
||||
s.Whitespace ^= 1 << '\n'
|
||||
var scanErr error
|
||||
s.Error = func(s *scanner.Scanner, message string) {
|
||||
@@ -154,9 +203,29 @@ func lex(name, source string) ([]token, error) {
|
||||
}
|
||||
var out []token
|
||||
end := 0
|
||||
pending := ""
|
||||
lastLine := 0
|
||||
lastToken := -1
|
||||
for {
|
||||
kind := s.Scan()
|
||||
out = append(out, token{s.TokenText(), s.Position.String(), kind, s.Position.Offset > end})
|
||||
if kind == scanner.Comment {
|
||||
comment := cleanComment(s.TokenText())
|
||||
if lastToken >= 0 && s.Position.Line == lastLine {
|
||||
out[lastToken].comment = joinComment(out[lastToken].comment, comment)
|
||||
} else {
|
||||
pending = joinComment(pending, comment)
|
||||
}
|
||||
end = s.Position.Offset + len(s.TokenText())
|
||||
continue
|
||||
}
|
||||
current := token{text: s.TokenText(), position: s.Position.String(), kind: kind, space: s.Position.Offset > end}
|
||||
if kind != '\n' {
|
||||
current.comment = pending
|
||||
pending = ""
|
||||
lastLine = s.Position.Line
|
||||
lastToken = len(out)
|
||||
}
|
||||
out = append(out, current)
|
||||
end = s.Position.Offset + len(s.TokenText())
|
||||
if kind == scanner.EOF {
|
||||
break
|
||||
@@ -300,6 +369,38 @@ func (p *parser) parse() error {
|
||||
}
|
||||
case "@":
|
||||
p.next()
|
||||
if p.peek() == "app" {
|
||||
if p.spec.appDeclared {
|
||||
return p.err("duplicate @app declaration")
|
||||
}
|
||||
// Defaults belong to the entry contract, never an imported module.
|
||||
if !strings.HasPrefix(p.tokens[p.at].position, p.spec.Entry+":") {
|
||||
return p.err("@app must be declared in the entry API file")
|
||||
}
|
||||
p.next()
|
||||
m, err := p.metadata()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(m) == 0 {
|
||||
return p.err("@app requires defaultResponse or request")
|
||||
}
|
||||
for key, value := range m {
|
||||
switch key {
|
||||
case "defaultResponse":
|
||||
case "request":
|
||||
p.spec.RequestPolicy, err = decodeRequestPolicy(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return p.err("unsupported @app property " + key)
|
||||
}
|
||||
}
|
||||
p.spec.DefaultResponse = m["defaultResponse"]
|
||||
p.spec.appDeclared = true
|
||||
continue
|
||||
}
|
||||
if err := p.expect("server"); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -400,6 +501,7 @@ func (p *parser) parseType() error {
|
||||
}
|
||||
typ := Type{Name: name, Position: pos}
|
||||
for p.skip(); p.peek() != "}"; p.skip() {
|
||||
description := p.tokens[p.at].comment
|
||||
name, err := p.ident()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -417,16 +519,22 @@ func (p *parser) parseType() error {
|
||||
}
|
||||
}
|
||||
if expression == "" {
|
||||
return p.err("field type required; embedded types are not supported")
|
||||
if p.tokens[p.at].kind == scanner.RawString {
|
||||
return p.err("composed types cannot carry field tags")
|
||||
}
|
||||
typ.Fields = append(typ.Fields, Field{Name: name, Description: description})
|
||||
continue
|
||||
}
|
||||
if p.tokens[p.at].kind != scanner.RawString {
|
||||
return p.err("field source tag required")
|
||||
}
|
||||
tag, err := strconv.Unquote(p.next().text)
|
||||
tagToken := p.next()
|
||||
description = joinComment(description, tagToken.comment)
|
||||
tag, err := strconv.Unquote(tagToken.text)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
typ.Fields = append(typ.Fields, Field{Name: name, Type: expression, Tag: tag})
|
||||
typ.Fields = append(typ.Fields, Field{Name: name, Type: expression, Tag: tag, Description: description})
|
||||
}
|
||||
p.next()
|
||||
p.spec.Types = append(p.spec.Types, typ)
|
||||
@@ -474,6 +582,12 @@ func (p *parser) parseService(server map[string]string) error {
|
||||
}
|
||||
seen[annotation] = true
|
||||
switch annotation {
|
||||
case "request":
|
||||
var props map[string]string
|
||||
props, e = p.metadata()
|
||||
if e == nil {
|
||||
route.RequestPolicy, e = parseRequestPolicy(props)
|
||||
}
|
||||
case "handlerMode":
|
||||
mode, err := p.ident()
|
||||
if err != nil {
|
||||
@@ -555,7 +669,7 @@ func (p *parser) parseService(server map[string]string) error {
|
||||
if p.peek() != "\n" && p.peek() != ";" && p.peek() != "}" {
|
||||
return p.err("unexpected route suffix")
|
||||
}
|
||||
if route.Response == "" && !seen["status"] && !route.Manual {
|
||||
if route.Response == "" && !seen["status"] && !route.Manual && route.Produces == "" {
|
||||
route.Status = 204
|
||||
}
|
||||
p.spec.Routes = append(p.spec.Routes, route)
|
||||
@@ -567,9 +681,48 @@ func (p *parser) routeType() (string, error) {
|
||||
if err := p.expect("("); err != nil {
|
||||
return "", err
|
||||
}
|
||||
prefix := ""
|
||||
if p.peek() == "[" {
|
||||
p.next()
|
||||
if err := p.expect("]"); err != nil {
|
||||
return "", err
|
||||
}
|
||||
prefix = "[]"
|
||||
} else if p.peek() == "*" {
|
||||
p.next()
|
||||
prefix = "*"
|
||||
}
|
||||
name, err := p.ident()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return name, p.expect(")")
|
||||
return prefix + name, p.expect(")")
|
||||
}
|
||||
|
||||
func joinComment(a, b string) string {
|
||||
if a == "" {
|
||||
return b
|
||||
}
|
||||
if b == "" {
|
||||
return a
|
||||
}
|
||||
return a + "\n" + b
|
||||
}
|
||||
func cleanComment(s string) string {
|
||||
if strings.HasPrefix(s, "//") {
|
||||
return strings.TrimSpace(strings.TrimPrefix(s, "//"))
|
||||
}
|
||||
s = strings.TrimSuffix(strings.TrimPrefix(s, "/*"), "*/")
|
||||
lines := strings.Split(s, "\n")
|
||||
for i, line := range lines {
|
||||
lines[i] = strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(line), "*"))
|
||||
}
|
||||
return strings.TrimSpace(strings.Join(lines, "\n"))
|
||||
}
|
||||
func fieldTag(f Field) string {
|
||||
tags, err := contract.ParseTags(f.Tag)
|
||||
if err != nil || f.Description == "" || tags["description"] != "" {
|
||||
return f.Tag
|
||||
}
|
||||
return f.Tag + " description:" + strconv.Quote(f.Description)
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ func TestInfrastructureProfileOwnership(t *testing.T) {
|
||||
}
|
||||
}
|
||||
// Restoring a missing seed must use the saved profile, not current defaults.
|
||||
if err := os.Remove(filepath.Join(dir, "internal/servicecontext/storage.go")); err != nil {
|
||||
if err := os.Remove(filepath.Join(dir, "internal/svc/storage.go")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := Generate(o); err == nil {
|
||||
@@ -62,7 +62,7 @@ func TestAPIInfrastructureConsumers(t *testing.T) {
|
||||
if _, err := Generate(Options{Dir: dir, API: filepath.Join(dir, "api/service.api"), Check: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
test := `package settings
|
||||
test := `package config
|
||||
import("testing";"os")
|
||||
func TestConfiguration(t *testing.T){
|
||||
t.Setenv("GO_OMNI_DB_DSN","test:test@tcp(127.0.0.1:1)/test")
|
||||
@@ -73,6 +73,8 @@ func TestConfiguration(t *testing.T){
|
||||
cfg,err:=Load("../../etc/config.yaml");if err!=nil{t.Fatal(err)}
|
||||
if cfg.Server.Log.File==""{t.Fatal("file logger absent")}
|
||||
t.Setenv("GO_OMNI_LOG_OUTPUT","stdout");cfg,err=Load("");if err!=nil||cfg.Server.Log.File!=""{t.Fatalf("override: %v",err)}
|
||||
t.Setenv("GO_OMNI_SERVER_LOG_FILE","./explicit.log");cfg,err=Load("");if err!=nil||cfg.Server.Log.File!="./explicit.log"{t.Fatalf("canonical log file: %v",err)}
|
||||
t.Setenv("GO_OMNI_SERVER_LOG_OUTPUT","stderr");cfg,err=Load("");if err!=nil||cfg.Server.Log.Output!="stderr"{t.Fatalf("canonical output: %v",err)}
|
||||
_=os.Getenv("unused")
|
||||
}`
|
||||
endpoint := ""
|
||||
@@ -82,7 +84,7 @@ func TestConfiguration(t *testing.T){
|
||||
if storage == "oss" {
|
||||
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
|
||||
}
|
||||
writeFile(t, filepath.Join(dir, "internal/settings/profile_test.go"), strings.ReplaceAll(test, `"ENDPOINT"`, `"`+endpoint+`"`))
|
||||
writeFile(t, filepath.Join(dir, "internal/config/profile_test.go"), strings.ReplaceAll(test, `"ENDPOINT"`, `"`+endpoint+`"`))
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "go", "test", "-mod=mod", "-race", "-count=1", "./...")
|
||||
|
||||
@@ -25,7 +25,7 @@ func ProjectEntry(directory string) (string, error) {
|
||||
if err = json.Unmarshal(data, &m); err != nil {
|
||||
return "", fmt.Errorf("invalid API project manifest: %w", err)
|
||||
}
|
||||
if m.Version != 1 || filepath.Ext(m.Entry) != ".api" {
|
||||
if m.Version != 2 || filepath.Ext(m.Entry) != ".api" {
|
||||
return "", fmt.Errorf("invalid API entry in %s; pass --api explicitly", manifestPath)
|
||||
}
|
||||
if err = safePath(root, m.Entry); err != nil {
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.yuntuo.hk/k2safe/go-omni/contract"
|
||||
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
||||
"mime"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -16,6 +18,21 @@ func (p *parser) parseBody(r *Route) error {
|
||||
}
|
||||
for key, value := range props {
|
||||
switch key {
|
||||
case "maxParts":
|
||||
r.MaxParts, err = strconv.Atoi(value)
|
||||
if err != nil || r.MaxParts < 1 || r.MaxParts > 10000 {
|
||||
return p.err("maxParts must be 1..10000")
|
||||
}
|
||||
case "maxFieldBytes", "maxFileBytes":
|
||||
limit, e := strconv.ParseInt(value, 10, 64)
|
||||
if e != nil || limit < 1 || limit > 1<<30 {
|
||||
return p.err("part limits must be 1..1073741824 bytes")
|
||||
}
|
||||
if key == "maxFieldBytes" {
|
||||
r.MaxFieldBytes = limit
|
||||
} else {
|
||||
r.MaxFileBytes = limit
|
||||
}
|
||||
case "mode":
|
||||
r.BodyMode = value
|
||||
case "maxBytes":
|
||||
@@ -33,28 +50,35 @@ func (p *parser) parseBody(r *Route) error {
|
||||
return nil
|
||||
}
|
||||
func validateRouteExtensions(r Route) error {
|
||||
if (r.MaxParts > 0 || r.MaxFieldBytes > 0 || r.MaxFileBytes > 0) && r.BodyMode != "multipart" {
|
||||
return errors.New("part limits require multipart mode")
|
||||
}
|
||||
|
||||
if err := r.RequestPolicy.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if (r.BodyMode == "raw" || r.BodyMode == "multipart") && r.RequestPolicy.Body != "" && r.RequestPolicy.Body != "auto" {
|
||||
return errors.New("raw/multipart cannot declare a JSON body policy")
|
||||
}
|
||||
if r.BodyMode != "" && r.BodyMode != "json" && r.BodyMode != "raw" && r.BodyMode != "multipart" {
|
||||
return errors.New("body mode must be json, raw or multipart")
|
||||
}
|
||||
if (r.BodyMode == "raw" || r.BodyMode == "multipart") && !r.Manual {
|
||||
return errors.New("raw/multipart body requires @handlerMode manual")
|
||||
}
|
||||
if r.CustomBinding && (r.Manual || r.Request == "") {
|
||||
return errors.New("@binding custom requires an automatic handler with a request DTO")
|
||||
}
|
||||
if r.MaxBodyBytes > 0 && (r.Method == "get" || r.Method == "head") {
|
||||
return errors.New("GET/HEAD cannot declare a body limit")
|
||||
}
|
||||
if (r.Method == "get" || r.Method == "head") && r.RequestPolicy.Body != "" && r.RequestPolicy.Body != "auto" && r.RequestPolicy.Body != "none" {
|
||||
return errors.New("GET/HEAD cannot declare a JSON body policy")
|
||||
}
|
||||
if r.Produces != "" {
|
||||
if !r.Manual {
|
||||
return errors.New("@produces requires @handlerMode manual")
|
||||
}
|
||||
media, params, err := mime.ParseMediaType(r.Produces)
|
||||
if err != nil || len(params) != 0 || media != r.Produces || strings.Contains(media, "*") {
|
||||
return errors.New("produces requires a concrete media type without parameters")
|
||||
}
|
||||
if r.Response != "" || r.Status == 204 || r.Method == "head" {
|
||||
return errors.New("@produces uses a manual response without a DTO, 204 or HEAD")
|
||||
return errors.New("@produces requires a stream response without a DTO, 204 or HEAD")
|
||||
}
|
||||
}
|
||||
if r.Manual && r.Status == 204 && r.Response != "" {
|
||||
@@ -64,6 +88,23 @@ func validateRouteExtensions(r Route) error {
|
||||
}
|
||||
|
||||
func decorateRequestOptions(op map[string]any, r Route) {
|
||||
if r.BodyMode == "raw" || r.BodyMode == "multipart" {
|
||||
r.RequestPolicy.Body = "auto"
|
||||
}
|
||||
op["x-omni-request-policy"] = r.RequestPolicy
|
||||
if r.BodyMode == "multipart" {
|
||||
limits, _ := (contract.MultipartOptions{MaxBodyBytes: r.MaxBodyBytes, MaxParts: r.MaxParts, MaxFieldBytes: r.MaxFieldBytes, MaxFileBytes: r.MaxFileBytes}).Normalized()
|
||||
op["x-omni-multipart-limits"] = map[string]any{"maxBytes": limits.MaxBodyBytes, "maxParts": limits.MaxParts, "maxFieldBytes": limits.MaxFieldBytes, "maxFileBytes": limits.MaxFileBytes}
|
||||
}
|
||||
if r.BodyMode != "raw" && r.BodyMode != "multipart" {
|
||||
if body, ok := op["requestBody"].(map[string]any); ok && r.RequestPolicy.Body == "optional" {
|
||||
body["required"] = false
|
||||
}
|
||||
if _, ok := op["requestBody"]; !ok && (r.RequestPolicy.Body == "emptyObject" || r.RequestPolicy.Body == "required" || r.RequestPolicy.Body == "optional") {
|
||||
op["requestBody"] = map[string]any{"required": r.RequestPolicy.Body != "optional", "content": map[string]any{"application/json": map[string]any{"schema": map[string]any{"type": "object", "additionalProperties": false}}}}
|
||||
}
|
||||
}
|
||||
|
||||
if r.Manual {
|
||||
op["x-omni-handler-mode"] = "manual"
|
||||
}
|
||||
@@ -93,3 +134,21 @@ func decorateRequestOptions(op map[string]any, r Route) {
|
||||
op["responses"].(map[string]any)[strconv.Itoa(r.Status)] = map[string]any{"description": "Success", "content": map[string]any{r.Produces: map[string]any{"schema": schema}}}
|
||||
}
|
||||
}
|
||||
|
||||
func parseRequestPolicy(props map[string]string) (contract.RequestPolicy, error) {
|
||||
data, err := json.Marshal(props)
|
||||
if err != nil {
|
||||
return contract.RequestPolicy{}, err
|
||||
}
|
||||
return decodeRequestPolicy(string(data))
|
||||
}
|
||||
func decodeRequestPolicy(raw string) (contract.RequestPolicy, error) {
|
||||
var p contract.RequestPolicy
|
||||
if err := apierrors.DecodeCatalogJSON([]byte(raw), &p); err != nil {
|
||||
return p, fmt.Errorf("request policy: %w", err)
|
||||
}
|
||||
return p, p.Validate()
|
||||
}
|
||||
func policyLiteral(p contract.RequestPolicy) string {
|
||||
return fmt.Sprintf("contract.RequestPolicy{UnknownQuery:%q,UTF8:%q,Charset:%q,Body:%q}", p.UnknownQuery, p.UTF8, p.Charset, p.Body)
|
||||
}
|
||||
|
||||
@@ -102,6 +102,18 @@ func validateResponses(spec *Spec) error {
|
||||
}
|
||||
seen[definition.Name] = true
|
||||
}
|
||||
if spec.DefaultResponse != "" && !seen[spec.DefaultResponse] {
|
||||
return fmt.Errorf("unknown App default response template %s", spec.DefaultResponse)
|
||||
}
|
||||
for i := range spec.Routes {
|
||||
r := &spec.Routes[i]
|
||||
if r.ResponseFormat == "" {
|
||||
r.ResponseFormat = spec.DefaultResponse
|
||||
}
|
||||
if r.ResponseFormat == "raw" && r.ErrorResponseFormat == "" {
|
||||
r.ErrorResponseFormat = spec.DefaultResponse
|
||||
}
|
||||
}
|
||||
for _, r := range spec.Routes {
|
||||
if r.ResponseFormat != "" && r.ResponseFormat != "raw" && !seen[r.ResponseFormat] {
|
||||
return fmt.Errorf("%s: unknown response template %s", r.Position, r.ResponseFormat)
|
||||
|
||||
78
cmd/go-omni/apigen/responseShape.go
Normal file
78
cmd/go-omni/apigen/responseShape.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.yuntuo.hk/k2safe/go-omni/contract"
|
||||
"git.yuntuo.hk/k2safe/go-omni/openapi"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Response roots allow named DTOs, []DTO, []primitive, *DTO and null.
|
||||
// A nil root slice is always encoded as an empty JSON array.
|
||||
func responseShape(value string) (kind, name string) {
|
||||
if value == "null" {
|
||||
return "null", ""
|
||||
}
|
||||
if strings.HasPrefix(value, "[]") {
|
||||
return "array", strings.TrimPrefix(value, "[]")
|
||||
}
|
||||
if strings.HasPrefix(value, "*") {
|
||||
return "nullable", strings.TrimPrefix(value, "*")
|
||||
}
|
||||
return "object", value
|
||||
}
|
||||
func responseGoType(value string) string {
|
||||
kind, name := responseShape(value)
|
||||
if kind == "null" || value == "" {
|
||||
return ""
|
||||
}
|
||||
if kind == "array" {
|
||||
if primitiveTypes[name] != nil {
|
||||
return "[]" + name
|
||||
}
|
||||
return "[]types." + name
|
||||
}
|
||||
return "*types." + name
|
||||
}
|
||||
func responseDataSchema(value string, bindings map[string]*contract.Binder) (*openapi.Schema, error) {
|
||||
kind, name := responseShape(value)
|
||||
if kind == "null" {
|
||||
return &openapi.Schema{Type: "object", Nullable: true, Enum: []any{nil}}, nil
|
||||
}
|
||||
if kind == "array" && primitiveTypes[name] != nil {
|
||||
if name == "File" {
|
||||
return nil, fmt.Errorf("File is request-only; use a response DTO")
|
||||
}
|
||||
// Reuse the contract compiler, including integer bounds and byte-slice rules.
|
||||
t := reflect.StructOf([]reflect.StructField{{Name: "Value", Type: reflect.SliceOf(primitiveTypes[name]), Tag: `json:"value"`}})
|
||||
b, err := contract.Describe(t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s, err := b.ResponseSchema()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.Properties["value"], nil
|
||||
}
|
||||
b, ok := bindings[name]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unknown response DTO %q; use Name, []Name, []string/[]int64/[]bool/[]any, *Name or null", name)
|
||||
}
|
||||
s, err := b.ResponseSchema()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch kind {
|
||||
case "array":
|
||||
return &openapi.Schema{Type: "array", Items: s}, nil
|
||||
case "nullable":
|
||||
s.Nullable = true
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
var primitiveTypes = map[string]reflect.Type{"File": reflect.TypeOf(contract.File{}), "any": reflect.TypeOf((*any)(nil)).Elem(), "string": reflect.TypeOf(""), "bool": reflect.TypeOf(false), "int": reflect.TypeOf(int(0)), "int8": reflect.TypeOf(int8(0)), "int16": reflect.TypeOf(int16(0)), "int32": reflect.TypeOf(int32(0)), "int64": reflect.TypeOf(int64(0)), "uint": reflect.TypeOf(uint(0)), "uint8": reflect.TypeOf(uint8(0)), "uint16": reflect.TypeOf(uint16(0)), "uint32": reflect.TypeOf(uint32(0)), "uint64": reflect.TypeOf(uint64(0)), "float32": reflect.TypeOf(float32(0)), "float64": reflect.TypeOf(float64(0))}
|
||||
|
||||
func responseNeedsTypes(value string) bool { return strings.Contains(responseGoType(value), "types.") }
|
||||
@@ -251,6 +251,7 @@ func TestResponseTemplateConsumer(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
writeFile(t, filepath.Join(dir, generatorConfigFile), `{"middleware":"handlers"}`)
|
||||
generate(source, true)
|
||||
writeFile(t, filepath.Join(dir, "response_test.go"), responseConsumerTest)
|
||||
for _, method := range []string{"Item", "Callback", "Partner"} {
|
||||
@@ -262,7 +263,7 @@ func TestResponseTemplateConsumer(t *testing.T) {
|
||||
if method == "Partner" {
|
||||
imports += `;"example.com/responseConsumer/internal/apierrors"`
|
||||
}
|
||||
writeFile(t, filepath.Join(dir, "internal/service/items", strings.ToLower(method)+".go"), "package items\nimport("+imports+")\nfunc(s *Service)"+method+"(ctx context.Context)(*types.ItemResponse,error){"+body+"}\n")
|
||||
writeFile(t, filepath.Join(dir, "internal/logic/items", strings.ToLower(method)+".go"), "package items\nimport("+imports+")\nfunc(s *Logic)"+method+"(ctx context.Context)(*types.ItemResponse,error){"+body+"}\n")
|
||||
}
|
||||
before := snapshot(t, dir)
|
||||
generate(filepath.Join(dir, "api/service.api"), false)
|
||||
@@ -310,7 +311,7 @@ func TestResponseTemplateConsumer(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
const responseConsumerTest = `package responseConsumer_test
|
||||
const responseConsumerTest = `package main_test
|
||||
import (
|
||||
"bytes";"encoding/json";"io";"net/http/httptest";"testing"
|
||||
omni "git.yuntuo.hk/k2safe/go-omni"
|
||||
@@ -322,7 +323,7 @@ import (
|
||||
item "example.com/responseConsumer/internal/handler/items"
|
||||
"example.com/responseConsumer/internal/types"
|
||||
"context"
|
||||
"example.com/responseConsumer/internal/servicecontext"
|
||||
"example.com/responseConsumer/internal/svc"
|
||||
)
|
||||
type service struct{}
|
||||
func(service)Item(context.Context)(*types.ItemResponse,error){return &types.ItemResponse{Name:"item"},nil}
|
||||
@@ -331,7 +332,7 @@ func(service)Partner(context.Context)(*types.ItemResponse,error){return nil,apie
|
||||
func TestResponseContract(t *testing.T){
|
||||
app:=omni.New();defer app.Close()
|
||||
cfg:=logx.Default();cfg.Writer=io.Discard;logger,err:=logx.New(cfg);if err!=nil{t.Fatal(err)};defer logger.Close()
|
||||
deps,err:=servicecontext.New(context.Background(),logger);if err!=nil{t.Fatal(err)};defer deps.Close()
|
||||
deps,err:=svc.New(context.Background(),logger);if err!=nil{t.Fatal(err)};defer deps.Close()
|
||||
deps.Middlewares["Auth"]=func(next httpx.Handler)httpx.Handler{return func(c *httpx.Context)error{if c.Header("Authorization")!="Bearer valid"{return apierrors.Unauthenticated()};return next(c)}}
|
||||
if err=handler.RegisterRoutes(app,deps);err!=nil{t.Fatal(err)}
|
||||
// Registered routes use the compiled policy before even running authentication.
|
||||
|
||||
151
cmd/go-omni/apigen/shared.go
Normal file
151
cmd/go-omni/apigen/shared.go
Normal file
@@ -0,0 +1,151 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const importsFile = "omni.imports.json"
|
||||
|
||||
// SharedRoot explicitly authorizes reads from one canonical local directory.
|
||||
// Version is a project-owned label; SHA256 optionally pins the imported source
|
||||
// set, not a remote tag. The CLI never downloads or executes shared contracts.
|
||||
type SharedRoot struct {
|
||||
Path string `json:"path"`
|
||||
Version string `json:"version,omitempty"`
|
||||
SHA256 string `json:"sha256,omitempty"`
|
||||
}
|
||||
type sharedConfig struct {
|
||||
Roots map[string]SharedRoot `json:"roots"`
|
||||
Errors *DocumentErrors `json:"errors,omitempty"`
|
||||
}
|
||||
|
||||
func readSharedConfig(root string) (sharedConfig, error) {
|
||||
stat, err := os.Lstat(filepath.Join(root, importsFile))
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return sharedConfig{}, nil
|
||||
}
|
||||
if err != nil {
|
||||
return sharedConfig{}, err
|
||||
}
|
||||
if !stat.Mode().IsRegular() {
|
||||
return sharedConfig{}, errors.New("api: shared root configuration must be a regular file")
|
||||
}
|
||||
f, err := os.Open(filepath.Join(root, importsFile))
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return sharedConfig{}, nil
|
||||
}
|
||||
if err != nil {
|
||||
return sharedConfig{}, err
|
||||
}
|
||||
defer f.Close()
|
||||
raw, err := io.ReadAll(io.LimitReader(f, 65537))
|
||||
if err != nil {
|
||||
return sharedConfig{}, err
|
||||
}
|
||||
if len(raw) > 65536 {
|
||||
return sharedConfig{}, errors.New("api: shared root config too large")
|
||||
}
|
||||
var cfg sharedConfig
|
||||
if err = apierrors.DecodeCatalogJSON(raw, &cfg); err != nil {
|
||||
return sharedConfig{}, err
|
||||
}
|
||||
if len(cfg.Roots) > 16 {
|
||||
return sharedConfig{}, errors.New("api: at most 16 shared roots")
|
||||
}
|
||||
for name, r := range cfg.Roots {
|
||||
if !lowerName.MatchString(name) || r.Path == "" || len(r.Version) > 128 {
|
||||
return sharedConfig{}, errors.New("api: invalid shared root")
|
||||
}
|
||||
if r.SHA256 != "" && (len(r.SHA256) != 64 || strings.Trim(r.SHA256, "0123456789abcdef") != "") {
|
||||
return sharedConfig{}, errors.New("api: shared sha256 must be 64 lowercase hex characters")
|
||||
}
|
||||
path := r.Path
|
||||
if !filepath.IsAbs(path) {
|
||||
path = filepath.Join(root, path)
|
||||
}
|
||||
r.Path, err = filepath.EvalSymlinks(path)
|
||||
if err != nil {
|
||||
return sharedConfig{}, err
|
||||
}
|
||||
r.Path, err = filepath.Abs(r.Path)
|
||||
if err != nil {
|
||||
return sharedConfig{}, err
|
||||
}
|
||||
stat, err := os.Stat(r.Path)
|
||||
if err != nil {
|
||||
return sharedConfig{}, err
|
||||
}
|
||||
if !stat.IsDir() {
|
||||
return sharedConfig{}, errors.New("api: shared root must be a directory")
|
||||
}
|
||||
cfg.Roots[name] = r
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
func sourceWithin(root, name string) (string, error) {
|
||||
real, err := filepath.EvalSymlinks(name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
rel, err := filepath.Rel(root, real)
|
||||
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
||||
return "", errors.New("api: imports must stay within their declared contract root")
|
||||
}
|
||||
return filepath.ToSlash(rel), nil
|
||||
}
|
||||
func sharedHash(sources map[string][]byte, alias string) string {
|
||||
values := map[string]string{}
|
||||
for name, data := range sources {
|
||||
if strings.HasPrefix(name, "@"+alias+"/") {
|
||||
values[strings.TrimPrefix(name, "@"+alias+"/")] = digest(data)
|
||||
}
|
||||
}
|
||||
raw, _ := json.Marshal(values)
|
||||
return digest(raw)
|
||||
}
|
||||
func sourceMetadata(spec *Spec) map[string]any {
|
||||
hashes := map[string]string{}
|
||||
for name, data := range spec.Sources {
|
||||
hashes[name] = digest(data)
|
||||
}
|
||||
shared := map[string]any{}
|
||||
for alias, r := range spec.SharedRoots {
|
||||
shared[alias] = map[string]string{"version": r.Version, "sha256": sharedHash(spec.Sources, alias)}
|
||||
}
|
||||
return map[string]any{"files": hashes, "shared": shared}
|
||||
}
|
||||
func sharedSeeds(spec *Spec, directory string) ([]byte, error) {
|
||||
cfg := sharedConfig{Roots: map[string]SharedRoot{}, Errors: spec.DocumentErrors}
|
||||
for name, r := range spec.SharedRoots {
|
||||
rel, err := filepath.Rel(filepath.Join(directory, "api"), r.Path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.Path = filepath.ToSlash(rel)
|
||||
cfg.Roots[name] = r
|
||||
}
|
||||
data, err := json.MarshalIndent(cfg, "", " ")
|
||||
return append(data, '\n'), err
|
||||
}
|
||||
func validateSharedPins(spec *Spec) error {
|
||||
names := make([]string, 0, len(spec.SharedRoots))
|
||||
for name := range spec.SharedRoots {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
for _, name := range names {
|
||||
r := spec.SharedRoots[name]
|
||||
if r.SHA256 != "" && r.SHA256 != sharedHash(spec.Sources, name) {
|
||||
return fmt.Errorf("api: shared contract %s checksum differs (got %s)", name, sharedHash(spec.Sources, name))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
96
cmd/go-omni/apigen/shared_test.go
Normal file
96
cmd/go-omni/apigen/shared_test.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestControlledSharedContracts(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
shared := filepath.Join(root, "shared")
|
||||
writeFile(t, filepath.Join(shared, "response.api"), "response Common (codeType: int32; successCode: 0; errorCode: 9999; dataMode: always)\ntype Item { Name string `json:\"name\"` }\n")
|
||||
for _, service := range []string{"api", "id"} {
|
||||
dir := filepath.Join(root, service)
|
||||
entry := filepath.Join(dir, "api/service.api")
|
||||
writeFile(t, entry, "import \"@common/response.api\"\n@app(defaultResponse: Common)\nservice demo {\n@handler get\nget /get returns (Item)\n}\n")
|
||||
config := `{"roots":{"common":{"path":"../../shared","version":"1.0.0"}}}`
|
||||
writeFile(t, filepath.Join(dir, "api/omni.imports.json"), config)
|
||||
spec, err := Parse(entry)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(spec.Sources) != 2 || len(spec.SourcePaths) != 2 {
|
||||
t.Fatalf("%+v", spec)
|
||||
}
|
||||
pin := sharedHash(spec.Sources, "common")
|
||||
config = strings.Replace(config, `"version":"1.0.0"`, `"version":"1.0.0","sha256":"`+pin+`"`, 1)
|
||||
writeFile(t, filepath.Join(dir, "api/omni.imports.json"), config)
|
||||
o := Options{Dir: dir, API: entry, Module: "example.com/" + service, FrameworkDir: frameworkRoot(t)}
|
||||
if _, err = Generate(o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
o.Check = true
|
||||
if _, err = Generate(o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = os.Stat(filepath.Join(dir, "api/@common")); !os.IsNotExist(err) {
|
||||
t.Fatal("shared contract copied into service")
|
||||
}
|
||||
document := readFile(t, filepath.Join(dir, "docs/openapi.json"))
|
||||
if !strings.Contains(document, pin) || !strings.Contains(document, "@common/response.api") {
|
||||
t.Fatal("missing provenance")
|
||||
}
|
||||
writeFile(t, filepath.Join(dir, "api/omni.imports.json"), strings.Replace(config, pin, strings.Repeat("0", 64), 1))
|
||||
if _, err = Parse(entry); err == nil {
|
||||
t.Fatal("checksum mismatch accepted")
|
||||
}
|
||||
}
|
||||
// An undeclared alias, traversal, symlink escape and cycle are all rejected.
|
||||
for _, imp := range []string{"@missing/response.api", "@common/../outside.api", "../shared/response.api", "@common/escape.api", "@common/cycle.api"} {
|
||||
local := filepath.Join(root, "consumer")
|
||||
writeFile(t, filepath.Join(local, "omni.imports.json"), `{"roots":{"common":{"path":"../shared"}}}`)
|
||||
writeFile(t, filepath.Join(root, "outside.api"), "type Outside {}\n")
|
||||
_ = os.Symlink(filepath.Join(root, "outside.api"), filepath.Join(shared, "escape.api"))
|
||||
writeFile(t, filepath.Join(shared, "cycle.api"), "import \"cycle.api\"\n")
|
||||
entry := filepath.Join(local, "service.api")
|
||||
writeFile(t, entry, "import "+strconvQuote(imp)+"\nservice demo{\n@handler get\nget /get\n}\n")
|
||||
if _, err := Parse(entry); err == nil {
|
||||
t.Fatal("unsafe import accepted", imp)
|
||||
}
|
||||
}
|
||||
}
|
||||
func strconvQuote(value string) string { data, _ := json.Marshal(value); return string(data) }
|
||||
func TestCatalogDocumentExportDoesNotGenerateRuntimeRegistry(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
entry := filepath.Join(dir, "service.api")
|
||||
writeFile(t, entry, "@app(defaultResponse: Common)\nresponse Common(codeType: int32; successCode: 0; errorCode: 9999; messageField: msg; dataMode: always)\nservice demo{\n@handler get\nget /get returns (null)\n}\n")
|
||||
var exported bytes.Buffer
|
||||
err := apierrors.ExportCatalog(&exported, apierrors.Catalog{DefaultStatus: 200, Definitions: map[string]apierrors.Definition{"TOO_LONG": {Code: 400002, Message: "{label}不能超过{max}个字符", Params: []apierrors.Parameter{{Name: "label", Type: "string", Example: "企业名称"}, {Name: "max", Type: "int", Example: 100}}}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeFile(t, filepath.Join(dir, "omni.errors.json"), exported.String())
|
||||
spec, err := Parse(entry)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c, err := Compile(spec)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(c.Spec.Errors) != 0 || !strings.Contains(string(c.Document), "企业名称不能超过100个字符") {
|
||||
t.Fatal(string(c.Document))
|
||||
}
|
||||
target := filepath.Join(t.TempDir(), "catalog")
|
||||
if _, err = Generate(Options{Dir: target, API: entry, Module: "example.com/catalog", FrameworkDir: frameworkRoot(t)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = os.Stat(filepath.Join(target, "internal/apierrors/errorsGen.go")); !os.IsNotExist(err) {
|
||||
t.Fatal("document metadata became a runtime error registry")
|
||||
}
|
||||
}
|
||||
71
cmd/go-omni/apigen/startup.go
Normal file
71
cmd/go-omni/apigen/startup.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func appSource(o Options, spec *Spec) string {
|
||||
source := fmt.Sprintf(`package handler
|
||||
import (
|
||||
"fmt"
|
||||
omni %q
|
||||
%q
|
||||
%q
|
||||
%q
|
||||
%q
|
||||
)
|
||||
// NewApp is generated HTTP composition. The caller owns deps and its logger.
|
||||
func NewApp(deps *svc.ServiceContext)(*omni.App,error){
|
||||
if deps==nil || deps.Lifecycle==nil{return nil,fmt.Errorf("service context and lifecycle controller required; see PROJECT.md upgrade instructions")}
|
||||
cfg:=deps.Config.Server
|
||||
cfg.LogInstance=deps.Logger
|
||||
for _,option:=range deps.AppOptions(){if option!=nil{option(&cfg)}}
|
||||
`, frameworkModule, frameworkModule+"/httpx", frameworkModule+"/config", frameworkModule+"/health", o.Module+"/internal/svc")
|
||||
source += fmt.Sprintf("cfg.Request=contract.MergePolicy(cfg.Request,%#v)\n", spec.RequestPolicy)
|
||||
source = strings.Replace(source, "\"fmt\"", "\"fmt\";"+fmt.Sprintf("%q", frameworkModule+"/contract"), 1)
|
||||
if spec.DefaultResponse != "" {
|
||||
for _, r := range spec.Responses {
|
||||
if r.Name == spec.DefaultResponse {
|
||||
data, _ := json.Marshal(r.Config)
|
||||
source += fmt.Sprintf("cfg.Response=newResponseTemplate(%q).Config();cfg.ResponseEncoder=nil\n", string(data))
|
||||
}
|
||||
}
|
||||
}
|
||||
source += "if err:=cfg.Validate();err!=nil{return nil,err}\n"
|
||||
source += "if err:=cfg.Endpoints.ValidateRoutes([]config.EndpointRoute{\n"
|
||||
for _, route := range spec.Routes {
|
||||
source += fmt.Sprintf("{Method:%q,Path:%q},\n", route.Method, route.Path)
|
||||
}
|
||||
if o.Storage == "local" {
|
||||
source += "{Method: \"GET\",Path: \"/storage/object\"},\n"
|
||||
}
|
||||
source += `}...);err!=nil{return nil,err}
|
||||
if (cfg.Endpoints.Liveness.Enabled || cfg.Endpoints.Readiness.Enabled) && deps.Health==nil{return nil,fmt.Errorf("enabled health endpoints require a checker")}
|
||||
app:=omni.New(omni.WithConfig(cfg))
|
||||
app.Use(deps.GlobalMiddleware()...)
|
||||
if err:=RegisterRoutes(app,deps);err!=nil{_ = app.Close();return nil,err}
|
||||
if cfg.Endpoints.OpenAPI.Enabled{app.GET(cfg.Endpoints.OpenAPI.Path,OpenAPI)}
|
||||
if cfg.Endpoints.Liveness.Enabled{
|
||||
var options []health.HandlerOption
|
||||
if cfg.Endpoints.Liveness.ResponseMode=="raw"{options=append(options,health.WithRawResponse())}
|
||||
app.GET(cfg.Endpoints.Liveness.Path,deps.Health.LivenessHandler(options...))
|
||||
}
|
||||
if cfg.Endpoints.Readiness.Enabled{
|
||||
options:=[]health.HandlerOption{health.WithShutdownContext(deps.Lifecycle.Context())}
|
||||
if cfg.Endpoints.Readiness.ResponseMode=="raw"{options=append(options,health.WithRawResponse())}
|
||||
app.GET(cfg.Endpoints.Readiness.Path,deps.Health.ReadinessHandler(options...))
|
||||
}
|
||||
`
|
||||
if o.Storage == "local" {
|
||||
source += `if deps.FileHTTP==nil{return nil,fmt.Errorf("local storage handler required")}
|
||||
for _,method:=range []string{"GET","HEAD","PUT"}{app.Handle(method,"/storage/object",httpx.WrapHandler(deps.FileHTTP))}
|
||||
`
|
||||
}
|
||||
source += "return app,nil\n}\n"
|
||||
if o.Storage != "local" {
|
||||
source = strings.Replace(source, fmt.Sprintf("%q", frameworkModule+"/httpx"), "", 1)
|
||||
}
|
||||
return source
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package apigen
|
||||
import "fmt"
|
||||
|
||||
var baseTemplates = map[string]string{
|
||||
"cmd/server/main.go": `package main
|
||||
"main.go": `package main
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
@@ -12,35 +12,33 @@ import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
omni "git.yuntuo.hk/k2safe/go-omni"
|
||||
"git.yuntuo.hk/k2safe/go-omni/logx"
|
||||
"git.yuntuo.hk/k2safe/go-omni/lifecycle"
|
||||
"MODULE/internal/config"
|
||||
"MODULE/internal/handler"
|
||||
"MODULE/internal/servicecontext"
|
||||
"MODULE/internal/settings"
|
||||
"MODULE/internal/svc"
|
||||
)
|
||||
func main(){
|
||||
path:=flag.String("config","etc/config.yaml","configuration file; empty uses defaults and environment")
|
||||
flag.Parse()
|
||||
if err:=run(*path);err!=nil{log.Fatal(err)}
|
||||
}
|
||||
func run(path string)(runErr error){
|
||||
ctx,stop:=signal.NotifyContext(context.Background(),os.Interrupt,syscall.SIGTERM)
|
||||
defer stop()
|
||||
cfg,err:=settings.Load(path);if err!=nil{return err}
|
||||
if err:=run(ctx,*path);err!=nil{log.Fatal(err)}
|
||||
}
|
||||
func run(ctx context.Context,path string)(runErr error){
|
||||
cfg,err:=config.Load(path);if err!=nil{return err}
|
||||
logger,err:=logx.New(cfg.Server.Log);if err!=nil{return err}
|
||||
defer func(){runErr=errors.Join(runErr,logger.Close())}()
|
||||
logx.SetDefault(logger)
|
||||
deps,err:=servicecontext.New(ctx,logger,cfg);if err!=nil{return err}
|
||||
defer func(){runErr=errors.Join(runErr,deps.Close())}()
|
||||
app:=omni.New(omni.WithConfig(cfg.Server))
|
||||
app.OnShutdown(logger.Close)
|
||||
app.OnShutdown(deps.Close)
|
||||
if err=handler.RegisterRoutes(app,deps);err!=nil{return err}
|
||||
app.GET("/openapi.json",handler.OpenAPI)
|
||||
app.GET("/health/live",deps.Health.LivenessHandler())
|
||||
app.GET("/health/ready",deps.Health.ReadinessHandler())
|
||||
logger.InfoContext(ctx,"service.starting","address",cfg.Server.Address)
|
||||
return app.RunContext(ctx,cfg.Server.Address)
|
||||
// Unjoined background work may still log. The owning process exits on this
|
||||
// error; do not close its borrowed logger before that work has finished.
|
||||
defer func(){if !errors.Is(runErr,lifecycle.ErrResourcesRetained){runErr=errors.Join(runErr,logger.Close())}}()
|
||||
deps,err:=svc.New(ctx,logger,cfg);if err!=nil{return err}
|
||||
// RunContext drains HTTP requests before returning. This scope owns dependencies
|
||||
// and closes them exactly once, before closing the borrowed logger.
|
||||
defer func(){runErr=deps.Finish(runErr)}()
|
||||
app,err:=handler.NewApp(deps);if err!=nil{return err}
|
||||
defer func(){runErr=errors.Join(runErr,app.Close())}()
|
||||
logger.InfoContext(ctx,"service.starting","address",app.Config().Address)
|
||||
return app.RunContext(deps.Lifecycle.Context())
|
||||
}
|
||||
`,
|
||||
".gitignore": "/server\n/var/\n.env\n.env.*\n!.env.example\n/.goOmni/api.lock\n",
|
||||
@@ -51,16 +49,31 @@ func projectREADME(module, entry string) string {
|
||||
|
||||
This project is generated from an editable API contract. Business code uses
|
||||
standard context.Context; generated HTTP handlers use Omni's transport-neutral httpx.
|
||||
main.go and HTTP composition are continuously generator-owned.
|
||||
|
||||
omni.gen.json selects repository: none (default) or group, and middleware:
|
||||
factories for new projects. Named middleware factories run after svc.Init;
|
||||
implement Dependencies accessors on ServiceContext and New<Name>(deps) once.
|
||||
Unimplemented/nil factories fail startup and clean registered dependencies.
|
||||
Existing projects retain middleware: handlers until explicitly migrated.
|
||||
Optional mappings generate typed assignments in internal/mapping/mappingsGen.go;
|
||||
configure source/target types and explicit conversion rules after resolving module
|
||||
dependencies. Missing fields fail generation and --check. See GENERATION.md in
|
||||
the framework repository for composition, null/reference semantics and migration.
|
||||
|
||||
## Run
|
||||
|
||||
`+"```sh"+`
|
||||
go mod tidy
|
||||
go run ./cmd/server
|
||||
go run .
|
||||
`+"```"+`
|
||||
|
||||
GET /health/live and /health/ready check the running service. GET /openapi.json
|
||||
serves the same document as docs/openapi.json. Bind address defaults to localhost.
|
||||
Configure server.endpoints to disable or move each built-in route. Health
|
||||
responseMode is app by default (project format), or raw for plain health data.
|
||||
Probe HTTP statuses remain 200/503 even when business errors use HTTP 200.
|
||||
OpenAPI remains an unwrapped document; disabled endpoints reserve no paths.
|
||||
Set GO_OMNI_TRANSPORT=native to select the native transport without changing services.
|
||||
The greeting starter works immediately; other new operation stubs return 501 until
|
||||
their business logic is implemented. Authentication hooks also fail closed with 501.
|
||||
@@ -83,9 +96,9 @@ go test ./...
|
||||
Commands inside a generated project discover the recorded API entry automatically.
|
||||
--dry-run previews create/update/remove actions without writing files.
|
||||
The --check command is read-only and suitable for CI. It fails on stale or missing
|
||||
generated files and missing business scaffolds. go.mod, configuration, main,
|
||||
Service, Repository, servicecontext, middleware hook bodies and API files are
|
||||
created once and then preserved byte for byte. Gen.go files and OpenAPI are owned
|
||||
generated files and missing business scaffolds. go.mod, configuration, Logic,
|
||||
Repository, svc, middleware hook bodies and API files are created once and then
|
||||
preserved byte for byte. main.go, Gen.go files and OpenAPI are owned
|
||||
by the generator; manual edits cause a conflict instead of being overwritten.
|
||||
Commit .goOmni/apiManifest.json alongside the generated files. Do not edit it.
|
||||
Avoid editing files while generation is running; api.lock serializes generators.
|
||||
@@ -95,33 +108,70 @@ business files remain: remove/adapt obsolete implementations explicitly if an
|
||||
API change makes their old types or signatures fail to compile. Every regeneration
|
||||
must be followed by go test ./... . Do not delete service files to change contracts.
|
||||
|
||||
Generation compares the last saved contract with the new one and reports affected
|
||||
Logic files, including nested DTO field/tag changes and streaming/body changes.
|
||||
REVIEW notices require a business review. ERROR diagnostics identify definite
|
||||
Logic signature, syntax or duplicate-method problems. Generation preserves
|
||||
business code and still writes valid generated output; fix those methods next.
|
||||
--check fails on definite Logic errors even after generated files are current.
|
||||
Import aliases, parameter names and moved methods within a group are supported.
|
||||
Project type aliases, embedded methods or build tags may need compiler verification
|
||||
and produce REVIEW notices. This source scan does not execute project code or
|
||||
replace go test ./... . Snapshot-based REVIEW notices describe changes since the
|
||||
last successful generation; they do not persist as an acknowledgement checklist.
|
||||
|
||||
## Contract tools
|
||||
|
||||
api format [--check] preserves comments and normalizes indentation. api upgrade
|
||||
previews changed infrastructure; --apply only replaces recognized prior seeds.
|
||||
Use @request policy overrides and @app(request: {...}) for query/JSON strictness.
|
||||
Explicitly exported api/omni.errors.json documents the Go-owned error catalog;
|
||||
the CLI never executes project Go. omni.imports.json permits named shared roots;
|
||||
workspace format/generate/check processes explicitly listed independent services.
|
||||
Full reference: https://git.yuntuo.hk/k2safe/go-omni/src/branch/main/DEVELOPMENT.md
|
||||
|
||||
## Layout
|
||||
|
||||
`+"```text"+`
|
||||
api/ # editable contract and imported types
|
||||
cmd/server/main.go # startup, lifecycle and route registration
|
||||
main.go # GENERATED startup, lifecycle and route registration
|
||||
etc/config.yaml # lowerCamelCase configuration
|
||||
internal/settings/ # defaults -> file -> environment
|
||||
internal/servicecontext/ # dependency initialization and cleanup
|
||||
internal/types/typesGen.go # generated request/response types
|
||||
internal/config/ # defaults -> file -> environment
|
||||
internal/svc/ # dependency initialization and cleanup
|
||||
internal/types/*Gen.go # DTOs split by source API, one types package
|
||||
internal/svc/runtimeGen.go # GENERATED lifecycle and infrastructure startup
|
||||
internal/apierrors/errorsGen.go # generated fresh error constructors, when declared
|
||||
internal/handler/<group>/ # generated binding and service adapters
|
||||
internal/service/<group>/ # developer-owned business operations
|
||||
internal/repository/<group>/ # developer-owned persistence contracts
|
||||
internal/middleware/ # named hooks and generated registry
|
||||
internal/handler/<group>/ # generated binding and Logic adapters
|
||||
internal/logic/<group>/ # developer-owned business operations
|
||||
internal/repository/<group>/ # optional; repository: group creates editable seeds
|
||||
internal/middleware/ # typed dependency interface, factories and registry
|
||||
omni.gen.json # developer-owned generator options and mappings
|
||||
internal/mapping/*Gen.go # optional compile-checked struct mappings
|
||||
docs/openapi.json # generated OpenAPI 3.0.3
|
||||
.goOmni/apiManifest.json # ownership and content hashes
|
||||
`+"```"+`
|
||||
|
||||
The API response type describes business data. Declare response Name (...) and
|
||||
select it with @server(response: Name) or @response Name to share a public body;
|
||||
select the App default with @app(defaultResponse: Name) in the entry API.
|
||||
@server(response: Name) and @response Name override it for groups/routes;
|
||||
@response raw returns the business JSON directly. Without a template, the default
|
||||
envelope includes code, message, data and requestId. Generated routes and OpenAPI
|
||||
use the API contract, independently of the app's default response configuration.
|
||||
Handlers reject nil successful responses. Body DTOs use application/json and have a 1 MiB limit by default.
|
||||
Use contract.Binder.Bind's optional limit in a custom adapter for larger payloads.
|
||||
use the same contract. 404/405 and ordinary App routes use the declared App default.
|
||||
No template is selected by declaration/import order. Without @app, existing framework
|
||||
response defaults apply. An explicit API App default takes precedence over response
|
||||
options in svc, keeping generated routes and OpenAPI consistent.
|
||||
Object responses reject nil; []DTO maps nil to []; *DTO permits null; returns(null) is a successful null value. No returns means 204. Body DTOs use application/json and have a 1 MiB limit by default.
|
||||
Use @body(maxBytes: ...) for route limits. @body(mode: raw|multipart; maxBytes: ...)
|
||||
adds a *httpx.Body argument to Logic, without handwritten handlers. Raw is the exact
|
||||
limited request stream; Parts supports sequential multipart reading with no temp files.
|
||||
@produces "application/octet-stream" (or another concrete media type) generates a
|
||||
Logic result of *httpx.Stream; the handler closes Content and streams it unwrapped.
|
||||
Signature verification and storage policy belong to Logic/middleware. A consumed
|
||||
request stream cannot be reread automatically; use one reader owner or explicitly
|
||||
bounded buffering. Stream reads respect configured server timeouts. Declare multipart/multipartJSON/file DTO fields for validated uploads; without those fields the existing streaming Body is passed. form tags remain URL query parameters. The optional manual
|
||||
handler/custom binder hooks are preserved, and are needed only for custom protocols.
|
||||
|
||||
Fields require exactly one source tag: json, path, form (URL query only), header.
|
||||
Fields require one source tag: json, path, form (URL query), header, multipart, multipartJSON or file. Multipart fields select full automatic binding before Logic; File/[]File are request-scoped disk-backed uploads. Use File.Open to stream them; generated cleanup owns their lifetime.
|
||||
All are required by presence unless optional, omitempty or default is specified.
|
||||
Zero and false are valid present values; binding:"required" rejects scalar zero.
|
||||
Use *T with optional when business logic needs to distinguish missing from zero.
|
||||
@@ -132,20 +182,52 @@ and binding compiler. Responses are expected to honor their declared constraints
|
||||
they are not validated on every response. Do not return nil slices/maps for a
|
||||
non-nullable collection (initialize empty collections instead).
|
||||
|
||||
Declare public business errors in error (...) and reference them with @errors.
|
||||
Go-owned errors can be registered once through svc.ServiceContext.AppOptions with
|
||||
omni.WithErrors(projecterrors.Catalog()). Generated routes inherit them unless they
|
||||
declare an independent catalog. Do not repeat Go-owned errors in the API contract.
|
||||
Alternatively, declare public business errors in error (...) and reference them with @errors.
|
||||
Use internal/apierrors constructors to share status, code and message with OpenAPI.
|
||||
Auth declarations connect a bearer/header/cookie scheme to a real named middleware.
|
||||
The selected auth runs first; @auth none removes inherited declarative auth.
|
||||
Validation policy remains in the middleware implementation. Public messages must
|
||||
not contain secrets; error causes may be included in server logs.
|
||||
|
||||
Generated svc.New receives Config and a borrowed Logger. Add dependencies in svc.Init,
|
||||
then immediately register each cleanup with OnClose. A later initialization failure
|
||||
runs registered cleanups in reverse order. Close is idempotent. GlobalMiddleware
|
||||
provides global hooks; Middlewares supplies named API hooks. AppOptions is evaluated
|
||||
once before route registration; logging is configured earlier through Config.
|
||||
The generated main runs HTTP draining, all OnStop hooks, all OnDrain hooks, resource
|
||||
OnClose callbacks, then logger close. Stop/drain hooks within a phase run concurrently;
|
||||
combine components with ordering dependencies into one pair. Pair OnStop/OnDrain
|
||||
with the same name. OnDrain returns nil only after final work and acknowledgements
|
||||
have finished. Use WorkContext for accepted background work; Context is an early
|
||||
shutdown notification. server.lifecycle configures stopTimeout (5s), drainTimeout
|
||||
(10s), and cancelTimeout (5s grace per timed-out phase). Zero uses defaults.
|
||||
Timeout/failure cancels WorkContext. If hooks cannot prove completion, resources
|
||||
and the borrowed logger are retained and the generated process exits unsuccessfully.
|
||||
Errors retain their causes. No automatic retry of close or force killing of goroutines.
|
||||
Do not close borrowed resources or attach duplicate cleanup hooks to App. The startup
|
||||
context cancels on signals; long-lived clients/workers that requests depend on need
|
||||
their own lifetime, canceled by OnClose after HTTP draining.
|
||||
|
||||
svc.RequestShutdown(err) lets a dependency report a fatal service failure.
|
||||
It returns immediately. Generated main stops accepting requests, drains them,
|
||||
closes dependencies and logs, then exits nonzero with the original cause.
|
||||
nil requests a normal stop. Concurrent calls keep the first non-nil failure;
|
||||
notifications during initialization or draining are retained. Close must stop
|
||||
and join project-owned monitors before the final shutdown result is returned.
|
||||
The svc lifecycle context signals draining, not permission to close resources
|
||||
still used by active requests. Liveness and readiness are different: shutdown
|
||||
makes readiness 503; it does not turn a running process's liveness into failure.
|
||||
|
||||
Named middleware is composed in declaration order (first is outermost). Implement
|
||||
the generated hooks or replace entries in servicecontext; never bypass an auth
|
||||
the generated hooks or replace entries in svc; never bypass an auth
|
||||
hook merely to make the server start. Middleware names do not imply a JWT policy.
|
||||
|
||||
This CLI supports a documented go-zero-style subset, not all goctl features.
|
||||
Database tables and repositories cannot be inferred from an HTTP contract. Add
|
||||
business repositories through servicecontext. Database and storage options are
|
||||
business repositories through svc. Database and storage options are
|
||||
composed at creation; the original profile is retained on regeneration.
|
||||
`, module, entry)
|
||||
}
|
||||
@@ -156,7 +238,7 @@ func infrastructureREADME(o Options) string {
|
||||
|
||||
Selected database: %s; storage: %s; logging: %s/%s.
|
||||
MySQL uses database/sql through deps.DB, including InTx and a readiness probe.
|
||||
Set GO_OMNI_DB_DSN; pool settings live in etc/config.yaml and GO_OMNI_DB_*.
|
||||
Use structured database/mysql configuration and GO_OMNI_DB_PASSWORD. Other connection/pool/TLS settings live in etc/config.yaml and GO_OMNI_DB_*.
|
||||
No tables are invented or migrated automatically from request/response DTOs.
|
||||
The none/memory selection opens no database; implement repositories per business.
|
||||
|
||||
|
||||
54
cmd/go-omni/apigen/testdata/arrays/consumer_test.go
vendored
Normal file
54
cmd/go-omni/apigen/testdata/arrays/consumer_test.go
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"example.com/arrays/internal/config"
|
||||
"example.com/arrays/internal/handler"
|
||||
"example.com/arrays/internal/svc"
|
||||
"git.yuntuo.hk/k2safe/go-omni/logx"
|
||||
"io"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestArrayWire(t *testing.T) {
|
||||
cfg := config.Default()
|
||||
cfg.Server.Log.Writer = io.Discard
|
||||
logger, err := logx.New(cfg.Server.Log)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer logger.Close()
|
||||
deps, err := svc.New(context.Background(), logger, cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer deps.Close()
|
||||
app, err := handler.NewApp(deps)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer app.Close()
|
||||
for path, want := range map[string]string{
|
||||
"/strings": `["企业","备注"]`, "/integers": `[9223372036854775807,-3]`, "/dynamic": `["text",7,true,null,{"name":"企业"},[false,2]]`, "/empty": `[]`, "/raw": `[true,false]`,
|
||||
} {
|
||||
w := httptest.NewRecorder()
|
||||
app.ServeHTTP(w, httptest.NewRequest("GET", path, nil))
|
||||
got := strings.TrimSpace(w.Body.String())
|
||||
if w.Code != 200 {
|
||||
t.Fatal(got)
|
||||
}
|
||||
if path != "/raw" {
|
||||
var obj map[string]json.RawMessage
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &obj); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got = string(obj["data"])
|
||||
}
|
||||
if got != want {
|
||||
t.Fatalf("%s got %s want %s", path, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
13
cmd/go-omni/apigen/testdata/arrays/values.go
vendored
Normal file
13
cmd/go-omni/apigen/testdata/arrays/values.go
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
package arrays
|
||||
|
||||
import "context"
|
||||
|
||||
func (s *Logic) Strings(context.Context) ([]string, error) { return []string{"企业", "备注"}, nil }
|
||||
func (s *Logic) Integers(context.Context) ([]int64, error) {
|
||||
return []int64{9223372036854775807, -3}, nil
|
||||
}
|
||||
func (s *Logic) Dynamic(context.Context) ([]any, error) {
|
||||
return []any{"text", 7, true, nil, map[string]any{"name": "企业"}, []any{false, 2}}, nil
|
||||
}
|
||||
func (s *Logic) Empty(context.Context) ([]any, error) { return nil, nil }
|
||||
func (s *Logic) Raw(context.Context) ([]bool, error) { return []bool{true, false}, nil }
|
||||
22
cmd/go-omni/apigen/testdata/business/accessFactory.go
vendored
Normal file
22
cmd/go-omni/apigen/testdata/business/accessFactory.go
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.yuntuo.hk/k2safe/go-omni/httpx"
|
||||
)
|
||||
|
||||
// This package deliberately imports no svc; the dependency is supplied explicitly.
|
||||
type AccessChecker interface {
|
||||
CheckAccess(context.Context, string) error
|
||||
}
|
||||
|
||||
func NewRequireAccess(checker AccessChecker) httpx.Middleware {
|
||||
return func(next httpx.Handler) httpx.Handler {
|
||||
return func(c *httpx.Context) error {
|
||||
if err := checker.CheckAccess(c.Context(), c.Header("Authorization")); err != nil {
|
||||
return err
|
||||
}
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
32
cmd/go-omni/apigen/testdata/business/client.go
vendored
Normal file
32
cmd/go-omni/apigen/testdata/business/client.go
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"example.com/business/internal/middleware"
|
||||
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
||||
"git.yuntuo.hk/k2safe/go-omni/httpx"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
// A local fixture checks wiring; production authentication is project-owned.
|
||||
type accessClient struct{ closed atomic.Bool }
|
||||
|
||||
func (c *accessClient) Close() error { c.closed.Store(true); return nil }
|
||||
func (c *accessClient) CheckAccess(ctx context.Context, token string) error {
|
||||
if c.closed.Load() {
|
||||
return apierrors.New(503, "CLOSED", "closed")
|
||||
}
|
||||
if httpx.Metadata(ctx).Route == "" {
|
||||
return apierrors.New(500, "NO_METADATA", "missing route")
|
||||
}
|
||||
if token == "reject" {
|
||||
return apierrors.New(403, "DENIED", "denied")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (c *ServiceContext) initAccess(ctx context.Context) error {
|
||||
c.Client = &accessClient{}
|
||||
c.OnClose(c.Client.Close)
|
||||
c.Middlewares["RequireAccess"] = middleware.NewRequireAccess(c.Client)
|
||||
return nil
|
||||
}
|
||||
119
cmd/go-omni/apigen/testdata/business/consumer_test.go
vendored
Normal file
119
cmd/go-omni/apigen/testdata/business/consumer_test.go
vendored
Normal file
@@ -0,0 +1,119 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"example.com/business/internal/config"
|
||||
"example.com/business/internal/handler"
|
||||
"example.com/business/internal/svc"
|
||||
"git.yuntuo.hk/k2safe/go-omni/logx"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestShapesAndPolicy(t *testing.T) {
|
||||
cfg := config.Default()
|
||||
cfg.Server.Log.Writer = io.Discard
|
||||
logger, err := logx.New(cfg.Server.Log)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer logger.Close()
|
||||
deps, err := svc.New(context.Background(), logger, cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer deps.Close()
|
||||
app, err := handler.NewApp(deps)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer app.Close()
|
||||
for _, tt := range []struct {
|
||||
method, path, body, media string
|
||||
status int
|
||||
data string
|
||||
raw bool
|
||||
}{
|
||||
{"GET", "/list", "", "", 200, `[]`, false},
|
||||
{"GET", "/list?mode=full&tags=a&tags=b", "", "", 200, `[{"name":"one"}]`, false},
|
||||
{"GET", "/nullable", "", "", 200, `null`, false},
|
||||
{"GET", "/nullable?mode=full", "", "", 200, `{"name":"one"}`, false},
|
||||
{"POST", "/nothing", "", "", 200, `null`, false},
|
||||
{"GET", "/rawNull", "", "", 200, `null`, true},
|
||||
{"GET", "/rawArray", "", "", 200, `[]`, true},
|
||||
{"DELETE", "/noContent", "", "", 204, "", true},
|
||||
{"POST", "/empty", `{}`, "application/json", 200, `null`, false},
|
||||
{"POST", "/empty", ``, "application/json", 400, "", false},
|
||||
{"POST", "/empty", `{"unknown":1}`, "application/json", 400, "", false},
|
||||
{"POST", "/absent", ``, "", 200, `null`, false},
|
||||
{"POST", "/absent", `{}`, "application/json", 400, "", false},
|
||||
{"POST", "/optional", ``, "", 200, `{"name":""}`, false},
|
||||
{"POST", "/optional", `{}`, "application/json", 400, "", false},
|
||||
{"POST", "/echo", `{"name":"ok"}`, "application/json", 200, `{"name":"ok"}`, false},
|
||||
{"POST", "/echo", `{"name":"ok"}`, "application/json; charset=latin1", 415, "", false},
|
||||
{"POST", "/echo", "{\"name\":\"\xff\"}", "application/json", 400, "", false},
|
||||
{"GET", "/list?unknown=1", "", "", 400, "", false},
|
||||
{"GET", "/rawNull?unknown=1", "", "", 400, "", false},
|
||||
{"POST", "/tolerant?unknown=1", "{\"name\":\"\xff\"}", "application/json; charset=latin1", 200, `{"name":"<22>"}`, false},
|
||||
} {
|
||||
t.Run(tt.method+tt.path+tt.body+tt.media, func(t *testing.T) {
|
||||
r := httptest.NewRequest(tt.method, tt.path, strings.NewReader(tt.body))
|
||||
if tt.media != "" {
|
||||
r.Header.Set("Content-Type", tt.media)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
app.ServeHTTP(w, r)
|
||||
if w.Code != tt.status {
|
||||
t.Fatalf("%d %s", w.Code, w.Body)
|
||||
}
|
||||
if tt.status >= 400 {
|
||||
return
|
||||
}
|
||||
data := strings.TrimSpace(w.Body.String())
|
||||
if !tt.raw {
|
||||
var obj map[string]json.RawMessage
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &obj); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
v, ok := obj["data"]
|
||||
if !ok {
|
||||
t.Fatal("missing data")
|
||||
}
|
||||
data = string(v)
|
||||
}
|
||||
if data != tt.data {
|
||||
t.Fatalf("data %s expected %s", data, tt.data)
|
||||
}
|
||||
})
|
||||
}
|
||||
var body bytes.Buffer
|
||||
form := multipart.NewWriter(&body)
|
||||
_ = form.WriteField("title", "title")
|
||||
_ = form.WriteField("metadata", `{"name":"meta"}`)
|
||||
f, err := form.CreateFormFile("attachment", "a.txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, _ = io.WriteString(f, "content")
|
||||
_ = form.Close()
|
||||
r := httptest.NewRequest("POST", "/upload", &body)
|
||||
r.Header.Set("Content-Type", form.FormDataContentType())
|
||||
w := httptest.NewRecorder()
|
||||
app.ServeHTTP(w, r)
|
||||
if w.Code != 200 || !strings.Contains(w.Body.String(), "title:meta:content") {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
denied := httptest.NewRequest("GET", "/list", nil)
|
||||
denied.Header.Set("Authorization", "reject")
|
||||
result := httptest.NewRecorder()
|
||||
app.ServeHTTP(result, denied)
|
||||
if result.Code != 403 {
|
||||
t.Fatal("injected middleware did not run", result.Code, result.Body.String())
|
||||
}
|
||||
|
||||
}
|
||||
42
cmd/go-omni/apigen/testdata/business/operations.go
vendored
Normal file
42
cmd/go-omni/apigen/testdata/business/operations.go
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"example.com/business/internal/types"
|
||||
"io"
|
||||
)
|
||||
|
||||
func (s *Logic) List(ctx context.Context, req *types.Query) ([]types.Item, error) {
|
||||
if req.Mode == "full" {
|
||||
return []types.Item{{Name: "one"}}, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
func (s *Logic) Nullable(ctx context.Context, req *types.Query) (*types.Item, error) {
|
||||
if req.Mode == "full" {
|
||||
return &types.Item{Name: "one"}, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
func (s *Logic) Nothing(ctx context.Context) error { return nil }
|
||||
func (s *Logic) RawNull(ctx context.Context) error { return nil }
|
||||
func (s *Logic) RawArray(ctx context.Context) ([]types.Item, error) { return nil, nil }
|
||||
func (s *Logic) Empty(ctx context.Context) error { return nil }
|
||||
func (s *Logic) Absent(ctx context.Context) error { return nil }
|
||||
func (s *Logic) Optional(ctx context.Context, req *types.Item) (*types.Item, error) { return req, nil }
|
||||
func (s *Logic) Echo(ctx context.Context, req *types.Item) (*types.Item, error) { return req, nil }
|
||||
func (s *Logic) Tolerant(ctx context.Context, req *types.Item) (*types.Item, error) { return req, nil }
|
||||
func (s *Logic) NoContent(ctx context.Context) error { return nil }
|
||||
|
||||
func (s *Logic) Upload(ctx context.Context, req *types.Upload) (*types.Item, error) {
|
||||
f, err := req.Attachment.Open()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
data, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &types.Item{Name: req.Title + ":" + req.Metadata.Name + ":" + string(data)}, nil
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package compatibility_test
|
||||
package main_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"example.com/compatibility/internal/handler"
|
||||
"example.com/compatibility/internal/servicecontext"
|
||||
"example.com/compatibility/internal/svc"
|
||||
omni "git.yuntuo.hk/k2safe/go-omni"
|
||||
"git.yuntuo.hk/k2safe/go-omni/logx"
|
||||
"io"
|
||||
@@ -25,7 +25,7 @@ func TestGeneratedCompatibility(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer logger.Close()
|
||||
deps, err := servicecontext.New(context.Background(), logger)
|
||||
deps, err := svc.New(context.Background(), logger)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -5,12 +5,12 @@ import (
|
||||
"example.com/compatibility/internal/types"
|
||||
)
|
||||
|
||||
func (s *Service) Echo(ctx context.Context, req *types.EchoReq) (*types.EchoResp, error) {
|
||||
func (s *Logic) Echo(ctx context.Context, req *types.EchoReq) (*types.EchoResp, error) {
|
||||
return &types.EchoResp{ID: req.ID, Data: req.Data}, nil
|
||||
}
|
||||
func (s *Service) Item(ctx context.Context, req *types.ItemReq) (*types.EchoResp, error) {
|
||||
func (s *Logic) Item(ctx context.Context, req *types.ItemReq) (*types.EchoResp, error) {
|
||||
return &types.EchoResp{ID: req.ID, Data: map[string]any{"found": true}}, nil
|
||||
}
|
||||
func (s *Service) Custom(ctx context.Context, req *types.CustomReq) (*types.CustomResp, error) {
|
||||
func (s *Logic) Custom(ctx context.Context, req *types.CustomReq) (*types.CustomResp, error) {
|
||||
return &types.CustomResp{Value: req.Value}, nil
|
||||
}
|
||||
|
||||
30
cmd/go-omni/apigen/testdata/composition/accessFactory.go
vendored
Normal file
30
cmd/go-omni/apigen/testdata/composition/accessFactory.go
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"git.yuntuo.hk/k2safe/go-omni/httpx"
|
||||
)
|
||||
|
||||
var ErrFactory = errors.New("factory fault")
|
||||
|
||||
func NewAccess(deps Dependencies) (httpx.Middleware, error) {
|
||||
if deps.Access() == nil {
|
||||
return nil, errors.New("dependencies not initialized")
|
||||
}
|
||||
switch deps.FactoryMode() {
|
||||
case "fail":
|
||||
return nil, ErrFactory
|
||||
case "nil":
|
||||
return nil, nil
|
||||
case "panic":
|
||||
panic(ErrFactory)
|
||||
}
|
||||
return func(next httpx.Handler) httpx.Handler {
|
||||
return func(c *httpx.Context) error {
|
||||
if err := deps.Access().Check(c.Context()); err != nil {
|
||||
return err
|
||||
}
|
||||
return next(c)
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
52
cmd/go-omni/apigen/testdata/composition/amount.go
vendored
Normal file
52
cmd/go-omni/apigen/testdata/composition/amount.go
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
package amount
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// This fixture's business policy is exact decimal HALF_UP. The framework does
|
||||
// not choose its precision, rounding mode or currency.
|
||||
type Money string
|
||||
|
||||
var decimalPattern = regexp.MustCompile(`^-?[0-9]+(\.[0-9]+)?$`)
|
||||
|
||||
func (m Money) String() string {
|
||||
if m == "" {
|
||||
return "0.00"
|
||||
}
|
||||
return string(m)
|
||||
}
|
||||
func (m Money) MarshalJSON() ([]byte, error) { return json.Marshal(m.String()) }
|
||||
func (m *Money) UnmarshalJSON(data []byte) error {
|
||||
var text string
|
||||
if err := json.Unmarshal(data, &text); err != nil {
|
||||
return err
|
||||
}
|
||||
if !decimalPattern.MatchString(text) {
|
||||
return errors.New("invalid decimal")
|
||||
}
|
||||
value, ok := new(big.Rat).SetString(text)
|
||||
if !ok {
|
||||
return errors.New("invalid decimal")
|
||||
}
|
||||
value.Mul(value, big.NewRat(100, 1))
|
||||
sign := value.Sign()
|
||||
value.Abs(value)
|
||||
cents, rem := new(big.Int), new(big.Int)
|
||||
cents.QuoRem(value.Num(), value.Denom(), rem)
|
||||
if rem.Mul(rem, big.NewInt(2)).Cmp(value.Denom()) >= 0 {
|
||||
cents.Add(cents, big.NewInt(1))
|
||||
}
|
||||
whole, fraction := new(big.Int), new(big.Int)
|
||||
whole.QuoRem(cents, big.NewInt(100), fraction)
|
||||
prefix := ""
|
||||
if sign < 0 && cents.Sign() != 0 {
|
||||
prefix = "-"
|
||||
}
|
||||
*m = Money(fmt.Sprintf("%s%s.%02d", prefix, whole.String(), fraction.Int64()))
|
||||
return nil
|
||||
}
|
||||
36
cmd/go-omni/apigen/testdata/composition/client.go
vendored
Normal file
36
cmd/go-omni/apigen/testdata/composition/client.go
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"example.com/composition/internal/middleware"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
var Closes atomic.Int64
|
||||
var ErrInit = errors.New("init fault")
|
||||
|
||||
type Client struct{ closed atomic.Bool }
|
||||
|
||||
func (c *Client) Check(ctx context.Context) error {
|
||||
if c.closed.Load() {
|
||||
return errors.New("client closed before HTTP drained")
|
||||
}
|
||||
return ctx.Err()
|
||||
}
|
||||
func (c *ServiceContext) initClient(ctx context.Context) error {
|
||||
c.client = &Client{}
|
||||
c.OnClose(func() error {
|
||||
if !c.client.closed.CompareAndSwap(false, true) {
|
||||
return errors.New("double close")
|
||||
}
|
||||
Closes.Add(1)
|
||||
return nil
|
||||
})
|
||||
if c.Config.FactoryMode == "initFail" {
|
||||
return ErrInit
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (c *ServiceContext) Access() middleware.AccessChecker { return c.client }
|
||||
func (c *ServiceContext) FactoryMode() string { return c.Config.FactoryMode }
|
||||
218
cmd/go-omni/apigen/testdata/composition/consumer_test.go
vendored
Normal file
218
cmd/go-omni/apigen/testdata/composition/consumer_test.go
vendored
Normal file
@@ -0,0 +1,218 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"example.com/composition/internal/amount"
|
||||
"example.com/composition/internal/config"
|
||||
"example.com/composition/internal/convert"
|
||||
"example.com/composition/internal/domain"
|
||||
"example.com/composition/internal/handler"
|
||||
"example.com/composition/internal/mapping"
|
||||
"example.com/composition/internal/middleware"
|
||||
"example.com/composition/internal/svc"
|
||||
"example.com/composition/internal/types"
|
||||
"git.yuntuo.hk/k2safe/go-omni/logx"
|
||||
)
|
||||
|
||||
func logger(t *testing.T) *logx.Logger {
|
||||
t.Helper()
|
||||
cfg := logx.Default()
|
||||
cfg.Writer = io.Discard
|
||||
l, e := logx.New(cfg)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
t.Cleanup(func() { _ = l.Close() })
|
||||
return l
|
||||
}
|
||||
func TestHTTPComposition(t *testing.T) {
|
||||
deps, e := svc.New(context.Background(), logger(t))
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
defer deps.Close()
|
||||
app, e := handler.NewApp(deps)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
defer app.Close()
|
||||
for _, path := range []string{"/enterprise", "/tenant/123", "/admin/123"} {
|
||||
for _, tc := range []struct {
|
||||
name, body string
|
||||
status int
|
||||
salary, nameValue string
|
||||
employer any
|
||||
}{
|
||||
{"exact", `{"name":"employee","salary":"90071992547409.91","employer":null,"enabled":false}`, 200, "90071992547409.91", "employee", nil},
|
||||
{"round", `{"salary":"1.005","employer":"0"}`, 200, "1.01", "", "0.00"},
|
||||
{"missing", `{}`, 200, "0.00", "", nil},
|
||||
{"null", `{"name":null,"salary":null,"employer":null,"enabled":null}`, 200, "0.00", "", nil},
|
||||
{"zero", `{"salary":"0","enabled":false}`, 200, "0.00", "", nil},
|
||||
{"invalid", `{"salary":"abc"}`, 400, "", "", nil},
|
||||
{"empty", `{"salary":""}`, 400, "", "", nil},
|
||||
{"numeric", `{"salary":100}`, 400, "", "", nil},
|
||||
{"flat", `{"SalaryFields":{"salary":"1"}}`, 400, "", "", nil},
|
||||
{"validation", `{"name":"` + strings.Repeat("x", 81) + `"}`, 400, "", "", nil},
|
||||
} {
|
||||
t.Run(path+tc.name, func(t *testing.T) {
|
||||
req := httptest.NewRequest("POST", path, strings.NewReader(tc.body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
app.Engine().ServeHTTP(w, req)
|
||||
if w.Code != tc.status {
|
||||
t.Fatalf("status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
if w.Code == 200 {
|
||||
var out map[string]any
|
||||
if e := json.Unmarshal(w.Body.Bytes(), &out); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if out["salary"] != tc.salary || out["employeeName"] != tc.nameValue || out["employer"] != tc.employer || out["id"] != "9007199254740993" {
|
||||
t.Fatal(out)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestFullFormAndMappingSemantics(t *testing.T) {
|
||||
name := "employee"
|
||||
money := amount.Money("2.00")
|
||||
yes := true
|
||||
req := types.EnterpriseRequest{Name: &name, Salary: &money, Enabled: &yes, Labels: []string{"one"}}
|
||||
first, e := mapping.FromEnterprise(req)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
second, e := mapping.FromEnterprise(types.EnterpriseRequest{})
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if first.EmployeeName != "employee" || !first.Enabled || second.EmployeeName != "" || second.Enabled || second.Salary.String() != "0.00" || second.Employer != nil {
|
||||
t.Fatal("full-form update became patch behavior")
|
||||
}
|
||||
// Direct assignments retain standard Go aliasing, including scalar pointers.
|
||||
first.Labels[0] = "shared"
|
||||
if req.Labels[0] != "shared" {
|
||||
t.Fatal("unexpected implicit deep copy")
|
||||
}
|
||||
req.Employer = &money
|
||||
first, e = mapping.FromEnterprise(req)
|
||||
if e != nil || first.Employer != req.Employer {
|
||||
t.Fatal("nullable pointer changed", e)
|
||||
}
|
||||
bad := "reject"
|
||||
failed, e := mapping.FromEnterprise(types.EnterpriseRequest{Name: &bad})
|
||||
if !errors.Is(e, convert.ErrName) || failed.EmployeeName != "" || !strings.Contains(e.Error(), "EmployeeName") {
|
||||
t.Fatal("converter cause/zero result lost", e)
|
||||
}
|
||||
empty, e := mapping.ToBatch(domain.Batch{})
|
||||
if e != nil || empty.Rows != nil {
|
||||
t.Fatal("nil slice changed", e)
|
||||
}
|
||||
src := domain.Batch{Rows: []domain.Row{}, Fixed: [2]domain.Row{{Value: "a"}, {Value: "b"}}, Optional: &name}
|
||||
out, e := mapping.ToBatch(src)
|
||||
if e != nil || out.Rows == nil || len(out.Rows) != 0 || out.Fixed[1].Value != "b" || out.Optional != src.Optional {
|
||||
t.Fatal(out, e)
|
||||
}
|
||||
src.Rows = []domain.Row{{Value: "one"}}
|
||||
out, e = mapping.ToBatch(src)
|
||||
if e != nil || out.Rows[0].Value != "one" {
|
||||
t.Fatal(out, e)
|
||||
}
|
||||
out.Rows[0].Value = "changed"
|
||||
if src.Rows[0].Value != "one" {
|
||||
t.Fatal("element mapping reused input slice")
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 32; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
result, err := mapping.FromEnterprise(types.EnterpriseRequest{Name: &name, Salary: &money})
|
||||
if err != nil || result.EmployeeName != name || result.Salary != money {
|
||||
t.Error(result, err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
func TestFactoryLifecycle(t *testing.T) {
|
||||
for _, mode := range []string{"fail", "nil", "initFail", "panic"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
before := svc.Closes.Load()
|
||||
cfg := config.Default()
|
||||
cfg.FactoryMode = mode
|
||||
if mode == "panic" {
|
||||
func() {
|
||||
defer func() {
|
||||
if recover() != middleware.ErrFactory {
|
||||
t.Error("panic cause changed")
|
||||
}
|
||||
}()
|
||||
_, _ = svc.New(context.Background(), logger(t), cfg)
|
||||
}()
|
||||
} else {
|
||||
deps, err := svc.New(context.Background(), logger(t), cfg)
|
||||
if deps != nil || err == nil {
|
||||
t.Fatal("factory did not fail closed")
|
||||
}
|
||||
if mode == "fail" && !errors.Is(err, middleware.ErrFactory) {
|
||||
t.Fatal("factory cause lost", err)
|
||||
}
|
||||
if mode == "initFail" && !errors.Is(err, svc.ErrInit) {
|
||||
t.Fatal("init cause lost", err)
|
||||
}
|
||||
}
|
||||
if svc.Closes.Load() != before+1 {
|
||||
t.Fatal("dependency not closed exactly once")
|
||||
}
|
||||
})
|
||||
}
|
||||
deps, e := svc.New(context.Background(), logger(t))
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if e = deps.Access().Check(context.Background()); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
before := svc.Closes.Load()
|
||||
if e = deps.Close(); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if e = deps.Close(); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if svc.Closes.Load() != before+1 {
|
||||
t.Fatal("normal close repeated")
|
||||
}
|
||||
}
|
||||
func TestGeneratedEntryListenFailure(t *testing.T) {
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer listener.Close()
|
||||
path := filepath.Join(t.TempDir(), "config.yaml")
|
||||
if err = os.WriteFile(path, []byte("server:\n address: "+listener.Addr().String()+"\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before := svc.Closes.Load()
|
||||
if err = run(context.Background(), path); err == nil {
|
||||
t.Fatal("listen should fail")
|
||||
}
|
||||
if svc.Closes.Load() != before+1 {
|
||||
t.Fatal("listen failure did not close dependencies")
|
||||
}
|
||||
}
|
||||
29
cmd/go-omni/apigen/testdata/composition/converters.go
vendored
Normal file
29
cmd/go-omni/apigen/testdata/composition/converters.go
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
package convert
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"example.com/composition/internal/amount"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var ErrName = errors.New("rejected name")
|
||||
|
||||
// Explicit project rules: full-form omission clears strings/bools and uses zero
|
||||
// for required money. Nullable employer amounts remain nil by direct assignment.
|
||||
func Name(value *string) (string, error) {
|
||||
if value == nil {
|
||||
return "", nil
|
||||
}
|
||||
if *value == "reject" {
|
||||
return "", ErrName
|
||||
}
|
||||
return *value, nil
|
||||
}
|
||||
func Money(value *amount.Money) amount.Money {
|
||||
if value == nil {
|
||||
return ""
|
||||
}
|
||||
return *value
|
||||
}
|
||||
func Bool(value *bool) bool { return value != nil && *value }
|
||||
func ID(value int64) string { return strconv.FormatInt(value, 10) }
|
||||
9
cmd/go-omni/apigen/testdata/composition/dependencies.go
vendored
Normal file
9
cmd/go-omni/apigen/testdata/composition/dependencies.go
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
package middleware
|
||||
|
||||
import "context"
|
||||
|
||||
type AccessChecker interface{ Check(context.Context) error }
|
||||
type Dependencies interface {
|
||||
Access() AccessChecker
|
||||
FactoryMode() string
|
||||
}
|
||||
29
cmd/go-omni/apigen/testdata/composition/domain.go
vendored
Normal file
29
cmd/go-omni/apigen/testdata/composition/domain.go
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
package domain
|
||||
|
||||
import "example.com/composition/internal/amount"
|
||||
|
||||
type Employee struct {
|
||||
EmployeeName string
|
||||
Salary amount.Money
|
||||
Employer *amount.Money
|
||||
Enabled bool
|
||||
Labels []string
|
||||
}
|
||||
type Result struct {
|
||||
Employee
|
||||
ID int64
|
||||
}
|
||||
|
||||
// Distinct container item types exercise explicitly selected element mappings.
|
||||
type Row struct{ Value string }
|
||||
type RowDTO struct{ Value string }
|
||||
type Batch struct {
|
||||
Rows []Row
|
||||
Fixed [2]Row
|
||||
Optional *string
|
||||
}
|
||||
type BatchDTO struct {
|
||||
Rows []RowDTO
|
||||
Fixed [2]RowDTO
|
||||
Optional *string
|
||||
}
|
||||
63
cmd/go-omni/apigen/testdata/composition/service.api
vendored
Normal file
63
cmd/go-omni/apigen/testdata/composition/service.api
vendored
Normal file
@@ -0,0 +1,63 @@
|
||||
scalar Money (
|
||||
goImport: "example.com/composition/internal/amount"
|
||||
goType: Money
|
||||
jsonInput: string
|
||||
jsonOutput: string
|
||||
)
|
||||
|
||||
type SalaryFields {
|
||||
// Employee display name.
|
||||
Name *string `json:"name,optional" binding:"max=80"`
|
||||
Salary *Money `json:"salary,optional"`
|
||||
Employer *Money `json:"employer,optional"`
|
||||
Enabled *bool `json:"enabled,optional"`
|
||||
Labels []string `json:"labels,optional"`
|
||||
}
|
||||
|
||||
type EnterpriseRequest {
|
||||
SalaryFields
|
||||
}
|
||||
type TenantRequest {
|
||||
SalaryFields
|
||||
CompanyID string `path:"companyId"`
|
||||
}
|
||||
type AdminRequest {
|
||||
SalaryFields
|
||||
CompanyID string `path:"companyId"`
|
||||
}
|
||||
type EmployeeResponse {
|
||||
ID string `json:"id"`
|
||||
EmployeeName string `json:"employeeName"`
|
||||
Salary Money `json:"salary"`
|
||||
Employer *Money `json:"employer"`
|
||||
Enabled bool `json:"enabled"`
|
||||
Labels []string `json:"labels"`
|
||||
}
|
||||
|
||||
@server (
|
||||
group: enterprise
|
||||
middleware: Access
|
||||
response: raw
|
||||
)
|
||||
service composition {
|
||||
@handler save
|
||||
post /enterprise (EnterpriseRequest) returns (EmployeeResponse)
|
||||
}
|
||||
@server (
|
||||
group: tenant
|
||||
middleware: Access
|
||||
response: raw
|
||||
)
|
||||
service composition {
|
||||
@handler save
|
||||
post /tenant/:companyId (TenantRequest) returns (EmployeeResponse)
|
||||
}
|
||||
@server (
|
||||
group: admin
|
||||
middleware: Access
|
||||
response: raw
|
||||
)
|
||||
service composition {
|
||||
@handler save
|
||||
post /admin/:companyId (AdminRequest) returns (EmployeeResponse)
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package consumer_test
|
||||
package main_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
|
||||
"example.com/errorCatalog/internal/apierrors"
|
||||
"example.com/errorCatalog/internal/handler"
|
||||
"example.com/errorCatalog/internal/servicecontext"
|
||||
"example.com/errorCatalog/internal/svc"
|
||||
omni "git.yuntuo.hk/k2safe/go-omni"
|
||||
omnierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
||||
"git.yuntuo.hk/k2safe/go-omni/httpx"
|
||||
@@ -35,7 +35,7 @@ func TestGeneratedCatalog(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer logger.Close()
|
||||
deps, err := servicecontext.New(context.Background(), logger)
|
||||
deps, err := svc.New(context.Background(), logger)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
13
cmd/go-omni/apigen/testdata/errorInheritance/bounded.go
vendored
Normal file
13
cmd/go-omni/apigen/testdata/errorInheritance/bounded.go
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"git.yuntuo.hk/k2safe/go-omni/httpx"
|
||||
"io"
|
||||
)
|
||||
|
||||
func (h *Handler) Bounded(c *httpx.Context) error {
|
||||
if _, err := io.Copy(io.Discard, c.Request().Body); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.Text(200, "ACK")
|
||||
}
|
||||
129
cmd/go-omni/apigen/testdata/errorInheritance/consumer_test.go
vendored
Normal file
129
cmd/go-omni/apigen/testdata/errorInheritance/consumer_test.go
vendored
Normal file
@@ -0,0 +1,129 @@
|
||||
package main_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"example.com/errorCatalog/internal/handler"
|
||||
"example.com/errorCatalog/internal/svc"
|
||||
omni "git.yuntuo.hk/k2safe/go-omni"
|
||||
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
||||
"git.yuntuo.hk/k2safe/go-omni/httpx"
|
||||
"git.yuntuo.hk/k2safe/go-omni/logx"
|
||||
"git.yuntuo.hk/k2safe/go-omni/response"
|
||||
)
|
||||
|
||||
func TestGoOwnedCatalogOnGeneratedRoutes(t *testing.T) {
|
||||
catalog := apierrors.Catalog{DefaultStatus: 400, Definitions: map[string]apierrors.Definition{
|
||||
"USERNAME_INVALID": {Code: 400002, Message: "用户名格式不正确"},
|
||||
"TEXT_TOO_LONG": {Status: 200, Code: 400003, Message: "{label}不能超过{max}个字符", Params: []apierrors.Parameter{{Name: "label", Type: "string"}, {Name: "max", Type: "int"}}},
|
||||
"INTERNAL_ERROR": {Status: 500, Code: 9999, Message: "系统错误"},
|
||||
"BODY_TOO_LARGE": {Status: 413, Code: 413001, Message: "请求体超限"},
|
||||
}}
|
||||
lc := logx.Default()
|
||||
lc.Writer = io.Discard
|
||||
logger, err := logx.New(lc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer logger.Close()
|
||||
deps, err := svc.New(context.Background(), logger)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer deps.Close()
|
||||
format := response.Config{CodeType: "int32", SuccessCode: 0, ErrorCode: 9999, MessageField: "msg", DataMode: "always", RequestIDField: "-"}
|
||||
app := omni.New(omni.WithErrors(catalog), omni.WithResponse(format))
|
||||
defer app.Close()
|
||||
resolve := func(c *httpx.Context) error {
|
||||
switch c.Request().URL.Query().Get("kind") {
|
||||
case "long":
|
||||
return apierrors.ByCode("TEXT_TOO_LONG").WithParams(map[string]any{"label": "企业名称", "max": 100})
|
||||
case "unknown":
|
||||
return apierrors.ByCode("NOT_REGISTERED")
|
||||
default:
|
||||
return apierrors.ByCode("USERNAME_INVALID")
|
||||
}
|
||||
}
|
||||
app.Use(func(next httpx.Handler) httpx.Handler {
|
||||
return func(c *httpx.Context) error {
|
||||
if c.Header("X-Early") == "true" {
|
||||
return resolve(c)
|
||||
}
|
||||
return next(c)
|
||||
}
|
||||
})
|
||||
app.GET("/ordinary", resolve)
|
||||
if err := handler.RegisterRoutes(app, deps); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Explicit route policy stays independent of App errors and response fields.
|
||||
local := apierrors.Catalog{Definitions: map[string]apierrors.Definition{"USERNAME_INVALID": {Status: 409, Code: 409001, Message: "路由提示"}}}
|
||||
localFormat := format
|
||||
localFormat.MessageField = "detail"
|
||||
app.HandleWithResponse("GET", "/independent", response.MustWithErrors(local, response.MustNew(localFormat)), resolve)
|
||||
for _, early := range []string{"false", "true"} {
|
||||
for _, test := range []struct {
|
||||
kind string
|
||||
status, code int
|
||||
message string
|
||||
}{
|
||||
{"invalid", 400, 400002, "用户名格式不正确"}, {"long", 200, 400003, "企业名称不能超过100个字符"}, {"unknown", 500, 9999, "系统错误"},
|
||||
} {
|
||||
var ordinary string
|
||||
for _, path := range []string{"/ordinary", "/plain", "/raw", "/cleared"} {
|
||||
r := httptest.NewRequest("GET", path+"?kind="+test.kind, nil)
|
||||
r.Header.Set("X-Early", early)
|
||||
w := httptest.NewRecorder()
|
||||
app.ServeHTTP(w, r)
|
||||
var body map[string]any
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
|
||||
t.Fatal(err, w.Body.String())
|
||||
}
|
||||
if w.Code != test.status || body["code"] != float64(test.code) || body["msg"] != test.message {
|
||||
t.Fatal(path, early, w.Code, body)
|
||||
}
|
||||
if value, ok := body["data"]; !ok || value != nil {
|
||||
t.Fatal("data:null missing", body)
|
||||
}
|
||||
if path == "/ordinary" {
|
||||
ordinary = w.Body.String()
|
||||
} else if w.Body.String() != ordinary {
|
||||
t.Fatal("generated response differs", ordinary, w.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
r := httptest.NewRequest("GET", "/independent", nil)
|
||||
r.Header.Set("X-Early", early)
|
||||
w := httptest.NewRecorder()
|
||||
app.ServeHTTP(w, r)
|
||||
if w.Code != 409 || !strings.Contains(w.Body.String(), `"detail":"路由提示"`) || !strings.Contains(w.Body.String(), `"code":409001`) {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
for _, path := range []string{"/plain", "/raw"} {
|
||||
w := httptest.NewRecorder()
|
||||
app.ServeHTTP(w, httptest.NewRequest("GET", path+"?kind=success", nil))
|
||||
if w.Code != 200 || !strings.Contains(w.Body.String(), `"ok":true`) {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
if path == "/raw" && strings.Contains(w.Body.String(), "code") {
|
||||
t.Fatal("raw success wrapped")
|
||||
}
|
||||
}
|
||||
for _, size := range []int{8, 65} {
|
||||
w := httptest.NewRecorder()
|
||||
app.ServeHTTP(w, httptest.NewRequest("POST", "/bounded", strings.NewReader(strings.Repeat("x", size))))
|
||||
if size == 8 {
|
||||
if w.Code != 200 || w.Body.String() != "ACK" {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
} else if w.Code != 413 || !strings.Contains(w.Body.String(), "请求体超限") {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
1
cmd/go-omni/apigen/testdata/errorInheritance/emptyService.go
vendored
Normal file
1
cmd/go-omni/apigen/testdata/errorInheritance/emptyService.go
vendored
Normal file
@@ -0,0 +1 @@
|
||||
package account
|
||||
29
cmd/go-omni/apigen/testdata/errorInheritance/service.go
vendored
Normal file
29
cmd/go-omni/apigen/testdata/errorInheritance/service.go
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
"example.com/errorCatalog/internal/types"
|
||||
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
||||
)
|
||||
|
||||
func check(kind string) (*types.CheckResponse, error) {
|
||||
switch kind {
|
||||
case "success":
|
||||
return &types.CheckResponse{OK: true}, nil
|
||||
case "long":
|
||||
return nil, apierrors.ByCode("TEXT_TOO_LONG").WithParams(map[string]any{"label": "企业名称", "max": 100})
|
||||
case "unknown":
|
||||
return nil, apierrors.ByCode("NOT_REGISTERED")
|
||||
default:
|
||||
return nil, apierrors.ByCode("USERNAME_INVALID")
|
||||
}
|
||||
}
|
||||
func (s *Logic) Plain(ctx context.Context, req *types.CheckRequest) (*types.CheckResponse, error) {
|
||||
return check(req.Kind)
|
||||
}
|
||||
func (s *Logic) Raw(ctx context.Context, req *types.CheckRequest) (*types.CheckResponse, error) {
|
||||
return check(req.Kind)
|
||||
}
|
||||
func (s *Logic) Cleared(ctx context.Context, req *types.CheckRequest) (*types.CheckResponse, error) {
|
||||
return check(req.Kind)
|
||||
}
|
||||
149
cmd/go-omni/apigen/testdata/errorParams/consumer_test.go
vendored
Normal file
149
cmd/go-omni/apigen/testdata/errorParams/consumer_test.go
vendored
Normal file
@@ -0,0 +1,149 @@
|
||||
package main_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"example.com/errorCatalog/internal/apierrors"
|
||||
"example.com/errorCatalog/internal/handler"
|
||||
"example.com/errorCatalog/internal/svc"
|
||||
omni "git.yuntuo.hk/k2safe/go-omni"
|
||||
omnierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
||||
"git.yuntuo.hk/k2safe/go-omni/httpx"
|
||||
"git.yuntuo.hk/k2safe/go-omni/logx"
|
||||
)
|
||||
|
||||
func TestGeneratedTemplateErrors(t *testing.T) {
|
||||
var document map[string]any
|
||||
if err := json.Unmarshal([]byte(handler.OpenAPIDocument), &document); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
operation := document["paths"].(map[string]any)["/token"].(map[string]any)["post"].(map[string]any)
|
||||
media := operation["responses"].(map[string]any)["200"].(map[string]any)["content"].(map[string]any)["application/json"].(map[string]any)
|
||||
example := media["examples"].(map[string]any)["TextTooLong"].(map[string]any)["value"].(map[string]any)
|
||||
if example["msg"] != "企业名称不能超过100个字符" || example["code"] != float64(400002) {
|
||||
t.Fatal(example)
|
||||
}
|
||||
if len(media["schema"].(map[string]any)["anyOf"].([]any)) != 2 {
|
||||
t.Fatal("missing success/failure schema")
|
||||
}
|
||||
found := false
|
||||
for _, raw := range document["x-omni-errors"].([]any) {
|
||||
entry := raw.(map[string]any)
|
||||
if entry["name"] == "TextTooLong" {
|
||||
found = true
|
||||
params := entry["params"].([]any)
|
||||
if len(params) != 2 || params[0].(map[string]any)["type"] != "string" || params[1].(map[string]any)["type"] != "int" {
|
||||
t.Fatal(params)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("missing template metadata")
|
||||
}
|
||||
// Assigning to a typed function also verifies declaration order and Go types.
|
||||
var typed func(string, int) *omnierrors.APIError = apierrors.TextTooLong
|
||||
private := errors.New("private cause")
|
||||
first := typed("企业名称", 100).WithCause(private)
|
||||
second := typed("备注", 500)
|
||||
if first.Message != "企业名称不能超过100个字符" || second.Message != "备注不能超过500个字符" || second.Cause != nil || !errors.Is(first, private) {
|
||||
t.Fatal(first, second)
|
||||
}
|
||||
if apierrors.UsernameInvalid().Message != "用户名格式不正确" {
|
||||
t.Fatal("fixed compatibility")
|
||||
}
|
||||
cat := apierrors.Catalog()
|
||||
cat.Definitions["TEXT_TOO_LONG"].Params[0].Type = "bool"
|
||||
if typed("企业名称", 100).Message != first.Message {
|
||||
t.Fatal("mutable catalog")
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
for n := 0; n < 40; n++ {
|
||||
wg.Add(1)
|
||||
go func(n int) {
|
||||
defer wg.Done()
|
||||
if n%2 == 0 {
|
||||
if typed("企业名称", 100).Message != first.Message {
|
||||
t.Error("isolation")
|
||||
}
|
||||
} else {
|
||||
if typed("备注", 500).Message != second.Message {
|
||||
t.Error("isolation")
|
||||
}
|
||||
}
|
||||
}(n)
|
||||
}
|
||||
wg.Wait()
|
||||
cfg := logx.Default()
|
||||
cfg.Writer = io.Discard
|
||||
logger, err := logx.New(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer logger.Close()
|
||||
deps, err := svc.New(context.Background(), logger)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer deps.Close()
|
||||
app := omni.New()
|
||||
defer app.Close()
|
||||
app.Use(func(next httpx.Handler) httpx.Handler {
|
||||
return func(c *httpx.Context) error {
|
||||
switch c.Header("X-Fixture") {
|
||||
case "company":
|
||||
return typed("企业名称", 100).WithCause(private)
|
||||
case "remark":
|
||||
return omnierrors.ByCode("TEXT_TOO_LONG").WithParams(map[string]any{"label": "备注", "max": 500})
|
||||
case "missing":
|
||||
return omnierrors.ByCode("TEXT_TOO_LONG").WithParams(map[string]any{"label": "企业名称"})
|
||||
case "type":
|
||||
return omnierrors.ByCode("TEXT_TOO_LONG").WithParams(map[string]any{"label": "企业名称", "max": "100"})
|
||||
case "fixed":
|
||||
return apierrors.UsernameInvalid()
|
||||
case "denied":
|
||||
return apierrors.ResourceDenied("资源")
|
||||
default:
|
||||
return c.OK(map[string]string{"token": "ok"})
|
||||
}
|
||||
}
|
||||
})
|
||||
if err := handler.RegisterRoutes(app, deps); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, path := range []string{"/token", "/raw"} {
|
||||
for _, test := range []struct {
|
||||
kind string
|
||||
status int
|
||||
want string
|
||||
}{
|
||||
{"company", 200, first.Message}, {"remark", 200, second.Message}, {"missing", 500, "internal server error"}, {"type", 500, "internal server error"}, {"fixed", 200, "用户名格式不正确"}, {"denied", 403, "资源不可用"}, {"", 200, `"token":"ok"`},
|
||||
} {
|
||||
method := "POST"
|
||||
if path == "/raw" {
|
||||
method = "GET"
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
r := httptest.NewRequest(method, path, nil)
|
||||
r.Header.Set("X-Fixture", test.kind)
|
||||
app.ServeHTTP(w, r)
|
||||
if w.Code != test.status || !strings.Contains(w.Body.String(), test.want) || strings.Contains(w.Body.String(), "private") || strings.Contains(w.Body.String(), "{label}") {
|
||||
t.Fatal(test.kind, w.Code, w.Body.String())
|
||||
}
|
||||
if test.kind == "company" || test.kind == "remark" {
|
||||
if !strings.Contains(w.Body.String(), `"code":400002`) || !strings.Contains(w.Body.String(), `"data":null`) {
|
||||
t.Fatal(w.Body.String())
|
||||
}
|
||||
}
|
||||
if path == "/raw" && test.kind == "" && strings.Contains(w.Body.String(), "code") {
|
||||
t.Fatal("raw response wrapped")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
49
cmd/go-omni/apigen/testdata/managed/background.go
vendored
Normal file
49
cmd/go-omni/apigen/testdata/managed/background.go
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"git.yuntuo.hk/k2safe/go-omni/task"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
type BackgroundState struct {
|
||||
Stop, Drain, Release, Started chan struct{}
|
||||
Wrote atomic.Bool
|
||||
Future *task.Future
|
||||
}
|
||||
|
||||
var Background atomic.Pointer[BackgroundState]
|
||||
|
||||
func initBackground(c *ServiceContext) error {
|
||||
state := &BackgroundState{Stop: make(chan struct{}), Drain: make(chan struct{}), Release: make(chan struct{}), Started: make(chan struct{})}
|
||||
baseline := Closed.Load()
|
||||
runner, err := task.New(task.Config{Workers: 1, QueueSize: 1, TaskTimeout: time.Minute, OnError: func(string, error) {}})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.OnStop("jobs", func(ctx context.Context) error { close(state.Stop); return runner.StopAccepting(ctx) })
|
||||
c.OnDrain("jobs", func(ctx context.Context) error { close(state.Drain); return runner.Wait(ctx) })
|
||||
state.Future, err = runner.Submit(c.WorkContext(), "finish", func(ctx context.Context) error {
|
||||
close(state.Started)
|
||||
if c.Config.BackgroundMode == "auto" {
|
||||
<-state.Stop
|
||||
} else {
|
||||
<-state.Release
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
}
|
||||
if Closed.Load() != baseline {
|
||||
return errors.New("shared client closed before background write")
|
||||
}
|
||||
state.Wrote.Store(true)
|
||||
return nil
|
||||
})
|
||||
if err == nil {
|
||||
<-state.Started
|
||||
}
|
||||
Background.Store(state)
|
||||
return err
|
||||
}
|
||||
36
cmd/go-omni/apigen/testdata/managed/client.go
vendored
Normal file
36
cmd/go-omni/apigen/testdata/managed/client.go
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
var Opened, Closed, DownloadClosed atomic.Int32
|
||||
var Started = make(chan struct{}, 1)
|
||||
var Release = make(chan struct{})
|
||||
var Current atomic.Pointer[ServiceContext]
|
||||
var FatalDependency = errors.New("test dependency cannot continue")
|
||||
|
||||
// This fake client exercises the same lifetime contract as a database/client.
|
||||
type Client struct{}
|
||||
|
||||
func initClient(ctx context.Context, c *ServiceContext) error {
|
||||
Current.Store(c)
|
||||
Opened.Add(1)
|
||||
c.Client = &Client{}
|
||||
c.OnClose(func() error { Closed.Add(1); c.Logger.Info("test.client.closed"); return nil })
|
||||
if c.Config.BackgroundMode != "" {
|
||||
if err := initBackground(c); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if c.Config.FailInit {
|
||||
return errors.New("client initialization failed")
|
||||
}
|
||||
if c.Config.ShutdownInit {
|
||||
c.RequestShutdown(FatalDependency)
|
||||
return nil
|
||||
}
|
||||
return ctx.Err()
|
||||
}
|
||||
235
cmd/go-omni/apigen/testdata/managed/consumer_test.go
vendored
Normal file
235
cmd/go-omni/apigen/testdata/managed/consumer_test.go
vendored
Normal file
@@ -0,0 +1,235 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"example.com/managed/internal/config"
|
||||
"example.com/managed/internal/handler"
|
||||
"example.com/managed/internal/svc"
|
||||
omni "git.yuntuo.hk/k2safe/go-omni"
|
||||
"git.yuntuo.hk/k2safe/go-omni/logx"
|
||||
)
|
||||
|
||||
func application(t *testing.T) *omni.App {
|
||||
t.Helper()
|
||||
cfg := config.Default()
|
||||
cfg.Server.Log.Writer = io.Discard
|
||||
logger, err := logx.New(cfg.Server.Log)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = logger.Close() })
|
||||
deps, err := svc.New(context.Background(), logger, cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = deps.Close() })
|
||||
app, err := handler.NewApp(deps)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = app.Close() })
|
||||
return app
|
||||
}
|
||||
func response(t *testing.T, app *omni.App, method, path string, body io.Reader, headers map[string]string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
req := httptest.NewRequest(method, path, body)
|
||||
for k, v := range headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
out := httptest.NewRecorder()
|
||||
app.ServeHTTP(out, req)
|
||||
return out
|
||||
}
|
||||
func assertError(t *testing.T, out *httptest.ResponseRecorder, status, code int, message, field string) {
|
||||
t.Helper()
|
||||
var data map[string]any
|
||||
if err := json.Unmarshal(out.Body.Bytes(), &data); err != nil {
|
||||
t.Fatal(out.Body.String(), err)
|
||||
}
|
||||
value, exists := data["data"]
|
||||
if out.Code != status || data["code"] != float64(code) || data[field] != message || !exists || value != nil {
|
||||
t.Fatalf("unexpected error: %d %s", out.Code, out.Body.String())
|
||||
}
|
||||
}
|
||||
func TestGeneratedAppPolicies(t *testing.T) {
|
||||
app := application(t)
|
||||
assertError(t, response(t, app, "GET", "/check?kind=fixed", nil, nil), 400, 400002, "用户名格式不正确", "msg")
|
||||
assertError(t, response(t, app, "GET", "/check?kind=params", nil, nil), 400, 400003, "企业名称不能超过100个字符", "msg")
|
||||
assertError(t, response(t, app, "GET", "/check?kind=unknown", nil, nil), 500, 9999, "系统错误", "msg")
|
||||
assertError(t, response(t, app, "GET", "/other?kind=fixed", nil, nil), 400, 400002, "用户名格式不正确", "detail")
|
||||
for _, path := range []string{"/check", "/missing", "/callback"} {
|
||||
assertError(t, response(t, app, "GET", path, nil, map[string]string{"X-Fail": "1"}), 400, 400002, "用户名格式不正确", "msg")
|
||||
}
|
||||
assertError(t, response(t, app, "GET", "/missing", nil, nil), 404, 404001, "接口不存在", "msg")
|
||||
assertError(t, response(t, app, "POST", "/check", nil, nil), 405, 405001, "请求方法不支持", "msg")
|
||||
out := response(t, app, "GET", "/raw", nil, nil)
|
||||
if strings.TrimSpace(out.Body.String()) != `{"bytes":3}` {
|
||||
t.Fatal(out.Body.String())
|
||||
}
|
||||
}
|
||||
func TestGeneratedTransfers(t *testing.T) {
|
||||
app := application(t)
|
||||
var body bytes.Buffer
|
||||
writer := multipart.NewWriter(&body)
|
||||
file, err := writer.CreateFormFile("file", "x.txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, _ = file.Write([]byte("hello"))
|
||||
_ = writer.Close()
|
||||
out := response(t, app, "POST", "/upload", &body, map[string]string{"Content-Type": writer.FormDataContentType()})
|
||||
if out.Code != 200 || !strings.Contains(out.Body.String(), `"bytes":5`) {
|
||||
t.Fatalf("upload: %d %s", out.Code, out.Body.String())
|
||||
}
|
||||
out = response(t, app, "POST", "/upload", strings.NewReader("bad"), map[string]string{"Content-Type": "application/json"})
|
||||
if out.Code != 415 {
|
||||
t.Fatal(out.Code, out.Body.String())
|
||||
}
|
||||
// Truncated uploads must use the project's public error catalog rather
|
||||
// than succeed or be mistaken for an internal failure.
|
||||
out = response(t, app, "POST", "/upload", strings.NewReader("--b\r\nContent-Disposition: form-data; name=\"file\"\r\n\r\nprivate file"), map[string]string{"Content-Type": "multipart/form-data; boundary=b"})
|
||||
assertError(t, out, 400, 400004, "上传内容不完整", "msg")
|
||||
out = response(t, app, "POST", "/callback", strings.NewReader("{ \"exact\" : 123 }"), map[string]string{"X-Signature": "valid"})
|
||||
if out.Code != 200 || out.Body.String() != "ACK:{ \"exact\" : 123 }" || out.Header().Get("Content-Type") != "text/plain" {
|
||||
t.Fatal(out.Code, out.Body.String())
|
||||
}
|
||||
assertError(t, response(t, app, "POST", "/callback", strings.NewReader("{}"), nil), 400, 400002, "用户名格式不正确", "msg")
|
||||
// Unknown length must be limited by reads too, not just Content-Length.
|
||||
req := httptest.NewRequest("POST", "/callback", strings.NewReader(strings.Repeat("x", 65)))
|
||||
req.ContentLength = -1
|
||||
req.Header.Set("X-Signature", "valid")
|
||||
out = httptest.NewRecorder()
|
||||
app.ServeHTTP(out, req)
|
||||
assertError(t, out, 413, 413001, "请求体太大", "msg")
|
||||
before := svc.DownloadClosed.Load()
|
||||
out = response(t, app, "GET", "/download", nil, nil)
|
||||
if out.Body.String() != "file-content" || out.Header().Get("Content-Type") != "application/octet-stream" || !strings.Contains(out.Header().Get("Content-Disposition"), "attachment") || svc.DownloadClosed.Load() != before+1 {
|
||||
t.Fatal(out.Code, out.Body.String(), out.Header())
|
||||
}
|
||||
}
|
||||
func configFile(t *testing.T, address string, fail bool) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "config.json")
|
||||
data, _ := json.Marshal(map[string]any{"server": map[string]any{"address": address}, "failInit": fail})
|
||||
if err := os.WriteFile(path, data, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
func waitReady(t *testing.T, url string, done <-chan error) {
|
||||
t.Helper()
|
||||
client := http.Client{Timeout: 100 * time.Millisecond}
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
select {
|
||||
case err := <-done:
|
||||
t.Fatalf("startup failed: %v", err)
|
||||
default:
|
||||
}
|
||||
res, err := client.Get(url + "/health/ready")
|
||||
if err == nil {
|
||||
_ = res.Body.Close()
|
||||
if res.StatusCode == 200 {
|
||||
return
|
||||
}
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
t.Fatal("server did not become ready")
|
||||
}
|
||||
func TestGeneratedMainLifecycle(t *testing.T) {
|
||||
t.Run("initializationFailure", func(t *testing.T) {
|
||||
before := svc.Closed.Load()
|
||||
if err := run(context.Background(), configFile(t, "127.0.0.1:0", true)); err == nil {
|
||||
t.Fatal("failure hidden")
|
||||
}
|
||||
if svc.Closed.Load() != before+1 {
|
||||
t.Fatal("initialized client leaked or closed twice")
|
||||
}
|
||||
})
|
||||
t.Run("listenFailure", func(t *testing.T) {
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer listener.Close()
|
||||
before := svc.Closed.Load()
|
||||
if err = run(context.Background(), configFile(t, listener.Addr().String(), false)); err == nil {
|
||||
t.Fatal("occupied listener accepted")
|
||||
}
|
||||
if svc.Closed.Load() != before+1 {
|
||||
t.Fatal("listen failure cleanup")
|
||||
}
|
||||
})
|
||||
t.Run("drainBeforeClose", func(t *testing.T) {
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
address := listener.Addr().String()
|
||||
_ = listener.Close()
|
||||
svc.Closed.Store(0)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
done := make(chan error, 1)
|
||||
path := configFile(t, address, false)
|
||||
go func() { done <- run(ctx, path) }()
|
||||
base := "http://" + address
|
||||
waitReady(t, base, done)
|
||||
requestDone := make(chan error, 1)
|
||||
go func() {
|
||||
res, err := http.Get(base + "/slow")
|
||||
if err == nil {
|
||||
data, _ := io.ReadAll(res.Body)
|
||||
_ = res.Body.Close()
|
||||
if res.StatusCode != 200 || !strings.Contains(string(data), `"bytes":1`) {
|
||||
err = io.ErrUnexpectedEOF
|
||||
}
|
||||
}
|
||||
requestDone <- err
|
||||
}()
|
||||
select {
|
||||
case <-svc.Started:
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("request did not enter logic")
|
||||
}
|
||||
cancel()
|
||||
time.Sleep(30 * time.Millisecond)
|
||||
if svc.Closed.Load() != 0 {
|
||||
t.Fatal("dependency closed before drain")
|
||||
}
|
||||
close(svc.Release)
|
||||
select {
|
||||
case err := <-requestDone:
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("request stuck")
|
||||
}
|
||||
select {
|
||||
case err := <-done:
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("shutdown stuck")
|
||||
}
|
||||
if svc.Closed.Load() != 1 {
|
||||
t.Fatal("client closed more/less than once", svc.Closed.Load())
|
||||
}
|
||||
})
|
||||
}
|
||||
81
cmd/go-omni/apigen/testdata/managed/operations.go
vendored
Normal file
81
cmd/go-omni/apigen/testdata/managed/operations.go
vendored
Normal file
@@ -0,0 +1,81 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"example.com/managed/internal/svc"
|
||||
"example.com/managed/internal/types"
|
||||
omnierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
||||
"git.yuntuo.hk/k2safe/go-omni/httpx"
|
||||
)
|
||||
|
||||
func (l *Logic) Check(ctx context.Context, req *types.CheckReq) (*types.Receipt, error) {
|
||||
switch req.Kind {
|
||||
case "fixed":
|
||||
return nil, omnierrors.ByCode("INVALID")
|
||||
case "params":
|
||||
return nil, omnierrors.ByCode("TOO_LONG").WithParams(map[string]any{"label": "企业名称", "max": 100})
|
||||
case "unknown":
|
||||
return nil, omnierrors.ByCode("UNKNOWN").WithCause(errors.New("private cause"))
|
||||
}
|
||||
return &types.Receipt{Bytes: 42}, nil
|
||||
}
|
||||
func (l *Logic) Other(ctx context.Context, req *types.CheckReq) (*types.Receipt, error) {
|
||||
return l.Check(ctx, req)
|
||||
}
|
||||
func (l *Logic) RawJSON(context.Context) (*types.Receipt, error) {
|
||||
return &types.Receipt{Bytes: 3}, nil
|
||||
}
|
||||
func (l *Logic) Upload(ctx context.Context, body *httpx.Body) (*types.Receipt, error) {
|
||||
var total int64
|
||||
for {
|
||||
part, err := body.Parts.NextPart()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
n, err := io.Copy(io.Discard, part)
|
||||
closeErr := part.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if closeErr != nil {
|
||||
return nil, closeErr
|
||||
}
|
||||
total += n
|
||||
}
|
||||
return &types.Receipt{Bytes: total}, nil
|
||||
}
|
||||
func (l *Logic) Callback(ctx context.Context, body *httpx.Body) (*httpx.Stream, error) {
|
||||
// The route's 64-byte limit bounds this example's memory use. A real signature
|
||||
// implementation can hash body.Raw incrementally and authenticate its headers.
|
||||
data, err := io.ReadAll(body.Raw)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if body.Header.Get("X-Signature") != "valid" {
|
||||
return nil, omnierrors.ByCode("INVALID")
|
||||
}
|
||||
return &httpx.Stream{Content: io.NopCloser(strings.NewReader("ACK:" + string(data)))}, nil
|
||||
}
|
||||
func (l *Logic) Download(context.Context) (*httpx.Stream, error) {
|
||||
return &httpx.Stream{Content: &countedReader{Reader: strings.NewReader("file-content")}, Name: "测试.txt"}, nil
|
||||
}
|
||||
|
||||
type countedReader struct{ io.Reader }
|
||||
|
||||
func (c *countedReader) Close() error { svc.DownloadClosed.Add(1); return nil }
|
||||
func (l *Logic) Slow(context.Context) (*types.Receipt, error) {
|
||||
before := svc.Closed.Load()
|
||||
svc.Started <- struct{}{}
|
||||
<-svc.Release
|
||||
if svc.Closed.Load() != before {
|
||||
return nil, errors.New("dependency closed before drain")
|
||||
}
|
||||
return &types.Receipt{Bytes: 1}, nil
|
||||
}
|
||||
34
cmd/go-omni/apigen/testdata/managed/options.go
vendored
Normal file
34
cmd/go-omni/apigen/testdata/managed/options.go
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
omni "git.yuntuo.hk/k2safe/go-omni"
|
||||
omnierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
||||
"git.yuntuo.hk/k2safe/go-omni/httpx"
|
||||
)
|
||||
|
||||
func (c *ServiceContext) AppOptions() []omni.Option {
|
||||
return []omni.Option{omni.WithErrors(omnierrors.Catalog{
|
||||
DefaultStatus: 400,
|
||||
Definitions: map[string]omnierrors.Definition{
|
||||
"INVALID": {Code: 400002, Message: "用户名格式不正确"},
|
||||
"TOO_LONG": {Code: 400003, Message: "{label}不能超过{max}个字符", Params: []omnierrors.Parameter{{Name: "label", Type: "string"}, {Name: "max", Type: "int"}}},
|
||||
"INTERNAL_ERROR": {Status: 500, Code: 9999, Message: "系统错误"},
|
||||
"NOT_FOUND": {Status: 404, Code: 404001, Message: "接口不存在"},
|
||||
"METHOD_NOT_ALLOWED": {Status: 405, Code: 405001, Message: "请求方法不支持"},
|
||||
"BODY_TOO_LARGE": {Status: 413, Code: 413001, Message: "请求体太大"},
|
||||
"INVALID_MULTIPART": {Status: 400, Code: 400004, Message: "上传内容不完整"},
|
||||
},
|
||||
})}
|
||||
}
|
||||
func (c *ServiceContext) GlobalMiddleware() []httpx.Middleware {
|
||||
return []httpx.Middleware{
|
||||
func(next httpx.Handler) httpx.Handler {
|
||||
return func(c *httpx.Context) error {
|
||||
if c.Header("X-Fail") != "" {
|
||||
return omnierrors.ByCode("INVALID")
|
||||
}
|
||||
return next(c)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
155
cmd/go-omni/apigen/testdata/managed/phases_test.go
vendored
Normal file
155
cmd/go-omni/apigen/testdata/managed/phases_test.go
vendored
Normal file
@@ -0,0 +1,155 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"example.com/managed/internal/svc"
|
||||
"git.yuntuo.hk/k2safe/go-omni/lifecycle"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func phasedConfig(t *testing.T, address, mode string, fail bool) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "config.json")
|
||||
drainTimeout := "1s"
|
||||
if mode == "stuck" {
|
||||
drainTimeout = "50ms"
|
||||
}
|
||||
data, _ := json.Marshal(map[string]any{"backgroundMode": mode, "failInit": fail, "server": map[string]any{"address": address, "lifecycle": map[string]string{"stopTimeout": "1s", "drainTimeout": drainTimeout, "cancelTimeout": "50ms"}}})
|
||||
if err := os.WriteFile(path, data, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
func await(t *testing.T, ch <-chan struct{}) {
|
||||
t.Helper()
|
||||
select {
|
||||
case <-ch:
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("phase did not arrive")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGeneratedBackgroundPhases(t *testing.T) {
|
||||
for _, transport := range []string{"nethttp", "native"} {
|
||||
t.Run(transport, func(t *testing.T) {
|
||||
t.Setenv("GO_OMNI_TRANSPORT", transport)
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
addr := listener.Addr().String()
|
||||
_ = listener.Close()
|
||||
svc.Started = make(chan struct{}, 1)
|
||||
svc.Release = make(chan struct{})
|
||||
before := svc.Closed.Load()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
done := make(chan error, 1)
|
||||
path := phasedConfig(t, addr, "hold", false)
|
||||
go func() { done <- run(ctx, path) }()
|
||||
waitReady(t, "http://"+addr, done)
|
||||
state := svc.Background.Load()
|
||||
defer func() {
|
||||
select {
|
||||
case <-state.Release:
|
||||
default:
|
||||
close(state.Release)
|
||||
}
|
||||
}()
|
||||
requestDone := make(chan error, 1)
|
||||
go func() {
|
||||
client := http.Client{Timeout: 3 * time.Second}
|
||||
resp, e := client.Get("http://" + addr + "/slow")
|
||||
if e == nil {
|
||||
_, _ = io.Copy(io.Discard, resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
if resp.StatusCode != 200 {
|
||||
e = errors.New("HTTP drain failed")
|
||||
}
|
||||
}
|
||||
requestDone <- e
|
||||
}()
|
||||
await(t, svc.Started)
|
||||
deps := svc.Current.Load()
|
||||
deps.RequestShutdown(svc.FatalDependency)
|
||||
if deps.WorkContext().Err() != nil {
|
||||
t.Fatal("accepted work canceled at signal")
|
||||
}
|
||||
select {
|
||||
case <-state.Stop:
|
||||
t.Fatal("stopped background before HTTP finished")
|
||||
default:
|
||||
}
|
||||
close(svc.Release)
|
||||
if err := <-requestDone; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
await(t, state.Drain)
|
||||
if svc.Closed.Load() != before {
|
||||
t.Fatal("client closed during background drain")
|
||||
}
|
||||
close(state.Release)
|
||||
if err := <-done; !errors.Is(err, svc.FatalDependency) || errors.Is(err, lifecycle.ErrResourcesRetained) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !state.Wrote.Load() || svc.Closed.Load() != before+1 {
|
||||
t.Fatal("missing final write or cleanup")
|
||||
}
|
||||
if err := state.Future.Wait(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGeneratedBackgroundFailureCleanup(t *testing.T) {
|
||||
for _, failInit := range []bool{false, true} {
|
||||
t.Run(map[bool]string{true: "initialization", false: "listen"}[failInit], func(t *testing.T) {
|
||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer l.Close()
|
||||
before := svc.Closed.Load()
|
||||
err = run(context.Background(), phasedConfig(t, l.Addr().String(), "auto", failInit))
|
||||
if err == nil || errors.Is(err, lifecycle.ErrResourcesRetained) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
state := svc.Background.Load()
|
||||
if !state.Wrote.Load() || svc.Closed.Load() != before+1 {
|
||||
t.Fatal("failure cleanup did not join background first")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGeneratedBackgroundTimeoutProcess(t *testing.T) {
|
||||
if path := os.Getenv("OMNI_PHASE_TIMEOUT_CONFIG"); path != "" {
|
||||
os.Args = []string{os.Args[0], "-config", path}
|
||||
main()
|
||||
return
|
||||
}
|
||||
path := phasedConfig(t, "127.0.0.1:0", "stuck", true)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestGeneratedBackgroundTimeoutProcess$")
|
||||
cmd.Env = append(os.Environ(), "OMNI_PHASE_TIMEOUT_CONFIG="+path)
|
||||
output, err := cmd.CombinedOutput()
|
||||
var exit *exec.ExitError
|
||||
if !errors.As(err, &exit) || exit.ExitCode() != 1 || ctx.Err() != nil {
|
||||
t.Fatal(err, string(output))
|
||||
}
|
||||
if strings.Contains(string(output), "test.client.closed") || !strings.Contains(string(output), "shared resources retained") || !strings.Contains(string(output), "client initialization failed") {
|
||||
t.Fatal(string(output))
|
||||
}
|
||||
}
|
||||
231
cmd/go-omni/apigen/testdata/managed/shutdown_test.go
vendored
Normal file
231
cmd/go-omni/apigen/testdata/managed/shutdown_test.go
vendored
Normal file
@@ -0,0 +1,231 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"example.com/managed/internal/config"
|
||||
"example.com/managed/internal/handler"
|
||||
"example.com/managed/internal/svc"
|
||||
"git.yuntuo.hk/k2safe/go-omni/health"
|
||||
"git.yuntuo.hk/k2safe/go-omni/logx"
|
||||
)
|
||||
|
||||
func TestGeneratedEndpointSettings(t *testing.T) {
|
||||
cfg := config.Default()
|
||||
cfg.Server.Log.Writer = io.Discard
|
||||
cfg.Server.Endpoints.Liveness.Path = "/livez"
|
||||
cfg.Server.Endpoints.Readiness.Path = "/readyz"
|
||||
cfg.Server.Endpoints.OpenAPI.Enabled = false
|
||||
logger, err := logx.New(cfg.Server.Log)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer logger.Close()
|
||||
deps, err := svc.New(context.Background(), logger, cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer deps.Close()
|
||||
app, err := handler.NewApp(deps)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer app.Close()
|
||||
for _, path := range []string{"/health/live", "/health/ready", "/openapi.json"} {
|
||||
if out := response(t, app, "GET", path, nil, nil); out.Code != 404 {
|
||||
t.Fatal(path, out.Code)
|
||||
}
|
||||
}
|
||||
for _, path := range []string{"/readyz", "/livez"} {
|
||||
out := response(t, app, "GET", path, nil, nil)
|
||||
var data map[string]any
|
||||
_ = json.Unmarshal(out.Body.Bytes(), &data)
|
||||
if out.Code != 200 || data["code"] != float64(0) || data["msg"] != "success" || data["message"] != nil || data["requestId"] != nil {
|
||||
t.Fatal(path, out.Code, out.Body.String())
|
||||
}
|
||||
}
|
||||
deps.RequestShutdown(errors.New("private shutdown cause"))
|
||||
out := response(t, app, "GET", "/readyz", nil, nil)
|
||||
if out.Code != 503 || strings.Contains(out.Body.String(), "private") {
|
||||
t.Fatal(out.Code, out.Body.String())
|
||||
}
|
||||
if out = response(t, app, "GET", "/livez", nil, nil); out.Code != 200 {
|
||||
t.Fatal("liveness should still work")
|
||||
}
|
||||
// NewApp uses the project's latest configuration, including endpoint switches.
|
||||
deps.Config.Server.Endpoints.Readiness.ResponseMode = "raw"
|
||||
deps.Config.Server.Endpoints.Liveness.Enabled = false
|
||||
deps.Config.Server.Endpoints.OpenAPI.Enabled = true
|
||||
deps.Config.Server.Endpoints.OpenAPI.Path = "/spec.json"
|
||||
app2, err := handler.NewApp(deps)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer app2.Close()
|
||||
out = response(t, app2, "GET", "/readyz", nil, nil)
|
||||
if out.Code != 503 || strings.Contains(out.Body.String(), `"code"`) || !strings.Contains(out.Body.String(), `"status":"not_ready"`) {
|
||||
t.Fatal(out.Body.String())
|
||||
}
|
||||
out = response(t, app2, "GET", "/spec.json", nil, nil)
|
||||
if out.Code != 200 || !strings.Contains(out.Body.String(), `"openapi":`) || strings.Contains(out.Body.String(), `"msg":"操作成功"`) {
|
||||
t.Fatal("OpenAPI was wrapped")
|
||||
}
|
||||
deps.Config.Server.Endpoints.Readiness.Path = "/check"
|
||||
if _, err = handler.NewApp(deps); err == nil || !strings.Contains(err.Error(), "overlaps business") {
|
||||
t.Fatal("collision accepted", err)
|
||||
}
|
||||
deps.Config.Server.Endpoints.Readiness.Enabled = false
|
||||
if a, err := handler.NewApp(deps); err != nil {
|
||||
t.Fatal("disabled endpoint reserves path", err)
|
||||
} else {
|
||||
_ = a.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func TestGeneratedFailedProbe(t *testing.T) {
|
||||
cfg := config.Default()
|
||||
cfg.Server.Log.Writer = io.Discard
|
||||
logger, _ := logx.New(cfg.Server.Log)
|
||||
defer logger.Close()
|
||||
deps, err := svc.New(context.Background(), logger, cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer deps.Close()
|
||||
if err = deps.Health.Add(health.Probe{Name: "database", Check: func(context.Context) error { return errors.New("private connection details") }}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
app, err := handler.NewApp(deps)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer app.Close()
|
||||
out := response(t, app, "GET", "/health/ready", nil, nil)
|
||||
if out.Code != 503 || strings.Contains(out.Body.String(), "private") || !strings.Contains(out.Body.String(), `"msg":`) {
|
||||
t.Fatal(out.Code, out.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGeneratedDependencyShutdown(t *testing.T) {
|
||||
for _, transport := range []string{"nethttp", "native"} {
|
||||
t.Run(transport, func(t *testing.T) {
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
address := listener.Addr().String()
|
||||
_ = listener.Close()
|
||||
t.Setenv("GO_OMNI_TRANSPORT", transport)
|
||||
svc.Started = make(chan struct{}, 1)
|
||||
svc.Release = make(chan struct{})
|
||||
before := svc.Closed.Load()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
done := make(chan error, 1)
|
||||
path := configFile(t, address, false)
|
||||
go func() { done <- run(ctx, path) }()
|
||||
base := "http://" + address
|
||||
waitReady(t, base, done)
|
||||
deps := svc.Current.Load()
|
||||
requestDone := make(chan error, 1)
|
||||
go func() {
|
||||
client := http.Client{Timeout: 3 * time.Second}
|
||||
res, err := client.Get(base + "/slow")
|
||||
if err == nil {
|
||||
_, _ = io.Copy(io.Discard, res.Body)
|
||||
_ = res.Body.Close()
|
||||
if res.StatusCode != 200 {
|
||||
err = io.ErrUnexpectedEOF
|
||||
}
|
||||
}
|
||||
requestDone <- err
|
||||
}()
|
||||
select {
|
||||
case <-svc.Started:
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("request did not start")
|
||||
}
|
||||
deps.RequestShutdown(svc.FatalDependency)
|
||||
// Inspect the same generated readiness handler during draining, even if
|
||||
// the network listener has already stopped accepting new connections.
|
||||
probe, _ := handler.NewApp(deps)
|
||||
out := httptest.NewRecorder()
|
||||
probe.ServeHTTP(out, httptest.NewRequest("GET", "/health/ready", nil))
|
||||
_ = probe.Close()
|
||||
if out.Code != 503 {
|
||||
t.Fatal("draining remained ready", out.Body.String())
|
||||
}
|
||||
if svc.Closed.Load() != before {
|
||||
t.Fatal("dependency closed before drain")
|
||||
}
|
||||
close(svc.Release)
|
||||
select {
|
||||
case err = <-requestDone:
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("request stuck")
|
||||
}
|
||||
select {
|
||||
case err = <-done:
|
||||
if !errors.Is(err, svc.FatalDependency) {
|
||||
t.Fatal("fatal cause lost", err)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("shutdown stuck")
|
||||
}
|
||||
if svc.Closed.Load() != before+1 {
|
||||
t.Fatal("cleanup did not run once")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGeneratedFatalProcessExit(t *testing.T) {
|
||||
if path := os.Getenv("OMNI_TEST_FATAL_CONFIG"); path != "" {
|
||||
os.Args = []string{os.Args[0], "-config", path}
|
||||
main()
|
||||
return
|
||||
}
|
||||
path := filepath.Join(t.TempDir(), "config.json")
|
||||
if err := os.WriteFile(path, []byte(`{"shutdownInit":true,"server":{"address":"127.0.0.1:0"}}`), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestGeneratedFatalProcessExit$")
|
||||
cmd.Env = append(os.Environ(), "OMNI_TEST_FATAL_CONFIG="+path)
|
||||
output, err := cmd.CombinedOutput()
|
||||
var exit *exec.ExitError
|
||||
if !errors.As(err, &exit) || exit.ExitCode() != 1 {
|
||||
t.Fatal("fatal shutdown must exit 1", err, string(output))
|
||||
}
|
||||
if !strings.Contains(string(output), svc.FatalDependency.Error()) || strings.Count(string(output), "test.client.closed") != 1 {
|
||||
t.Fatal("missing failure or cleanup", string(output))
|
||||
}
|
||||
}
|
||||
|
||||
func TestGeneratedEndpointConflictCleansUp(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "config.json")
|
||||
if err := os.WriteFile(path, []byte(`{"server":{"address":"127.0.0.1:0","endpoints":{"readiness":{"path":"/check"}}}}`), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before := svc.Closed.Load()
|
||||
err := run(context.Background(), path)
|
||||
if err == nil || !strings.Contains(err.Error(), "overlaps business") || svc.Closed.Load() != before+1 {
|
||||
t.Fatal("collision leaked resources or started serving", err)
|
||||
}
|
||||
}
|
||||
170
cmd/go-omni/apigen/testdata/nullable/consumer_test.go
vendored
Normal file
170
cmd/go-omni/apigen/testdata/nullable/consumer_test.go
vendored
Normal file
@@ -0,0 +1,170 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"example.com/nullable/internal/convert"
|
||||
"example.com/nullable/internal/domain"
|
||||
"example.com/nullable/internal/handler"
|
||||
"example.com/nullable/internal/mapping"
|
||||
"example.com/nullable/internal/svc"
|
||||
"example.com/nullable/internal/types"
|
||||
"git.yuntuo.hk/k2safe/go-omni/logx"
|
||||
)
|
||||
|
||||
func TestNullableMappings(t *testing.T) {
|
||||
for _, src := range []types.Parent{{}, {Children: []*types.Child{}}, {Child: &types.Child{}, Children: []*types.Child{nil, {Name: "two"}}}} {
|
||||
out, err := mapping.ToParent(src)
|
||||
if err != nil || (src.Child == nil) != (out.Child == nil) || (src.Children == nil) != (out.Children == nil) || len(src.Children) != len(out.Children) {
|
||||
t.Fatal(src, out, err)
|
||||
}
|
||||
if src.Child != nil && out.Child.Name != src.Child.Name {
|
||||
t.Fatal("zero child changed")
|
||||
}
|
||||
if len(out.Children) > 0 && (out.Children[0] != nil || out.Children[1].Name != "two") {
|
||||
t.Fatal(out)
|
||||
}
|
||||
}
|
||||
src := types.Parent{Child: &types.Child{Name: "one"}, Children: []*types.Child{{Name: "two"}}}
|
||||
out, err := mapping.ToParentPtr(src)
|
||||
if err != nil || out == nil || out.Child.Name != "one" {
|
||||
t.Fatal(out, err)
|
||||
}
|
||||
out.Child.Name = "changed"
|
||||
out.Children[0].Name = "changed"
|
||||
if src.Child.Name != "one" || src.Children[0].Name != "two" {
|
||||
t.Fatal("nested mapped values alias input")
|
||||
}
|
||||
plain := domain.Parent{Child: &domain.Child{Name: "shared"}}
|
||||
copy, err := mapping.ShallowCopy(plain)
|
||||
if err != nil || copy.Child != plain.Child {
|
||||
t.Fatal("ordinary assignment semantics changed", err)
|
||||
}
|
||||
zero, err := mapping.RequiredZero(types.Parent{})
|
||||
if err != nil || zero.Child.Name != "" {
|
||||
t.Fatal(zero, err)
|
||||
}
|
||||
for _, fn := range []func(types.Parent) (domain.Required, error){mapping.RequiredZero, mapping.RequiredError} {
|
||||
out, err := fn(types.Parent{Child: &types.Child{Name: "required"}})
|
||||
if err != nil || out.Child.Name != "required" {
|
||||
t.Fatal(out, err)
|
||||
}
|
||||
}
|
||||
_, err = mapping.RequiredError(types.Parent{})
|
||||
if err == nil || !strings.Contains(err.Error(), "RequiredError.Child") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = mapping.RequirePointer(types.Parent{})
|
||||
if err == nil || !strings.Contains(err.Error(), "RequirePointer.Child") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
lift, err := mapping.Lift(domain.ValueHolder{Child: domain.Child{Name: "lift"}})
|
||||
if err != nil || lift.Child == nil || lift.Child.Name != "lift" {
|
||||
t.Fatal(lift, err)
|
||||
}
|
||||
batch, err := mapping.Batch(domain.InputBatch{Fixed: [2]*domain.Child{nil, {Name: "array"}}})
|
||||
if err != nil || batch.Fixed[0] != nil || batch.Fixed[1].Name != "array" {
|
||||
t.Fatal(batch, err)
|
||||
}
|
||||
_, err = mapping.Batch(domain.InputBatch{Fixed: [2]*domain.Child{nil, {Name: "bad"}}})
|
||||
if !errors.Is(err, convert.ErrValue) || !strings.Contains(err.Error(), "Batch.Fixed[1]") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, input := range []types.Parent{{Child: &types.Child{Name: "bad"}}, {Children: []*types.Child{nil, {Name: "bad"}}}} {
|
||||
failed, err := mapping.ToParentPtr(input)
|
||||
if failed != nil || !errors.Is(err, convert.ErrValue) || !strings.Contains(err.Error(), "ToChild.Name") {
|
||||
t.Fatal(failed, err)
|
||||
}
|
||||
if len(input.Children) > 0 && !strings.Contains(err.Error(), "ToParent.Children[1]") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
value, err := mapping.ToParent(input)
|
||||
if value.Child != nil || value.Children != nil || !errors.Is(err, convert.ErrValue) {
|
||||
t.Fatal("partial result escaped", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExplicitNilAndConcurrentCalls(t *testing.T) {
|
||||
zero, err := mapping.ScalarZero(domain.ScalarInput{})
|
||||
if err != nil || zero != (domain.ScalarOutput{}) {
|
||||
t.Fatal(zero, err)
|
||||
}
|
||||
_, err = mapping.ScalarError(domain.ScalarInput{})
|
||||
if err == nil {
|
||||
t.Fatal("nil accepted")
|
||||
}
|
||||
text, count, enabled := "", int64(0), false
|
||||
out, err := mapping.ScalarError(domain.ScalarInput{Text: &text, Count: &count, Enabled: &enabled})
|
||||
if err != nil || out != (domain.ScalarOutput{}) {
|
||||
t.Fatal("non-nil zero rejected", out, err)
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 32; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
out, err := mapping.ToParent(types.Parent{Child: &types.Child{Name: "parallel"}})
|
||||
if err != nil || out.Child == nil || out.Child.Name != "parallel" {
|
||||
t.Error(out, err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func TestHTTPNullableMapping(t *testing.T) {
|
||||
cfg := logx.Default()
|
||||
cfg.Writer = io.Discard
|
||||
logger, err := logx.New(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer logger.Close()
|
||||
deps, err := svc.New(context.Background(), logger)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer deps.Close()
|
||||
app, err := handler.NewApp(deps)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer app.Close()
|
||||
for _, body := range []string{`{}`, `{"child":null}`, `{"child":{"name":""}}`, `{"child":{"name":"one"},"children":[null,{"name":"two"}]}`, `{"children":[]}`} {
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("POST", "/", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
app.Engine().ServeHTTP(w, req)
|
||||
if w.Code != 200 {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
var input, output types.Parent
|
||||
if err := json.Unmarshal([]byte(body), &input); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &output); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if (input.Child == nil) != (output.Child == nil) || (input.Children == nil) != (output.Children == nil) || len(input.Children) != len(output.Children) {
|
||||
t.Fatal(body, w.Body.String())
|
||||
}
|
||||
if input.Child != nil && input.Child.Name != output.Child.Name {
|
||||
t.Fatal(w.Body.String())
|
||||
}
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("POST", "/", strings.NewReader(`{"child":{"name":"bad"}}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
app.Engine().ServeHTTP(w, req)
|
||||
if w.Code != 500 || strings.Contains(w.Body.String(), convert.ErrValue.Error()) || strings.Contains(w.Body.String(), "ToChild") {
|
||||
t.Fatal("private cause leaked", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
15
cmd/go-omni/apigen/testdata/nullable/convert.go
vendored
Normal file
15
cmd/go-omni/apigen/testdata/nullable/convert.go
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
package convert
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var ErrValue = errors.New("private conversion cause")
|
||||
|
||||
func Name(value string) (string, error) {
|
||||
if value == "bad" {
|
||||
return "", fmt.Errorf("conversion refused: %w", ErrValue)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
23
cmd/go-omni/apigen/testdata/nullable/domain.go
vendored
Normal file
23
cmd/go-omni/apigen/testdata/nullable/domain.go
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
package domain
|
||||
|
||||
type Child struct{ Name string }
|
||||
type ChildDTO struct{ Name string }
|
||||
type Parent struct {
|
||||
Child *Child
|
||||
Children []*Child
|
||||
}
|
||||
type Required struct{ Child Child }
|
||||
type ValueHolder struct{ Child Child }
|
||||
type PointerHolder struct{ Child *ChildDTO }
|
||||
type InputBatch struct{ Fixed [2]*Child }
|
||||
type OutputBatch struct{ Fixed [2]*ChildDTO }
|
||||
type ScalarInput struct {
|
||||
Text *string
|
||||
Count *int64
|
||||
Enabled *bool
|
||||
}
|
||||
type ScalarOutput struct {
|
||||
Text string
|
||||
Count int64
|
||||
Enabled bool
|
||||
}
|
||||
15
cmd/go-omni/apigen/testdata/nullable/service.api
vendored
Normal file
15
cmd/go-omni/apigen/testdata/nullable/service.api
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
type Child {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
type Parent {
|
||||
Child *Child `json:"child,optional"`
|
||||
Children []*Child `json:"children,optional"`
|
||||
}
|
||||
@server (
|
||||
group: probe
|
||||
response: raw
|
||||
)
|
||||
service probe {
|
||||
@handler roundTrip
|
||||
post / (Parent) returns (Parent)
|
||||
}
|
||||
302
cmd/go-omni/apigen/upgrade.go
Normal file
302
cmd/go-omni/apigen/upgrade.go
Normal file
@@ -0,0 +1,302 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.yuntuo.hk/k2safe/go-omni/cmd/go-omni/scaffold"
|
||||
"go/ast"
|
||||
"go/format"
|
||||
goparser "go/parser"
|
||||
gotoken "go/token"
|
||||
"golang.org/x/mod/modfile"
|
||||
"golang.org/x/mod/semver"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type UpgradeChange struct {
|
||||
Path, Action string
|
||||
Manual bool
|
||||
before, after []byte
|
||||
mode os.FileMode
|
||||
exists bool
|
||||
}
|
||||
type UpgradePlan struct {
|
||||
Changes []UpgradeChange
|
||||
Module, Entry string
|
||||
}
|
||||
|
||||
// PlanUpgrade compares infrastructure against the exact previous release seeds.
|
||||
// A custom file is never classified as safe to replace. Logic, middleware,
|
||||
// options and repositories outside the changed templates are left untouched.
|
||||
func PlanUpgrade(dir string) (UpgradePlan, error) {
|
||||
var plan UpgradePlan
|
||||
root := canonical(dir)
|
||||
if err := safePath(root, manifestPath); err != nil {
|
||||
return plan, err
|
||||
}
|
||||
raw, err := os.ReadFile(filepath.Join(root, manifestPath))
|
||||
if err != nil {
|
||||
return plan, err
|
||||
}
|
||||
var m manifest
|
||||
if err = json.Unmarshal(raw, &m); err != nil {
|
||||
return plan, err
|
||||
}
|
||||
if m.Version != 2 || m.Profile == nil {
|
||||
return plan, errors.New("upgrade requires a managed v0.13+ project")
|
||||
}
|
||||
if filepath.Ext(m.Entry) != ".api" {
|
||||
return plan, errors.New("upgrade requires a valid API entry")
|
||||
}
|
||||
if err = safePath(root, m.Entry); err != nil {
|
||||
return plan, err
|
||||
}
|
||||
plan.Module, plan.Entry = m.Module, m.Entry
|
||||
if semver.IsValid(m.TemplateVersion) && semver.Compare(m.TemplateVersion, minimumVersion) >= 0 {
|
||||
return plan, nil
|
||||
}
|
||||
p := m.Profile
|
||||
legacy, err := scaffold.LegacyAPIInfrastructure(m.Module, p.Database, p.Storage, p.LogOutput, p.LogFormat)
|
||||
if err != nil {
|
||||
return plan, err
|
||||
}
|
||||
current, err := scaffold.APIInfrastructure(m.Module, p.Database, p.Storage, p.LogOutput, p.LogFormat)
|
||||
if err != nil {
|
||||
return plan, err
|
||||
}
|
||||
names := map[string]bool{}
|
||||
for name := range legacy {
|
||||
names[name] = true
|
||||
}
|
||||
for name := range current {
|
||||
if !strings.HasSuffix(name, "Gen.go") {
|
||||
names[name] = true
|
||||
}
|
||||
}
|
||||
// v0.14 already owns config/svc extensions. Only bump the module here;
|
||||
// Generate updates generator-owned main/runtime files without replaying the
|
||||
// v0.13 seed migration over developer configuration and dependency code.
|
||||
if semver.IsValid(m.TemplateVersion) && semver.Compare(m.TemplateVersion, "v0.14.0-preview") >= 0 {
|
||||
names = map[string]bool{}
|
||||
}
|
||||
sorted := make([]string, 0, len(names))
|
||||
for name := range names {
|
||||
sorted = append(sorted, name)
|
||||
}
|
||||
sort.Strings(sorted)
|
||||
for _, name := range sorted {
|
||||
// options.go is unchanged by this release. Its application customizations remain untouched.
|
||||
previous, next := legacy[name], current[name]
|
||||
if strings.HasSuffix(name, ".go") {
|
||||
if previous != nil {
|
||||
previous, err = format.Source(previous)
|
||||
if err != nil {
|
||||
return plan, err
|
||||
}
|
||||
}
|
||||
if next != nil {
|
||||
next, err = format.Source(next)
|
||||
if err != nil {
|
||||
return plan, err
|
||||
}
|
||||
}
|
||||
}
|
||||
if bytes.Equal(previous, next) {
|
||||
continue
|
||||
}
|
||||
if err = safePath(root, name); err != nil {
|
||||
return plan, err
|
||||
}
|
||||
existing, readErr := os.ReadFile(filepath.Join(root, name))
|
||||
if readErr != nil && !errors.Is(readErr, os.ErrNotExist) {
|
||||
return plan, readErr
|
||||
}
|
||||
exists := readErr == nil
|
||||
if bytes.Equal(existing, next) {
|
||||
continue
|
||||
}
|
||||
mode := os.FileMode(0644)
|
||||
if exists {
|
||||
stat, e := os.Stat(filepath.Join(root, name))
|
||||
if e != nil {
|
||||
return plan, e
|
||||
}
|
||||
mode = stat.Mode().Perm()
|
||||
}
|
||||
action := "update"
|
||||
if next == nil {
|
||||
action = "remove"
|
||||
} else if !exists {
|
||||
action = "create"
|
||||
}
|
||||
plan.Changes = append(plan.Changes, UpgradeChange{Path: name, Action: action, Manual: exists && !bytes.Equal(existing, previous), before: existing, after: next, mode: mode, exists: exists})
|
||||
}
|
||||
if err = safePath(root, "go.mod"); err != nil {
|
||||
return plan, err
|
||||
}
|
||||
raw, err = os.ReadFile(filepath.Join(root, "go.mod"))
|
||||
if err != nil {
|
||||
return plan, err
|
||||
}
|
||||
mf, err := modfile.Parse("go.mod", raw, nil)
|
||||
if err != nil {
|
||||
return plan, err
|
||||
}
|
||||
for _, r := range mf.Require {
|
||||
if r.Mod.Path == frameworkModule && semver.Compare(r.Mod.Version, minimumVersion) < 0 {
|
||||
if err = mf.AddRequire(frameworkModule, minimumVersion); err != nil {
|
||||
return plan, err
|
||||
}
|
||||
next, e := mf.Format()
|
||||
if e != nil {
|
||||
return plan, e
|
||||
}
|
||||
stat, e := os.Stat(filepath.Join(root, "go.mod"))
|
||||
if e != nil {
|
||||
return plan, e
|
||||
}
|
||||
plan.Changes = append(plan.Changes, UpgradeChange{Path: "go.mod", Action: "update", before: raw, after: next, exists: true, mode: stat.Mode().Perm()})
|
||||
break
|
||||
}
|
||||
}
|
||||
return plan, nil
|
||||
}
|
||||
|
||||
// PreviewUpgrade writes only proposed infrastructure files into an empty directory.
|
||||
// It never mutates the service or emits a replacement business tree.
|
||||
func PreviewUpgrade(dir, destination string) (UpgradePlan, error) {
|
||||
plan, err := PlanUpgrade(dir)
|
||||
if err != nil {
|
||||
return plan, err
|
||||
}
|
||||
if entries, e := os.ReadDir(destination); e == nil && len(entries) > 0 {
|
||||
return plan, errors.New("upgrade preview directory must be empty")
|
||||
} else if e != nil && !errors.Is(e, os.ErrNotExist) {
|
||||
return plan, e
|
||||
}
|
||||
for _, c := range plan.Changes {
|
||||
if c.after == nil {
|
||||
continue
|
||||
}
|
||||
if err = safePath(destination, c.Path); err != nil {
|
||||
return plan, err
|
||||
}
|
||||
if err = atomicWrite(destination, c.Path, c.after, 0644); err != nil {
|
||||
return plan, err
|
||||
}
|
||||
}
|
||||
note := "# Infrastructure upgrade preview\n\nOnly untouched v0.13 seed files can be replaced automatically. Custom files require a targeted edit; this preview is never copied over the project automatically.\n\nMove dependency initialization into ServiceContext.Init; embed *lifecycle.Runtime; register cleanup with OnClose. Remove old New/Close/OnClose implementations after preserving their project work in Init or cleanup hooks. Use structured database/mysql.Config and DatabaseOptions. Preserve business configuration fields, AppOptions, middleware and Logic.\n\n"
|
||||
for _, c := range plan.Changes {
|
||||
note += fmt.Sprintf("- %s %s (manual review: %t)\n", c.Action, c.Path, c.Manual)
|
||||
}
|
||||
if err = safePath(destination, "UPGRADE.md"); err != nil {
|
||||
return plan, err
|
||||
}
|
||||
return plan, atomicWrite(destination, "UPGRADE.md", []byte(note), 0644)
|
||||
}
|
||||
|
||||
// ApplyUpgrade applies only recognized seed changes under the same generation
|
||||
// lock. A generation failure restores every changed developer file. Any manual
|
||||
// review item blocks all writes; use PreviewUpgrade and migrate that file first.
|
||||
func ApplyUpgrade(dir string) (plan UpgradePlan, err error) {
|
||||
root := canonical(dir)
|
||||
if err = safePath(root, ".goOmni/api.lock"); err != nil {
|
||||
return plan, err
|
||||
}
|
||||
lock, err := os.OpenFile(filepath.Join(root, ".goOmni/api.lock"), os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600)
|
||||
if err != nil {
|
||||
return plan, err
|
||||
}
|
||||
_ = lock.Close()
|
||||
defer os.Remove(filepath.Join(root, ".goOmni/api.lock"))
|
||||
plan, err = PlanUpgrade(root)
|
||||
if err != nil {
|
||||
return plan, err
|
||||
}
|
||||
for _, c := range plan.Changes {
|
||||
if c.Manual {
|
||||
return plan, fmt.Errorf("upgrade: %s contains project changes; inspect an upgrade preview and migrate it explicitly", c.Path)
|
||||
}
|
||||
}
|
||||
var changed []UpgradeChange
|
||||
defer func() {
|
||||
if err != nil {
|
||||
for i := len(changed) - 1; i >= 0; i-- {
|
||||
c := changed[i]
|
||||
if c.exists {
|
||||
err = errors.Join(err, atomicWrite(root, c.Path, c.before, c.mode))
|
||||
} else {
|
||||
e := os.Remove(filepath.Join(root, c.Path))
|
||||
if !errors.Is(e, os.ErrNotExist) {
|
||||
err = errors.Join(err, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
for _, c := range plan.Changes {
|
||||
if c.after == nil {
|
||||
err = os.Remove(filepath.Join(root, c.Path))
|
||||
} else {
|
||||
err = atomicWrite(root, c.Path, c.after, c.mode)
|
||||
}
|
||||
if err != nil {
|
||||
return plan, err
|
||||
}
|
||||
changed = append(changed, c)
|
||||
}
|
||||
preview, e := Generate(Options{Dir: root, API: filepath.Join(root, plan.Entry), DryRun: true, lockHeld: true})
|
||||
if e != nil {
|
||||
return plan, e
|
||||
}
|
||||
if diagnosticErrors(preview.Diagnostics) > 0 {
|
||||
return plan, errors.New("upgrade: Logic checks failed; resolve diagnostics before applying")
|
||||
}
|
||||
_, err = Generate(Options{Dir: root, API: filepath.Join(root, plan.Entry), lockHeld: true})
|
||||
return plan, err
|
||||
}
|
||||
|
||||
func checkRuntimeExtension(root string) error {
|
||||
directory := filepath.Join(root, "internal/svc")
|
||||
files, err := os.ReadDir(directory)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, entry := range files {
|
||||
if !sourceFile(entry.Name()) || strings.HasSuffix(entry.Name(), "Gen.go") {
|
||||
continue
|
||||
}
|
||||
path := filepath.Join(directory, entry.Name())
|
||||
file, err := goparser.ParseFile(gotoken.NewFileSet(), path, nil, goparser.SkipObjectResolution)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, decl := range file.Decls {
|
||||
fn, ok := decl.(*ast.FuncDecl)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if fn.Recv == nil && fn.Name.Name == "New" {
|
||||
return fmt.Errorf("%s owns New; run go-omni api upgrade --dir %s and move project initialization into ServiceContext.Init", path, root)
|
||||
}
|
||||
if fn.Recv != nil && (fn.Name.Name == "Close" || fn.Name.Name == "OnClose" || fn.Name.Name == "Finish") {
|
||||
receiver := fn.Recv.List[0].Type
|
||||
if p, ok := receiver.(*ast.StarExpr); ok {
|
||||
receiver = p.X
|
||||
}
|
||||
if id, ok := receiver.(*ast.Ident); ok && id.Name == "ServiceContext" {
|
||||
return fmt.Errorf("%s overrides framework resource lifecycle; inspect go-omni api upgrade and register cleanup in Init", path)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
167
cmd/go-omni/apigen/upgrade_test.go
Normal file
167
cmd/go-omni/apigen/upgrade_test.go
Normal file
@@ -0,0 +1,167 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"git.yuntuo.hk/k2safe/go-omni/cmd/go-omni/scaffold"
|
||||
"go/format"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func oldInfrastructureProject(t *testing.T) string {
|
||||
t.Helper()
|
||||
dir := filepath.Join(t.TempDir(), "app")
|
||||
if _, err := New(Options{Dir: dir, Module: "example.com/upgrade", FrameworkDir: frameworkRoot(t)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
previous, err := scaffold.LegacyAPIInfrastructure("example.com/upgrade", "none", "none", "stderr", "json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for name, content := range previous {
|
||||
if strings.HasSuffix(name, ".go") {
|
||||
content, err = format.Source(content)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
writeFile(t, filepath.Join(dir, name), string(content))
|
||||
}
|
||||
var m manifest
|
||||
if err = json.Unmarshal([]byte(readFile(t, filepath.Join(dir, manifestPath))), &m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
delete(m.Generated, "internal/svc/runtimeGen.go")
|
||||
m.TemplateVersion = ""
|
||||
_ = os.Remove(filepath.Join(dir, "internal/svc/runtimeGen.go"))
|
||||
raw, _ := json.Marshal(m)
|
||||
writeFile(t, filepath.Join(dir, manifestPath), string(raw))
|
||||
path := filepath.Join(dir, "go.mod")
|
||||
writeFile(t, path, strings.ReplaceAll(readFile(t, path), minimumVersion, "v0.13.0-preview"))
|
||||
return dir
|
||||
}
|
||||
|
||||
func TestUpgradeRejectsEscapingEntry(t *testing.T) {
|
||||
dir := oldInfrastructureProject(t)
|
||||
var m manifest
|
||||
if err := json.Unmarshal([]byte(readFile(t, filepath.Join(dir, manifestPath))), &m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m.Entry = "../outside.api"
|
||||
raw, _ := json.Marshal(m)
|
||||
writeFile(t, filepath.Join(dir, manifestPath), string(raw))
|
||||
before := snapshot(t, dir)
|
||||
if _, err := ApplyUpgrade(dir); err == nil {
|
||||
t.Fatal("escaping API entry accepted")
|
||||
}
|
||||
if !sameFiles(before, snapshot(t, dir)) {
|
||||
t.Fatal("invalid upgrade wrote project")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpgradeV014PreservesExtensions(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "service")
|
||||
if _, err := New(Options{Dir: dir, Module: "example.com/upgrade", FrameworkDir: frameworkRoot(t)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var m manifest
|
||||
if err := json.Unmarshal([]byte(readFile(t, filepath.Join(dir, manifestPath))), &m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m.TemplateVersion = "v0.14.0-preview"
|
||||
raw, _ := json.Marshal(m)
|
||||
writeFile(t, filepath.Join(dir, manifestPath), string(raw))
|
||||
mod := filepath.Join(dir, "go.mod")
|
||||
writeFile(t, mod, strings.ReplaceAll(readFile(t, mod), minimumVersion, "v0.14.1-preview"))
|
||||
owned := map[string]string{}
|
||||
for _, name := range []string{"internal/svc/serviceContext.go", "internal/config/config.go", "etc/config.yaml"} {
|
||||
path := filepath.Join(dir, name)
|
||||
value := readFile(t, path)
|
||||
if strings.HasSuffix(name, ".go") {
|
||||
value += "\n// project extension kept\n"
|
||||
} else {
|
||||
value += "\n# project configuration kept\n"
|
||||
}
|
||||
writeFile(t, path, value)
|
||||
owned[path] = value
|
||||
}
|
||||
plan, err := PlanUpgrade(dir)
|
||||
if err != nil || len(plan.Changes) != 1 || plan.Changes[0].Path != "go.mod" {
|
||||
t.Fatal(plan, err)
|
||||
}
|
||||
if _, err := ApplyUpgrade(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for path, want := range owned {
|
||||
if readFile(t, path) != want {
|
||||
t.Fatal("developer file modified", path)
|
||||
}
|
||||
}
|
||||
if _, err := Generate(Options{Dir: dir, API: filepath.Join(dir, "api/service.api"), Check: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
func TestSafeInfrastructureUpgrade(t *testing.T) {
|
||||
dir := oldInfrastructureProject(t)
|
||||
before := snapshot(t, dir)
|
||||
if _, err := Generate(Options{Dir: dir, API: filepath.Join(dir, "api/service.api")}); err == nil {
|
||||
t.Fatal("legacy constructor accepted")
|
||||
}
|
||||
if !sameFiles(before, snapshot(t, dir)) {
|
||||
t.Fatal("failed generate wrote files")
|
||||
}
|
||||
plan, err := PlanUpgrade(dir)
|
||||
if err != nil || len(plan.Changes) == 0 {
|
||||
t.Fatal(plan, err)
|
||||
}
|
||||
for _, c := range plan.Changes {
|
||||
if c.Manual {
|
||||
t.Fatal(c.Path)
|
||||
}
|
||||
}
|
||||
preview := filepath.Join(t.TempDir(), "preview")
|
||||
if _, err = PreviewUpgrade(dir, preview); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !sameFiles(before, snapshot(t, dir)) {
|
||||
t.Fatal("preview changed project")
|
||||
}
|
||||
logic := readFile(t, filepath.Join(dir, "internal/logic/greet/greet.go"))
|
||||
if _, err = ApplyUpgrade(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = os.Stat(filepath.Join(dir, "internal/svc/close.go")); !os.IsNotExist(err) {
|
||||
t.Fatal("old lifecycle remains")
|
||||
}
|
||||
if readFile(t, filepath.Join(dir, "internal/logic/greet/greet.go")) != logic {
|
||||
t.Fatal("business changed")
|
||||
}
|
||||
if _, err = Generate(Options{Dir: dir, API: filepath.Join(dir, "api/service.api"), Check: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
plan, err = PlanUpgrade(dir)
|
||||
if err != nil || len(plan.Changes) != 0 {
|
||||
t.Fatal("upgrade not idempotent", plan, err)
|
||||
}
|
||||
}
|
||||
func TestUpgradePreservesCustomFilesAndRollsBack(t *testing.T) {
|
||||
for _, custom := range []bool{true, false} {
|
||||
dir := oldInfrastructureProject(t)
|
||||
if custom {
|
||||
path := filepath.Join(dir, "internal/svc/serviceContext.go")
|
||||
writeFile(t, path, readFile(t, path)+"\n// custom dependency\n")
|
||||
} else {
|
||||
path := filepath.Join(dir, "internal/logic/greet/greet.go")
|
||||
writeFile(t, path, "package greet;func(s *Logic)Greet(){}")
|
||||
}
|
||||
before := snapshot(t, dir)
|
||||
if _, err := ApplyUpgrade(dir); err == nil {
|
||||
t.Fatal("unsafe upgrade accepted")
|
||||
}
|
||||
if !sameFiles(before, snapshot(t, dir)) {
|
||||
t.Fatal("failed upgrade changed project")
|
||||
}
|
||||
}
|
||||
}
|
||||
144
cmd/go-omni/apigen/workspace.go
Normal file
144
cmd/go-omni/apigen/workspace.go
Normal file
@@ -0,0 +1,144 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type WorkspaceService struct {
|
||||
Dir string `json:"dir"`
|
||||
API string `json:"api,omitempty"`
|
||||
Module string `json:"module,omitempty"`
|
||||
}
|
||||
type WorkspaceConfig struct {
|
||||
Services []WorkspaceService `json:"services"`
|
||||
ErrorCatalogs []WorkspaceCatalog `json:"errorCatalogs,omitempty"`
|
||||
}
|
||||
|
||||
// Workspace operates independent Go modules listed in an explicit local file.
|
||||
// All projects are preflighted before generate/format writes. Each generator
|
||||
// transaction is atomic within its project; no cross-module transaction is promised.
|
||||
func Workspace(path, command string, w io.Writer) error {
|
||||
if command != "generate" && command != "check" && command != "format" {
|
||||
return errors.New("workspace command must be generate, check or format")
|
||||
}
|
||||
if w == nil {
|
||||
w = io.Discard
|
||||
}
|
||||
real, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
stat, err := os.Lstat(real)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !stat.Mode().IsRegular() || stat.Size() > 65536 {
|
||||
return errors.New("workspace configuration must be a regular file of at most 64 KiB")
|
||||
}
|
||||
raw, err := os.ReadFile(real)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var cfg WorkspaceConfig
|
||||
if err = apierrors.DecodeCatalogJSON(raw, &cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(cfg.Services) == 0 || len(cfg.Services) > 64 {
|
||||
return errors.New("workspace must list 1..64 services")
|
||||
}
|
||||
root := canonical(filepath.Dir(real))
|
||||
var options []Options
|
||||
seen := map[string]bool{}
|
||||
for _, service := range cfg.Services {
|
||||
if service.Dir == "" || service.Dir == "." {
|
||||
return errors.New("workspace requires distinct relative service directories")
|
||||
}
|
||||
if err = safePath(root, service.Dir+"/.goOmniWorkspaceCheck"); err != nil {
|
||||
return err
|
||||
}
|
||||
dir := filepath.Join(root, filepath.FromSlash(service.Dir))
|
||||
key := strings.ToLower(dir)
|
||||
for prior := range seen {
|
||||
if prior == key || strings.HasPrefix(key, prior+string(filepath.Separator)) || strings.HasPrefix(prior, key+string(filepath.Separator)) {
|
||||
return errors.New("workspace service directories overlap")
|
||||
}
|
||||
}
|
||||
seen[key] = true
|
||||
entry := service.API
|
||||
if entry == "" {
|
||||
entry, err = ProjectEntry(dir)
|
||||
} else {
|
||||
if err = safePath(dir, entry); err != nil {
|
||||
return err
|
||||
}
|
||||
entry = filepath.Join(dir, entry)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
options = append(options, Options{API: entry, Dir: dir, Module: service.Module, DryRun: true})
|
||||
}
|
||||
var catalogs map[string][]byte
|
||||
if command != "format" {
|
||||
catalogs, err = prepareWorkspaceCatalogs(root, cfg.ErrorCatalogs, command == "check")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for i := range options {
|
||||
options[i].documentCatalogs = catalogs
|
||||
}
|
||||
}
|
||||
var failures error
|
||||
for _, o := range options {
|
||||
if command == "format" {
|
||||
spec, e := Parse(o.API)
|
||||
if e == nil {
|
||||
_, e = Compile(spec)
|
||||
}
|
||||
err = e
|
||||
} else {
|
||||
_, err = Generate(o)
|
||||
}
|
||||
if err != nil {
|
||||
failures = errors.Join(failures, fmt.Errorf("%s: %w", o.Dir, err))
|
||||
}
|
||||
}
|
||||
if failures != nil {
|
||||
return failures
|
||||
}
|
||||
if command == "generate" {
|
||||
for path, data := range catalogs {
|
||||
if err = writeWorkspaceCatalog(path, data); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, o := range options {
|
||||
switch command {
|
||||
case "format":
|
||||
_, err = FormatProject(o.API, false)
|
||||
case "generate":
|
||||
o.DryRun = false
|
||||
_, err = Generate(o)
|
||||
case "check":
|
||||
_, fmtErr := FormatProject(o.API, true)
|
||||
o.DryRun = false
|
||||
o.Check = true
|
||||
_, genErr := Generate(o)
|
||||
err = errors.Join(fmtErr, genErr)
|
||||
}
|
||||
if err != nil {
|
||||
failures = errors.Join(failures, fmt.Errorf("%s: %w", o.Dir, err))
|
||||
fmt.Fprintf(w, "FAIL %s\n", o.Dir)
|
||||
} else {
|
||||
fmt.Fprintf(w, "OK %s\n", o.Dir)
|
||||
}
|
||||
}
|
||||
return failures
|
||||
}
|
||||
217
cmd/go-omni/apigen/workspaceCatalog.go
Normal file
217
cmd/go-omni/apigen/workspaceCatalog.go
Normal file
@@ -0,0 +1,217 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
gotoken "go/token"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
||||
"golang.org/x/mod/modfile"
|
||||
"golang.org/x/mod/module"
|
||||
)
|
||||
|
||||
// WorkspaceCatalog is an explicitly trusted Go provider: func() errors.Catalog.
|
||||
// Only workspace generate/check execute providers. Parsing an .api never does.
|
||||
type WorkspaceCatalog struct {
|
||||
Dir string `json:"dir"`
|
||||
Package string `json:"package"`
|
||||
Function string `json:"function"`
|
||||
Output string `json:"output"`
|
||||
}
|
||||
|
||||
type limitedCatalogBuffer struct {
|
||||
bytes.Buffer
|
||||
limit int
|
||||
}
|
||||
|
||||
func (b *limitedCatalogBuffer) Write(p []byte) (int, error) {
|
||||
if len(p) > b.limit-b.Len() {
|
||||
return 0, errors.New("catalog provider output exceeds limit")
|
||||
}
|
||||
return b.Buffer.Write(p)
|
||||
}
|
||||
|
||||
func exportWorkspaceCatalog(root string, cfg WorkspaceCatalog) ([]byte, error) {
|
||||
if cfg.Dir == "" || cfg.Output == "" || !gotoken.IsIdentifier(cfg.Function) || !gotoken.IsExported(cfg.Function) || module.CheckImportPath(cfg.Package) != nil {
|
||||
return nil, errors.New("workspace catalog requires dir, an importable package, an exported function and output")
|
||||
}
|
||||
if err := safePath(root, filepath.ToSlash(filepath.Join(cfg.Dir, ".goOmniExportCheck"))); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := safePath(root, cfg.Output); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if filepath.Ext(cfg.Output) != ".json" {
|
||||
return nil, errors.New("workspace catalog output must be .json")
|
||||
}
|
||||
dir := filepath.Join(root, filepath.FromSlash(cfg.Dir))
|
||||
if stat, err := os.Stat(filepath.Join(dir, "go.mod")); err != nil || !stat.Mode().IsRegular() {
|
||||
return nil, errors.New("workspace catalog dir must contain go.mod")
|
||||
}
|
||||
temp, err := os.MkdirTemp("", "go-omni-catalog-*")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer os.RemoveAll(temp)
|
||||
// Overlay a virtual Go file under the provider module. This allows internal
|
||||
// packages without creating files or changing go.mod/go.sum in that module.
|
||||
virtual := filepath.Join(dir, ".goOmniCatalogExport", "main.go")
|
||||
if _, err := os.Lstat(filepath.Dir(virtual)); !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, errors.New("reserved catalog export path already exists")
|
||||
}
|
||||
source := fmt.Sprintf("package main\nimport (\"os\"; provider %q; omnierrors %q)\nfunc main(){if err:=omnierrors.ExportCatalog(os.Stdout,provider.%s());err!=nil{os.Exit(1)}}\n", cfg.Package, frameworkModule+"/errors", cfg.Function)
|
||||
backing := filepath.Join(temp, "main.go")
|
||||
if err = os.WriteFile(backing, []byte(source), 0600); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
overlay, _ := json.Marshal(map[string]any{"Replace": map[string]string{virtual: backing}})
|
||||
overlayPath := filepath.Join(temp, "overlay.json")
|
||||
if err = os.WriteFile(overlayPath, overlay, 0600); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Keep all module metadata writes in the temporary directory as well.
|
||||
// -mod=readonly alone must not be relied on to protect every go.sum update.
|
||||
moduleData, err := os.ReadFile(filepath.Join(dir, "go.mod"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mf, err := modfile.Parse("go.mod", moduleData, nil)
|
||||
if err != nil {
|
||||
return nil, errors.New("workspace catalog has invalid go.mod")
|
||||
}
|
||||
replacements := append([]*modfile.Replace(nil), mf.Replace...)
|
||||
for _, r := range replacements {
|
||||
if r.New.Version == "" && !filepath.IsAbs(r.New.Path) {
|
||||
if err = mf.AddReplace(r.Old.Path, r.Old.Version, filepath.Join(dir, r.New.Path), ""); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
moduleData, err = mf.Format()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
modPath := filepath.Join(temp, "go.mod")
|
||||
if err = os.WriteFile(modPath, moduleData, 0600); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if sums, e := os.ReadFile(filepath.Join(dir, "go.sum")); e == nil {
|
||||
if err = os.WriteFile(filepath.Join(temp, "go.sum"), sums, 0600); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if !errors.Is(e, os.ErrNotExist) {
|
||||
return nil, e
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
||||
defer cancel()
|
||||
|
||||
executable := filepath.Join(temp, "catalogExport")
|
||||
// Build first, then execute the binary directly: cancellation kills the
|
||||
// provider itself instead of only terminating a go-run wrapper process.
|
||||
build := exec.CommandContext(ctx, "go", "build", "-mod=readonly", "-modfile", modPath, "-overlay", overlayPath, "-o", executable, virtual)
|
||||
build.Dir = dir
|
||||
build.Env = append(os.Environ(), "GOWORK=off")
|
||||
build.WaitDelay = 2 * time.Second
|
||||
build.Stdout = &limitedCatalogBuffer{limit: 64 << 10}
|
||||
build.Stderr = &limitedCatalogBuffer{limit: 64 << 10}
|
||||
if err = build.Run(); err != nil {
|
||||
return nil, fmt.Errorf("workspace catalog %s.%s: build failed (%v); check module dependencies and func() errors.Catalog signature", cfg.Package, cfg.Function, err)
|
||||
}
|
||||
cmd := exec.CommandContext(ctx, executable)
|
||||
cmd.Dir = dir
|
||||
cmd.Env = append(os.Environ(), "GOWORK=off")
|
||||
cmd.WaitDelay = 2 * time.Second
|
||||
stdout := &limitedCatalogBuffer{limit: 1 << 20}
|
||||
stderr := &limitedCatalogBuffer{limit: 64 << 10}
|
||||
cmd.Stdout = stdout
|
||||
cmd.Stderr = stderr
|
||||
if err = cmd.Run(); err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return nil, fmt.Errorf("workspace catalog %s: %w", cfg.Package, ctx.Err())
|
||||
}
|
||||
// Provider output can contain arbitrary application data. Do not echo it.
|
||||
return nil, fmt.Errorf("workspace catalog %s.%s: execution failed (%v); provider must return errors.Catalog without writing stdout", cfg.Package, cfg.Function, err)
|
||||
}
|
||||
var catalog apierrors.Catalog
|
||||
if err = apierrors.DecodeCatalogJSON(stdout.Bytes(), &catalog); err != nil {
|
||||
return nil, errors.New("workspace catalog provider did not emit a valid catalog")
|
||||
}
|
||||
var normalized bytes.Buffer
|
||||
if err = apierrors.ExportCatalog(&normalized, catalog); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return normalized.Bytes(), nil
|
||||
}
|
||||
|
||||
func prepareWorkspaceCatalogs(root string, configs []WorkspaceCatalog, check bool) (map[string][]byte, error) {
|
||||
if len(configs) > 16 {
|
||||
return nil, errors.New("workspace supports at most 16 catalog providers")
|
||||
}
|
||||
result := map[string][]byte{}
|
||||
for _, cfg := range configs {
|
||||
if err := safePath(root, cfg.Output); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
path := filepath.Join(root, filepath.FromSlash(cfg.Output))
|
||||
for prior := range result {
|
||||
if strings.EqualFold(prior, path) {
|
||||
return nil, errors.New("workspace catalog outputs overlap")
|
||||
}
|
||||
}
|
||||
data, err := exportWorkspaceCatalog(root, cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if check {
|
||||
old, err := os.ReadFile(path)
|
||||
if err != nil || !bytes.Equal(old, data) {
|
||||
return nil, fmt.Errorf("workspace catalog drift: %s; run workspace generate", cfg.Output)
|
||||
}
|
||||
}
|
||||
result[path] = data
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func writeWorkspaceCatalog(path string, data []byte) error {
|
||||
mode := os.FileMode(0644)
|
||||
if stat, err := os.Lstat(path); err == nil {
|
||||
if !stat.Mode().IsRegular() {
|
||||
return errors.New("workspace catalog output must be a regular file")
|
||||
}
|
||||
mode = stat.Mode().Perm()
|
||||
} else if !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
old, err := os.ReadFile(path)
|
||||
if err == nil && bytes.Equal(old, data) {
|
||||
return nil
|
||||
}
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
f, err := os.CreateTemp(filepath.Dir(path), ".omni-catalog-*")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Remove(f.Name())
|
||||
if err = f.Chmod(mode); err != nil {
|
||||
_ = f.Close()
|
||||
return err
|
||||
}
|
||||
_, writeErr := io.Copy(f, bytes.NewReader(data))
|
||||
err = errors.Join(writeErr, f.Close())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(f.Name(), path)
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user