Comparison

teal-src/util/datamapper.tl @ 11453:f0037234b2e9

util.datamapper: Enumerated elements E.g. error conditions or chat states.
author Kim Alvefur <zash@zash.se>
date Fri, 12 Mar 2021 01:33:15 +0100
parent 11452:c799bac7ca59
child 11454:1d9c1893cc5e
comparison
equal deleted inserted replaced
11452:c799bac7ca59 11453:f0037234b2e9
40 local prefix : string = nil 40 local prefix : string = nil
41 local is_attribute = false 41 local is_attribute = false
42 local is_text = false 42 local is_text = false
43 local name_is_value = false; 43 local name_is_value = false;
44 local single_attribute : string 44 local single_attribute : string
45 local enums : { any }
45 46
46 local proptype : js.schema_t.type_e 47 local proptype : js.schema_t.type_e
47 if propschema is js.schema_t then 48 if propschema is js.schema_t then
48 proptype = propschema.type 49 proptype = propschema.type
49 elseif propschema is js.schema_t.type_e then 50 elseif propschema is js.schema_t.type_e then
70 name_is_value = true 71 name_is_value = true
71 elseif propschema.xml.x_single_attribute then 72 elseif propschema.xml.x_single_attribute then
72 -- XXX Custom extension 73 -- XXX Custom extension
73 single_attribute = propschema.xml.x_single_attribute 74 single_attribute = propschema.xml.x_single_attribute
74 end 75 end
76 if propschema["const"] then
77 enums = { propschema["const"] }
78 elseif propschema["enum"] then
79 enums = propschema["enum"]
80 end
75 end 81 end
76 82
77 if name_is_value then 83 if name_is_value then
78 local c = s:get_child(nil, namespace); 84 local c : st.stanza_t
85 if proptype == "boolean" then
86 c = s:get_child(name, namespace);
87 elseif enums and proptype == "string" then
88 -- XXX O(n²) ?
89 -- Probably better to flip the table and loop over :childtags(nil, ns), should be 2xO(n)
90 -- BUT works first, optimize later
91 for i = 1, #enums do
92 c = s:get_child(enums[i] as string, namespace);
93 if c then break end
94 end
95 else
96 c = s:get_child(nil, namespace);
97 end
79 if c and proptype == "string" then 98 if c and proptype == "string" then
80 out[prop] = c.name; 99 out[prop] = c.name;
81 elseif proptype == "boolean" and c then 100 elseif proptype == "boolean" and c then
82 out[prop] = true; 101 out[prop] = true;
83 end 102 end