2021-08-04 13:28:27 +00:00
|
|
|
# https://github.com/michalmuskala/jason/issues/69
|
2021-08-18 11:54:18 +00:00
|
|
|
defmodule MatrixServer.EncodableMap do
|
2021-08-21 19:39:28 +00:00
|
|
|
alias MatrixServer.EncodableMap
|
2021-08-19 21:42:24 +00:00
|
|
|
alias MatrixServer.Types.{UserId, RoomId, EventId, GroupId, AliasId}
|
2021-08-04 13:28:27 +00:00
|
|
|
|
|
|
|
defstruct pairs: []
|
|
|
|
|
2021-08-18 11:54:18 +00:00
|
|
|
defimpl Jason.Encoder, for: EncodableMap do
|
2021-08-04 13:28:27 +00:00
|
|
|
def encode(%{pairs: pairs}, opts) do
|
|
|
|
Jason.Encode.keyword(pairs, opts)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def from_map(map) do
|
|
|
|
pairs =
|
|
|
|
map
|
|
|
|
|> Enum.map(fn
|
2021-08-19 21:42:24 +00:00
|
|
|
{k, v}
|
|
|
|
when is_struct(v, UserId) or is_struct(v, RoomId) or is_struct(v, EventId) or
|
|
|
|
is_struct(v, GroupId) or is_struct(v, AliasId) ->
|
2021-08-21 09:25:36 +00:00
|
|
|
# Simply convert IDs to a string.
|
2021-08-19 21:42:24 +00:00
|
|
|
{k, to_string(v)}
|
|
|
|
|
2021-08-04 13:28:27 +00:00
|
|
|
{k, v} when is_map(v) ->
|
|
|
|
{k, from_map(v)}
|
|
|
|
|
|
|
|
x ->
|
|
|
|
x
|
|
|
|
end)
|
|
|
|
|> Enum.sort()
|
|
|
|
|
2021-08-18 11:54:18 +00:00
|
|
|
%EncodableMap{pairs: pairs}
|
2021-08-04 13:28:27 +00:00
|
|
|
end
|
|
|
|
end
|