Durable staging handoff, transactional outbox, at-least-once worker with lease fencing, idempotent versioned pipeline, three variants, operator tooling, contract tests and evidence. Includes the review fixes: reprocess source selection and failure settlement, replay guards, stale-job retirement, PNG structural/raster validation, migration rollback safety and audit events.
178 lines
4.3 KiB
Go
178 lines
4.3 KiB
Go
// Command specgen produces the OpenAPI document used for code generation.
|
|
//
|
|
// The mandatory contract (contracts/openapi.json, version 1.5.0) describes the
|
|
// cross-field rule "text is non-empty OR attachment_ids is non-empty" with an
|
|
// anyOf. ogen v1.24 cannot generate Go types for two unnamed anyOf branches
|
|
// with the same name; ignoring the limitation silently drops entire operations.
|
|
// This tool removes exactly those two anyOf constraints from a derived document
|
|
// and fails on any other union, so a contract change cannot silently weaken
|
|
// code generation. The canonical contract is never modified; the application
|
|
// enforces the cross-field rule itself.
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
)
|
|
|
|
const (
|
|
contractPath = "contracts/openapi.json"
|
|
labPath = "lab.json"
|
|
outputPath = ".cache/openapi.codegen.json"
|
|
)
|
|
|
|
var rewriteTargets = [][]string{
|
|
{"components", "schemas", "Message"},
|
|
{"components", "schemas", "CreateMessage"},
|
|
}
|
|
|
|
func main() {
|
|
check := flag.Bool("check", false, "verify that the derived document matches the generated file")
|
|
flag.Parse()
|
|
|
|
derived, err := derive()
|
|
if err != nil {
|
|
fail(err)
|
|
}
|
|
|
|
if *check {
|
|
current, err := os.ReadFile(outputPath)
|
|
if err != nil {
|
|
fail(fmt.Errorf("read %s (run make generate-api first): %w", outputPath, err))
|
|
}
|
|
|
|
if !bytes.Equal(current, derived) {
|
|
fail(fmt.Errorf("%s is stale: run make generate-api", outputPath))
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
if err := os.MkdirAll(filepath.Dir(outputPath), 0o750); err != nil {
|
|
fail(fmt.Errorf("create cache directory: %w", err))
|
|
}
|
|
|
|
if err := os.WriteFile(outputPath, derived, 0o600); err != nil {
|
|
fail(fmt.Errorf("write %s: %w", outputPath, err))
|
|
}
|
|
}
|
|
|
|
func derive() ([]byte, error) {
|
|
lab, err := readObject(labPath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
spec, err := readObject(contractPath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
contractVersion, _ := lab["contract_version"].(string)
|
|
if contractVersion == "" {
|
|
return nil, fmt.Errorf("%s: contract_version is missing", labPath)
|
|
}
|
|
|
|
info, _ := spec["info"].(map[string]any)
|
|
version, _ := info["version"].(string)
|
|
if version != contractVersion {
|
|
return nil, fmt.Errorf(
|
|
"contract version mismatch: %s has %q, %s has %q",
|
|
labPath, contractVersion, contractPath, version,
|
|
)
|
|
}
|
|
|
|
number, _ := lab["number"].(float64)
|
|
if number != 5 {
|
|
return nil, fmt.Errorf("%s: expected lab number 5, got %v", labPath, lab["number"])
|
|
}
|
|
|
|
for _, path := range rewriteTargets {
|
|
schema, err := lookup(spec, path...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if _, ok := schema["anyOf"]; !ok {
|
|
return nil, fmt.Errorf("%s: anyOf is missing; re-check the codegen workaround", filepath.Join(path...))
|
|
}
|
|
|
|
delete(schema, "anyOf")
|
|
}
|
|
|
|
if err := rejectRemainingUnions(spec, nil); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
encoded, err := json.MarshalIndent(spec, "", " ")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("encode derived document: %w", err)
|
|
}
|
|
|
|
return append(encoded, '\n'), nil
|
|
}
|
|
|
|
func readObject(path string) (map[string]any, error) {
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read %s: %w", path, err)
|
|
}
|
|
|
|
var object map[string]any
|
|
if err := json.Unmarshal(raw, &object); err != nil {
|
|
return nil, fmt.Errorf("parse %s: %w", path, err)
|
|
}
|
|
|
|
return object, nil
|
|
}
|
|
|
|
func lookup(root map[string]any, path ...string) (map[string]any, error) {
|
|
current := root
|
|
|
|
for _, key := range path {
|
|
next, ok := current[key].(map[string]any)
|
|
if !ok {
|
|
return nil, fmt.Errorf("%s: object is missing", filepath.Join(path...))
|
|
}
|
|
|
|
current = next
|
|
}
|
|
|
|
return current, nil
|
|
}
|
|
|
|
func rejectRemainingUnions(value any, path []string) error {
|
|
switch typed := value.(type) {
|
|
case map[string]any:
|
|
for key, child := range typed {
|
|
if key == "anyOf" || key == "oneOf" {
|
|
return errors.New("unsupported union at " + filepath.Join(append(path, key)...) +
|
|
": extend the codegen workaround explicitly")
|
|
}
|
|
|
|
if err := rejectRemainingUnions(child, append(path, key)); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
case []any:
|
|
for index, child := range typed {
|
|
if err := rejectRemainingUnions(child, append(path, strconv.Itoa(index))); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func fail(err error) {
|
|
fmt.Fprintln(os.Stderr, "specgen:", err)
|
|
os.Exit(1)
|
|
}
|