Comparison

util/datamapper.lua @ 11437:87a684df4b65

util.datamapper: Invent extension for using tag name as value Useful for certain enum-like uses where the element name is the relevant information, e.g. chat states.
author Kim Alvefur <zash@zash.se>
date Sat, 06 Mar 2021 23:14:23 +0100
parent 11436:5df9ffc25bb4
child 11438:b7807583de34
comparison
equal deleted inserted replaced
11436:5df9ffc25bb4 11437:87a684df4b65
16 local name = prop 16 local name = prop
17 local namespace = s.attr.xmlns; 17 local namespace = s.attr.xmlns;
18 local prefix = nil 18 local prefix = nil
19 local is_attribute = false 19 local is_attribute = false
20 local is_text = false 20 local is_text = false
21 local name_is_value = false;
21 22
22 local proptype 23 local proptype
23 if type(propschema) == "table" then 24 if type(propschema) == "table" then
24 proptype = propschema.type 25 proptype = propschema.type
25 elseif type(propschema) == "string" then 26 elseif type(propschema) == "string" then
38 end 39 end
39 if propschema.xml.attribute then 40 if propschema.xml.attribute then
40 is_attribute = true 41 is_attribute = true
41 elseif propschema.xml.text then 42 elseif propschema.xml.text then
42 is_text = true 43 is_text = true
43 end 44 elseif propschema.xml.x_name_is_value then
44 end 45 name_is_value = true
45 46 end
46 if is_attribute then 47 end
48
49 if name_is_value then
50 local c = s:get_child(nil, namespace);
51 if c then
52 out[prop] = c.name;
53 end
54 elseif is_attribute then
47 local attr = name 55 local attr = name
48 if prefix then 56 if prefix then
49 attr = prefix .. ":" .. name 57 attr = prefix .. ":" .. name
50 elseif namespace ~= s.attr.xmlns then 58 elseif namespace ~= s.attr.xmlns then
51 attr = namespace .. "\1" .. name 59 attr = namespace .. "\1" .. name
122 local name = prop 130 local name = prop
123 local namespace = current_ns 131 local namespace = current_ns
124 local prefix = nil 132 local prefix = nil
125 local is_attribute = false 133 local is_attribute = false
126 local is_text = false 134 local is_text = false
135 local name_is_value = false;
127 136
128 if type(propschema) == "table" and propschema.xml then 137 if type(propschema) == "table" and propschema.xml then
129 138
130 if propschema.xml.name then 139 if propschema.xml.name then
131 name = propschema.xml.name 140 name = propschema.xml.name
140 149
141 if propschema.xml.attribute then 150 if propschema.xml.attribute then
142 is_attribute = true 151 is_attribute = true
143 elseif propschema.xml.text then 152 elseif propschema.xml.text then
144 is_text = true 153 is_text = true
154 elseif propschema.xml.x_name_is_value then
155 name_is_value = true
145 end 156 end
146 end 157 end
147 158
148 if is_attribute then 159 if is_attribute then
149 local attr = name 160 local attr = name
169 else 180 else
170 local propattr 181 local propattr
171 if namespace ~= current_ns then 182 if namespace ~= current_ns then
172 propattr = {xmlns = namespace} 183 propattr = {xmlns = namespace}
173 end 184 end
174 if proptype == "string" and type(v) == "string" then 185 if name_is_value and type(v) == "string" then
186 out:tag(v, propattr):up();
187 elseif proptype == "string" and type(v) == "string" then
175 out:text_tag(name, v, propattr) 188 out:text_tag(name, v, propattr)
176 elseif proptype == "number" and type(v) == "number" then 189 elseif proptype == "number" and type(v) == "number" then
177 out:text_tag(name, string.format("%g", v), propattr) 190 out:text_tag(name, string.format("%g", v), propattr)
178 elseif proptype == "integer" and type(v) == "number" then 191 elseif proptype == "integer" and type(v) == "number" then
179 out:text_tag(name, string.format("%d", v), propattr) 192 out:text_tag(name, string.format("%d", v), propattr)