Annotate

teal-src/util/datamapper.tl @ 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11450
25829015917c util.datamapper: Add Copyright header
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
1 -- Copyright (C) 2021 Kim Alvefur
25829015917c util.datamapper: Add Copyright header
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
2 --
25829015917c util.datamapper: Add Copyright header
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
3 -- This project is MIT/X11 licensed. Please see the
25829015917c util.datamapper: Add Copyright header
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
4 -- COPYING file in the source package for more information.
25829015917c util.datamapper: Add Copyright header
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
5 --
11451
ee4f2296e7df util.datamapper: Add references with notes
Kim Alvefur <zash@zash.se>
parents: 11450
diff changeset
6 -- Based on
ee4f2296e7df util.datamapper: Add references with notes
Kim Alvefur <zash@zash.se>
parents: 11450
diff changeset
7 -- https://json-schema.org/draft/2020-12/json-schema-core.html
ee4f2296e7df util.datamapper: Add references with notes
Kim Alvefur <zash@zash.se>
parents: 11450
diff changeset
8 -- https://json-schema.org/draft/2020-12/json-schema-validation.html
ee4f2296e7df util.datamapper: Add references with notes
Kim Alvefur <zash@zash.se>
parents: 11450
diff changeset
9 -- http://spec.openapis.org/oas/v3.0.1#xmlObject
ee4f2296e7df util.datamapper: Add references with notes
Kim Alvefur <zash@zash.se>
parents: 11450
diff changeset
10 -- https://github.com/OAI/OpenAPI-Specification/issues/630 (text:true)
ee4f2296e7df util.datamapper: Add references with notes
Kim Alvefur <zash@zash.se>
parents: 11450
diff changeset
11 --
ee4f2296e7df util.datamapper: Add references with notes
Kim Alvefur <zash@zash.se>
parents: 11450
diff changeset
12 -- XML Object Extensions:
ee4f2296e7df util.datamapper: Add references with notes
Kim Alvefur <zash@zash.se>
parents: 11450
diff changeset
13 -- text to refer to the text content at the same time as attributes
ee4f2296e7df util.datamapper: Add references with notes
Kim Alvefur <zash@zash.se>
parents: 11450
diff changeset
14 -- x_name_is_value for enum fields where the <tag-name/> is the value
ee4f2296e7df util.datamapper: Add references with notes
Kim Alvefur <zash@zash.se>
parents: 11450
diff changeset
15 -- x_single_attribute for <tag attr="this"/>
ee4f2296e7df util.datamapper: Add references with notes
Kim Alvefur <zash@zash.se>
parents: 11450
diff changeset
16 --
11452
c799bac7ca59 util.datamapper: Some TODOs
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
17 -- TODO arrays
c799bac7ca59 util.datamapper: Some TODOs
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
18 -- TODO pointers
c799bac7ca59 util.datamapper: Some TODOs
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
19 -- TODO cleanup / refactor
c799bac7ca59 util.datamapper: Some TODOs
Kim Alvefur <zash@zash.se>
parents: 11451
diff changeset
20 --
11450
25829015917c util.datamapper: Add Copyright header
Kim Alvefur <zash@zash.se>
parents: 11439
diff changeset
21
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 local st = require "util.stanza";
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 local js = require "util.jsonschema"
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 function toboolean ( s : string ) : boolean
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 if s == "true" or s == "1" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 return true
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 elseif s == "false" or s == "0" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 return false
11455
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
30 elseif s then
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
31 return true
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
32 end
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
33 end
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
34
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
35 local function totype(t : js.schema_t.type_e, s : string) : any
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
36 if t == "string" then
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
37 return s;
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
38 elseif t == "boolean" then
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
39 return toboolean(s)
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
40 elseif t == "number" or t == "integer" then
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
41 return tonumber(s)
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44
11454
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
45 local enum value_goes
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
46 "in_tag_name"
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
47 "in_text"
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
48 "in_text_tag"
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
49 "in_attribute"
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
50 "in_single_attribute"
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
51 "in_children"
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
52 end
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
53
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 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
55 local out : { string : any } = {}
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 if schema.properties then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 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
58 -- 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
59 local name = prop
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 local namespace = s.attr.xmlns;
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 local prefix : string = nil
11454
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
62 local value_where : value_goes = "in_text_tag"
11439
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
63 local single_attribute : string
11453
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11452
diff changeset
64 local enums : { any }
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 local proptype : js.schema_t.type_e
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 if propschema is js.schema_t then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 proptype = propschema.type
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 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
70 proptype = propschema
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72
11455
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
73 if proptype == "object" or proptype == "array" then
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
74 value_where = "in_children"
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
75 end
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
76
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 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
78 if propschema.xml.name then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 name = propschema.xml.name
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 if propschema.xml.namespace then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 namespace = propschema.xml.namespace
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 if propschema.xml.prefix then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 prefix = propschema.xml.prefix
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 if propschema.xml.attribute then
11454
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
88 value_where = "in_attribute"
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 elseif propschema.xml.text then
11451
ee4f2296e7df util.datamapper: Add references with notes
Kim Alvefur <zash@zash.se>
parents: 11450
diff changeset
90 -- XXX Not yet in OpenAPI
11454
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
91 value_where = "in_text"
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
92 elseif propschema.xml.x_name_is_value then
11451
ee4f2296e7df util.datamapper: Add references with notes
Kim Alvefur <zash@zash.se>
parents: 11450
diff changeset
93 -- XXX Custom extension
11454
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
94 value_where = "in_tag_name"
11439
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
95 elseif propschema.xml.x_single_attribute then
11451
ee4f2296e7df util.datamapper: Add references with notes
Kim Alvefur <zash@zash.se>
parents: 11450
diff changeset
96 -- XXX Custom extension
11439
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
97 single_attribute = propschema.xml.x_single_attribute
11454
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
98 value_where = "in_single_attribute"
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 end
11453
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11452
diff changeset
100 if propschema["const"] then
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11452
diff changeset
101 enums = { propschema["const"] }
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11452
diff changeset
102 elseif propschema["enum"] then
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11452
diff changeset
103 enums = propschema["enum"]
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11452
diff changeset
104 end
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106
11455
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
107 local value : string
11454
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
108 if value_where == "in_tag_name" then
11453
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11452
diff changeset
109 local c : st.stanza_t
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11452
diff changeset
110 if proptype == "boolean" then
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11452
diff changeset
111 c = s:get_child(name, namespace);
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11452
diff changeset
112 elseif enums and proptype == "string" then
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11452
diff changeset
113 -- XXX O(n²) ?
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11452
diff changeset
114 -- Probably better to flip the table and loop over :childtags(nil, ns), should be 2xO(n)
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11452
diff changeset
115 -- BUT works first, optimize later
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11452
diff changeset
116 for i = 1, #enums do
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11452
diff changeset
117 c = s:get_child(enums[i] as string, namespace);
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11452
diff changeset
118 if c then break end
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11452
diff changeset
119 end
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11452
diff changeset
120 else
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11452
diff changeset
121 c = s:get_child(nil, namespace);
f0037234b2e9 util.datamapper: Enumerated elements
Kim Alvefur <zash@zash.se>
parents: 11452
diff changeset
122 end
11455
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
123 value = c.name;
11454
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
124 elseif value_where == "in_attribute" then
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 local attr = name
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 if prefix then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 attr = prefix .. ':' .. name
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 elseif namespace ~= s.attr.xmlns then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 attr = namespace .. "\1" .. name
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 end
11455
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
131 value = s.attr[attr]
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132
11454
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
133 elseif value_where == "in_text" then
11455
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
134 value = s:get_text()
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135
11454
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
136 elseif value_where == "in_single_attribute" then
11439
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
137 local c = s:get_child(name, namespace)
11455
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
138 value = c and c.attr[single_attribute]
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
139 elseif value_where == "in_text_tag" then
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
140 value = s:get_child_text(name, namespace)
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
141 elseif value_where == "in_children" and propschema is js.schema_t then
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
142 if proptype == "object" then
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143 local c = s:get_child(name, namespace)
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 if c then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 out[prop] = parse_object(propschema, c);
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 -- else TODO
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149 end
11455
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
150 if value_where ~= "in_children" then
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
151 out[prop] = totype(proptype, value)
a5050e21ab08 util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents: 11454
diff changeset
152 end
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156 return out
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 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
160 if schema.type == "object" then
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 return parse_object(schema, s)
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 end
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164
11436
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
165 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
166 if schema.type == "object" then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
167
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
168 if schema.xml then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
169 if schema.xml.name then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
170 current_name = schema.xml.name
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
171 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
172 if schema.xml.namespace then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
173 current_ns = schema.xml.namespace
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
174 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
175 -- TODO prefix?
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 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
179
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
180 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
181 local v = t[prop]
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
182
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
183 if v ~= nil then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
184 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
185 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
186 proptype = propschema.type
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
187 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
188 proptype = propschema
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
189 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
190
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
191 local name = prop
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
192 local namespace = current_ns
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
193 local prefix : string = nil
11454
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
194 local value_where : value_goes = "in_text_tag"
11439
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
195 local single_attribute : string
11436
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
196
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
197 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
198
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
199 if propschema.xml.name then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
200 name = propschema.xml.name
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
201 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
202 if propschema.xml.namespace then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
203 namespace = propschema.xml.namespace
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
204 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
205
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
206 if propschema.xml.prefix then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
207 prefix = propschema.xml.prefix
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
208 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
209
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
210 if propschema.xml.attribute then
11454
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
211 value_where = "in_attribute"
11436
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
212 elseif propschema.xml.text then
11454
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
213 value_where = "in_text"
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
214 elseif propschema.xml.x_name_is_value then
11454
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
215 value_where = "in_tag_name"
11439
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
216 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
217 single_attribute = propschema.xml.x_single_attribute
11454
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
218 value_where = "in_single_attribute"
11436
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
219 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
220 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
221
11454
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
222 if value_where == "in_attribute" then
11436
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
223 local attr = name
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
224 if prefix then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
225 attr = prefix .. ':' .. name
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
226 elseif namespace ~= current_ns then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
227 attr = namespace .. "\1" .. name
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
228 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
229
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
230 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
231 out.attr[attr] = v
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
232 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
233 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
234 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
235 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
236 elseif proptype == "boolean" then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
237 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
238 end
11454
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
239 elseif value_where == "in_text" then
11436
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
240 if v is string then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
241 out:text(v)
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
242 end
11454
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
243 elseif value_where == "in_single_attribute" then
11439
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
244 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
245
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
246 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
247 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
248 end
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
249
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
250 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
251 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
252 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
253 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
254 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
255 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
256 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
257 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
258 end
9abcdfdcdb01 util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents: 11438
diff changeset
259 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
260
11436
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
261 else
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
262 local propattr : { string : string }
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
263 if namespace ~= current_ns then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
264 propattr = { xmlns = namespace }
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
265 end
11454
1d9c1893cc5e util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents: 11453
diff changeset
266 if value_where == "in_tag_name" then
11438
b7807583de34 util.datamapper: Add logic for "boolean" tags here the presence means true
Kim Alvefur <zash@zash.se>
parents: 11437
diff changeset
267 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
268 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
269 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
270 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
271 end
11437
87a684df4b65 util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents: 11436
diff changeset
272 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
273 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
274 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
275 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
276 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
277 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
278 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
279 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
280 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
281 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
282 if c then
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
283 out:add_direct_child(c);
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
284 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
285 -- else TODO
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
286 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
287 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
288 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
289 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
290 return out;
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
291
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
292 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
293 end
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
294
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
295 return {
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
296 parse = parse,
11436
5df9ffc25bb4 util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents: 11435
diff changeset
297 unparse = unparse,
11435
a1fa6202fa13 util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff changeset
298 }