-
Notifications
You must be signed in to change notification settings - Fork 0
/
quissh_aws_response_parser.erl
49 lines (40 loc) · 1.39 KB
/
quissh_aws_response_parser.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
-module(quissh_aws_response_parser).
-include_lib("xmerl/include/xmerl.hrl").
-export([parse/1]).
parse(Response) ->
{Elm, _} = xmerl_scan:string(Response),
parse_element(Elm).
parse_element(Elm) when is_record(Elm, xmlElement) ->
parse_element(Elm, #{}).
parse_element(_Elm = #xmlElement{
name = Name,
content = [#xmlText{value = Content}]
},
AccMap) ->
maps:put(Name, Content, AccMap);
parse_element(_Elm = #xmlElement{
name = item,
content = Children
},
AccMap) when is_list(Children) ->
AccMap2 = parse_element(Children, #{}),
OtherItems = maps:get(item, AccMap, []),
maps:put(item, [AccMap2|OtherItems], AccMap);
parse_element(_Elm = #xmlElement{
name = Name,
content = Children
},
AccMap) when is_list(Children) ->
AccMap2 = parse_element(Children, #{}),
AccMap3 = case maps:is_key(item, AccMap2) of
true -> maps:get(item, AccMap2);
_ -> AccMap2
end,
maps:put(Name, AccMap3, AccMap);
parse_element([HeadElm|TailElm], AccMap) ->
AccMap2 = parse_element(HeadElm, AccMap),
parse_element(TailElm, AccMap2);
parse_element([], AccMap) ->
AccMap;
parse_element(_Elm, AccMap) ->
AccMap.