Picnic.Decode (Picnic v0.1.0)

Copy Markdown View Source

Lenient decoding of API payloads.

The Picnic API is unofficial and its payloads drift. The decoder is built so that drift degrades gracefully instead of breaking callers:

  • By default responses stay plain maps — that always works.
  • Typed structs (Picnic.Schema.*) are an opt-in convenience: known fields are mapped in, unknown fields are ignored, missing fields become nil, and the complete original payload is stashed under :raw so nothing is ever lost.
  • A field with a surprising type is not an error: the value is kept as-is, a [:picnic, :schema, :drift] telemetry event is emitted, and a Logger.warning is logged. Callers who need a field the struct doesn't model can always reach into :raw.
  • Only a payload whose overall shape can't be reconciled with the struct at all (e.g. a bare string where an object was expected) becomes {:error, %Picnic.Error{category: :schema}}.

Summary

Functions

Optionally casts a Picnic.HTTP.request/4 result into a schema struct.

Casts one JSON object into schema's struct, leniently.

Functions

as(result, schema \\ nil)

@spec as(Picnic.HTTP.result(), module() | nil) :: Picnic.HTTP.result()

Optionally casts a Picnic.HTTP.request/4 result into a schema struct.

With schema set to nil the result passes through untouched (plain map). With a Picnic.Schema module, maps are cast into the struct and lists are cast element-wise. Errors pass through untouched.

cast(body, schema)

@spec cast(map(), module()) :: struct()

Casts one JSON object into schema's struct, leniently.

Never raises on payload contents: unknown fields are dropped, missing ones are nil, mistyped ones are kept verbatim (with a drift warning), and the whole original ends up under :raw.