optable

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package optable writes the operation table: every operation of the bundled specs with the chi route pattern the router serves it under and the classes it declares (x-cost, x-cost-expensive-query, x-idempotency, x-pagination, and whether it declares the shared IdempotencyKey). Admission (RATE-01) and the Idempotency-Key gate (IDEM-01) look a request up in it before routing, so the spec stays the one place the classes are declared.

Role

devtools layer: the library behind tools/optable, which make generate runs after the strict servers are generated; never linked into the server binary. It imports only the standard library and gopkg.in/yaml.v3.

Inputs

  • The bundled specs, api/<service>.openapi.yaml: each one's servers[0].url, the base path router.go mounts its generated server at, and every operation of its paths with its extensions and parameters.

Outputs

  • Load: one Row per operation, sorted by pattern and method.
  • ChiPattern: the pattern a generated chi server registers for a spec path under its base.
  • Render: the formatted Go source of the table, a var Table of operations.Operation literals that sets only the fields a spec declares.
  • Run: that source written to the given file, in the package its directory names, and the number of operations.
  • An error for a directory with no spec, a spec that does not parse, is not a mapping or has no server URL, two operations with one method and pattern, and a file that cannot be written.

Invariants

  • The table is generated and committed: make generate rewrites it, and make verify-generate fails when it differs from what the bundles give (decision 0002).
  • A pattern is the base path and oapi-codegen's own conversion of the spec path (SwaggerUriToChiUri), so it equals what the generated server registers; the operations package's tests compare the two for every generated operation.
  • IdempotencyKey is true only for a reference to the shared parameter, #/components/parameters/IdempotencyKey, on the operation or its path item.

References

  • Decision 0043, "Cost classes and how admission finds them" (the table and the chi lookup) and "Replay for keyed operations" (the keyed operations): docs/research/decisions/0043-api-reliability.md.
  • Decision 0002: docs/research/decisions/0002-spec-first-flat-crud-handwritten-item-paths.md.
  • https://github.com/weaveplatform/artifactweave/issues/1405

Status

Implemented #1405 (CON-02). RATE-01 (#1407) and IDEM-01 (#1406) read the table at run time.

Testing

  • TestUnitLoadAndRender: a fixture spec's operations become rows with their patterns, classes and key flag, sorted, and render as Go source that sets only the declared fields.
  • TestUnitRun: the table is written in the package of its directory.
  • TestUnitErrors: each error above.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChiPattern

func ChiPattern(base, path string) string

ChiPattern is the route pattern the generated chi server registers for a spec path under its server's base URL.

func Render

func Render(pkg string, rows []Row) ([]byte, error)

Render writes the table as the Go source of package pkg: a var Table of operations.Operation literals, formatted, with only the fields that are set, so a class that is still missing reads as absent rather than as an empty string.

func Run

func Run(apiDir, out string) (int, error)

Run loads apiDir and writes the table to out, in the package named by out's directory, and returns the number of operations written.

Types

type Row

type Row struct {
	Service        string
	Method         string
	Pattern        string
	OperationID    string
	Handwritten    bool
	Cost           string
	ExpensiveQuery []string
	IdempotencyKey bool
	Idempotency    string
	Pagination     string
}

Row is one operation of a bundled spec, as the generated table lists it.

func Load

func Load(apiDir string) ([]Row, error)

Load reads every api/<service>.openapi.yaml under apiDir and returns its operations, sorted by pattern and method.

Jump to

Keyboard shortcuts

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