Skip to content

Add utility to optionalize a struct #2168

@aurelleb

Description

@aurelleb

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestlong term goalPlanned to support in the future

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions