Software /
code /
prosody
Comparison
teal-src/util/datamapper.tl @ 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 |
---|---|
17 local name = prop | 17 local name = prop |
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 | 23 |
23 local proptype : js.schema_t.type_e | 24 local proptype : js.schema_t.type_e |
24 if propschema is js.schema_t then | 25 if propschema is js.schema_t then |
25 proptype = propschema.type | 26 proptype = propschema.type |
26 elseif propschema is js.schema_t.type_e then | 27 elseif propschema is js.schema_t.type_e then |
39 end | 40 end |
40 if propschema.xml.attribute then | 41 if propschema.xml.attribute then |
41 is_attribute = true | 42 is_attribute = true |
42 elseif propschema.xml.text then | 43 elseif propschema.xml.text then |
43 is_text = true | 44 is_text = true |
44 end | 45 elseif propschema.xml.x_name_is_value then |
45 end | 46 name_is_value = true |
46 | 47 end |
47 if is_attribute then | 48 end |
49 | |
50 if name_is_value then | |
51 local c = s:get_child(nil, namespace); | |
52 if c then | |
53 out[prop] = c.name; | |
54 end | |
55 elseif is_attribute then | |
48 local attr = name | 56 local attr = name |
49 if prefix then | 57 if prefix then |
50 attr = prefix .. ':' .. name | 58 attr = prefix .. ':' .. name |
51 elseif namespace ~= s.attr.xmlns then | 59 elseif namespace ~= s.attr.xmlns then |
52 attr = namespace .. "\1" .. name | 60 attr = namespace .. "\1" .. name |
123 local name = prop | 131 local name = prop |
124 local namespace = current_ns | 132 local namespace = current_ns |
125 local prefix : string = nil | 133 local prefix : string = nil |
126 local is_attribute = false | 134 local is_attribute = false |
127 local is_text = false | 135 local is_text = false |
136 local name_is_value = false; | |
128 | 137 |
129 if propschema is js.schema_t and propschema.xml then | 138 if propschema is js.schema_t and propschema.xml then |
130 | 139 |
131 if propschema.xml.name then | 140 if propschema.xml.name then |
132 name = propschema.xml.name | 141 name = propschema.xml.name |
141 | 150 |
142 if propschema.xml.attribute then | 151 if propschema.xml.attribute then |
143 is_attribute = true | 152 is_attribute = true |
144 elseif propschema.xml.text then | 153 elseif propschema.xml.text then |
145 is_text = true | 154 is_text = true |
155 elseif propschema.xml.x_name_is_value then | |
156 name_is_value = true | |
146 end | 157 end |
147 end | 158 end |
148 | 159 |
149 if is_attribute then | 160 if is_attribute then |
150 local attr = name | 161 local attr = name |
170 else | 181 else |
171 local propattr : { string : string } | 182 local propattr : { string : string } |
172 if namespace ~= current_ns then | 183 if namespace ~= current_ns then |
173 propattr = { xmlns = namespace } | 184 propattr = { xmlns = namespace } |
174 end | 185 end |
175 if proptype == "string" and v is string then | 186 if name_is_value and v is string then |
187 out:tag(v, propattr):up(); | |
188 elseif proptype == "string" and v is string then | |
176 out:text_tag(name, v, propattr) | 189 out:text_tag(name, v, propattr) |
177 elseif proptype == "number" and v is number then | 190 elseif proptype == "number" and v is number then |
178 out:text_tag(name, string.format("%g", v), propattr) | 191 out:text_tag(name, string.format("%g", v), propattr) |
179 elseif proptype == "integer" and v is number then | 192 elseif proptype == "integer" and v is number then |
180 out:text_tag(name, string.format("%d", v), propattr) | 193 out:text_tag(name, string.format("%d", v), propattr) |