Packages
electric_client
0.9.2
0.10.3
0.10.2
0.10.1
0.10.1-beta-1
0.10.0
0.9.5-beta-1
0.9.4
0.9.4-beta-1
0.9.3
0.9.2
0.9.1
0.9.0
0.8.3
0.8.3-beta-1
0.8.2
0.8.1
0.8.0
0.8.0-beta-1
0.7.3
0.7.2
0.7.1
0.7.0
0.6.5
0.6.5-beta-5
0.6.5-beta-4
0.6.5-beta-3
0.6.5-beta-2
0.6.5-beta-1
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.0
0.5.0-beta-1
0.4.1
0.4.0
0.3.2
0.3.1
0.3.0
0.3.0-beta.4
0.3.0-beta.3
0.3.0-beta.2
0.2.6-pre-1
retired
0.2.6-beta.1
0.2.6-beta.0
0.2.5
0.2.4
0.2.4-pre-8
0.2.4-pre-7
0.2.4-pre-6
0.2.4-pre-5
0.2.4-pre-4
0.2.4-pre-3
0.2.4-pre-2
0.2.4-pre-1
0.2.3
0.2.3-rc-1
0.2.2
0.2.2-rc-1
0.2.1
0.2.1-rc-3
0.2.1-rc-2
0.2.1-rc-1
0.2.0
0.1.2
0.1.1
0.1.0
0.1.0-dev-9
0.1.0-dev-8
0.1.0-dev-7
0.1.0-dev-6
0.1.0-dev-5
0.1.0-dev-4
0.1.0-dev-3
0.1.0-dev-2
0.1.0-dev-17
0.1.0-dev-16
0.1.0-dev-15
0.1.0-dev-14
0.1.0-dev-13
0.1.0-dev-12
0.1.0-dev-11
0.1.0-dev-10
0.1.0-dev
Elixir client for ElectricSQL
Current section
Files
Jump to
Current section
Files
lib/electric/client/ecto_adapter/array_decoder.ex
if Code.ensure_loaded?(Ecto) do
defmodule Electric.Client.EctoAdapter.ArrayDecoder do
alias Electric.Client.EctoAdapter
def decode!("{}", _type), do: []
def decode!(encoded_array, type) when is_binary(encoded_array) do
{"", [result]} =
decode_array(encoded_array, [], {EctoAdapter.cast_to(type), type, encoded_array})
result
end
##############################
defp decode_array("", acc, _state) do
{"", :lists.reverse(acc)}
end
defp decode_array(<<"{}", rest::bitstring>>, acc, state) do
decode_array(rest, [[] | acc], state)
end
defp decode_array(<<"{", rest::bitstring>>, acc, state) do
{rest, array} = decode_array(rest, [], state)
decode_array(rest, [array | acc], state)
end
defp decode_array(<<"}", rest::bitstring>>, acc, _state) do
{rest, :lists.reverse(acc)}
end
defp decode_array(<<?,, rest::bitstring>>, acc, state) do
decode_array(rest, acc, state)
end
defp decode_array(<<?", rest::bitstring>>, acc, state) do
{rest, elem} = decode_quoted_elem(rest, [], state)
decode_array(rest, [elem | acc], state)
end
defp decode_array(rest, acc, state) do
{rest, elem} = decode_elem(rest, [], state)
decode_array(rest, [elem | acc], state)
end
##############################
defp decode_elem(<<",", _::bitstring>> = rest, acc, state),
do: {rest, cast(acc, state)}
defp decode_elem(<<"}", _::bitstring>> = rest, acc, state),
do: {rest, cast(acc, state)}
defp decode_elem(<<c::utf8, rest::bitstring>>, acc, state),
do: decode_elem(rest, [acc | <<c::utf8>>], state)
defp decode_elem("", _acc, {_cast_fun, type, source}) do
raise Ecto.CastError, type: {:array, type}, value: source
end
##############################
defp decode_quoted_elem(<<?", rest::bitstring>>, acc, state),
do: {rest, cast_quoted(acc, state)}
defp decode_quoted_elem(<<"\\\"", rest::bitstring>>, acc, state),
do: decode_quoted_elem(rest, [acc | [?"]], state)
defp decode_quoted_elem(<<c::utf8, rest::bitstring>>, acc, state),
do: decode_quoted_elem(rest, [acc | <<c::utf8>>], state)
defp decode_quoted_elem("", _acc, {_cast_fun, type, source}) do
raise Ecto.CastError, type: {:array, type}, value: source
end
##############################
defp cast(iodata, {cast_fun, _type, _source}) do
iodata
|> IO.iodata_to_binary()
|> case do
"NULL" -> nil
value -> cast_fun.(value)
end
end
defp cast_quoted(iodata, {cast_fun, _type, _source}) do
iodata
|> IO.iodata_to_binary()
|> cast_fun.()
end
end
end