Skip to content

Commit 040d6a4

Browse files
committed
custom nullable comments and tests
1 parent d60582b commit 040d6a4

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

include/glaze/core/reflect.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,8 @@ namespace glz
380380
template <class T, size_t I>
381381
using field_t = std::remove_cvref_t<refl_t<T, I>>;
382382

383-
// Check if a custom_t getter (To) returns a nullable type
383+
// Check if a custom_t getter (To) returns a nullable type (write side).
384+
// Complement of custom_type_is_nullable which checks the From/setter (read side).
384385
template <class V>
385386
consteval bool custom_getter_returns_nullable()
386387
{
@@ -474,6 +475,8 @@ namespace glz
474475
}
475476
}();
476477

478+
// Check if a custom_t setter (From) accepts a nullable type (read side).
479+
// Complement of custom_getter_returns_nullable which checks the To/getter (write side).
477480
template <class V, class From>
478481
consteval bool custom_type_is_nullable()
479482
{

tests/json_test/nullable_lambda_test.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,29 @@ suite custom_nullable_optional_tests = [] {
165165
expect(buffer == R"({"value":100,"status":"high"})") << "Got: " << buffer;
166166
};
167167

168+
"json write then read round-trip with value"_test = [] {
169+
my_struct_custom_optional src{};
170+
src.value = 100; // getter returns "high"
171+
172+
std::string buffer;
173+
expect(not glz::write_json(src, buffer));
174+
175+
my_struct_custom_optional dst{};
176+
expect(not glz::read_json(dst, buffer));
177+
178+
// setter receives "high" -> sets value to 50
179+
expect(dst.value == 50) << "Got: " << dst.value;
180+
};
181+
182+
"json read with field absent - should not error"_test = [] {
183+
my_struct_custom_optional dst{};
184+
dst.value = 99;
185+
186+
// JSON with "status" omitted — the setter should not be called
187+
expect(not glz::read_json(dst, R"({"value":42})"));
188+
expect(dst.value == 42) << "Got: " << dst.value;
189+
};
190+
168191
"custom getter with skip_null_members false"_test = [] {
169192
constexpr auto opts = glz::opts{.skip_null_members = false};
170193
my_struct_custom_optional obj{};

0 commit comments

Comments
 (0)