Skip to content

Write an extractor

An extractor turns the code of a service into a spec sheet. It runs as a step of the service's own build, one per language and framework. valerify itself only reads documents, so any language can have an extractor, as long as what it writes is the format below. The exact definition is docs/format.md in the repository of the tool (where things are).

What a spec sheet is

One JSON document per service version, of the kind specSheet, with:

  • name and version: the name the service publishes its sheets under, and the version the sheet describes.
  • extractor: who wrote the sheet, by name and version. Findings about what could not be determined point to it.
  • offers: what the service provides: the documents it sends and reads, its endpoints, and the types they use, with every field's type, presence and whether it may be null.
  • needs: per upstream, what the service relies on. Each field it uses is expected (it must be there), accepted (used when it is there) or ignored (known, never read or sent). Fields a consumer ignores do not appear in its code, so the extractor has to be told which they are.

What an extractor has to do

  • Describe the wire, not the code. Ask the serializer what it writes and what it reads, with the configuration the project uses, instead of reading the type definitions and guessing. Where a serializer cannot describe itself, observe it with generated values.
  • Say what it cannot know. An aspect that cannot be determined is listed as undeterminable, with the reason. A wrong statement is worse than none: it defeats every check that follows.
  • Check its own claims. The parsers and serializers of a language differ in details, such as how a number or a missing field is read. An extractor verifies the behaviour it relies on instead of assuming it.
  • Write the document in the canonical form, strictly: a defined key order, no unknown keys, no empty collections, a boolean only when it is true. valerify validate rejects anything else, with its position.
  • Name itself. The extractor states its own identity in every sheet, filled in by the extractor, not by the project.

For Rust

The crate valerify-format has the document types, the strict reading, the validation and the canonical writer, so an extractor in Rust does not need to implement them. The first extractor, for serde, derives the wire form of a type from its #[serde] attributes with serde's own attribute parser and can check its statements against what serde does for a sample the project supplies. A project lists the documents of its API in a small helper crate of its own and writes the sheet into its build output.

Check yourself against valerify's format

valerify is the provider of its own format, and every extractor is a consumer of it: the tool generates the sheet of its document format from code, in its own pipeline, and an extractor checks the sheet it writes against that sheet, with valerify check, in its own pipeline. The first subject of the extractor for Rust is that format itself.