Skip to content

Commit

Permalink
Merge pull request #589 from Overbryd/feat/expose_structs
Browse files Browse the repository at this point in the history
add Stripe.Converter.structs() to enumerate all generated structs
  • Loading branch information
snewcomer authored Apr 4, 2020
2 parents 9a551af + 8a2c6d8 commit dfa1f9f
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/stripe/converter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dfa1f9f

Please sign in to comment.