From cb8e3d0cbe4d78d1034aa89d0eadf4630dc2e604 Mon Sep 17 00:00:00 2001 From: Russell McQueeney Date: Sat, 23 May 2020 16:04:18 -0700 Subject: [PATCH 1/3] Update decoder.ex --- lib/protobuf/decoder.ex | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/protobuf/decoder.ex b/lib/protobuf/decoder.ex index 0fb33f3d..3c81247c 100644 --- a/lib/protobuf/decoder.ex +++ b/lib/protobuf/decoder.ex @@ -17,6 +17,13 @@ defmodule Protobuf.Decoder do reverse_repeated(struct, repeated_fields) end + def decode_delimited_stream(data, module) do + Stream.unfold(data, fn data -> + {message_bin, rest} = decode_varint(data, :delimiter) + {decode(message_bin, module), rest} + end) + end + @doc false def decode_raw(data) do raw_decode_key(data, []) @@ -142,21 +149,14 @@ defmodule Protobuf.Decoder do type: type, oneof: oneof, name_atom: name_atom, - embedded?: embedded, - options: options + embedded?: embedded } = prop } -> key = if oneof, do: oneof_field(prop, msg_props), else: name_atom struct = if embedded do - embedded_msg = - if is_nil(options) do - decode(val, type) - else - Protobuf.FieldOptionsProcessor.decode_type(val, type, options) - end - + embedded_msg = decode(val, type) val = if is_map, do: %{embedded_msg.key => embedded_msg.value}, else: embedded_msg val = if oneof, do: {name_atom, val}, else: val @@ -164,12 +164,7 @@ defmodule Protobuf.Decoder do Map.put(struct, key, val) else - val = if is_nil(options) do - decode_type_m(type, key, val) - else - Protobuf.FieldOptionsProcessor.decode_type(val, type, options) - end - + val = decode_type_m(type, key, val) val = if oneof, do: {name_atom, val}, else: val val = @@ -351,6 +346,11 @@ defmodule Protobuf.Decoder do raw_decode_key(rest, [bytes | result]) end + defp raw_handle_varint(:delimiter, <>, result, len) do + <> = bin + {bytes, rest} + end + defp raw_handle_varint(:packed, <<>>, result, val) do [val | result] end From e1d2532389745ec2b5ff12702b5be29277728576 Mon Sep 17 00:00:00 2001 From: Russell McQueeney Date: Sat, 23 May 2020 16:06:45 -0700 Subject: [PATCH 2/3] Update decoder.ex --- lib/protobuf/decoder.ex | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/protobuf/decoder.ex b/lib/protobuf/decoder.ex index 3c81247c..9de2afcc 100644 --- a/lib/protobuf/decoder.ex +++ b/lib/protobuf/decoder.ex @@ -149,14 +149,21 @@ defmodule Protobuf.Decoder do type: type, oneof: oneof, name_atom: name_atom, - embedded?: embedded + embedded?: embedded, + options: options } = prop } -> key = if oneof, do: oneof_field(prop, msg_props), else: name_atom struct = if embedded do - embedded_msg = decode(val, type) + embedded_msg = + if is_nil(options) do + decode(val, type) + else + Protobuf.FieldOptionsProcessor.decode_type(val, type, options) + end + val = if is_map, do: %{embedded_msg.key => embedded_msg.value}, else: embedded_msg val = if oneof, do: {name_atom, val}, else: val @@ -164,7 +171,12 @@ defmodule Protobuf.Decoder do Map.put(struct, key, val) else - val = decode_type_m(type, key, val) + val = if is_nil(options) do + decode_type_m(type, key, val) + else + Protobuf.FieldOptionsProcessor.decode_type(val, type, options) + end + val = if oneof, do: {name_atom, val}, else: val val = From 174193e1297b53ce6599b5ad1af26cd9d7a3320a Mon Sep 17 00:00:00 2001 From: Russell McQueeney Date: Sat, 23 May 2020 16:08:49 -0700 Subject: [PATCH 3/3] Update decoder.ex --- lib/protobuf/decoder.ex | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/protobuf/decoder.ex b/lib/protobuf/decoder.ex index 9de2afcc..cbdfa1e1 100644 --- a/lib/protobuf/decoder.ex +++ b/lib/protobuf/decoder.ex @@ -16,11 +16,13 @@ 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 data -> - {message_bin, rest} = decode_varint(data, :delimiter) - {decode(message_bin, module), rest} + Stream.unfold(data, fn + <<>> -> nil + data -> + {message_bin, rest} = decode_varint(data, :delimiter) + {decode(message_bin, module), rest} end) end