Software /
code /
prosody
Comparison
util/datamapper.lua @ 11457:6a51749af7f4
util.datamapper: Add initial support for parsing arrays
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 18 Mar 2021 12:57:25 +0100 |
parent | 11456:4e376a43fe40 |
child | 11458:0e00fa518688 |
comparison
equal
deleted
inserted
replaced
11456:4e376a43fe40 | 11457:6a51749af7f4 |
---|---|
36 proptype = propschema.type | 36 proptype = propschema.type |
37 elseif type(propschema) == "string" then | 37 elseif type(propschema) == "string" then |
38 proptype = propschema | 38 proptype = propschema |
39 end | 39 end |
40 | 40 |
41 if proptype == "object" or proptype == "array" then | |
42 value_where = "in_children" | |
43 end | |
44 | |
41 if type(propschema) == "table" then | 45 if type(propschema) == "table" then |
42 local xml = propschema.xml | 46 local xml = propschema.xml |
43 if xml then | 47 if xml then |
44 if xml.name then | 48 if xml.name then |
45 name = xml.name | 49 name = xml.name |
48 namespace = xml.namespace | 52 namespace = xml.namespace |
49 end | 53 end |
50 if xml.prefix then | 54 if xml.prefix then |
51 prefix = xml.prefix | 55 prefix = xml.prefix |
52 end | 56 end |
53 | 57 if proptype == "array" and xml.wrapped then |
54 if xml.attribute then | 58 value_where = "in_wrapper" |
59 elseif xml.attribute then | |
55 value_where = "in_attribute" | 60 value_where = "in_attribute" |
56 elseif xml.text then | 61 elseif xml.text then |
57 value_where = "in_text" | 62 value_where = "in_text" |
58 elseif xml.x_name_is_value then | 63 elseif xml.x_name_is_value then |
59 value_where = "in_tag_name" | 64 value_where = "in_tag_name" |
67 elseif propschema["enum"] then | 72 elseif propschema["enum"] then |
68 enums = propschema["enum"] | 73 enums = propschema["enum"] |
69 end | 74 end |
70 end | 75 end |
71 | 76 |
72 if proptype == "object" or proptype == "array" then | |
73 value_where = "in_children" | |
74 end | |
75 | |
76 return proptype, value_where, name, namespace, prefix, single_attribute, enums | 77 return proptype, value_where, name, namespace, prefix, single_attribute, enums |
77 end | 78 end |
78 | 79 |
79 local function parse_object(schema, s) | 80 local parse_object |
81 local parse_array | |
82 | |
83 function parse_object(schema, s) | |
80 local out = {} | 84 local out = {} |
81 if schema.properties then | 85 if schema.properties then |
82 for prop, propschema in pairs(schema.properties) do | 86 for prop, propschema in pairs(schema.properties) do |
83 | 87 |
84 local proptype, value_where, name, namespace, prefix, single_attribute, enums = unpack_propschema(propschema, prop, s.attr.xmlns) | 88 local proptype, value_where, name, namespace, prefix, single_attribute, enums = unpack_propschema(propschema, prop, s.attr.xmlns) |
121 if proptype == "object" then | 125 if proptype == "object" then |
122 local c = s:get_child(name, namespace) | 126 local c = s:get_child(name, namespace) |
123 if c then | 127 if c then |
124 out[prop] = parse_object(propschema, c); | 128 out[prop] = parse_object(propschema, c); |
125 end | 129 end |
126 | 130 elseif proptype == "array" then |
127 end | 131 out[prop] = parse_array(propschema, s); |
128 end | 132 else |
129 if value_where ~= "in_children" then | 133 error("unreachable") |
134 end | |
135 elseif value_where == "in_wrapper" and type(propschema) == "table" and proptype == "array" then | |
136 local wrapper = s:get_child(name, namespace); | |
137 if wrapper then | |
138 out[prop] = parse_array(propschema, wrapper); | |
139 else | |
140 error("unreachable") | |
141 end | |
142 else | |
143 error("unreachable") | |
144 end | |
145 if value_where ~= "in_children" and value_where ~= "in_wrapper" then | |
130 out[prop] = totype(proptype, value) | 146 out[prop] = totype(proptype, value) |
131 end | 147 end |
132 end | 148 end |
133 end | 149 end |
134 | 150 |
151 return out | |
152 end | |
153 | |
154 function parse_array(schema, s) | |
155 local proptype, value_where, child_name, namespace = unpack_propschema(schema.items, nil, s.attr.xmlns) | |
156 local out = {} | |
157 for c in s:childtags(child_name, namespace) do | |
158 local value; | |
159 if value_where == "in_text_tag" then | |
160 value = c:get_text(); | |
161 else | |
162 error("NYI") | |
163 end | |
164 | |
165 if value ~= nil then | |
166 table.insert(out, value); | |
167 end | |
168 end | |
135 return out | 169 return out |
136 end | 170 end |
137 | 171 |
138 local function parse(schema, s) | 172 local function parse(schema, s) |
139 if schema.type == "object" then | 173 if schema.type == "object" then |
140 return parse_object(schema, s) | 174 return parse_object(schema, s) |
175 elseif schema.type == "array" then | |
176 return parse_array(schema, s) | |
177 else | |
178 error("top-level scalars unsupported") | |
141 end | 179 end |
142 end | 180 end |
143 | 181 |
144 local function unparse(schema, t, current_name, current_ns) | 182 local function unparse(schema, t, current_name, current_ns) |
145 if schema.type == "object" then | 183 if schema.type == "object" then |