From 8a2c6d86f80770c3402cf7fcaf8aa94009bcd974 Mon Sep 17 00:00:00 2001 From: Lukas Rieder Date: Fri, 3 Apr 2020 18:56:34 +0200 Subject: [PATCH] add Stripe.Converter.structs() to enumerate all generated structs to use with users protocols --- lib/stripe/converter.ex | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/stripe/converter.ex b/lib/stripe/converter.ex index c0801eb3..6a3ddb0c 100644 --- a/lib/stripe/converter.ex +++ b/lib/stripe/converter.ex @@ -68,6 +68,29 @@ defmodule Stripe.Converter do @no_convert_maps ~w(metadata supported_bank_account_currencies) + @doc """ + Returns a list of structs to be used for providing JSON-encoders. + + ## Examples + + Say you are using Jason to encode your JSON, you can provide the following protocol, + to directly encode all structs of this library into JSON. + + ``` + for struct <- Stripe.Converter.structs() do + defimpl Jason.Encoder, for: struct do + def encode(value, opts) do + Jason.Encode.map(Map.delete(value, :__struct__), opts) + end + end + end + ``` + """ + def structs() do + (@supported_objects -- @no_convert_maps) + |> Enum.map(&Stripe.Util.object_name_to_module/1) + end + @spec convert_value(any) :: any defp convert_value(%{"object" => object_name} = value) when is_binary(object_name) do case Enum.member?(@supported_objects, object_name) do