gen

package
v0.9.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 24, 2026 License: MIT Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package gen 实现 appkit gen 的四个代码生成器: contract(契约五件套:接口/DTO/wrapper/client/server + openapi 派生导出)、 events(事件类型)、errors(错误码)、wrap(契约 wrapper,ProvideContract 闭环)。

共同约定:生成物首行是标准 "Code generated" 头;输出前经 go/format 格式化; 同一输入重复生成逐字节稳定(golden 测试可校验,CI 用 git diff 防手改)。

Index

Constants

This section is empty.

Variables

View Source
var ErrContractDrift = errors.New("APPKIT_GEN_CONTRACT_DRIFT")

ErrContractDrift identifies generated files that do not match contract.yaml. The diagnostic code is stable for CLI and Agent consumers.

View Source
var ErrContractIncompatible = errors.New("APPKIT_CONTRACT_INCOMPATIBLE")

Functions

func CheckContract added in v0.9.0

func CheckContract(inPath, outDir string) error

CheckContract compares the exact generated bytes with outDir without creating, deleting, or changing any target files. This checks generated consistency, not semantic compatibility with a previous contract version.

func CheckContractCompatibility added in v0.9.0

func CheckContractCompatibility(base, candidate string) error

CheckContractCompatibility checks AppKit's generated contract model, not arbitrary OpenAPI or implementation semantics. Pass means the conservative rules below found no incompatible changes; consumer compilation/runtime tests remain required (for example, unkeyed Go struct literals are not modeled).

func CheckContractCompatibilitySources added in v0.9.0

func CheckContractCompatibilitySources(baseName string, base []byte, candidateName string, candidate []byte) error

CheckContractCompatibilitySources reads neither sourceName from disk. Existing methods, DTOs and fields must remain; method additions also fail because widening the shared Service interface breaks existing implementors. New DTOs and optional fields are permitted, except that adding the first request/response field changes a Go method signature and is rejected.

func Contract

func Contract(inPath, outDir string) error

Contract 读取契约 yaml,在 outDir 生成契约包的全套文件:

service.gen.go  Service 接口 + 传值 DTO(本文件渲染)
wrap.gen.go     进程内 wrapper(复用 Wrap 的接口校验与渲染链路)
client.gen.go   远程 client:同一接口,contract.Call + 幂等有界重试
server.gen.go   HTTP 暴露:与 client 互为镜像的编解码
openapi.yaml    派生导出(文档 / oasdiff 门禁;非事实源)

方法 HTTP 形态唯一:POST + JSON body,错误一律 problem+json—— 契约调用是 RPC 语义,client/server 两侧的编解码因此只有一份约定。

func Errors

func Errors(inPath, outPath string) error

Errors 读取错误码 yaml,生成 Code* 常量与 Err* sentinel。见 appkit gen errors。

func Events

func Events(inPath, outPath string) error

Events 读取事件 schema yaml,生成事件类型文件(struct + Topic 常量 + Event()/Parse* 方法)。见 appkit gen events。

func RenderContract added in v0.9.0

func RenderContract(inPath string) (map[string][]byte, error)

RenderContract reads and validates contract.yaml, returning every generated filename and its exact contents. It performs no writes and does not inspect an output directory. Callers can use the same rendering for generation, checking, or a reviewable workspace plan. Returned buffers belong to the caller.

func RenderContractSource added in v0.9.0

func RenderContractSource(sourceName string, data []byte) (map[string][]byte, error)

RenderContractSource renders exactly the supplied contract bytes without reading or writing files. sourceName is used only for validation diagnostics; it need not name an existing file. This lets a workspace plan render the same input snapshot whose digest it records, without reopening a mutable path.

func RenderErrorsSource added in v0.9.0

func RenderErrorsSource(sourceName string, data []byte) ([]byte, error)

RenderErrorsSource renders exactly the supplied codes.yaml snapshot. It performs no filesystem access; sourceName is used only for diagnostics. The formatted result belongs to the caller and does not alias data.

func RenderEventsSource added in v0.9.0

func RenderEventsSource(sourceName string, data []byte) ([]byte, error)

RenderEventsSource renders exactly the supplied events.yaml snapshot. It performs no filesystem access; sourceName is used only for diagnostics. The formatted result belongs to the caller and does not alias data.

func RenderWrapSources added in v0.9.0

func RenderWrapSources(files map[string][]byte, iface, system string) ([]byte, error)

RenderWrapSources renders a wrapper from a captured package-source snapshot without accessing the filesystem. Keys are diagnostic filenames; only .go files other than *_test.go are considered, in lexical filename order. As with Wrap, the first matching interface is used and later files are not parsed. Callers supply the files of one package, without traversing subdirectories. Neither the map nor its contents are modified; the result belongs to the caller.

func Wrap

func Wrap(srcDir, iface, system, outPath string) error

Wrap 解析 srcDir 包中的接口 iface,生成同包的契约 wrapper 文件: wrapped<Iface> 每个方法体经 contract.Call(事务守卫/ctx 防火墙/超时/错误规范化), Wrap<Iface> 供 appkit.ProvideContract 使用——裸实现进不了 registry。

Types

type ContractCompatibilityError added in v0.9.0

type ContractCompatibilityError struct {
	Issues []ContractCompatibilityIssue `json:"issues"`
}

func (*ContractCompatibilityError) Error added in v0.9.0

func (*ContractCompatibilityError) Unwrap added in v0.9.0

func (e *ContractCompatibilityError) Unwrap() error

type ContractCompatibilityIssue added in v0.9.0

type ContractCompatibilityIssue struct {
	Location string `json:"location"`
	Reason   string `json:"reason"`
}

type ContractDrift added in v0.9.0

type ContractDrift struct {
	Path   string
	Reason string
}

ContractDrift describes one generated filename. Reason is missing, stale, or not_regular; unrelated handwritten files are outside the generator's scope.

type ContractDriftError added in v0.9.0

type ContractDriftError struct {
	Files []ContractDrift
}

ContractDriftError reports every drifted generated file in filename order.

func (*ContractDriftError) Error added in v0.9.0

func (err *ContractDriftError) Error() string

func (*ContractDriftError) Unwrap added in v0.9.0

func (err *ContractDriftError) Unwrap() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL