Software /
code /
prosody
Comparison
util/datamapper.lua @ 11455:a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Now it gets the text, attribute or name first, then turns it into
whatever the schema wants. This should be easier to further factor out
into preparation for array support.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 14 Mar 2021 03:06:37 +0100 |
parent | 11454:1d9c1893cc5e |
child | 11456:4e376a43fe40 |
comparison
equal
deleted
inserted
replaced
11454:1d9c1893cc5e | 11455:a5050e21ab08 |
---|---|
3 local function toboolean(s) | 3 local function toboolean(s) |
4 if s == "true" or s == "1" then | 4 if s == "true" or s == "1" then |
5 return true | 5 return true |
6 elseif s == "false" or s == "0" then | 6 elseif s == "false" or s == "0" then |
7 return false | 7 return false |
8 elseif s then | |
9 return true | |
10 end | |
11 end | |
12 | |
13 local function totype(t, s) | |
14 if t == "string" then | |
15 return s | |
16 elseif t == "boolean" then | |
17 return toboolean(s) | |
18 elseif t == "number" or t == "integer" then | |
19 return tonumber(s) | |
8 end | 20 end |
9 end | 21 end |
10 | 22 |
11 local value_goes = {} | 23 local value_goes = {} |
12 | 24 |
27 proptype = propschema.type | 39 proptype = propschema.type |
28 elseif type(propschema) == "string" then | 40 elseif type(propschema) == "string" then |
29 proptype = propschema | 41 proptype = propschema |
30 end | 42 end |
31 | 43 |
44 if proptype == "object" or proptype == "array" then | |
45 value_where = "in_children" | |
46 end | |
47 | |
32 if type(propschema) == "table" and propschema.xml then | 48 if type(propschema) == "table" and propschema.xml then |
33 if propschema.xml.name then | 49 if propschema.xml.name then |
34 name = propschema.xml.name | 50 name = propschema.xml.name |
35 end | 51 end |
36 if propschema.xml.namespace then | 52 if propschema.xml.namespace then |
57 elseif propschema["enum"] then | 73 elseif propschema["enum"] then |
58 enums = propschema["enum"] | 74 enums = propschema["enum"] |
59 end | 75 end |
60 end | 76 end |
61 | 77 |
78 local value | |
62 if value_where == "in_tag_name" then | 79 if value_where == "in_tag_name" then |
63 local c | 80 local c |
64 if proptype == "boolean" then | 81 if proptype == "boolean" then |
65 c = s:get_child(name, namespace); | 82 c = s:get_child(name, namespace); |
66 elseif enums and proptype == "string" then | 83 elseif enums and proptype == "string" then |
72 end | 89 end |
73 end | 90 end |
74 else | 91 else |
75 c = s:get_child(nil, namespace); | 92 c = s:get_child(nil, namespace); |
76 end | 93 end |
77 if c and proptype == "string" then | 94 value = c.name; |
78 out[prop] = c.name; | |
79 elseif proptype == "boolean" and c then | |
80 out[prop] = true; | |
81 end | |
82 elseif value_where == "in_attribute" then | 95 elseif value_where == "in_attribute" then |
83 local attr = name | 96 local attr = name |
84 if prefix then | 97 if prefix then |
85 attr = prefix .. ":" .. name | 98 attr = prefix .. ":" .. name |
86 elseif namespace ~= s.attr.xmlns then | 99 elseif namespace ~= s.attr.xmlns then |
87 attr = namespace .. "\1" .. name | 100 attr = namespace .. "\1" .. name |
88 end | 101 end |
89 if proptype == "string" then | 102 value = s.attr[attr] |
90 out[prop] = s.attr[attr] | |
91 elseif proptype == "integer" or proptype == "number" then | |
92 | |
93 out[prop] = tonumber(s.attr[attr]) | |
94 elseif proptype == "boolean" then | |
95 out[prop] = toboolean(s.attr[attr]) | |
96 | |
97 end | |
98 | 103 |
99 elseif value_where == "in_text" then | 104 elseif value_where == "in_text" then |
100 if proptype == "string" then | 105 value = s:get_text() |
101 out[prop] = s:get_text() | |
102 elseif proptype == "integer" or proptype == "number" then | |
103 out[prop] = tonumber(s:get_text()) | |
104 end | |
105 | 106 |
106 elseif value_where == "in_single_attribute" then | 107 elseif value_where == "in_single_attribute" then |
107 local c = s:get_child(name, namespace) | 108 local c = s:get_child(name, namespace) |
108 local a = c and c.attr[single_attribute] | 109 value = c and c.attr[single_attribute] |
109 if proptype == "string" then | 110 elseif value_where == "in_text_tag" then |
110 out[prop] = a | 111 value = s:get_child_text(name, namespace) |
111 elseif proptype == "integer" or proptype == "number" then | 112 elseif value_where == "in_children" and type(propschema) == "table" then |
112 out[prop] = tonumber(a) | 113 if proptype == "object" then |
113 elseif proptype == "boolean" then | |
114 out[prop] = toboolean(a) | |
115 end | |
116 else | |
117 | |
118 if proptype == "string" then | |
119 out[prop] = s:get_child_text(name, namespace) | |
120 elseif proptype == "integer" or proptype == "number" then | |
121 out[prop] = tonumber(s:get_child_text(name, namespace)) | |
122 elseif proptype == "object" and type(propschema) == "table" then | |
123 local c = s:get_child(name, namespace) | 114 local c = s:get_child(name, namespace) |
124 if c then | 115 if c then |
125 out[prop] = parse_object(propschema, c); | 116 out[prop] = parse_object(propschema, c); |
126 end | 117 end |
127 | 118 |
128 end | 119 end |
120 end | |
121 if value_where ~= "in_children" then | |
122 out[prop] = totype(proptype, value) | |
129 end | 123 end |
130 end | 124 end |
131 end | 125 end |
132 | 126 |
133 return out | 127 return out |