@@ -187,14 +187,13 @@ get_property(priority, Msg) ->
187187convert_to (? MODULE , Msg , _Env ) ->
188188 Msg ;
189189convert_to (TargetProto , Msg , Env ) ->
190- TargetProto :convert_from (? MODULE ,
191- msg_to_sections (Msg , fun (X ) -> X end ),
192- Env ).
190+ TargetProto :convert_from (? MODULE , msg_to_sections (Msg ), Env ).
193191
194192serialize (Sections ) ->
195193 encode_bin (Sections ).
196194
197- protocol_state (Msg0 = # msg {header = Header0 }, Anns ) ->
195+ protocol_state (Msg0 = # msg {header = Header0 ,
196+ message_annotations = MA0 }, Anns ) ->
198197 Redelivered = maps :get (redelivered , Anns , false ),
199198 FirstAcquirer = not Redelivered ,
200199 Header = case Header0 of
@@ -203,20 +202,25 @@ protocol_state(Msg0 = #msg{header = Header0}, Anns) ->
203202 # 'v1_0.header' {} ->
204203 Header0 # 'v1_0.header' {first_acquirer = FirstAcquirer }
205204 end ,
206- Msg = Msg0 # msg {header = Header },
207205
208- #{? ANN_EXCHANGE := Exchange ,
209- ? ANN_ROUTING_KEYS := [RKey | _ ]} = Anns ,
210- % % any x-* annotations get added as message annotations
211- AnnsToAdd = maps :filter (fun (Key , _ ) -> mc_util :is_x_header (Key ) end , Anns ),
212-
213- MACFun = fun (MAC ) ->
214- add_message_annotations (
215- AnnsToAdd #{<<" x-exchange" >> => wrap (utf8 , Exchange ),
216- <<" x-routing-key" >> => wrap (utf8 , RKey )}, MAC )
217- end ,
218-
219- msg_to_sections (Msg , MACFun ).
206+ MA = maps :fold (fun (? ANN_EXCHANGE , Exchange , L ) ->
207+ maps_upsert (<<" x-exchange" >>, {utf8 , Exchange }, L );
208+ (? ANN_ROUTING_KEYS , RKeys , L ) ->
209+ RKey = hd (RKeys ),
210+ maps_upsert (<<" x-routing-key" >>, {utf8 , RKey }, L );
211+ (<<" x-" , _ /binary >> = K , V , L )
212+ when V =/= undefined ->
213+ % % any x-* annotations get added as message annotations
214+ maps_upsert (K , mc_util :infer_type (V ), L );
215+ (<<" timestamp_in_ms" >>, V , L ) ->
216+ maps_upsert (<<" x-opt-rabbitmq-received-time" >>, {timestamp , V }, L );
217+ (_ , _ , Acc ) ->
218+ Acc
219+ end , MA0 , Anns ),
220+
221+ Msg = Msg0 # msg {header = Header ,
222+ message_annotations = MA },
223+ msg_to_sections (Msg ).
220224
221225prepare (_For , Msg ) ->
222226 Msg .
@@ -225,13 +229,14 @@ prepare(_For, Msg) ->
225229
226230msg_to_sections (# msg {header = H ,
227231 delivery_annotations = DAC ,
228- message_annotations = MAC0 ,
232+ message_annotations = MAC ,
229233 properties = P ,
230234 application_properties = APC ,
231235 data = Data ,
232- footer = FC }, MacFun ) ->
236+ footer = FC }) ->
233237 Tail = case FC of
234- [] -> [];
238+ [] ->
239+ [];
235240 _ ->
236241 [# 'v1_0.footer' {content = FC }]
237242 end ,
@@ -242,34 +247,40 @@ msg_to_sections(#msg{header = H,
242247 Data ++ Tail
243248 end ,
244249 S1 = case APC of
245- [] -> S0 ;
250+ [] ->
251+ S0 ;
246252 _ ->
247253 [# 'v1_0.application_properties' {content = APC } | S0 ]
248254 end ,
249255 S2 = case P of
250- undefined -> S1 ;
256+ undefined ->
257+ S1 ;
251258 _ ->
252259 [P | S1 ]
253260 end ,
254- S3 = case MacFun (MAC0 ) of
255- [] -> S2 ;
256- MAC ->
261+ S3 = case MAC of
262+ [] ->
263+ S2 ;
264+ _ ->
257265 [# 'v1_0.message_annotations' {content = MAC } | S2 ]
258266 end ,
259267 S4 = case DAC of
260- [] -> S3 ;
268+ [] ->
269+ S3 ;
261270 _ ->
262271 [# 'v1_0.delivery_annotations' {content = DAC } | S3 ]
263272 end ,
264273 case H of
265- undefined -> S4 ;
274+ undefined ->
275+ S4 ;
266276 _ ->
267277 [H | S4 ]
268278 end .
269279
270-
271-
272-
280+ maps_upsert (Key , TaggedVal , KVList ) ->
281+ TaggedKey = {symbol , Key },
282+ Elem = {TaggedKey , TaggedVal },
283+ lists :keystore (TaggedKey , 1 , KVList , Elem ).
273284
274285encode_bin (undefined ) ->
275286 <<>>;
@@ -377,22 +388,6 @@ decode([#'v1_0.amqp_value'{} = B | Rem], #msg{} = Msg) ->
377388 % % an amqp value can only be a singleton
378389 decode (Rem , Msg # msg {data = B }).
379390
380- add_message_annotations (Anns , MA0 ) ->
381- maps :fold (fun (K , V , Acc ) ->
382- map_add (symbol , K , mc_util :infer_type (V ), Acc )
383- end , MA0 , Anns ).
384-
385- map_add (_T , _Key , undefined , Acc ) ->
386- Acc ;
387- map_add (KeyType , Key , TaggedValue , Acc0 ) ->
388- TaggedKey = wrap (KeyType , Key ),
389- lists_upsert ({TaggedKey , TaggedValue }, Acc0 ).
390-
391- wrap (_Type , undefined ) ->
392- undefined ;
393- wrap (Type , Val ) ->
394- {Type , Val }.
395-
396391key_find (K , [{{_ , K }, {_ , V }} | _ ]) ->
397392 V ;
398393key_find (K , [_ | Rem ]) ->
@@ -478,13 +473,3 @@ essential_properties(#msg{message_annotations = MA} = Msg) ->
478473 Acc
479474 end , Anns , MA )
480475 end .
481-
482- lists_upsert (New , L ) ->
483- lists_upsert (New , L , [], L ).
484-
485- lists_upsert ({Key , _ } = New , [{Key , _ } | Rem ], Pref , _All ) ->
486- lists :reverse (Pref , [New | Rem ]);
487- lists_upsert (New , [Item | Rem ], Pref , All ) ->
488- lists_upsert (New , Rem , [Item | Pref ], All );
489- lists_upsert (New , [], _Pref , All ) ->
490- [New | All ].
0 commit comments