Software /
code /
prosody
Annotate
teal-src/util/datamapper.tl @ 11466:c098d07e6717
util.datamapper: Finally implement support for parsing arrays
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 20 Mar 2021 20:45:06 +0100 |
parent | 11465:19a88b61ab4e |
child | 11467:88792dd2bee9 |
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 | 17 -- TODO arrays |
18 -- TODO pointers | |
19 -- TODO cleanup / refactor | |
11459
86904555bffc
teal: Use new integer support in Teal 0.13.0
Kim Alvefur <zash@zash.se>
parents:
11458
diff
changeset
|
20 -- TODO s/number/integer/ once we have appropriate math.type() compat |
11452 | 21 -- |
11450
25829015917c
util.datamapper: Add Copyright header
Kim Alvefur <zash@zash.se>
parents:
11439
diff
changeset
|
22 |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 local st = require "util.stanza"; |
11461
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
24 local json = require"util.json" |
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
25 |
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
26 local json_type_name = json.json_type_name; |
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
27 local json_schema_object = require "util.jsonschema" |
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
28 local type schema_t = boolean | json_type_name | json_schema_object |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 local function toboolean ( s : string ) : boolean |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 if s == "true" or s == "1" then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 return true |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 elseif s == "false" or s == "0" then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 return false |
11455
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
35 elseif s then |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
36 return true |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
37 end |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
38 end |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
39 |
11461
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
40 local function totype(t : json_type_name, s : string) : any |
11455
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
41 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
|
42 return s; |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
43 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
|
44 return toboolean(s) |
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
45 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
|
46 return tonumber(s) |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
50 local enum value_goes |
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
51 "in_tag_name" |
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
52 "in_text" |
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
53 "in_text_tag" |
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
54 "in_attribute" |
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
55 "in_single_attribute" |
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
56 "in_children" |
11457
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
57 "in_wrapper" |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
58 end |
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
59 |
11461
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
60 local function unpack_propschema( propschema : schema_t, propname : string, current_ns : string ) |
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
61 : json_type_name, value_goes, string, string, string, string, { any } |
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
62 local proptype : json_type_name = "string" |
11466
c098d07e6717
util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11465
diff
changeset
|
63 local value_where : value_goes = propname and "in_text_tag" or "in_text" |
11456
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
64 local name = propname |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
65 local namespace = current_ns |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
66 local prefix : string |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
67 local single_attribute : string |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
68 local enums : { any } |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
69 |
11461
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
70 if propschema is json_schema_object then |
11456
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
71 proptype = propschema.type |
11461
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
72 elseif propschema is json_type_name then |
11456
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
73 proptype = propschema |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
74 end |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
75 |
11457
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
76 if proptype == "object" or proptype == "array" then |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
77 value_where = "in_children" |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
78 end |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
79 |
11461
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
80 if propschema is json_schema_object then |
11456
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
81 local xml = propschema.xml |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
82 if xml then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
83 if xml.name then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
84 name = xml.name |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
85 end |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
86 if xml.namespace then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
87 namespace = xml.namespace |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
88 end |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
89 if xml.prefix then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
90 prefix = xml.prefix |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
91 end |
11457
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
92 if proptype == "array" and xml.wrapped then |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
93 value_where = "in_wrapper" |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
94 elseif xml.attribute then |
11456
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
95 value_where = "in_attribute" |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
96 elseif xml.text then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
97 value_where = "in_text" |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
98 elseif xml.x_name_is_value then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
99 value_where = "in_tag_name" |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
100 elseif xml.x_single_attribute then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
101 single_attribute = xml.x_single_attribute |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
102 value_where = "in_single_attribute" |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
103 end |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
104 end |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
105 if propschema["const"] then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
106 enums = { propschema["const"] } |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
107 elseif propschema["enum"] then |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
108 enums = propschema["enum"] |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
109 end |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
110 end |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
111 |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
112 return proptype, value_where, name, namespace, prefix, single_attribute, enums |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
113 end |
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
114 |
11461
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
115 local parse_object : function (schema : schema_t, s : st.stanza_t) : { string : any } |
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
116 local parse_array : function (schema : schema_t, s : st.stanza_t) : { any } |
11456
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
117 |
11465
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
118 local function extract_value (s : st.stanza_t, value_where : value_goes, proptype : json.json_type_name, name : string, namespace : string, prefix : string, single_attribute : string, enums : { any }) : string |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
119 if value_where == "in_tag_name" then |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
120 local c : st.stanza_t |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
121 if proptype == "boolean" then |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
122 c = s:get_child(name, namespace); |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
123 elseif enums and proptype == "string" then |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
124 -- XXX O(n²) ? |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
125 -- Probably better to flip the table and loop over :childtags(nil, ns), should be 2xO(n) |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
126 -- BUT works first, optimize later |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
127 for i = 1, #enums do |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
128 c = s:get_child(enums[i] as string, namespace); |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
129 if c then break end |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
130 end |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
131 else |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
132 c = s:get_child(nil, namespace); |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
133 end |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
134 if c then |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
135 return c.name; |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
136 end |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
137 elseif value_where == "in_attribute" then |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
138 local attr = name |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
139 if prefix then |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
140 attr = prefix .. ':' .. name |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
141 elseif namespace ~= s.attr.xmlns then |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
142 attr = namespace .. "\1" .. name |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
143 end |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
144 return s.attr[attr] |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
145 |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
146 elseif value_where == "in_text" then |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
147 return s:get_text() |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
148 |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
149 elseif value_where == "in_single_attribute" then |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
150 local c = s:get_child(name, namespace) |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
151 return c and c.attr[single_attribute] |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
152 elseif value_where == "in_text_tag" then |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
153 return s:get_child_text(name, namespace) |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
154 end |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
155 end |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
156 |
11461
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
157 function parse_object (schema : schema_t, s : st.stanza_t) : { string : any } |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 local out : { string : any } = {} |
11461
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
159 if schema is json_schema_object and schema.properties then |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
160 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
|
161 |
11456
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
162 local proptype, value_where, name, namespace, prefix, single_attribute, enums = unpack_propschema(propschema, prop, s.attr.xmlns) |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
163 |
11465
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
164 if value_where == "in_children" and propschema is json_schema_object then |
11455
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
165 if proptype == "object" then |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
166 local c = s:get_child(name, namespace) |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 if c then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 out[prop] = parse_object(propschema, c); |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
169 end |
11457
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
170 elseif proptype == "array" then |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
171 out[prop] = parse_array(propschema, s); |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
172 else |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
173 error "unreachable" |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
174 end |
11461
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
175 elseif value_where == "in_wrapper" and propschema is json_schema_object and proptype == "array" then |
11457
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
176 local wrapper = s:get_child(name, namespace); |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
177 if wrapper then |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
178 out[prop] = parse_array(propschema, wrapper); |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
179 else |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
180 error "unreachable" |
11461
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
181 end |
11457
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
182 else |
11465
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
183 local value : string = extract_value (s, value_where, proptype, name, namespace, prefix, single_attribute, enums) |
19a88b61ab4e
util.datamapper: Factor out extraction of the XML part to use
Kim Alvefur <zash@zash.se>
parents:
11464
diff
changeset
|
184 |
11455
a5050e21ab08
util.datamapper: Separate extraction of xml from coercion to target type
Kim Alvefur <zash@zash.se>
parents:
11454
diff
changeset
|
185 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
|
186 end |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
187 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
188 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
189 |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
190 return out |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
191 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
192 |
11461
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
193 function parse_array (schema : json_schema_object, s : st.stanza_t) : { any } |
11466
c098d07e6717
util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11465
diff
changeset
|
194 local proptype, value_where, child_name, namespace, prefix, single_attribute, enums = unpack_propschema(schema.items, nil, s.attr.xmlns) |
c098d07e6717
util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11465
diff
changeset
|
195 local attr_name : string |
c098d07e6717
util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11465
diff
changeset
|
196 if value_where == "in_single_attribute" then -- FIXME this shouldn't be needed |
c098d07e6717
util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11465
diff
changeset
|
197 value_where = "in_attribute"; |
c098d07e6717
util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11465
diff
changeset
|
198 attr_name = single_attribute; |
c098d07e6717
util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11465
diff
changeset
|
199 end |
11457
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
200 local out : { any } = {} |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
201 for c in s:childtags(child_name, namespace) do |
11466
c098d07e6717
util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11465
diff
changeset
|
202 local value : string = extract_value (c, value_where, proptype, attr_name or child_name, namespace, prefix, single_attribute, enums) |
11457
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
203 |
11466
c098d07e6717
util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11465
diff
changeset
|
204 table.insert(out, totype(proptype, value)); |
11457
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
205 end |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
206 return out; |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
207 end |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
208 |
11461
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
209 local function parse (schema : json_schema_object, s : st.stanza_t) : table |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
210 if schema.type == "object" then |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
211 return parse_object(schema, s) |
11457
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
212 elseif schema.type == "array" then |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
213 return parse_array(schema, s) |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
214 else |
6a51749af7f4
util.datamapper: Add initial support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11456
diff
changeset
|
215 error "top-level scalars unsupported" |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
216 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
217 end |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
218 |
11462
d1982b7eb00d
util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents:
11461
diff
changeset
|
219 local function unparse ( schema : json_schema_object, t : table, current_name : string, current_ns : string, ctx : st.stanza_t ) : st.stanza_t |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
220 |
11461
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
221 if schema.xml then |
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
222 if schema.xml.name then |
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
223 current_name = schema.xml.name |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
224 end |
11461
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
225 if schema.xml.namespace then |
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
226 current_ns = schema.xml.namespace |
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
227 end |
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
228 -- TODO prefix? |
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
229 end |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
230 |
11462
d1982b7eb00d
util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents:
11461
diff
changeset
|
231 local out = ctx or st.stanza(current_name, { xmlns = current_ns }) |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
232 |
11458
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
233 if schema.type == "object" then |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
234 |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
235 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
|
236 local v = t[prop] |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
237 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
238 if v ~= nil then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
239 |
11456
4e376a43fe40
util.datamapper: Factor out common schema unpacking
Kim Alvefur <zash@zash.se>
parents:
11455
diff
changeset
|
240 local proptype, value_where, name, namespace, prefix, single_attribute = unpack_propschema(propschema, prop, current_ns) |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
241 |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
242 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
|
243 local attr = name |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
244 if prefix then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
245 attr = prefix .. ':' .. name |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
246 elseif namespace ~= current_ns then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
247 attr = namespace .. "\1" .. name |
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 |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
250 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
|
251 out.attr[attr] = v |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
252 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
|
253 out.attr[attr] = string.format("%g", v) |
11459
86904555bffc
teal: Use new integer support in Teal 0.13.0
Kim Alvefur <zash@zash.se>
parents:
11458
diff
changeset
|
254 elseif proptype == "integer" and v is number then -- TODO is integer |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
255 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
|
256 elseif proptype == "boolean" then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
257 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
|
258 end |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
259 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
|
260 if v is string then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
261 out:text(v) |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
262 end |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
263 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
|
264 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
|
265 |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
266 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
|
267 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
|
268 end |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
269 |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
270 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
|
271 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
|
272 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
|
273 propattr[single_attribute] = string.format("%g", v) |
11459
86904555bffc
teal: Use new integer support in Teal 0.13.0
Kim Alvefur <zash@zash.se>
parents:
11458
diff
changeset
|
274 elseif proptype == "integer" and v is number then -- TODO is integer |
11439
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
275 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
|
276 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
|
277 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
|
278 end |
9abcdfdcdb01
util.datamapper: Add support for mapping of elements where only one attribute matters
Kim Alvefur <zash@zash.se>
parents:
11438
diff
changeset
|
279 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
|
280 |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
281 else |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
282 local propattr : { string : string } |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
283 if namespace ~= current_ns then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
284 propattr = { xmlns = namespace } |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
285 end |
11454
1d9c1893cc5e
util.datamapper: Use enum instead of mutually exclusive booleans
Kim Alvefur <zash@zash.se>
parents:
11453
diff
changeset
|
286 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
|
287 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
|
288 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
|
289 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
|
290 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
|
291 end |
11437
87a684df4b65
util.datamapper: Invent extension for using tag name as value
Kim Alvefur <zash@zash.se>
parents:
11436
diff
changeset
|
292 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
|
293 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
|
294 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
|
295 out:text_tag(name, string.format("%g", v), propattr) |
11459
86904555bffc
teal: Use new integer support in Teal 0.13.0
Kim Alvefur <zash@zash.se>
parents:
11458
diff
changeset
|
296 elseif proptype == "integer" and v is number then -- TODO is integer |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
297 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
|
298 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
|
299 out:text_tag(name, v and "1" or "0", propattr) |
11461
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
300 elseif proptype == "object" and propschema is json_schema_object and v is table then |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
301 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
|
302 if c then |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
303 out:add_direct_child(c); |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
304 end |
11461
766b0eddd12c
util.datamapper: Deal with type name changes in util.jsonschema
Kim Alvefur <zash@zash.se>
parents:
11459
diff
changeset
|
305 elseif proptype == "array" and propschema is json_schema_object and v is table then |
11462
d1982b7eb00d
util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents:
11461
diff
changeset
|
306 if value_where == "in_wrapper" then |
d1982b7eb00d
util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents:
11461
diff
changeset
|
307 local c = unparse(propschema, v, name, namespace); |
d1982b7eb00d
util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents:
11461
diff
changeset
|
308 if c then |
11458
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
309 out:add_direct_child(c); |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
310 end |
11462
d1982b7eb00d
util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents:
11461
diff
changeset
|
311 else |
d1982b7eb00d
util.datamapper: Fix arrays nesting one level too deep
Kim Alvefur <zash@zash.se>
parents:
11461
diff
changeset
|
312 unparse(propschema, v, name, namespace, out); |
11458
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
313 end |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
314 else |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
315 error "NYI" |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
316 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
317 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
318 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
319 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
320 return out; |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
321 |
11458
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
322 elseif schema.type == "array" then |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
323 local proptype, value_where, name, namespace = unpack_propschema(schema.items, current_name, current_ns) |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
324 -- TODO , prefix, single_attribute |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
325 if proptype == "string" then |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
326 for _, item in ipairs(t as { string }) do |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
327 if value_where == "in_text_tag" then |
11466
c098d07e6717
util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11465
diff
changeset
|
328 out:text_tag(name, item, { xmlns = namespace }); |
c098d07e6717
util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11465
diff
changeset
|
329 else |
c098d07e6717
util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11465
diff
changeset
|
330 error "NYI" |
c098d07e6717
util.datamapper: Finally implement support for parsing arrays
Kim Alvefur <zash@zash.se>
parents:
11465
diff
changeset
|
331 end |
11458
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
332 end |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
333 else |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
334 error "NYI" |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
335 end |
0e00fa518688
util.datamapper: Limited support for unparsing simple arrays of strings
Kim Alvefur <zash@zash.se>
parents:
11457
diff
changeset
|
336 return out; |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
337 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
338 end |
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
339 |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
340 return { |
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
341 parse = parse, |
11436
5df9ffc25bb4
util.datamapper: Add 'unparse' for turning tables into XML
Kim Alvefur <zash@zash.se>
parents:
11435
diff
changeset
|
342 unparse = unparse, |
11435
a1fa6202fa13
util.datamapper: Library for extracting data from stanzas
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
343 } |