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 ¶
- Variables
- func CheckContract(inPath, outDir string) error
- func CheckContractCompatibility(base, candidate string) error
- func CheckContractCompatibilitySources(baseName string, base []byte, candidateName string, candidate []byte) error
- func Contract(inPath, outDir string) error
- func Errors(inPath, outPath string) error
- func Events(inPath, outPath string) error
- func RenderContract(inPath string) (map[string][]byte, error)
- func RenderContractSource(sourceName string, data []byte) (map[string][]byte, error)
- func RenderErrorsSource(sourceName string, data []byte) ([]byte, error)
- func RenderEventsSource(sourceName string, data []byte) ([]byte, error)
- func RenderWrapSources(files map[string][]byte, iface, system string) ([]byte, error)
- func Wrap(srcDir, iface, system, outPath string) error
- type ContractCompatibilityError
- type ContractCompatibilityIssue
- type ContractDrift
- type ContractDriftError
Constants ¶
This section is empty.
Variables ¶
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.
var ErrContractIncompatible = errors.New("APPKIT_CONTRACT_INCOMPATIBLE")
Functions ¶
func CheckContract ¶ added in v0.9.0
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
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 ¶
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 Events ¶
Events 读取事件 schema yaml,生成事件类型文件(struct + Topic 常量 + Event()/Parse* 方法)。见 appkit gen events。
func RenderContract ¶ added in v0.9.0
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
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
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
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
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.
Types ¶
type ContractCompatibilityError ¶ added in v0.9.0
type ContractCompatibilityError struct {
Issues []ContractCompatibilityIssue `json:"issues"`
}
func (*ContractCompatibilityError) Error ¶ added in v0.9.0
func (e *ContractCompatibilityError) Error() string
func (*ContractCompatibilityError) Unwrap ¶ added in v0.9.0
func (e *ContractCompatibilityError) Unwrap() error
type ContractCompatibilityIssue ¶ added in v0.9.0
type ContractDrift ¶ added in v0.9.0
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