@@ -34,9 +34,12 @@ defmodule Protobuf.Encoder do
34
34
35
35
# We encode the fields in order since some recommended conformance tests expect us to do so.
36
36
encoded =
37
- ordered_tags
38
- |> Enum . map ( fn fnum -> Map . fetch! ( field_props , fnum ) end )
39
- |> encode_fields ( syntax , struct , oneofs , _acc = [ ] )
37
+ for fnum <- ordered_tags ,
38
+ prop = Map . fetch! ( field_props , fnum ) ,
39
+ encoded = encode_field ( prop , struct , oneofs , syntax ) ,
40
+ encoded != :skip do
41
+ encoded
42
+ end
40
43
41
44
encoded = [ encoded | encode_unknown_fields ( struct ) ]
42
45
@@ -47,26 +50,6 @@ defmodule Protobuf.Encoder do
47
50
end
48
51
end
49
52
50
- defp encode_fields ( _fields = [ ] , _syntax , _struct , _oneofs , acc ) do
51
- acc
52
- end
53
-
54
- defp encode_fields (
55
- [ prop | rest ] ,
56
- syntax ,
57
- struct ,
58
- oneofs ,
59
- acc
60
- ) do
61
- acc =
62
- case encode_field ( prop , struct , oneofs , syntax ) do
63
- { :ok , iodata } -> [ acc | iodata ]
64
- :skip -> acc
65
- end
66
-
67
- encode_fields ( rest , syntax , struct , oneofs , acc )
68
- end
69
-
70
53
defp encode_field ( % FieldProps { name_atom: name , oneof: oneof } = prop , struct , oneofs , syntax ) do
71
54
val =
72
55
if oneof do
@@ -109,8 +92,7 @@ defmodule Protobuf.Encoder do
109
92
if skip_field? ( syntax , val , prop ) or skip_enum? ( syntax , val , prop ) do
110
93
:skip
111
94
else
112
- iodata = apply_or_map ( val , repeated? , & [ fnum | Wire . encode ( type , & 1 ) ] )
113
- { :ok , iodata }
95
+ apply_or_map ( val , repeated? , & [ fnum | Wire . encode ( type , & 1 ) ] )
114
96
end
115
97
end
116
98
@@ -124,23 +106,20 @@ defmodule Protobuf.Encoder do
124
106
syntax ,
125
107
% FieldProps { encoded_fnum: fnum , repeated?: repeated? , map?: map? , type: type } = prop
126
108
) do
127
- result =
128
- apply_or_map ( val , repeated? || map? , fn val ->
129
- val = transform_module ( val , type )
130
-
131
- if skip_field? ( syntax , val , prop ) do
132
- ""
133
- else
134
- val = if map? , do: struct ( type , % { key: elem ( val , 0 ) , value: elem ( val , 1 ) } ) , else: val
135
-
136
- # so that oneof {:atom, val} can be encoded
137
- encoded = encode_from_type ( type , val )
138
- byte_size = IO . iodata_length ( encoded )
139
- [ fnum | Varint . encode ( byte_size ) ] ++ encoded
140
- end
141
- end )
142
-
143
- { :ok , result }
109
+ apply_or_map ( val , repeated? || map? , fn val ->
110
+ val = transform_module ( val , type )
111
+
112
+ if skip_field? ( syntax , val , prop ) do
113
+ ""
114
+ else
115
+ val = if map? , do: struct ( type , % { key: elem ( val , 0 ) , value: elem ( val , 1 ) } ) , else: val
116
+
117
+ # so that oneof {:atom, val} can be encoded
118
+ encoded = encode_from_type ( type , val )
119
+ byte_size = IO . iodata_length ( encoded )
120
+ [ fnum | Varint . encode ( byte_size ) ] ++ encoded
121
+ end
122
+ end )
144
123
end
145
124
146
125
defp do_encode_field ( :packed , val , syntax , % FieldProps { type: type , encoded_fnum: fnum } = prop ) do
@@ -149,7 +128,7 @@ defmodule Protobuf.Encoder do
149
128
else
150
129
encoded = Enum . map ( val , & Wire . encode ( type , & 1 ) )
151
130
byte_size = IO . iodata_length ( encoded )
152
- { :ok , [ fnum | Varint . encode ( byte_size ) ] ++ encoded }
131
+ [ fnum | Varint . encode ( byte_size ) ] ++ encoded
153
132
end
154
133
end
155
134
@@ -264,8 +243,8 @@ defmodule Protobuf.Encoder do
264
243
case Protobuf.Extension . get_extension_props ( mod , ext_mod , key ) do
265
244
% { field_props: prop } ->
266
245
case do_encode_field ( class_field ( prop ) , val , :proto2 , prop ) do
267
- { :ok , iodata } -> [ acc | iodata ]
268
246
:skip -> acc
247
+ iodata -> [ acc | iodata ]
269
248
end
270
249
271
250
_ ->
0 commit comments