1 |
|
%% Copyright (c) 2023 Peter Morgan <peter.james.morgan@gmail.com> |
2 |
|
%% |
3 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
4 |
|
%% you may not use this file except in compliance with the License. |
5 |
|
%% You may obtain a copy of the License at |
6 |
|
%% |
7 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
8 |
|
%% |
9 |
|
%% Unless required by applicable law or agreed to in writing, software |
10 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
11 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 |
|
%% See the License for the specific language governing permissions and |
13 |
|
%% limitations under the License. |
14 |
|
|
15 |
|
%% @doc Encoder combinators applying their child encoder multiple times. |
16 |
|
|
17 |
|
-module(narcs_multi). |
18 |
|
|
19 |
|
-export([count/2]). |
20 |
|
-export([count/3]). |
21 |
|
-export([count/4]). |
22 |
|
|
23 |
|
|
24 |
|
-spec count(narcs:encoder(), narcs:encoder()) -> narcs:encoder(). |
25 |
|
|
26 |
|
%% @doc Write the encoded length of items, followed by each encoded |
27 |
|
%% item. |
28 |
|
|
29 |
|
count(NumOfItemEncoder, ItemEncoder) -> |
30 |
:-( |
fun |
31 |
|
(Items) -> |
32 |
:-( |
[NumOfItemEncoder(length(Items)), |
33 |
:-( |
[ItemEncoder(Item) || Item <- Items]] |
34 |
|
end. |
35 |
|
|
36 |
|
|
37 |
|
count(NumOfItemEncoder, KeyEncoder, ValueEncoder) -> |
38 |
:-( |
?FUNCTION_NAME(NumOfItemEncoder, undefined, KeyEncoder, ValueEncoder). |
39 |
|
|
40 |
|
|
41 |
|
count(NumOfItemEncoder, IteratorOrder, KeyEncoder, ValueEncoder) -> |
42 |
:-( |
fun |
43 |
|
(Items) -> |
44 |
:-( |
[NumOfItemEncoder(maps:size(Items)), |
45 |
|
maps:fold( |
46 |
|
fun |
47 |
|
(Key, Value, A) -> |
48 |
:-( |
[KeyEncoder(Key), ValueEncoder(Value) | A] |
49 |
|
end, |
50 |
|
[], |
51 |
|
maps:iterator(Items, IteratorOrder))] |
52 |
|
end. |