View Source Craftgate.Serializable (Craftgate v1.0.42)

The module that contains macros to declare typed structs that can be serialized and deserialized easily and in a recursive fashion, and has the necessary typespecs declared for developer assistance and compile-time type checking for Dialyzer.

Under the hood, uses Construct to declare the typed fields. See the Construct documentation to see how to specify the types.

To declare a struct, simply use the Craftgate.Serializable module and specify the list of fields as a keyword list where the keys represent the field names and values represent the types of those fields.

Make sure to specify the field names as :snake_case atoms to follow Elixir conventions and don't worry about serialization or deserialization as they will be converted into "camelCase" keys (or vice-versa) during conversion.

Example usage:

defmodule Craftgate.Request.Dto.FraudCheckParameters do
  use Craftgate.Serializable, [
    buyer_external_id: :string,
    buyer_phone_number: :string,
    buyer_email: :string
  ]
end

Link to this section Summary

Functions

Attempts to parse successful inputs as the given type as, or erronous inputs into an instance of Craftgate.Error and result as an OK or error tuple.

Bangified version of parse/2

Serializes the given input into an OK or error tuple containing a JSON string using Jason.encode/2

Bangified version of serialize/1

Link to this section Functions

@spec parse(any(), atom()) :: {:ok, any()} | {:error, any()}

Attempts to parse successful inputs as the given type as, or erronous inputs into an instance of Craftgate.Error and result as an OK or error tuple.

The behavior of this method will branch into several possibilities depending on the structure of the input, as well as the value of the as parameter. Below is a list of all possibilities in order of preference.

  • If input is an error tuple (e.g. {:error, ...}) it returns the tuple immediately
  • If input is an OK tuple (e.g. {:ok, ...}) it proceeds to parse the second element of the tuple
  • If input is an HTTPoison.Response instance with a status_code between 200 and 300, it proceeds to parse the body of the response object
  • If input is an HTTPoison.Response instance with a different status code, it attempts to parse "errors" property of the response data into an instance of Craftgate.Error
  • If as is :void, it returns {:ok, nil}
  • If as is String and the input is a string, it returns the input in an OK tuple (e.g. {:ok, input})
  • If input is a string, it parses the input as a map and tries to convert it into an instance of as
  • If input is a map with an "errors" element, it tries to parse the error data into an instance of Craftgate.Error
  • If input is a map with a "data" element, it tries to parse the "data" key into an instance of as, where:
    • If as is :map, it returns the data in an OK tuple (e.g. {:ok, data})
    • If as is an atom, it converts the keys of the map into :snake_case atoms, and attempts to convert the resulting map into an instance of as using Construct.Cast.make/3
@spec parse!(any(), atom()) :: any() | no_return()

Bangified version of parse/2

@spec serialize(any()) :: {:ok, binary()} | {:error, any()}

Serializes the given input into an OK or error tuple containing a JSON string using Jason.encode/2

@spec serialize!(any()) :: binary() | no_return()

Bangified version of serialize/1