Mercurial > prosody-hg
comparison util/datamapper.lua @ 13883:b713da7527d2
teal: Regenerate Lua files
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Fri, 09 May 2025 00:50:36 +0200 |
| parents | d10957394a3c |
| children |
comparison
equal
deleted
inserted
replaced
| 13882:1ff24220fa2c | 13883:b713da7527d2 |
|---|---|
| 1 -- This file is generated from teal-src/util/datamapper.lua | 1 -- This file is generated from teal-src/prosody/util/datamapper.tl |
| 2 | |
| 3 if not math.type then | 2 if not math.type then |
| 4 require("prosody.util.mathcompat") | 3 require("prosody.util.mathcompat") |
| 5 end | 4 end |
| 6 | 5 |
| 7 local st = require("prosody.util.stanza"); | 6 local st = require("prosody.util.stanza"); |
| 7 local json = require("prosody.util.json") | |
| 8 local pointer = require("prosody.util.jsonpointer"); | 8 local pointer = require("prosody.util.jsonpointer"); |
| 9 | 9 |
| 10 local schema_t = {} | 10 local json_schema_object = require("prosody.util.jsonschema") |
| 11 | 11 |
| 12 local function toboolean(s) | 12 local function toboolean(s) |
| 13 if s == "true" or s == "1" then | 13 if s == "true" or s == "1" then |
| 14 return true | 14 return true |
| 15 elseif s == "false" or s == "0" then | 15 elseif s == "false" or s == "0" then |
| 29 return toboolean(s) | 29 return toboolean(s) |
| 30 elseif t == "number" or t == "integer" then | 30 elseif t == "number" or t == "integer" then |
| 31 return tonumber(s) | 31 return tonumber(s) |
| 32 end | 32 end |
| 33 end | 33 end |
| 34 | |
| 35 local value_goes = {} | |
| 36 | 34 |
| 37 local function resolve_schema(schema, root) | 35 local function resolve_schema(schema, root) |
| 38 if type(schema) == "table" then | 36 if type(schema) == "table" then |
| 39 if schema["$ref"] and schema["$ref"]:sub(1, 1) == "#" then | 37 if schema["$ref"] and schema["$ref"]:sub(1, 1) == "#" then |
| 40 return pointer.resolve(root, schema["$ref"]:sub(2)) | 38 return pointer.resolve(root, schema["$ref"]:sub(2)) |
| 67 local single_attribute | 65 local single_attribute |
| 68 local enums | 66 local enums |
| 69 | 67 |
| 70 if type(propschema) == "table" then | 68 if type(propschema) == "table" then |
| 71 proptype = guess_schema_type(propschema); | 69 proptype = guess_schema_type(propschema); |
| 72 elseif type(propschema) == "string" then | |
| 73 error("schema as string is not supported: " .. propschema .. " {" .. current_ns .. "}" .. propname) | |
| 74 end | 70 end |
| 75 | 71 |
| 76 if proptype == "object" or proptype == "array" then | 72 if proptype == "object" or proptype == "array" then |
| 77 value_where = "in_children" | 73 value_where = "in_children" |
| 78 end | 74 end |
| 101 single_attribute = xml.x_single_attribute | 97 single_attribute = xml.x_single_attribute |
| 102 value_where = "in_single_attribute" | 98 value_where = "in_single_attribute" |
| 103 end | 99 end |
| 104 end | 100 end |
| 105 if propschema["const"] then | 101 if propschema["const"] then |
| 106 enums = {propschema["const"]} | 102 enums = { propschema["const"] } |
| 107 elseif propschema["enum"] then | 103 elseif propschema["enum"] then |
| 108 enums = propschema["enum"] | 104 enums = propschema["enum"] |
| 109 end | 105 end |
| 110 end | 106 end |
| 111 | 107 |
| 135 return c.name | 131 return c.name |
| 136 end | 132 end |
| 137 elseif value_where == "in_attribute" then | 133 elseif value_where == "in_attribute" then |
| 138 local attr = name | 134 local attr = name |
| 139 if prefix then | 135 if prefix then |
| 140 attr = prefix .. ":" .. name | 136 attr = prefix .. ':' .. name |
| 141 elseif namespace and namespace ~= s.attr.xmlns then | 137 elseif namespace and namespace ~= s.attr.xmlns then |
| 142 attr = namespace .. "\1" .. name | 138 attr = namespace .. "\1" .. name |
| 143 end | 139 end |
| 144 return s.attr[attr] | 140 return s.attr[attr] |
| 145 | 141 |
| 241 local function toxmlstring(proptype, v) | 237 local function toxmlstring(proptype, v) |
| 242 if proptype == "string" and type(v) == "string" then | 238 if proptype == "string" and type(v) == "string" then |
| 243 return v | 239 return v |
| 244 elseif proptype == "number" and type(v) == "number" then | 240 elseif proptype == "number" and type(v) == "number" then |
| 245 return string.format("%g", v) | 241 return string.format("%g", v) |
| 246 elseif proptype == "integer" and type(v) == "number" then | 242 elseif proptype == "integer" and math.type(v) == "integer" then |
| 247 return string.format("%d", v) | 243 return string.format("%d", v) |
| 248 elseif proptype == "boolean" then | 244 elseif proptype == "boolean" then |
| 249 return v and "1" or "0" | 245 return v and "1" or "0" |
| 250 end | 246 end |
| 251 end | 247 end |
| 252 | 248 |
| 253 local unparse | 249 local unparse |
| 254 | 250 |
| 255 local function unparse_property(out, v, proptype, propschema, value_where, name, namespace, current_ns, prefix, | 251 local function unparse_property(out, v, proptype, propschema, value_where, name, namespace, current_ns, prefix, single_attribute, root) |
| 256 single_attribute, root) | |
| 257 | 252 |
| 258 if value_where == "in_attribute" then | 253 if value_where == "in_attribute" then |
| 259 local attr = name | 254 local attr = name |
| 260 if prefix then | 255 if prefix then |
| 261 attr = prefix .. ":" .. name | 256 attr = prefix .. ':' .. name |
| 262 elseif namespace and namespace ~= current_ns then | 257 elseif namespace and namespace ~= current_ns then |
| 263 attr = namespace .. "\1" .. name | 258 attr = namespace .. "\1" .. name |
| 264 end | 259 end |
| 265 | 260 |
| 266 out.attr[attr] = toxmlstring(proptype, v) | 261 out.attr[attr] = toxmlstring(proptype, v) |
| 278 out:tag(name, propattr):up(); | 273 out:tag(name, propattr):up(); |
| 279 | 274 |
| 280 else | 275 else |
| 281 local propattr | 276 local propattr |
| 282 if namespace ~= current_ns then | 277 if namespace ~= current_ns then |
| 283 propattr = {xmlns = namespace} | 278 propattr = { xmlns = namespace } |
| 284 end | 279 end |
| 285 if value_where == "in_tag_name" then | 280 if value_where == "in_tag_name" then |
| 286 if proptype == "string" and type(v) == "string" then | 281 if proptype == "string" and type(v) == "string" then |
| 287 out:tag(v, propattr):up(); | 282 out:tag(v, propattr):up(); |
| 288 elseif proptype == "boolean" and v == true then | 283 elseif proptype == "boolean" and v == true then |
| 322 current_ns = schema.xml.namespace | 317 current_ns = schema.xml.namespace |
| 323 end | 318 end |
| 324 | 319 |
| 325 end | 320 end |
| 326 | 321 |
| 327 local out = ctx or st.stanza(current_name, {xmlns = current_ns}) | 322 local out = ctx or st.stanza(current_name, { xmlns = current_ns }) |
| 328 | 323 |
| 329 local s_type = guess_schema_type(schema) | 324 local s_type = guess_schema_type(schema) |
| 330 if s_type == "object" then | 325 if s_type == "object" then |
| 331 | 326 |
| 332 for prop, propschema in pairs(schema.properties) do | 327 for prop, propschema in pairs(schema.properties) do |
| 348 end | 343 end |
| 349 return out | 344 return out |
| 350 end | 345 end |
| 351 end | 346 end |
| 352 | 347 |
| 353 return {parse = parse; unparse = unparse} | 348 return { parse = parse; unparse = unparse } |
