feat: support primitive response arrays and shared catalog tooling

This commit is contained in:
2026-09-25 02:22:24 +08:00
parent 1f2d8b6a6a
commit 64e3bd6cfc
40 changed files with 1656 additions and 1348 deletions

View File

@@ -1,5 +1,7 @@
# go-omni CLI
v0.16 增加基础类型/动态 JSON 数组返回、OpenAPI 公共引用和 workspace Go Catalog 统一导出。见 [接入与筛选规则](../../DEVELOPMENT.md#同一份-go-错误目录供多个服务生成)。
v0.14 新增 `api format [--check]`、`api upgrade [--dry-run|--preview <空目录>|--apply]`,以及 `workspace format|generate|check --file omni.workspace.json`。详见 [通用开发指南](../../DEVELOPMENT.md)。
新项目采用根目录 `main.go` 与 `internal/config、svc、logic、handler、types`。完整职责、启动扩展、流式接口和生成文件边界见 [项目指南](../../PROJECT.md)。
@@ -8,7 +10,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.15.0-preview
go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.16.0-preview
go-omni new --module example.com/hello ./hello
# 从自己的契约生成;service.api 及其相对 import 文件需要在本地存在
@@ -43,14 +45,14 @@ main、Gen.go、OpenAPI 和 manifest 由生成器维护;业务文件按字节
## 版本与迁移
CLI 与框架依赖固定使用 v0.15.0-preview。远程安装的 CLI 默认把自身版本写入 go.mod,不添加本地 replace;`--framework-version` 可显式指定已发布的兼容版本。仅在开发框架源码时使用本地替换:
CLI 与框架依赖固定使用 v0.16.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.15.0-preview 的分阶段 Runtime 能力,继续使用 manifest v2;GORM 可选适配器从 v0.14.1-preview 提供。已有项目使用 [定点升级与预览](../../DEVELOPMENT.md#v013-项目升级):原始模板可自动替换,含业务修改的基础文件按预览合并;通用清理由 Runtime 负责。独立工具命令不会被删除。受管理项目通过 `.api` 模块导入增加业务,不使用旧 `add` 命令的手动路由装配。
模板最低需要 v0.16.0-preview 的统一环境变量别名能力,继续使用 manifest v2;GORM 可选适配器从 v0.14.1-preview 提供。已有项目使用 [定点升级与预览](../../DEVELOPMENT.md#v013-项目升级):原始模板可自动替换,含业务修改的基础文件按预览合并;通用清理由 Runtime 负责。独立工具命令不会被删除。受管理项目通过 `.api` 模块导入增加业务,不使用旧 `add` 命令的手动路由装配。
## 数据库迁移命令

View File

@@ -0,0 +1,104 @@
package apigen
import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
)
func TestPrimitiveArrayContracts(t *testing.T) {
for _, name := range []string{"string", "bool", "int", "int8", "int16", "int32", "int64", "uint", "uint16", "uint32", "uint64", "float32", "float64", "any"} {
t.Run(name, func(t *testing.T) {
spec, err := parseText(t, fmt.Sprintf("service arrays{\n@handler list\nget /list returns ([]%s)\n}", name))
if err != nil {
t.Fatal(err)
}
c, err := Compile(spec)
if err != nil {
t.Fatal(err)
}
schema := operation(t, document(t, c), "/list", "get")["responses"].(map[string]any)["200"].(map[string]any)["content"].(map[string]any)["application/json"].(map[string]any)["schema"].(map[string]any)
data := schema["properties"].(map[string]any)["data"].(map[string]any)
if data["type"] != "array" || data["items"] == nil {
t.Fatal(data)
}
if name == "any" && len(data["items"].(map[string]any)) != 0 {
t.Fatal("dynamic JSON was constrained")
}
if responseGoType("[]"+name) != "[]"+name || responseNeedsTypes("[]"+name) {
t.Fatal("wrong Go signature/import")
}
})
}
for _, name := range []string{"uint8", "File", "Unknown", "[]string"} {
spec, err := parseText(t, fmt.Sprintf("service arrays{\n@handler list\nget /list returns ([]%s)\n}", name))
if err == nil {
_, err = Compile(spec)
}
if err == nil {
t.Fatal("unsupported root accepted", name)
}
}
}
func TestPrimitiveArrayConsumer(t *testing.T) {
if os.Getenv("GO_OMNI_SCAFFOLD_INTEGRATION") != "1" {
t.Skip("set GO_OMNI_SCAFFOLD_INTEGRATION=1")
}
dir := filepath.Join(t.TempDir(), "arrays")
o := Options{Dir: dir, API: filepath.Join(frameworkRoot(t), "examples/contracts/arrays.api"), Module: "example.com/arrays", FrameworkDir: frameworkRoot(t)}
if err := generateManagedTestProject(t, o); err != nil {
t.Fatal(err)
}
for _, name := range []string{"strings", "integers", "dynamic", "empty", "raw"} {
writeFile(t, filepath.Join(dir, "internal/logic/arrays/"+name+".go"), "package arrays\n")
}
writeFile(t, filepath.Join(dir, "internal/logic/arrays/values.go"), readFile(t, "testdata/arrays/values.go"))
writeFile(t, filepath.Join(dir, "consumer_test.go"), readFile(t, "testdata/arrays/consumer_test.go"))
o.API = filepath.Join(dir, "api/arrays.api")
if err := generateManagedTestProject(t, o); err != nil {
t.Fatal(err)
}
if !strings.Contains(readFile(t, filepath.Join(dir, "internal/logic/arrays/values.go")), "企业") {
t.Fatal("logic overwritten")
}
o.Check = true
if err := generateManagedTestProject(t, o); err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "go", "test", "-race", "-mod=mod", "-count=1", "./...")
cmd.Dir = dir
cmd.Env = append(os.Environ(), "GOWORK=off")
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("%v\n%s", err, out)
}
if os.Getenv("GO_OMNI_RELEASE_CLI") != "" {
cmd = exec.CommandContext(ctx, "go", "list", "-m", "-json", frameworkModule)
cmd.Dir = dir
cmd.Env = append(os.Environ(), "GOWORK=off")
raw, err := cmd.CombinedOutput()
if err != nil {
t.Fatal(err, string(raw))
}
var module struct {
Version, Sum string
Replace any
}
if err = json.Unmarshal(raw, &module); err != nil {
t.Fatal(err)
}
if module.Version != os.Getenv("GO_OMNI_RELEASE_VERSION") || module.Replace != nil || module.Sum == "" {
t.Fatal("not a fixed remote consumer")
}
t.Logf("remote framework %s checksum %s; no local replace", module.Version, module.Sum)
}
}

View File

