Skip to content

Commit 2586301

Browse files
authored
Document glz::convert_struct (#1491)
* Adding documentation for glz::convert_struct * Adding comments for convert_struct * Fix integer parsing test from float
1 parent 2677323 commit 2586301

File tree

3 files changed

+85
-6
lines changed

3 files changed

+85
-6
lines changed

docs/reflection.md

+62
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,65 @@ static_assert(glz::refl<T>::size == 3);
1414
static_assert(glz::refl<T>::keys[0] == "a");
1515
```
1616
17+
## glz::convert_struct
18+
19+
The `glz::convert_struct` function show a simple application of Glaze reflection at work. It allows two different structs with the same member names to convert from one to the other. If any of the fields don't have matching names, a compile time error will be generated.
20+
21+
```c++
22+
struct a_type
23+
{
24+
float fluff = 1.1f;
25+
int goo = 1;
26+
std::string stub = "a";
27+
};
28+
29+
struct b_type
30+
{
31+
float fluff = 2.2f;
32+
int goo = 2;
33+
std::string stub = "b";
34+
};
35+
36+
struct c_type
37+
{
38+
std::optional<float> fluff = 3.3f;
39+
std::optional<int> goo = 3;
40+
std::optional<std::string> stub = "c";
41+
};
42+
43+
suite convert_tests = [] {
44+
"convert a to b"_test = [] {
45+
a_type in{};
46+
b_type out{};
47+
48+
glz::convert_struct(in, out);
49+
50+
expect(out.fluff == 1.1f);
51+
expect(out.goo == 1);
52+
expect(out.stub == "a");
53+
};
54+
55+
"convert a to c"_test = [] {
56+
a_type in{};
57+
c_type out{};
58+
59+
glz::convert_struct(in, out);
60+
61+
expect(out.fluff.value() == 1.1f);
62+
expect(out.goo.value() == 1);
63+
expect(out.stub.value() == "a");
64+
};
65+
66+
"convert c to a"_test = [] {
67+
c_type in{};
68+
a_type out{};
69+
70+
glz::convert_struct(in, out);
71+
72+
expect(out.fluff == 3.3f);
73+
expect(out.goo == 3);
74+
expect(out.stub == "c");
75+
};
76+
};
77+
```
78+

include/glaze/core/convert_struct.hpp

+11-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@
66
#include "glaze/core/common.hpp"
77
#include "glaze/core/reflect.hpp"
88

9-
// For generic struct to struct conversion based on reflected fields
10-
// This will convert a struct with optionals to non-optionals
11-
// More conversion can be added in the future
12-
139
namespace glz
1410
{
11+
/**
12+
* @brief Provides generic struct to struct conversion based on reflected fields.
13+
*
14+
* Uses reflected fields to perform a generic conversion from `In` to `Out`.
15+
* Additional conversion rules can be added in the future, but optional like types are supported.
16+
*
17+
* @tparam In Type of the input struct with optional fields.
18+
* @tparam Out Type of the output struct with non-optional fields.
19+
* @param[in] in The input struct instance.
20+
* @param[out] out The output struct instance to be populated.
21+
*/
1522
template <class In, class Out>
1623
void convert_struct(In&& in, Out&& out)
1724
{

tests/json_test/json_test.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -2265,9 +2265,19 @@ suite read_tests = [] {
22652265
for (size_t i = 0; i < 1000; ++i) {
22662266
const auto f = dist(gen);
22672267
expect(not glz::write_json(f, buffer));
2268+
2269+
// Check if 'f' is exactly an integer
2270+
const bool is_integer = (std::floor(f) == f);
2271+
22682272
int64_t integer{};
2269-
auto ec = glz::read_json(integer, buffer);
2270-
expect(ec);
2273+
if (is_integer) {
2274+
auto ec = glz::read_json(integer, buffer);
2275+
expect(!ec); // Expect no error when reading as integer
2276+
expect(integer == int64_t(f));
2277+
} else {
2278+
auto ec = glz::read_json(integer, buffer);
2279+
expect(ec); // Expect an error when reading a non-integer as integer
2280+
}
22712281
}
22722282
}
22732283
};

0 commit comments

Comments
 (0)