aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2021-03-22 22:24:39 +0100
committerKim Alvefur <zash@zash.se>2021-03-22 22:24:39 +0100
commite783d99cc2eb5703de84757e8ac7346946a77648 (patch)
tree447247ec286faaaa77ac51f4fd2b08ef06143163
parentf2f1714e47fb441f70fd77319765221c5e06c800 (diff)
downloadprosody-e783d99cc2eb5703de84757e8ac7346946a77648.tar.gz
prosody-e783d99cc2eb5703de84757e8ac7346946a77648.zip
util.datamapper: Factor out conversion from any value to XML string
Since this was the last severely duplicated code left.
-rw-r--r--teal-src/util/datamapper.tl46
-rw-r--r--util/datamapper.lua46
2 files changed, 34 insertions, 58 deletions
diff --git a/teal-src/util/datamapper.tl b/teal-src/util/datamapper.tl
index 3b652639..39d35e82 100644
--- a/teal-src/util/datamapper.tl
+++ b/teal-src/util/datamapper.tl
@@ -231,6 +231,18 @@ local function parse (schema : json_schema_object, s : st.stanza_t) : table
end
end
+local function toxmlstring(proptype : json_type_name, v : any) : string
+ if proptype == "string" and v is string then
+ return v
+ elseif proptype == "number" and v is number then
+ return string.format("%g", v)
+ elseif proptype == "integer" and v is number then -- TODO is integer
+ return string.format("%d", v)
+ elseif proptype == "boolean" then
+ return v and "1" or "0"
+ end
+end
+
local unparse : function (json_schema_object, table, string, string, st.stanza_t) : st.stanza_t
local function unparse_property(out : st.stanza_t, v : any, proptype : json_type_name, propschema : schema_t, value_where : value_goes, name : string, namespace : string, current_ns : string, prefix : string, single_attribute : string)
@@ -242,19 +254,9 @@ local function unparse_property(out : st.stanza_t, v : any, proptype : json_type
attr = namespace .. "\1" .. name
end
- if proptype == "string" and v is string then
- out.attr[attr] = v
- elseif proptype == "number" and v is number then
- out.attr[attr] = string.format("%g", v)
- elseif proptype == "integer" and v is number then -- TODO is integer
- out.attr[attr] = string.format("%d", v)
- elseif proptype == "boolean" then
- out.attr[attr] = v and "1" or "0"
- end
+ out.attr[attr] = toxmlstring(proptype, v)
elseif value_where == "in_text" then
- if v is string then
- out:text(v)
- end
+ out:text(toxmlstring(proptype, v))
elseif value_where == "in_single_attribute" then
assert(single_attribute)
local propattr : { string : string } = {}
@@ -263,15 +265,7 @@ local function unparse_property(out : st.stanza_t, v : any, proptype : json_type
propattr.xmlns = namespace
end
- if proptype == "string" and v is string then
- propattr[single_attribute] = v
- elseif proptype == "number" and v is number then
- propattr[single_attribute] = string.format("%g", v)
- elseif proptype == "integer" and v is number then -- TODO is integer
- propattr[single_attribute] = string.format("%d", v)
- elseif proptype == "boolean" and v is boolean then
- propattr[single_attribute] = v and "1" or "0"
- end
+ propattr[single_attribute] = toxmlstring(proptype, v)
out:tag(name, propattr):up();
else
@@ -285,14 +279,6 @@ local function unparse_property(out : st.stanza_t, v : any, proptype : json_type
elseif proptype == "boolean" and v == true then
out:tag(name, propattr):up();
end
- elseif proptype == "string" and v is string then
- out:text_tag(name, v, propattr)
- elseif proptype == "number" and v is number then
- out:text_tag(name, string.format("%g", v), propattr)
- elseif proptype == "integer" and v is number then -- TODO is integer
- out:text_tag(name, string.format("%d", v), propattr)
- elseif proptype == "boolean" and v is boolean then
- out:text_tag(name, v and "1" or "0", propattr)
elseif proptype == "object" and propschema is json_schema_object and v is table then
local c = unparse(propschema, v, name, namespace);
if c then
@@ -307,6 +293,8 @@ local function unparse_property(out : st.stanza_t, v : any, proptype : json_type
else
unparse(propschema, v, name, namespace, out);
end
+ else
+ out:text_tag(name, toxmlstring(proptype, v), propattr)
end
end
end
diff --git a/util/datamapper.lua b/util/datamapper.lua
index 96b4a42e..3377413f 100644
--- a/util/datamapper.lua
+++ b/util/datamapper.lua
@@ -199,6 +199,18 @@ local function parse(schema, s)
end
end
+local function toxmlstring(proptype, v)
+ if proptype == "string" and type(v) == "string" then
+ return v
+ elseif proptype == "number" and type(v) == "number" then
+ return string.format("%g", v)
+ elseif proptype == "integer" and type(v) == "number" then
+ return string.format("%d", v)
+ elseif proptype == "boolean" then
+ return v and "1" or "0"
+ end
+end
+
local unparse
local function unparse_property(out, v, proptype, propschema, value_where, name, namespace, current_ns, prefix, single_attribute)
@@ -210,19 +222,9 @@ local function unparse_property(out, v, proptype, propschema, value_where, name,
attr = namespace .. "\1" .. name
end
- if proptype == "string" and type(v) == "string" then
- out.attr[attr] = v
- elseif proptype == "number" and type(v) == "number" then
- out.attr[attr] = string.format("%g", v)
- elseif proptype == "integer" and type(v) == "number" then
- out.attr[attr] = string.format("%d", v)
- elseif proptype == "boolean" then
- out.attr[attr] = v and "1" or "0"
- end
+ out.attr[attr] = toxmlstring(proptype, v)
elseif value_where == "in_text" then
- if type(v) == "string" then
- out:text(v)
- end
+ out:text(toxmlstring(proptype, v))
elseif value_where == "in_single_attribute" then
assert(single_attribute)
local propattr = {}
@@ -231,15 +233,7 @@ local function unparse_property(out, v, proptype, propschema, value_where, name,
propattr.xmlns = namespace
end
- if proptype == "string" and type(v) == "string" then
- propattr[single_attribute] = v
- elseif proptype == "number" and type(v) == "number" then
- propattr[single_attribute] = string.format("%g", v)
- elseif proptype == "integer" and type(v) == "number" then
- propattr[single_attribute] = string.format("%d", v)
- elseif proptype == "boolean" and type(v) == "boolean" then
- propattr[single_attribute] = v and "1" or "0"
- end
+ propattr[single_attribute] = toxmlstring(proptype, v)
out:tag(name, propattr):up();
else
@@ -253,14 +247,6 @@ local function unparse_property(out, v, proptype, propschema, value_where, name,
elseif proptype == "boolean" and v == true then
out:tag(name, propattr):up();
end
- elseif proptype == "string" and type(v) == "string" then
- out:text_tag(name, v, propattr)
- elseif proptype == "number" and type(v) == "number" then
- out:text_tag(name, string.format("%g", v), propattr)
- elseif proptype == "integer" and type(v) == "number" then
- out:text_tag(name, string.format("%d", v), propattr)
- elseif proptype == "boolean" and type(v) == "boolean" then
- out:text_tag(name, v and "1" or "0", propattr)
elseif proptype == "object" and type(propschema) == "table" and type(v) == "table" then
local c = unparse(propschema, v, name, namespace);
if c then
@@ -275,6 +261,8 @@ local function unparse_property(out, v, proptype, propschema, value_where, name,
else
unparse(propschema, v, name, namespace, out);
end
+ else
+ out:text_tag(name, toxmlstring(proptype, v), propattr)
end
end
end