Software /
code /
prosody
Annotate
teal-src/util/datamapper.tl @ 11442:95f0d77175ca
util.jsonschema: Implement the "contains" keyword
And apparently I had mistaken this for an array
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 09 Mar 2021 02:35:00 +0100 |
parent | 11439:9abcdfdcdb01 |
child | 11450:25829015917c |
rev | line source |
---|---|
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 local st = require "util.stanza"; |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 local js = require "util.jsonschema" |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 local function toboolean ( s : string ) : boolean |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 if s == "true" or s == "1" then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 return true |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 elseif s == "false" or s == "0" then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 return false |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 local function parse_object (schema : js.schema_t, s : st.stanza_t) : table |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local out : { string : any } = {} |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 if schema.properties then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 for prop, propschema in pairs(schema.properties) do |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 -- TODO factor out, if it's generic enough |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 local name = prop |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 local namespace = s.attr.xmlns; |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 local prefix : string = nil |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 local is_attribute = false |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 local is_text = false |
11437
87a684df4b65
util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents:
11436
diff
changeset
|
22 local name_is_value = false; |
11439
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
23 local single_attribute : string |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 local proptype : js.schema_t.type_e |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 if propschema is js.schema_t then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 proptype = propschema.type |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 elseif propschema is js.schema_t.type_e then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 proptype = propschema |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 if propschema is js.schema_t and propschema.xml then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 if propschema.xml.name then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 name = propschema.xml.name |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 if propschema.xml.namespace then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 namespace = propschema.xml.namespace |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 if propschema.xml.prefix then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 prefix = propschema.xml.prefix |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 if propschema.xml.attribute then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 is_attribute = true |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 elseif propschema.xml.text then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 is_text = true |
11437
87a684df4b65
util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents:
11436
diff
changeset
|
46 elseif propschema.xml.x_name_is_value then |
87a684df4b65
util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents:
11436
diff
changeset
|
47 name_is_value = true |
11439
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
48 elseif propschema.xml.x_single_attribute then |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
49 single_attribute = propschema.xml.x_single_attribute |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 |
11437
87a684df4b65
util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents:
11436
diff
changeset
|
53 if name_is_value then |
87a684df4b65
util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents:
11436
diff
changeset
|
54 local c = s:get_child(nil, namespace); |
11438
b7807583de34
util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents:
11437
diff
changeset
|
55 if c and proptype == "string" then |
11437
87a684df4b65
util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents:
11436
diff
changeset
|
56 out[prop] = c.name; |
11438
b7807583de34
util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents:
11437
diff
changeset
|
57 elseif proptype == "boolean" and c then |
b7807583de34
util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents:
11437
diff
changeset
|
58 out[prop] = true; |
11437
87a684df4b65
util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents:
11436
diff
changeset
|
59 end |
87a684df4b65
util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents:
11436
diff
changeset
|
60 elseif is_attribute then |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 local attr = name |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 if prefix then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 attr = prefix .. ':' .. name |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 elseif namespace ~= s.attr.xmlns then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 attr = namespace .. "\1" .. name |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 if proptype == "string" then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 out[prop] = s.attr[attr] |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 elseif proptype == "integer" or proptype == "number" then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 -- TODO floor if integer ? |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 out[prop] = tonumber(s.attr[attr]) |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 elseif proptype == "boolean" then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 out[prop] = toboolean(s.attr[attr]) |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 -- else TODO |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 elseif is_text then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 if proptype == "string" then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 out[prop] = s:get_text() |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 elseif proptype == "integer" or proptype == "number" then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 out[prop] = tonumber(s:get_text()) |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 |
11439
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
84 elseif single_attribute then |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
85 local c = s:get_child(name, namespace) |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
86 local a = c and c.attr[single_attribute] |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
87 if proptype == "string" then |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
88 out[prop] = a |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
89 elseif proptype == "integer" or proptype == "number" then |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
90 out[prop] = tonumber(a) |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
91 elseif proptype == "boolean" then |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
92 out[prop] = toboolean(a) |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
93 end |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 else |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 if proptype == "string" then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 out[prop] = s:get_child_text(name, namespace) |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 elseif proptype == "integer" or proptype == "number" then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 out[prop] = tonumber(s:get_child_text(name, namespace)) |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 elseif proptype == "object" and propschema is js.schema_t then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 local c = s:get_child(name, namespace) |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 if c then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 out[prop] = parse_object(propschema, c); |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 -- else TODO |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 return out |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
112 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 local function parse (schema : js.schema_t, s : st.stanza_t) : table |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 if schema.type == "object" then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
116 return parse_object(schema, s) |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
120 local function unparse ( schema : js.schema_t, t : table, current_name : string, current_ns : string ) : st.stanza_t |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
121 if schema.type == "object" then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
122 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
123 if schema.xml then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
124 if schema.xml.name then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
125 current_name = schema.xml.name |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
126 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
127 if schema.xml.namespace then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
128 current_ns = schema.xml.namespace |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
129 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
130 -- TODO prefix? |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
131 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
132 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
133 local out = st.stanza(current_name, { xmlns = current_ns }) |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
134 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
135 for prop, propschema in pairs(schema.properties) do |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
136 local v = t[prop] |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
137 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
138 if v ~= nil then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
139 local proptype : js.schema_t.type_e |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
140 if propschema is js.schema_t then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
141 proptype = propschema.type |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
142 elseif propschema is js.schema_t.type_e then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
143 proptype = propschema |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
144 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
145 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
146 local name = prop |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
147 local namespace = current_ns |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
148 local prefix : string = nil |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
149 local is_attribute = false |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
150 local is_text = false |
11437
87a684df4b65
util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents:
11436
diff
changeset
|
151 local name_is_value = false; |
11439
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
152 local single_attribute : string |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
153 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
154 if propschema is js.schema_t and propschema.xml then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
155 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
156 if propschema.xml.name then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
157 name = propschema.xml.name |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
158 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
159 if propschema.xml.namespace then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
160 namespace = propschema.xml.namespace |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
161 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
162 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
163 if propschema.xml.prefix then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
164 prefix = propschema.xml.prefix |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
165 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
166 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
167 if propschema.xml.attribute then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
168 is_attribute = true |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
169 elseif propschema.xml.text then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
170 is_text = true |
11437
87a684df4b65
util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents:
11436
diff
changeset
|
171 elseif propschema.xml.x_name_is_value then |
87a684df4b65
util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents:
11436
diff
changeset
|
172 name_is_value = true |
11439
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
173 elseif propschema.xml.x_single_attribute then |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
174 single_attribute = propschema.xml.x_single_attribute |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
175 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
176 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
177 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
178 if is_attribute then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
179 local attr = name |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
180 if prefix then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
181 attr = prefix .. ':' .. name |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
182 elseif namespace ~= current_ns then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
183 attr = namespace .. "\1" .. name |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
184 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
185 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
186 if proptype == "string" and v is string then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
187 out.attr[attr] = v |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
188 elseif proptype == "number" and v is number then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
189 out.attr[attr] = string.format("%g", v) |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
190 elseif proptype == "integer" and v is number then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
191 out.attr[attr] = string.format("%d", v) |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
192 elseif proptype == "boolean" then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
193 out.attr[attr] = v and "1" or "0" |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
194 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
195 elseif is_text then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
196 if v is string then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
197 out:text(v) |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
198 end |
11439
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
199 elseif single_attribute then |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
200 local propattr : { string : string } = {} |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
201 |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
202 if namespace ~= current_ns then |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
203 propattr.xmlns = namespace |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
204 end |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
205 |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
206 if proptype == "string" and v is string then |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
207 propattr[single_attribute] = v |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
208 elseif proptype == "number" and v is number then |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
209 propattr[single_attribute] = string.format("%g", v) |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
210 elseif proptype == "integer" and v is number then |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
211 propattr[single_attribute] = string.format("%d", v) |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
212 elseif proptype == "boolean" and v is boolean then |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
213 propattr[single_attribute] = v and "1" or "0" |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
214 end |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
215 out:tag(name, propattr):up(); |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
216 |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
217 else |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
218 local propattr : { string : string } |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
219 if namespace ~= current_ns then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
220 propattr = { xmlns = namespace } |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
221 end |
11438
b7807583de34
util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents:
11437
diff
changeset
|
222 if name_is_value then |
b7807583de34
util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents:
11437
diff
changeset
|
223 if proptype == "string" and v is string then |
b7807583de34
util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents:
11437
diff
changeset
|
224 out:tag(v, propattr):up(); |
b7807583de34
util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents:
11437
diff
changeset
|
225 elseif proptype == "boolean" and v == true then |
b7807583de34
util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents:
11437
diff
changeset
|
226 out:tag(name, propattr):up(); |
b7807583de34
util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents:
11437
diff
changeset
|
227 end |
11437
87a684df4b65
util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents:
11436
diff
changeset
|
228 elseif proptype == "string" and v is string then |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
229 out:text_tag(name, v, propattr) |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
230 elseif proptype == "number" and v is number then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
231 out:text_tag(name, string.format("%g", v), propattr) |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
232 elseif proptype == "integer" and v is number then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
233 out:text_tag(name, string.format("%d", v), propattr) |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
234 elseif proptype == "boolean" and v is boolean then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
235 out:text_tag(name, v and "1" or "0", propattr) |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
236 elseif proptype == "object" and propschema is js.schema_t and v is table then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
237 local c = unparse(propschema, v, name, namespace); |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
238 if c then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
239 out:add_direct_child(c); |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
240 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
241 -- else TODO |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
242 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
243 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
244 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
245 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
246 return out; |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
247 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
248 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
249 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
250 |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
251 return { |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
252 parse = parse, |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
253 unparse = unparse, |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
254 } |