package main import ( "fmt" "go/format" "sort" "strings" ) // contractMetadata derives consumer references and recovery helpers from the // same operations that generate the wire client. Audience is not authorization. func contractMetadata(document map[string]any) (map[string]string, error) { outputs := map[string]string{} var code strings.Builder code.WriteString("// Code generated by apigen. DO NOT EDIT.\npackage protocol\n\n") code.WriteString("type RecoveryPolicy string\nconst (\n RecoverySafeRead RecoveryPolicy = \"safe-read\"\n RecoveryRepeatIdenticalIntent RecoveryPolicy = \"repeat-identical-intent\"\n RecoveryRefreshBeforeEdit RecoveryPolicy = \"refresh-before-edit\"\n RecoveryNoAutomaticReplay RecoveryPolicy = \"no-automatic-replay\"\n)\n") paths := document["paths"].(map[string]any) names := make([]string, 0, len(paths)) for name := range paths { names = append(names, name) } sort.Strings(names) views := map[string]*strings.Builder{} for _, audience := range []string{"client", "extension", "internal-transfer"} { view := &strings.Builder{} fmt.Fprintf(view, "# %s API reference\n\nGenerated from [OpenAPI](openapi.yaml) by\n`go -C cli generate ./internal/daemon`. Audience grouping does not grant or\nrestrict access.\n\nSee [API workflows](http-api-workflows.md) for conditional edits and recovery.\n", strings.ReplaceAll(audience, "-", " ")) views[audience] = view } codes := map[string]bool{} code.WriteString("// OperationRecovery returns no automatic replay for unknown operations.\nfunc OperationRecovery(operation string) RecoveryPolicy { switch operation {\n") for _, path := range names { item := paths[path].(map[string]any) for _, method := range []string{"get", "put", "post", "patch", "delete"} { op, ok := item[method].(map[string]any) if !ok { continue } id, _ := op["operationId"].(string) audience, _ := op["x-albedo-audience"].(string) policy, _ := op["x-albedo-recovery"].(string) view, ok := views[audience] if !ok { return nil, fmt.Errorf("%s has no supported audience", id) } switch policy { case "safe-read", "repeat-identical-intent", "refresh-before-edit", "no-automatic-replay": default: return nil, fmt.Errorf("%s has no recovery policy", id) } fmt.Fprintf(&code, "case %q: return RecoveryPolicy(%q)\n", id, policy) fmt.Fprintf(view, "\n## %s\n\n`%s %s`\n\nRecovery: `%s`.\n", id, strings.ToUpper(method), path, policy) if problems, ok := op["x-albedo-problem-codes"].([]any); ok { view.WriteString("\nDocumented HTTP problem codes:\n\n") for _, problem := range problems { name := problem.(string) codes[name] = true fmt.Fprintf(view, "- `%s`\n", name) } } } } code.WriteString("default: return RecoveryNoAutomaticReplay\n}}\n\n// Problem constants are known values, not a closed validation enum.\nconst (\n") names = names[:0] for name := range codes { names = append(names, name) } sort.Strings(names) for _, name := range names { var symbol strings.Builder for _, part := range strings.Split(name, "_") { if part != "" { symbol.WriteString(strings.ToUpper(part[:1]) + part[1:]) } } fmt.Fprintf(&code, "Problem%s = %q\n", symbol.String(), name) } code.WriteString(")\n\n// Failed reports a domain failure after admission, independently of HTTP status.\nfunc (r CompactionResult) Failed() bool { return r.State == \"failed\" }\nfunc (r CompactionReceipt) Terminal() bool { return r.State == \"completed\" || r.State == \"interrupted\" }\n") formatted, err := format.Source([]byte(code.String())) if err != nil { return nil, err } outputs["cli/internal/daemon/protocol/recovery.gen.go"] = string(formatted) for audience, view := range views { outputs["docs/api-"+audience+".gen.md"] = view.String() } return outputs, nil }