Software /
code /
prosody
Comparison
teal-src/util/datamapper.tl @ 11436:5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 07 Mar 2021 00:57:36 +0100 |
parent | 11435:a1fa6202fa13 |
child | 11437:87a684df4b65 |
comparison
equal
deleted
inserted
replaced
11435:a1fa6202fa13 | 11436:5df9ffc25bb4 |
---|---|
92 if schema.type == "object" then | 92 if schema.type == "object" then |
93 return parse_object(schema, s) | 93 return parse_object(schema, s) |
94 end | 94 end |
95 end | 95 end |
96 | 96 |
97 local function unparse ( schema : js.schema_t, t : table, current_name : string, current_ns : string ) : st.stanza_t | |
98 if schema.type == "object" then | |
99 | |
100 if schema.xml then | |
101 if schema.xml.name then | |
102 current_name = schema.xml.name | |
103 end | |
104 if schema.xml.namespace then | |
105 current_ns = schema.xml.namespace | |
106 end | |
107 -- TODO prefix? | |
108 end | |
109 | |
110 local out = st.stanza(current_name, { xmlns = current_ns }) | |
111 | |
112 for prop, propschema in pairs(schema.properties) do | |
113 local v = t[prop] | |
114 | |
115 if v ~= nil then | |
116 local proptype : js.schema_t.type_e | |
117 if propschema is js.schema_t then | |
118 proptype = propschema.type | |
119 elseif propschema is js.schema_t.type_e then | |
120 proptype = propschema | |
121 end | |
122 | |
123 local name = prop | |
124 local namespace = current_ns | |
125 local prefix : string = nil | |
126 local is_attribute = false | |
127 local is_text = false | |
128 | |
129 if propschema is js.schema_t and propschema.xml then | |
130 | |
131 if propschema.xml.name then | |
132 name = propschema.xml.name | |
133 end | |
134 if propschema.xml.namespace then | |
135 namespace = propschema.xml.namespace | |
136 end | |
137 | |
138 if propschema.xml.prefix then | |
139 prefix = propschema.xml.prefix | |
140 end | |
141 | |
142 if propschema.xml.attribute then | |
143 is_attribute = true | |
144 elseif propschema.xml.text then | |
145 is_text = true | |
146 end | |
147 end | |
148 | |
149 if is_attribute then | |
150 local attr = name | |
151 if prefix then | |
152 attr = prefix .. ':' .. name | |
153 elseif namespace ~= current_ns then | |
154 attr = namespace .. "\1" .. name | |
155 end | |
156 | |
157 if proptype == "string" and v is string then | |
158 out.attr[attr] = v | |
159 elseif proptype == "number" and v is number then | |
160 out.attr[attr] = string.format("%g", v) | |
161 elseif proptype == "integer" and v is number then | |
162 out.attr[attr] = string.format("%d", v) | |
163 elseif proptype == "boolean" then | |
164 out.attr[attr] = v and "1" or "0" | |
165 end | |
166 elseif is_text then | |
167 if v is string then | |
168 out:text(v) | |
169 end | |
170 else | |
171 local propattr : { string : string } | |
172 if namespace ~= current_ns then | |
173 propattr = { xmlns = namespace } | |
174 end | |
175 if proptype == "string" and v is string then | |
176 out:text_tag(name, v, propattr) | |
177 elseif proptype == "number" and v is number then | |
178 out:text_tag(name, string.format("%g", v), propattr) | |
179 elseif proptype == "integer" and v is number then | |
180 out:text_tag(name, string.format("%d", v), propattr) | |
181 elseif proptype == "boolean" and v is boolean then | |
182 out:text_tag(name, v and "1" or "0", propattr) | |
183 elseif proptype == "object" and propschema is js.schema_t and v is table then | |
184 local c = unparse(propschema, v, name, namespace); | |
185 if c then | |
186 out:add_direct_child(c); | |
187 end | |
188 -- else TODO | |
189 end | |
190 end | |
191 end | |
192 end | |
193 return out; | |
194 | |
195 end | |
196 end | |
197 | |
97 return { | 198 return { |
98 parse = parse, | 199 parse = parse, |
99 -- unparse = unparse, -- TODO | 200 unparse = unparse, |
100 } | 201 } |