-
Notifications
You must be signed in to change notification settings - Fork 226
Add utility to optionalize a struct #2168
Description
I don't know if this would be in scope or feasible at all with the way reflection is currently emulated, but here is the idea.
If I have this struct:
struct Meta {
std::vector<std::string> hobbies;
};
struct Person {
std::string title;
int age;
Meta meta;
}I could use something like glz::optionalize to turn it into:
struct Person {
std::optional<std::string> title;
std::optional<int> age;
std::optional<glz::optionalize<Meta>> meta; // maybe optionalize could provide an option for whether nested structs get recursively optionalized
}That way we can just create a partial for any T simply doing using PartialPerson = glz::optionalize<Person>.
This kind of construct is very useful, notably for configuration, where there might be a default configuration that gets merged with a user configuration that only specifies a few values to override. The merging is done just fine with glz::merge, but the ability to produce a fully optionalized type would remove the need to hardcode it and effectively maintain two types. That way partial updates can be done in type-safe way.
Right now, I'm forced to maintain different types for this which is very messy