~fnux/telegram-tl-elixir

65bff5f87e999a8de71dc0fd5c18d24da8ee10aa — Timothée Floure 4 years ago 021ef24
Workarounds for flags in layer 57 (TL.Build, TL.Parse)
2 files changed, 19 insertions(+), 0 deletions(-)

M lib/tl/build.ex
M lib/tl/parse.ex
M lib/tl/build.ex => lib/tl/build.ex +3 -0
@@ 44,9 44,12 @@ defmodule TL.Build do
        bin =
          if (is_binary data), do: data, else: encode_signed(data)
        serialize_string(bin)
      :"#" ->
        serialize(0,:int) # ¯\(ツ)/¯ (issue 3)
      _ ->
        cond do
          Atom.to_string(type) =~ ~r/^vector/ui -> box(:vector, data, type)
          Atom.to_string(type) =~ ~r/^flags.\d\?[a-zA-Z]*$/ui -> <<>> # ¯\(ツ)/¯ (issue 3)
          true -> data
        end
    end

M lib/tl/parse.ex => lib/tl/parse.ex +16 -0
@@ 78,6 78,7 @@ defmodule TL.Parse do
  # Deserialize the first element of the binary (given its type). Return the
  # tail.
  defp deserialize(data, type, :return_tail) do
    #IO.inspect {type, data}
    case type do
    # Basic types
      :int ->


@@ 114,10 115,13 @@ defmodule TL.Parse do
        deserialize(data, :string, :return_tail)
      :vector ->
        unbox(:vector, data)
      :"#" -> # issue 3
        deserialize(data, :int, :return_tail)
      # Anything else.
      _ ->
        cond do
          Atom.to_string(type) =~ ~r/^vector/ui -> unbox(:vector, data, type)
          Atom.to_string(type) =~ ~r/^flags.\d\?[a-zA-Z]*$/ui -> unbox(:flag, data, type)
          true -> unbox(:object, data, type)
        end
    end


@@ 191,6 195,18 @@ defmodule TL.Parse do
    {map, tail}
  end

  # 'flags'
  defp unbox(:flag, data, type) do
    [_, index, wrapped_type] = Regex.run(
      ~r/^flags.(\d)\?([a-zA-Z]*)$/ui, Atom.to_string(type)
    )

    case index do
      "0" -> {nil, data}
      _ -> deserialize(data, String.to_atom(wrapped_type), :return_tail)
    end
  end

  # Vector deserialization
  defp unbox(:vector, data, type) do
    # Extract internal type (:Vector<type>)