Comparison

teal-src/util/datamapper.tl @ 11439:9abcdfdcdb01

util.datamapper: Add support for mapping of elements where only one attribute matters E.g. <feature var='foo'/> in XEP-0030 and some other simple specifications.
author Kim Alvefur <zash@zash.se>
date Sun, 07 Mar 2021 12:48:49 +0100
parent 11438:b7807583de34
child 11450:25829015917c
comparison
equal deleted inserted replaced
11438:b7807583de34 11439:9abcdfdcdb01
18 local namespace = s.attr.xmlns; 18 local namespace = s.attr.xmlns;
19 local prefix : string = nil 19 local prefix : string = nil
20 local is_attribute = false 20 local is_attribute = false
21 local is_text = false 21 local is_text = false
22 local name_is_value = false; 22 local name_is_value = false;
23 local single_attribute : string
23 24
24 local proptype : js.schema_t.type_e 25 local proptype : js.schema_t.type_e
25 if propschema is js.schema_t then 26 if propschema is js.schema_t then
26 proptype = propschema.type 27 proptype = propschema.type
27 elseif propschema is js.schema_t.type_e then 28 elseif propschema is js.schema_t.type_e then
42 is_attribute = true 43 is_attribute = true
43 elseif propschema.xml.text then 44 elseif propschema.xml.text then
44 is_text = true 45 is_text = true
45 elseif propschema.xml.x_name_is_value then 46 elseif propschema.xml.x_name_is_value then
46 name_is_value = true 47 name_is_value = true
48 elseif propschema.xml.x_single_attribute then
49 single_attribute = propschema.xml.x_single_attribute
47 end 50 end
48 end 51 end
49 52
50 if name_is_value then 53 if name_is_value then
51 local c = s:get_child(nil, namespace); 54 local c = s:get_child(nil, namespace);
76 out[prop] = s:get_text() 79 out[prop] = s:get_text()
77 elseif proptype == "integer" or proptype == "number" then 80 elseif proptype == "integer" or proptype == "number" then
78 out[prop] = tonumber(s:get_text()) 81 out[prop] = tonumber(s:get_text())
79 end 82 end
80 83
84 elseif single_attribute then
85 local c = s:get_child(name, namespace)
86 local a = c and c.attr[single_attribute]
87 if proptype == "string" then
88 out[prop] = a
89 elseif proptype == "integer" or proptype == "number" then
90 out[prop] = tonumber(a)
91 elseif proptype == "boolean" then
92 out[prop] = toboolean(a)
93 end
81 else 94 else
82 95
83 if proptype == "string" then 96 if proptype == "string" then
84 out[prop] = s:get_child_text(name, namespace) 97 out[prop] = s:get_child_text(name, namespace)
85 elseif proptype == "integer" or proptype == "number" then 98 elseif proptype == "integer" or proptype == "number" then
134 local namespace = current_ns 147 local namespace = current_ns
135 local prefix : string = nil 148 local prefix : string = nil
136 local is_attribute = false 149 local is_attribute = false
137 local is_text = false 150 local is_text = false
138 local name_is_value = false; 151 local name_is_value = false;
152 local single_attribute : string
139 153
140 if propschema is js.schema_t and propschema.xml then 154 if propschema is js.schema_t and propschema.xml then
141 155
142 if propschema.xml.name then 156 if propschema.xml.name then
143 name = propschema.xml.name 157 name = propschema.xml.name
154 is_attribute = true 168 is_attribute = true
155 elseif propschema.xml.text then 169 elseif propschema.xml.text then
156 is_text = true 170 is_text = true
157 elseif propschema.xml.x_name_is_value then 171 elseif propschema.xml.x_name_is_value then
158 name_is_value = true 172 name_is_value = true
173 elseif propschema.xml.x_single_attribute then
174 single_attribute = propschema.xml.x_single_attribute
159 end 175 end
160 end 176 end
161 177
162 if is_attribute then 178 if is_attribute then
163 local attr = name 179 local attr = name
178 end 194 end
179 elseif is_text then 195 elseif is_text then
180 if v is string then 196 if v is string then
181 out:text(v) 197 out:text(v)
182 end 198 end
199 elseif single_attribute then
200 local propattr : { string : string } = {}
201
202 if namespace ~= current_ns then
203 propattr.xmlns = namespace
204 end
205
206 if proptype == "string" and v is string then
207 propattr[single_attribute] = v
208 elseif proptype == "number" and v is number then
209 propattr[single_attribute] = string.format("%g", v)
210 elseif proptype == "integer" and v is number then
211 propattr[single_attribute] = string.format("%d", v)
212 elseif proptype == "boolean" and v is boolean then
213 propattr[single_attribute] = v and "1" or "0"
214 end
215 out:tag(name, propattr):up();
216
183 else 217 else
184 local propattr : { string : string } 218 local propattr : { string : string }
185 if namespace ~= current_ns then 219 if namespace ~= current_ns then
186 propattr = { xmlns = namespace } 220 propattr = { xmlns = namespace }
187 end 221 end