Skip to content

Commit 911a4a5

Browse files
committed
optional nodes, better raw nodes
1 parent 31e6c44 commit 911a4a5

1 file changed

Lines changed: 26 additions & 24 deletions

File tree

include/rsl/serializer/xml/serializer.hpp

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class Serializer {
1515
XmlNode document;
1616
XmlNode* cursor = nullptr;
1717

18-
1918
static std::string stringify_value(bool value) { return value ? "true" : "false"; }
2019

2120
template <typename T>
@@ -29,14 +28,14 @@ class Serializer {
2928
output.reserve(value.size());
3029

3130
for (char ch : value) {
32-
switch (ch) {
33-
case '&': output.append("&amp;"); break;
34-
case '<': output.append("&lt;"); break;
35-
case '>': output.append("&gt;"); break;
36-
case '"': output.append("&quot;"); break;
37-
case '\'': output.append("&#39;"); break;
38-
default: output.push_back(ch); break;
39-
}
31+
switch (ch) {
32+
case '&': output.append("&amp;"); break;
33+
case '<': output.append("&lt;"); break;
34+
case '>': output.append("&gt;"); break;
35+
case '"': output.append("&quot;"); break;
36+
case '\'': output.append("&#39;"); break;
37+
default: output.push_back(ch); break;
38+
}
4039
}
4140
return output;
4241
}
@@ -49,6 +48,12 @@ class Serializer {
4948
public:
5049
Serializer() = default;
5150

51+
// template <is_optional R, typename T>
52+
// void operator()(R meta, T&& value) {
53+
// // check for annotation?
54+
// meta.descend(*this, std::forward<T>(value));
55+
// }
56+
5257
template <has_members R, typename T>
5358
void operator()(R meta, T&& value) {
5459
if constexpr (!has_identifier(meta.info)) {
@@ -79,7 +84,7 @@ class Serializer {
7984
throw std::runtime_error("Root node must be a structure");
8085
}
8186

82-
meta.descend(*this, std::forward<T>(value));
87+
meta.descend(*this, value);
8388
}
8489

8590
template <typename R, typename T>
@@ -93,32 +98,29 @@ class Serializer {
9398
throw std::runtime_error("Attributes must have identifiers.");
9499
} else {
95100
if constexpr (is_optional<R>) {
96-
if (value.has_value()) {
97-
cursor->attributes[std::string(identifier_of(meta.info))] = stringify_value(*value);
101+
if (!value.has_value()) {
102+
return;
98103
}
104+
cursor->attributes[std::string(identifier_of(meta.info))] = stringify_value(*value);
99105
} else {
100106
cursor->attributes[std::string(identifier_of(meta.info))] = stringify_value(value);
101107
}
102108
}
103-
} else if constexpr (meta::has_annotation(meta.info, ^^xml::annotations::Raw)){
109+
} else if constexpr (meta::has_annotation(meta.info, ^^xml::annotations::Raw)) {
104110
if constexpr (is_optional<R>) {
105111
if (!value.has_value()) {
106112
return;
107113
}
108-
}
109-
110-
cursor = cursor->add({std::string(identifier_of(meta.info))});
111-
if constexpr (is_optional<R>) {
112-
if (value.has_value()) {
113-
cursor->raw_content = stringify_value(*value);
114-
}
114+
cursor->raw_content = stringify_value(*value);
115115
} else {
116116
cursor->raw_content = stringify_value(value);
117117
}
118-
cursor = cursor->parent;
119-
} else {
120-
throw std::runtime_error(std::format("Non-structural fields can only be attributes. {}",
121-
define_static_string(display_string_of(meta.info))));
118+
} else if constexpr (is_optional<R> &&
119+
meta::has_annotation(meta.info, ^^xml::annotations::Node)) {
120+
if (!value.has_value()) {
121+
return;
122+
}
123+
(*this)(rsl::serializer::Meta<typename std::remove_cvref_t<T>::value_type>(), *value);
122124
}
123125
}
124126

0 commit comments

Comments
 (0)