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
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
inputis an error tuple (e.g.{:error, ...}) it returns the tuple immediately - If
inputis an OK tuple (e.g.{:ok, ...}) it proceeds to parse the second element of the tuple - If
inputis anHTTPoison.Responseinstance with astatus_codebetween200and300, it proceeds to parse thebodyof the response object - If
inputis anHTTPoison.Responseinstance with a different status code, it attempts to parse"errors"property of the response data into an instance ofCraftgate.Error - If
asis:void, it returns{:ok, nil} - If
asisStringand theinputis a string, it returns the input in an OK tuple (e.g.{:ok, input}) - If
inputis a string, it parses the input as a map and tries to convert it into an instance ofas - If
inputis a map with an"errors"element, it tries to parse the error data into an instance ofCraftgate.Error - If
inputis a map with a"data"element, it tries to parse the"data"key into an instance ofas, where:- If
asis:map, it returns the data in an OK tuple (e.g.{:ok, data}) - If
asis an atom, it converts the keys of the map into:snake_caseatoms, and attempts to convert the resulting map into an instance ofasusingConstruct.Cast.make/3
- If
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