@@ -2,41 +2,73 @@ package apigen
import (
"errors"
"fmt"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
"git.yuntuo.hk/k2safe/go-omni/response"
"io"
"os"
"path/filepath"
"strings"
)
const catalogDocumentFile = "omni.errors.json"
func readDocumentCatalog(root string, spec *Spec) error {
path := filepath.Join(root, catalogDocumentFile)
stat, err := os.Lstat(path)
if errors.Is(err, os.ErrNotExist) {
return nil
// DocumentErrors selects documentation only. Runtime App inheritance and
// route-local @errors declarations are never changed by this metadata.
// A nil Include lists all errors; [] lists none. Routes override Include.
type DocumentErrors struct {
Source string `json:"source"`
Include []string `json:"include"`
Routes map[string][]string `json:"routes,omitempty"`
}
func readDocumentCatalog(root string, spec *Spec, overrides map[string][]byte) error {
name := catalogDocumentFile
path := filepath.Join(root, name)
// Shared metadata uses the same explicitly authorized roots as .api imports.
if selection := spec.DocumentErrors; selection != nil {
if _, err := os.Lstat(path); !errors.Is(err, os.ErrNotExist) {
return errors.New("api: use either local omni.errors.json or shared errors.source, not both")
}
name = selection.Source
alias, sub, ok := strings.Cut(strings.TrimPrefix(name, "@"), "/")
declared, found := spec.SharedRoots[alias]
if !strings.HasPrefix(name, "@") || !ok || !found || sub == "" || filepath.Ext(sub) != ".json" {
return errors.New("api: errors.source must name a JSON file in a declared @root")
}
if err := safePath(declared.Path, sub); err != nil {
return err
}
path = filepath.Join(declared.Path, filepath.FromSlash(sub))
}
if err != nil {
return err
}
if !stat.Mode().IsRegular() {
return errors.New("api: error metadata must be a regular file")
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
raw, err := io.ReadAll(io.LimitReader(file, (1<<20)+1))
if err != nil {
return err
raw, ok := overrides[path]
if !ok {
stat, err := os.Lstat(path)
if errors.Is(err, os.ErrNotExist) && spec.DocumentErrors == nil {
return nil
}
if err != nil {
return err
}
if !stat.Mode().IsRegular() {
return errors.New("api: error metadata must be a regular file")
}
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
raw, err = io.ReadAll(io.LimitReader(f, (1<<20)+1))
if err != nil {
return err
}
}
if len(raw) > 1<<20 {
return errors.New("api: error metadata exceeds 1 MiB")
}
var catalog apierrors.Catalog
if err = apierrors.DecodeCatalogJSON(raw, &catalog); err != nil {
if err := apierrors.DecodeCatalogJSON(raw, &catalog); err != nil {
return err
}
registry, err := apierrors.NewRegistry(catalog)
@@ -45,10 +77,70 @@ func readDocumentCatalog(root string, spec *Spec) error {
}
normalized := registry.Catalog()
spec.DocumentCatalog = &normalized
spec.Sources[catalogDocumentFile] = raw
spec.SourcePaths[catalogDocumentFile] = path
spec.Sources[name] = raw
spec.SourcePaths[name] = path
return validateDocumentSelection(spec)
}
func documentRouteCatalog(spec *Spec, route Route) apierrors.Catalog {
catalog := spec.DocumentCatalog.Clone()
if spec.DocumentErrors == nil {
return catalog
}
include := spec.DocumentErrors.Include
if selected, ok := spec.DocumentErrors.Routes[strings.ToUpper(route.Method)+" "+route.Path]; ok {
include = selected
}
if include != nil {
selected := map[string]apierrors.Definition{}
for _, key := range include {
selected[key] = catalog.Definitions[key]
}
catalog.Definitions = selected
}
return catalog
}
func validateDocumentSelection(spec *Spec) error {
cfg := spec.DocumentErrors
if cfg == nil {
return nil
}
validate := func(keys []string) error {
seen := map[string]bool{}
for _, key := range keys {
if _, ok := spec.DocumentCatalog.Definitions[key]; !ok {
return fmt.Errorf("api: unknown documentation error %q", key)
}
if seen[key] {
return fmt.Errorf("api: duplicate documentation error %q", key)
}
seen[key] = true
}
return nil
}
if err := validate(cfg.Include); err != nil {
return err
}
routes := map[string]Route{}
for _, r := range spec.Routes {
routes[strings.ToUpper(r.Method)+" "+r.Path] = r
}
for key, keys := range cfg.Routes {
r, ok := routes[key]
if !ok {
return fmt.Errorf("api: unknown documentation route %q", key)
}
if len(r.Errors) > 0 {
return fmt.Errorf("api: %s already declares route-local errors", key)
}
if err := validate(keys); err != nil {
return err
}
}
return nil
}
func validateDocumentCatalog(spec *Spec) error {
if spec.DocumentCatalog == nil {
return nil
@@ -61,7 +153,7 @@ func validateDocumentCatalog(spec *Spec) error {
if err != nil {
return err
}
if err = policy.ValidateErrors(*spec.DocumentCatalog); err != nil {
if err = policy.ValidateErrors(documentRouteCatalog(spec, r)); err != nil {
return err
}
}

View File

@@ -70,7 +70,7 @@ func Compile(spec *Spec) (*Compiled, error) {
resolving := map[string]bool{}
var resolve func(string, int) (reflect.Type, error)
var expression func(ast.Expr, int) (reflect.Type, error)
primitives := map[string]reflect.Type{"File": reflect.TypeOf(contract.File{}), "any": reflect.TypeOf((*any)(nil)).Elem(), "string": reflect.TypeOf(""), "bool": reflect.TypeOf(false), "int": reflect.TypeOf(int(0)), "int8": reflect.TypeOf(int8(0)), "int16": reflect.TypeOf(int16(0)), "int32": reflect.TypeOf(int32(0)), "int64": reflect.TypeOf(int64(0)), "uint": reflect.TypeOf(uint(0)), "uint8": reflect.TypeOf(uint8(0)), "uint16": reflect.TypeOf(uint16(0)), "uint32": reflect.TypeOf(uint32(0)), "uint64": reflect.TypeOf(uint64(0)), "float32": reflect.TypeOf(float32(0)), "float64": reflect.TypeOf(float64(0))}
expression = func(e ast.Expr, depth int) (reflect.Type, error) {
if depth > 32 {
return nil, errors.New("type nesting exceeds 32")
@@ -80,7 +80,7 @@ func Compile(spec *Spec) (*Compiled, error) {
if t, ok := scalarTypes[e.Name]; ok {
return t, nil
}
if t, ok := primitives[e.Name]; ok {
if t, ok := primitiveTypes[e.Name]; ok {
return t, nil
}
return resolve(e.Name, depth+1)
@@ -313,6 +313,7 @@ func Compile(spec *Spec) (*Compiled, error) {
return nil, err
}
decorateDocument(doc, spec, c.Bindings)
compactDocument(doc)
c.Document, err = json.MarshalIndent(doc, "", " ")
return c, err
}

View File

@@ -213,7 +213,7 @@ func decorateDocument(doc map[string]any, spec *Spec, bindings map[string]*contr
if len(r.Errors) == 0 && documentRegistry != nil {
registry = documentRegistry
op["x-omni-error-catalog-scope"] = "app"
for key, definition := range registry.Catalog().Definitions {
for key, definition := range documentRouteCatalog(spec, r).Definitions {
grouped[definition.Status] = append(grouped[definition.Status], ErrorDefinition{Name: key, Code: key, Status: definition.Status, Message: definition.Message, BusinessCode: definition.Code, Params: definition.Params})
}
}

View File

@@ -0,0 +1,86 @@
package apigen
import "encoding/json"
// compactDocument only replaces standard OpenAPI Reference Object positions.
// Payloads (including examples containing a literal "$ref") are never walked.
// Mixed HTTP 200 responses retain both success and error alternatives.
func compactDocument(doc map[string]any) {
type slot struct {
parent map[string]any
key string
}
components := doc["components"].(map[string]any)
var responses, schemas, examples []slot
for _, path := range doc["paths"].(map[string]any) {
for _, value := range path.(map[string]any) {
op, ok := value.(map[string]any)
if !ok {
continue
}
rs, _ := op["responses"].(map[string]any)
for status := range rs {
responses = append(responses, slot{rs, status})
}
}
}
factor := func(kind string, slots []slot) {
counts := map[string]int{}
for _, s := range slots {
raw, _ := json.Marshal(s.parent[s.key])
counts[string(raw)]++
}
shared, _ := components[kind].(map[string]any)
if shared == nil {
shared = map[string]any{}
}
for _, s := range slots {
raw, _ := json.Marshal(s.parent[s.key])
count := counts[string(raw)]
if count < 2 {
continue
}
name := "Omni_" + digest(raw)
ref := map[string]any{"$ref": "#/components/" + kind + "/" + name}
refJSON, _ := json.Marshal(ref)
// Keep small objects inline if components would increase size.
if (count-1)*len(raw) <= count*len(refJSON)+len(name)+4 {
continue
}
shared[name] = s.parent[s.key]
s.parent[s.key] = map[string]any{"$ref": "#/components/" + kind + "/" + name}
}
if len(shared) > 0 {
components[kind] = shared
}
}
// Share whole responses before counting inner objects. Otherwise a catalog
// copied across 100 routes can create hundreds of unnecessary components
// that only one shared response ultimately uses.
factor("responses", responses)
collectMedia := func(value any) {
resp := value.(map[string]any)
content, _ := resp["content"].(map[string]any)
for _, value := range content {
media := value.(map[string]any)
if media["schema"] != nil {
schemas = append(schemas, slot{media, "schema"})
}
es, _ := media["examples"].(map[string]any)
for name := range es {
examples = append(examples, slot{es, name})
}
}
}
for _, s := range responses {
collectMedia(s.parent[s.key])
}
sharedResponses, _ := components["responses"].(map[string]any)
for _, resp := range sharedResponses {
collectMedia(resp)
}
factor("examples", examples)
factor("schemas", schemas)
}

View File

@@ -0,0 +1,107 @@
package apigen
import (
"encoding/json"
"fmt"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
"strings"
"testing"
)
func TestSharedErrorDocumentReferences(t *testing.T) {
source := `@app(defaultResponse: Common)
response Common(codeType:int32;successCode:0;errorCode:9999;messageField:msg;dataMode:always)
service demo {
`
for i := 0; i < 16; i++ {
kind := "string"
if i%2 == 0 {
kind = "int64"
}
source += fmt.Sprintf("@handler list%d\nget /list%d returns ([]%s)\n", i, i, kind)
}
source += "}\n"
spec, err := parseText(t, source)
if err != nil {
t.Fatal(err)
}
catalog := apierrors.Catalog{DefaultStatus: 400, Definitions: map[string]apierrors.Definition{}}
for i := 0; i < 80; i++ {
status := 400
if i%2 == 0 {
status = 200
}
catalog.Definitions[fmt.Sprintf("ERROR_%d", i)] = apierrors.Definition{Status: status, Code: 400000 + i, Message: strings.Repeat("说明", 40) + "{label}不能超过{max}个字符", Params: []apierrors.Parameter{{Name: "label", Type: "string", Example: "企业名称"}, {Name: "max", Type: "int", Example: 100}}}
}
spec.DocumentCatalog = &catalog
compiled, err := Compile(spec)
if err != nil {
t.Fatal(err)
}
var raw map[string]any
if err = json.Unmarshal(compiled.Document, &raw); err != nil {
t.Fatal(err)
}
components := raw["components"].(map[string]any)
for _, kind := range []string{"responses", "examples"} {
if len(components[kind].(map[string]any)) == 0 {
t.Fatal("missing shared", kind)
}
}
expanded := document(t, compiled)
for i := 0; i < 16; i++ {
responses := operation(t, expanded, fmt.Sprintf("/list%d", i), "get")["responses"].(map[string]any)
for _, status := range []string{"200", "400"} {
media := responses[status].(map[string]any)["content"].(map[string]any)["application/json"].(map[string]any)
if len(media["examples"].(map[string]any)) != 40 {
t.Fatal("examples lost")
}
if status == "200" {
choices := media["schema"].(map[string]any)["anyOf"].([]any)
if len(choices) != 2 || choices[0].(map[string]any)["properties"].(map[string]any)["data"].(map[string]any)["type"] != "array" {
t.Fatal("200 success shape lost")
}
}
for _, example := range media["examples"].(map[string]any) {
value := example.(map[string]any)["value"].(map[string]any)
if _, ok := value["data"]; !ok || value["data"] != nil || !strings.Contains(value["msg"].(string), "企业名称不能超过100个字符") {
t.Fatal(value)
}
}
}
}
expandedBytes, _ := json.MarshalIndent(expanded, "", " ")
if len(compiled.Document)*5 > len(expandedBytes) {
t.Fatalf("poor dedup: %d vs %d", len(compiled.Document), len(expandedBytes))
}
again, err := Compile(spec)
if err != nil || string(again.Document) != string(compiled.Document) {
t.Fatal("non-deterministic document", err)
}
before, _ := json.Marshal(raw)
compactDocument(raw)
data, _ := json.Marshal(raw)
if string(data) != string(before) {
t.Fatal("compaction not idempotent")
}
}
func TestSharedSchemasAndLiteralExampleReference(t *testing.T) {
doc := map[string]any{"components": map[string]any{}, "paths": map[string]any{}}
paths := doc["paths"].(map[string]any)
for _, name := range []string{"first", "second", "third"} {
schema := map[string]any{"type": "object", "description": strings.Repeat("schema description ", 40)}
example := map[string]any{"summary": strings.Repeat("example ", 50), "value": map[string]any{"$ref": "literal business field"}}
paths["/"+name] = map[string]any{"get": map[string]any{"responses": map[string]any{"200": map[string]any{"description": name, "content": map[string]any{"application/json": map[string]any{"schema": schema, "examples": map[string]any{"same": example}}}}}}}
}
compactDocument(doc)
components := doc["components"].(map[string]any)
if len(components["schemas"].(map[string]any)) != 1 {
t.Fatal("schema not shared")
}
for _, example := range components["examples"].(map[string]any) {
if example.(map[string]any)["value"].(map[string]any)["$ref"] != "literal business field" {
t.Fatal("payload reference interpreted")
}
}
}

View File

@@ -19,11 +19,12 @@ import (
)
const frameworkModule = "git.yuntuo.hk/k2safe/go-omni"
const minimumVersion = "v0.15.0-preview"
const minimumVersion = "v0.16.0-preview"
const generatedHeader = "// Code generated by go-omni api. DO NOT EDIT.\n"
type Options struct {
lockHeld bool
documentCatalogs map[string][]byte
Database, Storage string
API, Dir, Module, FrameworkDir, FrameworkVersion, LogOutput, LogFormat string
Check bool
@@ -48,7 +49,7 @@ func Generate(o Options) (Output, error) {
if o.API == "" || o.Dir == "" {
return Output{}, errors.New("--api and --dir are required")
}
spec, err := Parse(o.API)
spec, err := parseWithCatalogs(o.API, o.documentCatalogs)
if err != nil {
return Output{}, err
}
@@ -145,13 +146,13 @@ func generate(o Options, c *Compiled, starter bool) (Output, error) {
if !filepath.IsAbs(path) {
path = filepath.Join(directory, path)
}
if _, err := os.Stat(filepath.Join(path, "lifecycle", "runtime.go")); err == nil {
if source, err := os.ReadFile(filepath.Join(path, "config", "env.go")); err == nil && strings.Contains(string(source), "func ServerEnvAliases(") {
localCurrent = true
}
}
}
if !localCurrent {
return Output{}, fmt.Errorf("existing framework dependency predates the generated phased lifecycle support; use %s or a current local replacement", minimumVersion)
return Output{}, fmt.Errorf("existing framework dependency predates the generated configuration helpers; use %s or a current local replacement", minimumVersion)
}
}
if o.FrameworkDir != "" {
@@ -454,7 +455,7 @@ func renderProject(o Options, c *Compiled, starter bool) (map[string][]byte, map
continue
}
hasAutomatic = true
needsTypes = needsTypes || r.Request != "" || responseGoType(r.Response) != ""
needsTypes = needsTypes || r.Request != "" || responseNeedsTypes(r.Response)
needsBinder = needsBinder || !r.CustomBinding
}
text += fmt.Sprintf("%q;%q;", frameworkModule+"/httpx", o.Module+"/internal/svc")
@@ -570,7 +571,7 @@ func renderProject(o Options, c *Compiled, starter bool) (map[string][]byte, map
}
}
service := fmt.Sprintf("package %s\nimport(\"context\"; apierrors %q;", packageName, frameworkModule+"/errors")
if r.Request != "" || responseGoType(r.Response) != "" {
if r.Request != "" || responseNeedsTypes(r.Response) {
service += fmt.Sprintf("%q;", o.Module+"/internal/types")
}
if streamingBody || r.Produces != "" {

View File

@@ -74,7 +74,7 @@ func document(t *testing.T, c *Compiled) map[string]any {
if err := json.Unmarshal(c.Document, &doc); err != nil {
t.Fatal(err)
}
return doc
return expandDocumentRefs(t, doc, doc).(map[string]any)
}
func operation(t *testing.T, doc map[string]any, path, method string) map[string]any {
t.Helper()
@@ -303,3 +303,21 @@ func TestAuthMetadataWithParameterNamePrefixes(t *testing.T) {
t.Fatal("auth metadata lost")
}
}
// Existing semantic assertions resolve the standard local references emitted
// by the compiler. documentRefs_test additionally checks the actual references.
func expandDocumentRefs(t *testing.T, doc map[string]any, v any) any {
t.Helper()
switch v:=v.(type){
case map[string]any:
if ref,ok:=v["$ref"].(string);ok && strings.HasPrefix(ref,"#/components/") && len(v)==1 {
var target any=doc
for _,part:=range strings.Split(strings.TrimPrefix(ref,"#/"),"/"){m,ok:=target.(map[string]any);if !ok{t.Fatal("invalid reference",ref)};target=m[part]}
if target==nil{t.Fatal("dangling reference",ref)};return expandDocumentRefs(t,doc,target)
}
out:=map[string]any{};for k,x:=range v{out[k]=expandDocumentRefs(t,doc,x)};return out
case []any:
out:=make([]any,len(v));for i,x:=range v{out[i]=expandDocumentRefs(t,doc,x)};return out
default:return v
}
}

View File

@@ -17,6 +17,7 @@ import (
type Spec struct {
DocumentCatalog *apierrors.Catalog
DocumentErrors *DocumentErrors
SharedRoots map[string]SharedRoot
SourcePaths map[string]string
RequestPolicy contract.RequestPolicy
@@ -72,7 +73,9 @@ type parser struct {
imports []string
}
func Parse(path string) (*Spec, error) {
func Parse(path string) (*Spec, error) { return parseWithCatalogs(path, nil) }
func parseWithCatalogs(path string, catalogs map[string][]byte) (*Spec, error) {
entry, err := filepath.Abs(path)
if err != nil {
return nil, err
@@ -82,11 +85,12 @@ func Parse(path string) (*Spec, error) {
return nil, err
}
root := filepath.Dir(entry)
roots, err := readSharedRoots(root)
shared, err := readSharedConfig(root)
if err != nil {
return nil, err
}
spec := &Spec{Version: "1.0.0", SharedRoots: roots, SourcePaths: map[string]string{}, Sources: map[string][]byte{}, Entry: filepath.Base(entry)}
roots := shared.Roots
spec := &Spec{Version: "1.0.0", DocumentErrors: shared.Errors, SharedRoots: roots, SourcePaths: map[string]string{}, Sources: map[string][]byte{}, Entry: filepath.Base(entry)}
visiting, visited := map[string]bool{}, map[string]bool{}
total := 0
var visit func(string, string, int) error
@@ -169,7 +173,7 @@ func Parse(path string) (*Spec, error) {
if err = visit(entry, "", 0); err != nil {
return nil, err
}
if err = readDocumentCatalog(root, spec); err != nil {
if err = readDocumentCatalog(root, spec, catalogs); err != nil {
return nil, err
}
if err = validateSharedPins(spec); err != nil {

View File

@@ -73,6 +73,8 @@ func TestConfiguration(t *testing.T){
cfg,err:=Load("../../etc/config.yaml");if err!=nil{t.Fatal(err)}
if cfg.Server.Log.File==""{t.Fatal("file logger absent")}
t.Setenv("GO_OMNI_LOG_OUTPUT","stdout");cfg,err=Load("");if err!=nil||cfg.Server.Log.File!=""{t.Fatalf("override: %v",err)}
t.Setenv("GO_OMNI_SERVER_LOG_FILE","./explicit.log");cfg,err=Load("");if err!=nil||cfg.Server.Log.File!="./explicit.log"{t.Fatalf("canonical log file: %v",err)}
t.Setenv("GO_OMNI_SERVER_LOG_OUTPUT","stderr");cfg,err=Load("");if err!=nil||cfg.Server.Log.Output!="stderr"{t.Fatalf("canonical output: %v",err)}
_=os.Getenv("unused")
}`
endpoint := ""

View File

@@ -4,11 +4,12 @@ import (
"fmt"
"git.yuntuo.hk/k2safe/go-omni/contract"
"git.yuntuo.hk/k2safe/go-omni/openapi"
"reflect"
"strings"
)
// Response roots deliberately allow named DTOs, []DTO, *DTO and null. Keeping
// these four shapes explicit makes nil/empty behavior uniform across encoders.
// Response roots allow named DTOs, []DTO, []primitive, *DTO and null.
// A nil root slice is always encoded as an empty JSON array.
func responseShape(value string) (kind, name string) {
if value == "null" {
return "null", ""
@@ -27,6 +28,9 @@ func responseGoType(value string) string {
return ""
}
if kind == "array" {
if primitiveTypes[name] != nil {
return "[]" + name
}
return "[]types." + name
}
return "*types." + name
@@ -36,9 +40,25 @@ func responseDataSchema(value string, bindings map[string]*contract.Binder) (*op
if kind == "null" {
return &openapi.Schema{Type: "object", Nullable: true, Enum: []any{nil}}, nil
}
if kind == "array" && primitiveTypes[name] != nil {
if name == "File" {
return nil, fmt.Errorf("File is request-only; use a response DTO")
}
// Reuse the contract compiler, including integer bounds and byte-slice rules.
t := reflect.StructOf([]reflect.StructField{{Name: "Value", Type: reflect.SliceOf(primitiveTypes[name]), Tag: `json:"value"`}})
b, err := contract.Describe(t)
if err != nil {
return nil, err
}
s, err := b.ResponseSchema()
if err != nil {
return nil, err
}
return s.Properties["value"], nil
}
b, ok := bindings[name]
if !ok {
return nil, fmt.Errorf("unknown response DTO %q; use Name, []Name, *Name or null", name)
return nil, fmt.Errorf("unknown response DTO %q; use Name, []Name, []string/[]int64/[]bool/[]any, *Name or null", name)
}
s, err := b.ResponseSchema()
if err != nil {
@@ -52,3 +72,7 @@ func responseDataSchema(value string, bindings map[string]*contract.Binder) (*op
}
return s, nil
}
var primitiveTypes = map[string]reflect.Type{"File": reflect.TypeOf(contract.File{}), "any": reflect.TypeOf((*any)(nil)).Elem(), "string": reflect.TypeOf(""), "bool": reflect.TypeOf(false), "int": reflect.TypeOf(int(0)), "int8": reflect.TypeOf(int8(0)), "int16": reflect.TypeOf(int16(0)), "int32": reflect.TypeOf(int32(0)), "int64": reflect.TypeOf(int64(0)), "uint": reflect.TypeOf(uint(0)), "uint8": reflect.TypeOf(uint8(0)), "uint16": reflect.TypeOf(uint16(0)), "uint32": reflect.TypeOf(uint32(0)), "uint64": reflect.TypeOf(uint64(0)), "float32": reflect.TypeOf(float32(0)), "float64": reflect.TypeOf(float64(0))}
func responseNeedsTypes(value string) bool { return strings.Contains(responseGoType(value), "types.") }

View File

@@ -23,48 +23,49 @@ type SharedRoot struct {
SHA256 string `json:"sha256,omitempty"`
}
type sharedConfig struct {
Roots map[string]SharedRoot `json:"roots"`
Roots map[string]SharedRoot `json:"roots"`
Errors *DocumentErrors `json:"errors,omitempty"`
}
func readSharedRoots(root string) (map[string]SharedRoot, error) {
func readSharedConfig(root string) (sharedConfig, error) {
stat, err := os.Lstat(filepath.Join(root, importsFile))
if errors.Is(err, os.ErrNotExist) {
return nil, nil
return sharedConfig{}, nil
}
if err != nil {
return nil, err
return sharedConfig{}, err
}
if !stat.Mode().IsRegular() {
return nil, errors.New("api: shared root configuration must be a regular file")
return sharedConfig{}, errors.New("api: shared root configuration must be a regular file")
}
f, err := os.Open(filepath.Join(root, importsFile))
if errors.Is(err, os.ErrNotExist) {
return nil, nil
return sharedConfig{}, nil
}
if err != nil {
return nil, err
return sharedConfig{}, err
}
defer f.Close()
raw, err := io.ReadAll(io.LimitReader(f, 65537))
if err != nil {
return nil, err
return sharedConfig{}, err
}
if len(raw) > 65536 {
return nil, errors.New("api: shared root config too large")
return sharedConfig{}, errors.New("api: shared root config too large")
}
var cfg sharedConfig
if err = apierrors.DecodeCatalogJSON(raw, &cfg); err != nil {
return nil, err
return sharedConfig{}, err
}
if len(cfg.Roots) > 16 {
return nil, errors.New("api: at most 16 shared roots")
return sharedConfig{}, errors.New("api: at most 16 shared roots")
}
for name, r := range cfg.Roots {
if !lowerName.MatchString(name) || r.Path == "" || len(r.Version) > 128 {
return nil, errors.New("api: invalid shared root")
return sharedConfig{}, errors.New("api: invalid shared root")
}
if r.SHA256 != "" && (len(r.SHA256) != 64 || strings.Trim(r.SHA256, "0123456789abcdef") != "") {
return nil, errors.New("api: shared sha256 must be 64 lowercase hex characters")
return sharedConfig{}, errors.New("api: shared sha256 must be 64 lowercase hex characters")
}
path := r.Path
if !filepath.IsAbs(path) {
@@ -72,22 +73,22 @@ func readSharedRoots(root string) (map[string]SharedRoot, error) {
}
r.Path, err = filepath.EvalSymlinks(path)
if err != nil {
return nil, err
return sharedConfig{}, err
}
r.Path, err = filepath.Abs(r.Path)
if err != nil {
return nil, err
return sharedConfig{}, err
}
stat, err := os.Stat(r.Path)
if err != nil {
return nil, err
return sharedConfig{}, err
}
if !stat.IsDir() {
return nil, errors.New("api: shared root must be a directory")
return sharedConfig{}, errors.New("api: shared root must be a directory")
}
cfg.Roots[name] = r
}
return cfg.Roots, nil
return cfg, nil
}
func sourceWithin(root, name string) (string, error) {
real, err := filepath.EvalSymlinks(name)
@@ -122,7 +123,7 @@ func sourceMetadata(spec *Spec) map[string]any {
return map[string]any{"files": hashes, "shared": shared}
}
func sharedSeeds(spec *Spec, directory string) ([]byte, error) {
cfg := sharedConfig{Roots: map[string]SharedRoot{}}
cfg := sharedConfig{Roots: map[string]SharedRoot{}, Errors: spec.DocumentErrors}
for name, r := range spec.SharedRoots {
rel, err := filepath.Rel(filepath.Join(directory, "api"), r.Path)
if err != nil {

View File

@@ -0,0 +1,54 @@
package main
import (
"context"
"encoding/json"
"example.com/arrays/internal/config"
"example.com/arrays/internal/handler"
"example.com/arrays/internal/svc"
"git.yuntuo.hk/k2safe/go-omni/logx"
"io"
"net/http/httptest"
"strings"
"testing"
)
func TestArrayWire(t *testing.T) {
cfg := config.Default()
cfg.Server.Log.Writer = io.Discard
logger, err := logx.New(cfg.Server.Log)
if err != nil {
t.Fatal(err)
}
defer logger.Close()
deps, err := svc.New(context.Background(), logger, cfg)
if err != nil {
t.Fatal(err)
}
defer deps.Close()
app, err := handler.NewApp(deps)
if err != nil {
t.Fatal(err)
}
defer app.Close()
for path, want := range map[string]string{
"/strings": `["企业","备注"]`, "/integers": `[9223372036854775807,-3]`, "/dynamic": `["text",7,true,null,{"name":"企业"},[false,2]]`, "/empty": `[]`, "/raw": `[true,false]`,
} {
w := httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest("GET", path, nil))
got := strings.TrimSpace(w.Body.String())
if w.Code != 200 {
t.Fatal(got)
}
if path != "/raw" {
var obj map[string]json.RawMessage
if err := json.Unmarshal(w.Body.Bytes(), &obj); err != nil {
t.Fatal(err)
}
got = string(obj["data"])
}
if got != want {
t.Fatalf("%s got %s want %s", path, got, want)
}
}
}

View File

@@ -0,0 +1,13 @@
package arrays
import "context"
func (s *Logic) Strings(context.Context) ([]string, error) { return []string{"企业", "备注"}, nil }
func (s *Logic) Integers(context.Context) ([]int64, error) {
return []int64{9223372036854775807, -3}, nil
}
func (s *Logic) Dynamic(context.Context) ([]any, error) {
return []any{"text", 7, true, nil, map[string]any{"name": "企业"}, []any{false, 2}}, nil
}
func (s *Logic) Empty(context.Context) ([]any, error) { return nil, nil }
func (s *Logic) Raw(context.Context) ([]bool, error) { return []bool{true, false}, nil }

View File

@@ -16,7 +16,8 @@ type WorkspaceService struct {
Module string `json:"module,omitempty"`
}
type WorkspaceConfig struct {
Services []WorkspaceService `json:"services"`
Services []WorkspaceService `json:"services"`
ErrorCatalogs []WorkspaceCatalog `json:"errorCatalogs,omitempty"`
}
// Workspace operates independent Go modules listed in an explicit local file.
@@ -83,6 +84,16 @@ func Workspace(path, command string, w io.Writer) error {
}
options = append(options, Options{API: entry, Dir: dir, Module: service.Module, DryRun: true})
}
var catalogs map[string][]byte
if command != "format" {
catalogs, err = prepareWorkspaceCatalogs(root, cfg.ErrorCatalogs, command == "check")
if err != nil {
return err
}
for i := range options {
options[i].documentCatalogs = catalogs
}
}
var failures error
for _, o := range options {
if command == "format" {
@@ -101,6 +112,13 @@ func Workspace(path, command string, w io.Writer) error {
if failures != nil {
return failures
}
if command == "generate" {
for path, data := range catalogs {
if err = writeWorkspaceCatalog(path, data); err != nil {
return err
}
}
}
for _, o := range options {
switch command {
case "format":

View File

@@ -0,0 +1,217 @@
package apigen
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
gotoken "go/token"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
)
// WorkspaceCatalog is an explicitly trusted Go provider: func() errors.Catalog.
// Only workspace generate/check execute providers. Parsing an .api never does.
type WorkspaceCatalog struct {
Dir string `json:"dir"`
Package string `json:"package"`
Function string `json:"function"`
Output string `json:"output"`
}
type limitedCatalogBuffer struct {
bytes.Buffer
limit int
}
func (b *limitedCatalogBuffer) Write(p []byte) (int, error) {
if len(p) > b.limit-b.Len() {
return 0, errors.New("catalog provider output exceeds limit")
}
return b.Buffer.Write(p)
}
func exportWorkspaceCatalog(root string, cfg WorkspaceCatalog) ([]byte, error) {
if cfg.Dir == "" || cfg.Output == "" || !gotoken.IsIdentifier(cfg.Function) || !gotoken.IsExported(cfg.Function) || module.CheckImportPath(cfg.Package) != nil {
return nil, errors.New("workspace catalog requires dir, an importable package, an exported function and output")
}
if err := safePath(root, filepath.ToSlash(filepath.Join(cfg.Dir, ".goOmniExportCheck"))); err != nil {
return nil, err
}
if err := safePath(root, cfg.Output); err != nil {
return nil, err
}
if filepath.Ext(cfg.Output) != ".json" {
return nil, errors.New("workspace catalog output must be .json")
}
dir := filepath.Join(root, filepath.FromSlash(cfg.Dir))
if stat, err := os.Stat(filepath.Join(dir, "go.mod")); err != nil || !stat.Mode().IsRegular() {
return nil, errors.New("workspace catalog dir must contain go.mod")
}
temp, err := os.MkdirTemp("", "go-omni-catalog-*")
if err != nil {
return nil, err
}
defer os.RemoveAll(temp)
// Overlay a virtual Go file under the provider module. This allows internal
// packages without creating files or changing go.mod/go.sum in that module.
virtual := filepath.Join(dir, ".goOmniCatalogExport", "main.go")
if _, err := os.Lstat(filepath.Dir(virtual)); !errors.Is(err, os.ErrNotExist) {
return nil, errors.New("reserved catalog export path already exists")
}
source := fmt.Sprintf("package main\nimport (\"os\"; provider %q; omnierrors %q)\nfunc main(){if err:=omnierrors.ExportCatalog(os.Stdout,provider.%s());err!=nil{os.Exit(1)}}\n", cfg.Package, frameworkModule+"/errors", cfg.Function)
backing := filepath.Join(temp, "main.go")
if err = os.WriteFile(backing, []byte(source), 0600); err != nil {
return nil, err
}
overlay, _ := json.Marshal(map[string]any{"Replace": map[string]string{virtual: backing}})
overlayPath := filepath.Join(temp, "overlay.json")
if err = os.WriteFile(overlayPath, overlay, 0600); err != nil {
return nil, err
}
// Keep all module metadata writes in the temporary directory as well.
// -mod=readonly alone must not be relied on to protect every go.sum update.
moduleData, err := os.ReadFile(filepath.Join(dir, "go.mod"))
if err != nil {
return nil, err
}
mf, err := modfile.Parse("go.mod", moduleData, nil)
if err != nil {
return nil, errors.New("workspace catalog has invalid go.mod")
}
replacements := append([]*modfile.Replace(nil), mf.Replace...)
for _, r := range replacements {
if r.New.Version == "" && !filepath.IsAbs(r.New.Path) {
if err = mf.AddReplace(r.Old.Path, r.Old.Version, filepath.Join(dir, r.New.Path), ""); err != nil {
return nil, err
}
}
}
moduleData, err = mf.Format()
if err != nil {
return nil, err
}
modPath := filepath.Join(temp, "go.mod")
if err = os.WriteFile(modPath, moduleData, 0600); err != nil {
return nil, err
}
if sums, e := os.ReadFile(filepath.Join(dir, "go.sum")); e == nil {
if err = os.WriteFile(filepath.Join(temp, "go.sum"), sums, 0600); err != nil {
return nil, err
}
} else if !errors.Is(e, os.ErrNotExist) {
return nil, e
}
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
defer cancel()
executable := filepath.Join(temp, "catalogExport")
// Build first, then execute the binary directly: cancellation kills the
// provider itself instead of only terminating a go-run wrapper process.
build := exec.CommandContext(ctx, "go", "build", "-mod=readonly", "-modfile", modPath, "-overlay", overlayPath, "-o", executable, virtual)
build.Dir = dir
build.Env = append(os.Environ(), "GOWORK=off")
build.WaitDelay = 2 * time.Second
build.Stdout = &limitedCatalogBuffer{limit: 64 << 10}
build.Stderr = &limitedCatalogBuffer{limit: 64 << 10}
if err = build.Run(); err != nil {
return nil, fmt.Errorf("workspace catalog %s.%s: build failed (%v); check module dependencies and func() errors.Catalog signature", cfg.Package, cfg.Function, err)
}
cmd := exec.CommandContext(ctx, executable)
cmd.Dir = dir
cmd.Env = append(os.Environ(), "GOWORK=off")
cmd.WaitDelay = 2 * time.Second
stdout := &limitedCatalogBuffer{limit: 1 << 20}
stderr := &limitedCatalogBuffer{limit: 64 << 10}
cmd.Stdout = stdout
cmd.Stderr = stderr
if err = cmd.Run(); err != nil {
if ctx.Err() != nil {
return nil, fmt.Errorf("workspace catalog %s: %w", cfg.Package, ctx.Err())
}
// Provider output can contain arbitrary application data. Do not echo it.
return nil, fmt.Errorf("workspace catalog %s.%s: execution failed (%v); provider must return errors.Catalog without writing stdout", cfg.Package, cfg.Function, err)
}
var catalog apierrors.Catalog
if err = apierrors.DecodeCatalogJSON(stdout.Bytes(), &catalog); err != nil {
return nil, errors.New("workspace catalog provider did not emit a valid catalog")
}
var normalized bytes.Buffer
if err = apierrors.ExportCatalog(&normalized, catalog); err != nil {
return nil, err
}
return normalized.Bytes(), nil
}
func prepareWorkspaceCatalogs(root string, configs []WorkspaceCatalog, check bool) (map[string][]byte, error) {
if len(configs) > 16 {
return nil, errors.New("workspace supports at most 16 catalog providers")
}
result := map[string][]byte{}
for _, cfg := range configs {
if err := safePath(root, cfg.Output); err != nil {
return nil, err
}
path := filepath.Join(root, filepath.FromSlash(cfg.Output))
for prior := range result {
if strings.EqualFold(prior, path) {
return nil, errors.New("workspace catalog outputs overlap")
}
}
data, err := exportWorkspaceCatalog(root, cfg)
if err != nil {
return nil, err
}
if check {
old, err := os.ReadFile(path)
if err != nil || !bytes.Equal(old, data) {
return nil, fmt.Errorf("workspace catalog drift: %s; run workspace generate", cfg.Output)
}
}
result[path] = data
}
return result, nil
}
func writeWorkspaceCatalog(path string, data []byte) error {
mode := os.FileMode(0644)
if stat, err := os.Lstat(path); err == nil {
if !stat.Mode().IsRegular() {
return errors.New("workspace catalog output must be a regular file")
}
mode = stat.Mode().Perm()
} else if !errors.Is(err, os.ErrNotExist) {
return err
}
old, err := os.ReadFile(path)
if err == nil && bytes.Equal(old, data) {
return nil
}
if err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
f, err := os.CreateTemp(filepath.Dir(path), ".omni-catalog-*")
if err != nil {
return err
}
defer os.Remove(f.Name())
if err = f.Chmod(mode); err != nil {
_ = f.Close()
return err
}
_, writeErr := io.Copy(f, bytes.NewReader(data))
err = errors.Join(writeErr, f.Close())
if err != nil {
return err
}
return os.Rename(f.Name(), path)
}

View File

@@ -0,0 +1,168 @@
package apigen
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
)
func TestWorkspaceSharedGoCatalog(t *testing.T) {
root := t.TempDir()
writeFile(t, filepath.Join(root, "go.mod"), fmt.Sprintf("module example.com/catalogs\n\ngo 1.24.0\nrequire %s v0.15.0-preview\nreplace %s => %s\n", frameworkModule, frameworkModule, strconvQuote(frameworkRoot(t))))
writeFile(t, filepath.Join(root, "internal/apierrors/catalog.go"), `package apierrors
import errors "git.yuntuo.hk/k2safe/go-omni/errors"
func Catalog() errors.Catalog {return errors.Catalog{DefaultStatus:400,Definitions:map[string]errors.Definition{
"INVALID":{Code:"INVALID",Message:"invalid input"},"MISSING":{Status:404,Code:"MISSING",Message:"missing"},
}}}
`)
if err := os.MkdirAll(filepath.Join(root, "contracts"), 0755); err != nil {
t.Fatal(err)
}
for _, name := range []string{"api", "id"} {
dir := filepath.Join(root, "services", name)
if _, err := New(Options{Dir: dir, Module: "example.com/" + name, FrameworkDir: frameworkRoot(t)}); err != nil {
t.Fatal(err)
}
writeFile(t, filepath.Join(dir, "api/omni.imports.json"), `{"roots":{"common":{"path":"../../../contracts"}},"errors":{"source":"@common/errors.json","include":["INVALID"],"routes":{"GET /api/greet/:name":["MISSING"]}}}`)
}
workspace := filepath.Join(root, "omni.workspace.json")
writeFile(t, workspace, `{"errorCatalogs":[{"dir":".","package":"example.com/catalogs/internal/apierrors","function":"Catalog","output":"contracts/errors.json"}],"services":[{"dir":"services/api"},{"dir":"services/id"}]}`)
if err := Workspace(workspace, "generate", io.Discard); err != nil {
t.Fatal(err)
}
if err := Workspace(workspace, "format", io.Discard); err != nil {
t.Fatal(err)
}
if err := Workspace(workspace, "generate", io.Discard); err != nil {
t.Fatal(err)
}
before := snapshot(t, root)
if err := Workspace(workspace, "check", io.Discard); err != nil {
t.Fatal(err)
}
if !sameFiles(before, snapshot(t, root)) {
t.Fatal("check wrote project files")
}
for _, name := range []string{"api", "id"} {
entry := filepath.Join(root, "services", name, "api/service.api")
spec, err := Parse(entry)
if err != nil {
t.Fatal(err)
}
c, err := Compile(spec)
if err != nil {
t.Fatal(err)
}
op := operation(t, document(t, c), "/api/greet/{name}", "get")
if op["responses"].(map[string]any)["404"] == nil || len(spec.Errors) != 0 || len(spec.Routes[0].Errors) != 0 {
t.Fatal("selection changed runtime catalog")
}
if _, err = os.Stat(filepath.Join(filepath.Dir(entry), catalogDocumentFile)); !os.IsNotExist(err) {
t.Fatal("shared catalog was copied")
}
}
shared := filepath.Join(root, "contracts/errors.json")
data := readFile(t, shared)
writeFile(t, shared, strings.Replace(data, "invalid input", "stale", 1))
before = snapshot(t, root)
if err := Workspace(workspace, "check", io.Discard); err == nil || !strings.Contains(err.Error(), "catalog drift") {
t.Fatal(err)
}
if !sameFiles(before, snapshot(t, root)) {
t.Fatal("drift check wrote files")
}
if err := Workspace(workspace, "generate", io.Discard); err != nil {
t.Fatal(err)
}
if got := readFile(t, shared); got != data {
t.Fatal("catalog not refreshed")
}
// Failed provider/preflight may not update even the shared metadata.
source := filepath.Join(root, "internal/apierrors/catalog.go")
writeFile(t, source, readFile(t, source)+"\ninvalid go source")
before = snapshot(t, root)
if err := Workspace(workspace, "generate", io.Discard); err == nil {
t.Fatal("provider failure ignored")
}
if !sameFiles(before, snapshot(t, root)) {
t.Fatal("provider failure mutated project")
}
}
func TestDocumentSelectionBoundaries(t *testing.T) {
root := t.TempDir()
local := filepath.Join(root, "api")
shared := filepath.Join(root, "shared")
entry := filepath.Join(local, "service.api")
writeFile(t, entry, "service demo {\n@handler list\nget /list returns ([]string)\n}\n")
writeFile(t, filepath.Join(shared, "errors.json"), `{"defaultStatus":400,"definitions":{"INVALID":{"code":400002,"message":"invalid"}}}`)
for _, cfg := range []string{
`{"source":"@common/errors.json","include":["UNKNOWN"]}`,
`{"source":"@common/errors.json","routes":{"GET /absent":["INVALID"]}}`,
`{"source":"@common/../api/service.api"}`,
`{"source":"../shared/errors.json"}`,
} {
writeFile(t, filepath.Join(local, importsFile), `{"roots":{"common":{"path":"../shared"}},"errors":`+cfg+`}`)
if _, err := Parse(entry); err == nil {
t.Fatal("bad selection accepted", cfg)
}
}
writeFile(t, filepath.Join(local, importsFile), `{"roots":{"common":{"path":"../shared"}},"errors":{"source":"@common/errors.json","include":[]}}`)
spec, err := Parse(entry)
if err != nil {
t.Fatal(err)
}
if len(documentRouteCatalog(spec, spec.Routes[0]).Definitions) != 0 {
t.Fatal("empty include ignored")
}
seeded, err := sharedSeeds(spec, filepath.Join(root, "generated"))
if err != nil {
t.Fatal(err)
}
var cfg sharedConfig
if err = json.Unmarshal(seeded, &cfg); err != nil {
t.Fatal(err)
}
if cfg.Errors.Include == nil || !bytes.Contains(seeded, []byte(`"include": []`)) {
t.Fatal("empty selection lost during relocation")
}
}
func TestWorkspaceCatalogPathsAndProviderValidation(t *testing.T) {
root := t.TempDir()
writeFile(t, filepath.Join(root, "go.mod"), "module example.com/test\ngo 1.24.0\n")
good := WorkspaceCatalog{Dir: ".", Package: "example.com/test/internal/errors", Function: "Catalog", Output: "errors.json"}
cases := []WorkspaceCatalog{}
for _, change := range []func(*WorkspaceCatalog){
func(c *WorkspaceCatalog) { c.Dir = "../outside" },
func(c *WorkspaceCatalog) { c.Output = "../outside.json" },
func(c *WorkspaceCatalog) { c.Package = "example.com/pkg;bad" },
func(c *WorkspaceCatalog) { c.Function = "Catalog()" },
func(c *WorkspaceCatalog) { c.Output = "go.mod" },
} {
c := good
change(&c)
cases = append(cases, c)
}
for _, c := range cases {
if _, err := exportWorkspaceCatalog(root, c); err == nil {
t.Fatal("bad provider accepted", c)
}
}
outside := filepath.Join(t.TempDir(), "errors.json")
writeFile(t, outside, `{}`)
if err := os.Symlink(outside, filepath.Join(root, "errors.json")); err != nil {
t.Fatal(err)
}
if _, err := prepareWorkspaceCatalogs(root, []WorkspaceCatalog{good}, false); err == nil {
t.Fatal("symlink output accepted")
}
if readFile(t, outside) != `{}` {
t.Fatal("outside file modified")
}
}

View File

@@ -8,7 +8,7 @@ import (
)
func TestNewWithRemoteFrameworkVersion(t *testing.T) {
for _, flag := range [][]string{{"--framework-version", "v0.15.0-preview"}, {"--framework-version=v0.15.0-preview"}} {
for _, flag := range [][]string{{"--framework-version", "v0.16.0-preview"}, {"--framework-version=v0.16.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.15.0-preview") || strings.Contains(string(content), "replace ") {
if !strings.Contains(string(content), "go-omni v0.16.0-preview") || strings.Contains(string(content), "replace ") {
t.Fatalf("CLI did not pin remote dependency: %s", content)
}
}

View File

@@ -1,7 +1,7 @@
package settings
import (
"os"
"strings"
{{if and (not .API) (ne .Storage "none")}}"strings"{{end}}
"time"
"git.yuntuo.hk/k2safe/go-omni/config"
"git.yuntuo.hk/k2safe/go-omni/logx"
@@ -41,10 +41,10 @@ func Load(path string)(Config,error){
cfg:=Default();var err error
cfg,err=config.ApplyDefaults(cfg);if err!=nil{return Config{},err}
if path!="" {cfg,err=config.DecodeFile(path,cfg);if err!=nil{return Config{},err}}
cfg,err=config.OverlayEnv(cfg,config.EnvOptions{Prefix:"GO_OMNI_"});if err!=nil{return Config{},err}
// Preserve the documented framework aliases (HTTP_ADDR, LOG_LEVEL, etc).
cfg.Server,err=config.ApplyEnv(cfg.Server,"");if err!=nil{return Config{},err}
if output:=strings.TrimSpace(os.Getenv("GO_OMNI_LOG_OUTPUT"));output!="" && strings.TrimSpace(os.Getenv("GO_OMNI_LOG_FILE"))=="" {cfg.Server.Log.File="";cfg.Server.Log.Rotation=logx.RotationConfig{}}
cfg,err=config.OverlayEnv(cfg,config.EnvOptions{Prefix:"GO_OMNI_",Empty:"ignore",Aliases:config.ServerEnvAliases("GO_OMNI_","server")});if err!=nil{return Config{},err}
output,outputSet:=os.LookupEnv("GO_OMNI_SERVER_LOG_OUTPUT");if !outputSet {output=os.Getenv("GO_OMNI_LOG_OUTPUT")}
file,fileSet:=os.LookupEnv("GO_OMNI_SERVER_LOG_FILE");if !fileSet {file=os.Getenv("GO_OMNI_LOG_FILE")}
if output!="" && file=="" {cfg.Server.Log.File="";cfg.Server.Log.Rotation=logx.RotationConfig{}}
return cfg,cfg.Validate()
}
func(c Config)Validate()error{

View File

@@ -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.15.0-preview"
const defaultFrameworkVersion = "v0.16.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.