LINE.Bot.Deserializer (line_bot_sdk v0.2.0)

Copy Markdown

Helper functions for deserializing responses into models.

Summary

Functions

Decode a JSON response body into a struct.

Update the provided model with a deserialization of a nested value.

Evaluate the response from a Req request.

Build a struct of module from a raw string-keyed map.

Types

response_mapping()

@type response_mapping() :: [{status_code(), false | %{} | module()}]

status_code()

@type status_code() :: :default | 100..599

Functions

decode(response, module)

@spec decode(Req.Response.t() | map(), module() | false | %{}) ::
  struct() | map() | Req.Response.t()

Decode a JSON response body into a struct.

Parameters

  • response - The Req.Response struct or a map
  • module - The module to decode the response body into (or false to skip decoding)

Returns

The decoded struct, map, or response.

deserialize(model, field, atom, module)

@spec deserialize(
  struct(),
  atom(),
  :date | :datetime | :list | :map | :struct,
  module()
) :: struct()

Update the provided model with a deserialization of a nested value.

evaluate_response(error, mapping)

@spec evaluate_response(
  {:ok, Req.Response.t()} | {:error, term()},
  response_mapping()
) ::
  {:ok, struct() | Req.Response.t()} | {:error, Req.Response.t() | term()}

Evaluate the response from a Req request.

Parameters

  • result - The result from Req.request/2
  • mapping - A list of {status_code, module} tuples for response decoding

Returns

  • {:ok, struct} or {:ok, Req.Response.t()} on success
  • {:error, term} on failure

raw_to_struct(map, module)

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

Build a struct of module from a raw string-keyed map.

Used by generated decode/1 discriminator dispatch clause to construct a concrete struct without triggering discriminator re-dispatch.