pinterest / riffed

Provides idiomatic Elixir bindings for Apache Thrift
Apache License 2.0
308 stars 37 forks source link

Using riffed for deserialization of binary data #54

Closed elaser closed 7 years ago

elaser commented 7 years ago

Hello! I'm currently using riffed to try and deserialize messages coming in from a kafka stream. I've created a models.ex with:

defmodule Models do
  use Riffed.Struct, common_types: [:Location, ...],
    event_types:[:Event, ...]
end

My thrift files are compiled from the instructions provided on this github.

My function for converting binary -> Thrift struct (is copied from @scohen here

def to_thrift(record_binary, struct_definition) do

    try do
      with({:ok, memory_buffer_transport} <- :thrift_memory_buffer.new(record_binary),
           {:ok, binary_protocol} <- :thrift_binary_protocol.new(memory_buffer_transport),
           {_, {:ok, record}} <- :thrift_protocol.read(binary_protocol, struct_definition)) do

        {:ok, Models.to_elixir(record, struct_definition)}
      end

    rescue e ->
      IO.inspect e
        {:error, :cant_decode}
    end
  end

I'm looking at my logs and I keep seeing something like:

%FunctionClauseError{args: nil, arity: 2, clauses: nil, function: :skip,
 kind: nil, module: :thrift_protocol}

Has anyone run into this kind of error and if so what is the workaround? Thanks!

elaser commented 7 years ago

got this working