Duffel.Schema (Duffel v0.1.0)

Copy Markdown View Source

Opt-in typed views over Duffel API responses.

Resource functions return raw string-keyed maps — that is the default and it does not change. When you want a struct with named fields instead, pass the map to the matching schema's from_map/1:

{:ok, order} = Duffel.Orders.get(client, "ord_123")
order = Duffel.Schema.Order.from_map(order)

order.booking_reference
#=> "RZPNX8"

Nested resources that have their own schema are decoded too, so order.slices is a list of Duffel.Schema.Slice structs and each slice.segments is a list of Duffel.Schema.Segment structs.

Fields without a dedicated schema (an offer's owner airline, a place, a payment requirement) are left as raw maps. Decoding is shallow and total: unknown keys are dropped, missing keys become nil, missing lists become [], and a missing nested object stays nil.

Decoding is also safe to repeat — handing from_map/1 something it has already decoded gives the same struct back, so mapping over a list twice costs nothing and raises nothing.

Schemas cover three areas:

To decode a page of results, map over its data:

{:ok, page} = Duffel.Orders.list(client)
orders = Enum.map(page.data, &Duffel.Schema.Order.from_map/1)

Summary

Functions

Decodes a single raw map into a struct of module.

Decodes a list of raw maps into structs of module.

Functions

cast(map, module)

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

Decodes a single raw map into a struct of module.

Returns nil when given nil, so a missing nested field stays nil rather than becoming an empty struct.

cast_list(list, module)

@spec cast_list([map()] | nil, module()) :: [struct()]

Decodes a list of raw maps into structs of module.

Returns [] when given nil, so a missing list field decodes to an empty list rather than nil.