feat: replace mapping generation with checked copy utilities
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
# go-omni CLI
|
||||
|
||||
v0.18 新增:可空嵌套映射、`mappingFiles` 配置拆分、显式 nil 策略和可选 `pointerResult`。原 v0.17 配置继续有效,见 [生成能力指南](../../GENERATION.md)。
|
||||
v0.19 移除结构映射生成:`omni.gen.json` 只保留 Repository 和中间件选项。普通接口直接使用生成 DTO;需要类型转换时用普通 Go 或可选 [copyx](../../COPY.md)。旧 `mappings` / `mappingFiles` 必须按迁移步骤清理,CLI 不会静默删除仍在调用的旧生成文件。
|
||||
|
||||
v0.17 增加公共字段组合、可选结构映射、自动中间件工厂和按需 Repository。配置和迁移见 [生成能力指南](../../GENERATION.md)。
|
||||
公共字段组合、中间件工厂和按需 Repository 继续支持,见 [生成能力指南](../../GENERATION.md)。
|
||||
|
||||
v0.16 增加基础类型/动态 JSON 数组返回、OpenAPI 公共引用和 workspace Go Catalog 统一导出。见 [接入与筛选规则](../../DEVELOPMENT.md#同一份-go-错误目录供多个服务生成)。
|
||||
|
||||
@@ -14,7 +14,7 @@ v0.14 新增 `api format [--check]`、`api upgrade [--dry-run|--preview <空目
|
||||
|
||||
```sh
|
||||
# 仓库读取权限与 PATH 配置见根目录 README
|
||||
go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.18.0-preview
|
||||
go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.19.0-preview
|
||||
go-omni new --module example.com/hello ./hello
|
||||
|
||||
# 从自己的契约生成;service.api 及其相对 import 文件需要在本地存在
|
||||
@@ -49,14 +49,14 @@ main、Gen.go、OpenAPI 和 manifest 由生成器维护;业务文件按字节
|
||||
|
||||
## 版本与迁移
|
||||
|
||||
CLI 与框架依赖固定使用 v0.18.0-preview。远程安装的 CLI 默认把自身版本写入 go.mod,不添加本地 replace;`--framework-version` 可显式指定已发布的兼容版本。仅在开发框架源码时使用本地替换:
|
||||
CLI 与框架依赖固定使用 v0.19.0-preview。远程安装的 CLI 默认把自身版本写入 go.mod,不添加本地 replace;`--framework-version` 可显式指定已发布的兼容版本。仅在开发框架源码时使用本地替换:
|
||||
|
||||
```sh
|
||||
# 在 go-omni 源码仓库中执行
|
||||
go run ./cmd/go-omni new --module example.com/hello --framework-dir . ../hello
|
||||
```
|
||||
|
||||
当前模板与依赖以 v0.18.0-preview 为基线,继续使用 manifest v2;GORM 可选适配器从 v0.14.1-preview 提供。已有项目使用 [定点升级与预览](../../DEVELOPMENT.md#v013-项目升级):原始模板可自动替换,含业务修改的基础文件按预览合并;通用清理由 Runtime 负责。独立工具命令不会被删除。受管理项目通过 `.api` 模块导入增加业务,不使用旧 `add` 命令的手动路由装配。
|
||||
当前模板与依赖以 v0.19.0-preview 为基线,继续使用 manifest v2;GORM 可选适配器从 v0.14.1-preview 提供。已有项目使用 [定点升级与预览](../../DEVELOPMENT.md#v013-项目升级):原始模板可自动替换,含业务修改的基础文件按预览合并;通用清理由 Runtime 负责。独立工具命令不会被删除。受管理项目通过 `.api` 模块导入增加业务,不使用旧 `add` 命令的手动路由装配。
|
||||
|
||||
## 数据库迁移命令
|
||||
|
||||
|
||||
95
cmd/go-omni/apigen/copyConsumer_test.go
Normal file
95
cmd/go-omni/apigen/copyConsumer_test.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package apigen
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func runConsumerGo(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 TestCopyxThreeEndpointConsumer(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)
|
||||
}
|
||||
for path, fixture := range map[string]string{
|
||||
"internal/amount/money.go": "amount.go", "internal/domain/employee.go": "domain.go", "internal/convert/fields.go": "converters.go", "internal/convert/copy.go": "copy.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",
|
||||
} {
|
||||
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))
|
||||
for _, group := range []string{"Enterprise", "Tenant", "Admin"} {
|
||||
text := fmt.Sprintf(`package %s
|
||||
import("context";"example.com/composition/internal/types";"example.com/composition/internal/convert";"example.com/composition/internal/domain")
|
||||
func(s *Logic)Save(ctx context.Context,req *types.%sRequest)(*types.EmployeeResponse,error){
|
||||
input,err:=convert.Employee(*req);if err!=nil{return nil,err}
|
||||
result,err:=convert.Response(domain.Result{Employee:input,ID:9007199254740993});if err!=nil{return nil,err};return &result,nil
|
||||
}
|
||||
`, strings.ToLower(group), group)
|
||||
writeFile(t, filepath.Join(dir, "internal/logic", strings.ToLower(group), "save.go"), text)
|
||||
}
|
||||
runConsumerGo(t, dir, "mod", "tidy")
|
||||
runConsumerGo(t, dir, "test", "-race", "-count=1", "./...")
|
||||
o.API = filepath.Join(dir, "api/service.api")
|
||||
before := snapshot(t, dir)
|
||||
o.DryRun = true
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
o.DryRun = false
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
o.Check = true
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !sameFiles(before, snapshot(t, dir)) {
|
||||
t.Fatal("generation changed business files")
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "internal/mapping")); !os.IsNotExist(err) {
|
||||
t.Fatal("mapping directory generated")
|
||||
}
|
||||
// Adding a common field requires one contract edit and one independent
|
||||
// business field. Three endpoint converters need no new rules.
|
||||
contract := readFile(t, o.API)
|
||||
contract = strings.Replace(contract, "type SalaryFields {", "type SalaryFields {\n Note string `json:\"note,optional\"`", 1)
|
||||
writeFile(t, o.API, contract)
|
||||
domainPath := filepath.Join(dir, "internal/domain/employee.go")
|
||||
writeFile(t, domainPath, strings.Replace(readFile(t, domainPath), "type Employee struct {", "type Employee struct {\n Note string", 1))
|
||||
o.Check = false
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
o.Check = true
|
||||
if err := generateManagedTestProject(t, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runConsumerGo(t, dir, "test", "-race", "-count=1", "./...")
|
||||
runConsumerGo(t, dir, "build", ".")
|
||||
}
|
||||
@@ -19,7 +19,7 @@ import (
|
||||
)
|
||||
|
||||
const frameworkModule = "git.yuntuo.hk/k2safe/go-omni"
|
||||
const minimumVersion = "v0.18.0-preview"
|
||||
const minimumVersion = "v0.19.0-preview"
|
||||
const generatedHeader = "// Code generated by go-omni api. DO NOT EDIT.\n"
|
||||
|
||||
type Options struct {
|
||||
@@ -252,9 +252,6 @@ func generate(o Options, c *Compiled, starter bool) (Output, error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := renderMappings(o, generated, seeds); err != nil {
|
||||
return Output{}, err
|
||||
}
|
||||
currentConfig, stillExists, err := readGeneratorConfig(directory)
|
||||
if err != nil {
|
||||
return Output{}, err
|
||||
|
||||
@@ -15,14 +15,11 @@ import (
|
||||
|
||||
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.
|
||||
// GeneratorConfig contains developer-owned scaffolding options only.
|
||||
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
|
||||
Repository string `json:"repository,omitempty"`
|
||||
Middleware string `json:"middleware,omitempty"`
|
||||
inputDigest string
|
||||
}
|
||||
|
||||
func readGeneratorConfig(root string) (GeneratorConfig, bool, error) {
|
||||
@@ -40,7 +37,7 @@ func readGeneratorConfig(root string) (GeneratorConfig, bool, error) {
|
||||
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")
|
||||
return cfg, false, errors.New("omni.gen.json is missing; restore the project-owned generation options before regenerating")
|
||||
}
|
||||
cfg.Middleware = "handlers"
|
||||
} else if !errors.Is(e, os.ErrNotExist) {
|
||||
@@ -54,72 +51,36 @@ func readGeneratorConfig(root string) (GeneratorConfig, bool, error) {
|
||||
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)
|
||||
raw, err := readGeneratorConfigFile(root, generatorConfigFile)
|
||||
if err != nil {
|
||||
return cfg, false, err
|
||||
}
|
||||
var keys map[string]json.RawMessage
|
||||
if err = apierrors.DecodeCatalogJSON(raw, &keys); err != nil {
|
||||
return cfg, false, fmt.Errorf("omni.gen.json: %w", err)
|
||||
}
|
||||
for _, key := range []string{"mappings", "mappingFiles"} {
|
||||
if _, exists := keys[key]; exists {
|
||||
return cfg, false, errors.New("mapping generation was removed: migrate callers to copyx or ordinary Go, remove mappings/mappingFiles from omni.gen.json, then remove internal/mapping/mappingsGen.go; see COPY.md")
|
||||
}
|
||||
}
|
||||
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))
|
||||
digest := sha256.Sum256(raw)
|
||||
cfg.inputDigest = fmt.Sprintf("%x", digest)
|
||||
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) {
|
||||
func readGeneratorConfigFile(root, name string) ([]byte, error) {
|
||||
if err := safePath(root, name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -2,140 +2,95 @@ package apigen
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"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")
|
||||
func TestWorkspaceRejectsRemovedMappingWithoutWrites(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
for _, name := range []string{"one", "two"} {
|
||||
if _, err := New(Options{Dir: filepath.Join(root, name), Module: "example.com/" + name, FrameworkDir: frameworkRoot(t)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
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)
|
||||
firstAPI := filepath.Join(root, "one/api/service.api")
|
||||
writeFile(t, firstAPI, strings.Replace(readFile(t, firstAPI), "Greet a visitor", "Changed description", 1))
|
||||
writeFile(t, filepath.Join(root, "two", generatorConfigFile), `{"mappingFiles":["../../private.json"]}`)
|
||||
workspace := filepath.Join(root, "omni.workspace.json")
|
||||
writeFile(t, workspace, `{"services":[{"dir":"one"},{"dir":"two"}]}`)
|
||||
before := snapshot(t, root)
|
||||
for _, operation := range []string{"generate", "check"} {
|
||||
if err := Workspace(workspace, operation, io.Discard); err == nil || !strings.Contains(err.Error(), "mapping generation was removed") {
|
||||
t.Fatal(operation, err)
|
||||
}
|
||||
if !sameFiles(before, snapshot(t, root)) {
|
||||
t.Fatal("workspace migration failure wrote files")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
func TestRemovedMappingConfiguration(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "project")
|
||||
if _, err := New(Options{Dir: dir, Module: "example.com/removed", FrameworkDir: frameworkRoot(t)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, raw := range []string{`{"mappings":[]}`, `{"mappingFiles":[]}`, `{"mappings":null}`, `{"mappingFiles":["../../private.json"]}`} {
|
||||
writeFile(t, filepath.Join(dir, generatorConfigFile), raw)
|
||||
before := snapshot(t, dir)
|
||||
for _, o := range []Options{{Dir: dir}, {Dir: dir, Check: true}, {Dir: dir, DryRun: true}} {
|
||||
o.API = filepath.Join(dir, "api/service.api")
|
||||
if _, err := Generate(o); err == nil || !strings.Contains(err.Error(), "mapping generation was removed") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !sameFiles(before, snapshot(t, dir)) {
|
||||
t.Fatal("read changed configuration")
|
||||
t.Fatal("rejected config wrote project")
|
||||
}
|
||||
})
|
||||
}
|
||||
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") {
|
||||
func TestOldMappingFileRequiresExplicitMigration(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "project")
|
||||
if _, err := New(Options{Dir: dir, Module: "example.com/removed", FrameworkDir: frameworkRoot(t)}); err != nil {
|
||||
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") {
|
||||
name := "internal/mapping/mappingsGen.go"
|
||||
body := generatedHeader + "package mapping\n"
|
||||
writeFile(t, filepath.Join(dir, name), body)
|
||||
var m manifest
|
||||
if err := json.Unmarshal([]byte(readFile(t, filepath.Join(dir, manifestPath))), &m); err != nil {
|
||||
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)
|
||||
m.Generated[name] = digest([]byte(body))
|
||||
raw, _ := json.Marshal(m)
|
||||
writeFile(t, filepath.Join(dir, manifestPath), string(raw))
|
||||
before := snapshot(t, dir)
|
||||
o := Options{Dir: dir, API: filepath.Join(dir, "api/service.api")}
|
||||
for _, mode := range []string{"generate", "check", "dry-run"} {
|
||||
o.Check = mode == "check"
|
||||
o.DryRun = mode == "dry-run"
|
||||
if _, err := Generate(o); err == nil || !strings.Contains(err.Error(), "mapping generation was removed") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !sameFiles(before, snapshot(t, dir)) {
|
||||
t.Fatal("legacy file deleted or project changed")
|
||||
}
|
||||
}
|
||||
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") {
|
||||
if err := os.Remove(filepath.Join(dir, name)); err != nil {
|
||||
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)
|
||||
o.Check = false
|
||||
o.DryRun = false
|
||||
if _, err := Generate(o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
o.Check = true
|
||||
if _, err := Generate(o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(readFile(t, filepath.Join(dir, manifestPath)), name) {
|
||||
t.Fatal("old ownership retained")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,453 +0,0 @@
|
||||
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
|
||||
}
|
||||
@@ -1,175 +0,0 @@
|
||||
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
|
||||
}
|
||||
@@ -1,250 +0,0 @@
|
||||
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
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
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
|
||||
}
|
||||
@@ -1,266 +0,0 @@
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -56,10 +56,11 @@ 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.
|
||||
Ordinary Logic uses the generated Request/Response types directly. Where an
|
||||
independent business type is needed, use ordinary Go or the optional copyx package.
|
||||
There are no mappings/mappingFiles settings or generated mapping files in v0.19.
|
||||
See COPY.md in the framework repository for copy policies and migration steps,
|
||||
and GENERATION.md for field composition and middleware factories.
|
||||
|
||||
## Run
|
||||
|
||||
@@ -145,8 +146,7 @@ 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
|
||||
omni.gen.json # developer-owned repository/middleware options
|
||||
docs/openapi.json # generated OpenAPI 3.0.3
|
||||
.goOmni/apiManifest.json # ownership and content hashes
|
||||
`+"```"+`
|
||||
|
||||
@@ -18,7 +18,6 @@ import (
|
||||
"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"
|
||||
@@ -86,16 +85,16 @@ func TestHTTPComposition(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestFullFormAndMappingSemantics(t *testing.T) {
|
||||
func TestFullFormAndCopySemantics(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)
|
||||
first, e := convert.Employee(req)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
second, e := mapping.FromEnterprise(types.EnterpriseRequest{})
|
||||
second, e := convert.Employee(types.EnterpriseRequest{})
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
@@ -108,26 +107,26 @@ func TestFullFormAndMappingSemantics(t *testing.T) {
|
||||
t.Fatal("unexpected implicit deep copy")
|
||||
}
|
||||
req.Employer = &money
|
||||
first, e = mapping.FromEnterprise(req)
|
||||
first, e = convert.Employee(req)
|
||||
if e != nil || first.Employer != req.Employer {
|
||||
t.Fatal("nullable pointer changed", e)
|
||||
}
|
||||
bad := "reject"
|
||||
failed, e := mapping.FromEnterprise(types.EnterpriseRequest{Name: &bad})
|
||||
failed, e := convert.Employee(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{})
|
||||
empty, e := convert.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)
|
||||
out, e := convert.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)
|
||||
out, e = convert.ToBatch(src)
|
||||
if e != nil || out.Rows[0].Value != "one" {
|
||||
t.Fatal(out, e)
|
||||
}
|
||||
@@ -140,7 +139,7 @@ func TestFullFormAndMappingSemantics(t *testing.T) {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
result, err := mapping.FromEnterprise(types.EnterpriseRequest{Name: &name, Salary: &money})
|
||||
result, err := convert.Employee(types.EnterpriseRequest{Name: &name, Salary: &money})
|
||||
if err != nil || result.EmployeeName != name || result.Salary != money {
|
||||
t.Error(result, err)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package convert
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"example.com/composition/internal/amount"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
@@ -19,11 +18,4 @@ func Name(value *string) (string, error) {
|
||||
}
|
||||
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) }
|
||||
|
||||
29
cmd/go-omni/apigen/testdata/composition/copy.go
vendored
Normal file
29
cmd/go-omni/apigen/testdata/composition/copy.go
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
package convert
|
||||
|
||||
import (
|
||||
"example.com/composition/internal/domain"
|
||||
"example.com/composition/internal/types"
|
||||
"git.yuntuo.hk/k2safe/go-omni/copyx"
|
||||
)
|
||||
|
||||
// The three endpoints reuse one ordinary Go helper and their existing scalar.
|
||||
// There is no field list or package/type name JSON to keep in sync.
|
||||
func Employee[S any](source S) (domain.Employee, error) {
|
||||
var target domain.Employee
|
||||
err := copyx.Copy(&target, source,
|
||||
copyx.WithFieldNames[S, domain.Employee](map[string]string{"Name": "EmployeeName"}),
|
||||
copyx.WithNilPolicy(copyx.NilZero),
|
||||
copyx.WithConverter(Name),
|
||||
)
|
||||
return target, err
|
||||
}
|
||||
func Response(source domain.Result) (types.EmployeeResponse, error) {
|
||||
var target types.EmployeeResponse
|
||||
err := copyx.Copy(&target, source, copyx.WithConverter(func(v int64) (string, error) { return ID(v), nil }))
|
||||
return target, err
|
||||
}
|
||||
func ToBatch(source domain.Batch) (domain.BatchDTO, error) {
|
||||
var target domain.BatchDTO
|
||||
err := copyx.Copy(&target, source)
|
||||
return target, err
|
||||
}
|
||||
@@ -14,7 +14,7 @@ type Result struct {
|
||||
ID int64
|
||||
}
|
||||
|
||||
// Distinct container item types exercise explicitly selected element mappings.
|
||||
// Distinct container item types exercise explicit Go collection copying.
|
||||
type Row struct{ Value string }
|
||||
type RowDTO struct{ Value string }
|
||||
type Batch struct {
|
||||
|
||||
@@ -1,170 +0,0 @@
|
||||
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
15
cmd/go-omni/apigen/testdata/nullable/convert.go
vendored
@@ -1,15 +0,0 @@
|
||||
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
23
cmd/go-omni/apigen/testdata/nullable/domain.go
vendored
@@ -1,23 +0,0 @@
|
||||
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
15
cmd/go-omni/apigen/testdata/nullable/service.api
vendored
@@ -1,15 +0,0 @@
|
||||
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)
|
||||
}
|
||||
@@ -117,6 +117,17 @@ func apply(root, module, entry string, generated, seeds map[string][]byte, check
|
||||
var changes []change
|
||||
// Check ALL previous outputs, even stale ones, before writing any new file.
|
||||
for name, hash := range old.Generated {
|
||||
// Migration guard only; old mappings are never regenerated or silently
|
||||
// deleted. Once the project removes them, forget the stale ownership.
|
||||
if name == "internal/mapping/mappingsGen.go" {
|
||||
if err = safePath(root, name); err != nil {
|
||||
return result, err
|
||||
}
|
||||
if _, e := os.Lstat(filepath.Join(root, name)); !errors.Is(e, os.ErrNotExist) {
|
||||
return result, errors.New("mapping generation was removed: migrate callers to copyx or ordinary Go and remove internal/mapping/mappingsGen.go before generating; see COPY.md")
|
||||
}
|
||||
continue
|
||||
}
|
||||
if !ownedPath(name) {
|
||||
return result, fmt.Errorf("api: invalid owned path %q", name)
|
||||
}
|
||||
@@ -273,9 +284,6 @@ func apply(root, module, entry string, generated, seeds map[string][]byte, check
|
||||
}
|
||||
func digest(data []byte) string { sum := sha256.Sum256(data); return hex.EncodeToString(sum[:]) }
|
||||
func ownedPath(name string) bool {
|
||||
if name == "internal/mapping/mappingsGen.go" {
|
||||
return true
|
||||
}
|
||||
if strings.HasPrefix(name, "internal/types/dto") && strings.HasSuffix(name, "Gen.go") && goName.MatchString(strings.TrimSuffix(filepath.Base(name), ".go")) {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func TestNewWithRemoteFrameworkVersion(t *testing.T) {
|
||||
for _, flag := range [][]string{{"--framework-version", "v0.18.0-preview"}, {"--framework-version=v0.18.0-preview"}} {
|
||||
for _, flag := range [][]string{{"--framework-version", "v0.19.0-preview"}, {"--framework-version=v0.19.0-preview"}} {
|
||||
project := filepath.Join(t.TempDir(), "billing")
|
||||
args := append([]string{"--module", "example.com/billing"}, flag...)
|
||||
if err := runNew(append(args, project)); err != nil {
|
||||
@@ -18,7 +18,7 @@ func TestNewWithRemoteFrameworkVersion(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(content), "go-omni v0.18.0-preview") || strings.Contains(string(content), "replace ") {
|
||||
if !strings.Contains(string(content), "go-omni v0.19.0-preview") || strings.Contains(string(content), "replace ") {
|
||||
t.Fatalf("CLI did not pin remote dependency: %s", content)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ const frameworkModule = "git.yuntuo.hk/k2safe/go-omni"
|
||||
// Source builds can carry an unpublished VCS version (including +dirty).
|
||||
// Keep this fallback in sync with the next release; installed CLIs use their
|
||||
// downloaded module version, which also has a module checksum.
|
||||
const defaultFrameworkVersion = "v0.18.0-preview"
|
||||
const defaultFrameworkVersion = "v0.19.0-preview"
|
||||
|
||||
// This target is not a claim that the release already exists. Source users
|
||||
// select a local framework checkout until the module templates are published.
|
||||
|
||||
Reference in New Issue
Block a user