Current section

Files

Jump to
split_client lib boundary ordered_attributes.ex
Raw

lib/boundary/ordered_attributes.ex

defmodule SplitClient.Boundary.OrderedAttributes do
# A way to have consistent ordering of attributes that
# must be passed as JSON or used as cache keys
@moduledoc false
defstruct value: []
def new(value) when is_map(value) do
Enum.into(value, Keyword.new())
|> new()
end
def new(value) do
value = Enum.sort_by(value, &elem(&1, 0))
%__MODULE__{value: value}
end
defimpl Jason.Encoder do
def encode(%{value: value}, opts) do
Jason.Encode.keyword(value, opts)
end
end
end