Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update decoder.ex #8

Open
wants to merge 3 commits into
base: brex-head
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/protobuf/decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ defmodule Protobuf.Decoder do
struct = build_struct(kvs, msg_props, module.new())
reverse_repeated(struct, repeated_fields)
end

def decode_delimited_stream(data, module) do
Stream.unfold(data, fn
<<>> -> nil
data ->
{message_bin, rest} = decode_varint(data, :delimiter)
{decode(message_bin, module), rest}
end)
end

@doc false
def decode_raw(data) do
Expand Down Expand Up @@ -351,6 +360,11 @@ defmodule Protobuf.Decoder do
raw_decode_key(rest, [bytes | result])
end

defp raw_handle_varint(:delimiter, <<bin::bits>>, result, len) do
<<bytes::bytes-size(len), rest::bits>> = bin
{bytes, rest}
end

defp raw_handle_varint(:packed, <<>>, result, val) do
[val | result]
end
Expand Down