Step 4: Auto-Detect and Route Formats
In a real-world system, you might need a single endpoint that can handle multiple formats. For example, perhaps you want to convert incoming JSON to Protobuf, but also be able to accept Protobuf and convert it back to JSON.
This step provides the JSON preparation mapping for that design. Encoding, decoding, and format routing require an adapter supported by the target Edge release.
The Goal
Prepare the sensor identifier, temperature, and Unix-millisecond timestamp that the adapter will encode. The snippet below covers only that preparation step.
The "Detect by Header -> Route" Pattern
For a complete pipeline, define how the HTTP input exposes Content-Type, then
use a switch processor to select the adapter's encode or decode path. The
preparation fragment below does not implement header detection or routing.
Implementation
-
Prepare the JSON branch: The current Edge runtime does not provide the Protobuf processors assumed by the original example. This partial snippet prepares the canonical object that a supported adapter must encode.
Partial snippet: prepare JSON for a Protobuf adapter- mapping: |
root = {
"sensor_id": this.sensor_id,
"temperature": this.temperature,
"timestamp_unix_ms": this.timestamp.ts_parse("2006-01-02T15:04:05Z07:00").ts_unix_milli()
} -
Add and test the adapter: Select an adapter that is actually supported in the target Edge release, then test both encode and decode paths with a checked-in schema and fixture bytes.
Verification
The snippet validates the preparation mapping only. Binary output, schema compatibility, and round-trip fidelity remain unproven until the selected adapter is run with fixture bytes.