xmachines-codec-avro
Avro for xmachines-rs machines, an xmachines-codec codec named avro: input events, emitted events and snapshots in the Schema Registry wire format, a zero byte (MAGIC, which is also the format byte of its snapshot records), the schema id (four bytes, big-endian), then the Avro binary datum.
When to use it
Use it when the records of a machine's topics are governed by a Schema Registry (Confluent's, or Redpanda's built-in one), so that producers and consumers in other languages read and write them with the registered schemas, and evolve them compatibly.
AvroSchemas generates the schemas from a bundle's JSON Schemas with the rules of the protobuf codec's event messages, and registers them under subjects of a registry::Registry (the Confluent REST API with the feature registry-http, or registry::MemoryRegistry):
- Input events (
schemas.events) and emitted events (schemas.emitted) are each a union of records, one per event type in schema order, namedxmachines.events.<machine>.<EventType>/xmachines.emitted.<machine>.<EventType>; the record name is the event type (typeis not a field). Producers using a single record schema per type (record-name subjects) work too. - Objects with properties are records (required properties are plain fields, optional ones
["null", T]defaulting to null), string enums are enums (when every value is a valid Avro name), integerslong, numbersdouble, arraysarray, records (additionalProperties)map, discriminated unions (oneOfobjects sharing aconstkey) a union of records without the key, and anything else (free-form JSON) a string holding the JSON text (marked"xstate.json": true). - Snapshots (XState's
SystemSnapshot) use a fixed schema (xmachines.snapshot.v1), the root machine's context typed byschemas.contextwhen there is one. Every field is optional, and keys or values a record does not describe go to itsextralist (key and JSON text), so every snapshot round-trips; ordered maps (actors, children, registry) are arrays of{ key, value }.
Records are read with schema resolution: a datum written with any registered schema (found by its id) is resolved against the generated one, so producers can evolve their schemas compatibly. Records with the generated schemas' own ids are written and read by a direct binary codec driven by the schemas' shapes, byte-identical to apache-avro.
Example
use Arc;
use json;
use ;
let bundle = json!;
let mut schemas = from_bundle.unwrap;
// Subjects of the input events, emitted events and snapshots; the bundle has no emitted events.
schemas.register.unwrap;
let codec = codec;
let events = codec.events.unwrap;
let mut record = Vecnew;
events.encode.unwrap;
assert_eq!;
assert_eq!;
Features
registry-http:registry::HttpRegistry, a blocking client of the Schema Registry's REST API (ureqwith rustls), with basic authentication and a timeout. Off by default;registry::MemoryRegistryis always there, for tests and tools.
Pins
apache-avro 0.21 resolves other writers' schemas. It must be 0.20 or later: 0.19 turned on serde_json/arbitrary_precision for the whole build, which changes how every number serializes.
Related crates
xmachines-codec: the codec traits and their registry.xmachines-codec-protobuf: the same mapping rules for protobuf.xmachines-kafka: itsavrofeature registers this codec with the configuredschemaRegistry;xmachines-kafka avro events|emitted|snapshot <bundle>prints a schema.
License
MIT