forked from mtusi2026/mtusi2026lab7
145 lines
3.7 KiB
Go
145 lines
3.7 KiB
Go
// Command specgen derives the OpenAPI document used for code generation.
|
|
// The mandatory 1.7.0 contract is never edited. ogen v1.24 cannot generate the
|
|
// two unnamed anyOf branches expressing text OR attachment_ids; domain code
|
|
// enforces that cross-field rule. Any other unsupported union is a hard error.
|
|
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 derived generation input")
|
|
flag.Parse()
|
|
derived, err := derive()
|
|
if err != nil {
|
|
fail(err)
|
|
}
|
|
if *check {
|
|
current, err := os.ReadFile(outputPath)
|
|
if err != nil {
|
|
fail(fmt.Errorf("read codegen input: %w", 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 generation input: %w", 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: %q versus %q", contractVersion, version)
|
|
}
|
|
number, _ := lab["number"].(float64)
|
|
if number != 7 {
|
|
return nil, fmt.Errorf("%s: expected lab 7, 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: expected cross-field union is missing", filepath.Join(path...))
|
|
}
|
|
delete(schema, "anyOf")
|
|
}
|
|
if err := rejectRemainingUnions(spec, []string{}); err != nil {
|
|
return nil, err
|
|
}
|
|
encoded, err := json.MarshalIndent(spec, "", " ")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("encode generation input: %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)...))
|
|
}
|
|
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)
|
|
}
|