Skip to content

Add rfl::Default #534

@autoantwort

Description

@autoantwort

In #163 (comment) / #167 (comment) the following new type was proposed

struct DatabaseAccessData
{
	rfl::Default<std::string> username = "admin";
	rfl::Default<std::string> password = "admin";

	rfl::Default<std::string> ip = "127.0.0.1";
	rfl::Default<std::string> database = "test";
	rfl::Default<unsigned short> port = 3306;
};

but DefaultIfMissing was implemented instead since all fields should be defaulted in these issues. I would have preferred the rfl::Default solution since it allows us to have required fields and optional fields with default values at the same time (currently not possible, see #372) and iiuc DefaultIfMissing could be implemented as a real processor that changes the type to rfl::Default.
The rfl::Default solution would also allow #396.

My use case is somethink like this where some fields are required but others are not and they have default values:

struct DatabaseAccessData
{
	rfl::Default<std::string> username = "admin";  // optional field with default
	std::string password;  // required field

	std::string ip; // required field
	rfl::Default<std::string> database = "test";  // optional field with default
	rfl::Default<unsigned short> port = 3306;   // optional field with default
};

A workaround would be std::optional, but using .value_or every time I want to access a field is suboptimal and spreads the default values across the whole code base. This also prevents the generation of a json schema that contains default values.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions