You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would expect the generated C++ code to validate the reason field to make sure it is a valid enum value. Or at least I'd want to have a helper that could validate it for me.
I see this as a few incremental improvements depending on what we want to do.
Generate a validator
Minmal of what we should do so that users can manually validate. Something like:
structEnumTrait<::McuCommand> {
staticconstexprboolis_valid(std::underlying_type_t<::McuCommand> value) {
switch(value) {
case ::McuCommand::ACK:
case ::McuCommand::NACK:
case ::McuCommand::BUSY:
returntrue;
default:
returnfalse;
}
}
}
Add some min and max helpers
It might be useful for quick checks to include a min and max value. Something like:
structEnumTrait<::McuCommand> {
staticconstexpr std::underlying_type_t<::McuCommand> min = ::McuCommand::ACK;
staticconstexpr std::underlying_type_t<::McuCommand> max = ::McuCommand::BUSY;
staticconstexprboolis_valid(std::underlying_type_t<::McuCommand> value) {
if (value < min || value > max) { returnfalse; }
// <snip>
}
}
Add a proper validator
We could also generate a proper validator that will work with emboss views:
structEnumValidator<McuCommand> {
template <typename ValueType>
staticconstexprboolValueIsOk(ValueType value) {
// add some checks that the value would fit in the underlying typereturn EnumTrait<McuCommand>::is_valid(static_cast<std::underlying_type_t<::McuCommand>>(value));
}
};
Hook the validator in
We could go further and use the validator by default. Pros: This is the right thing to do Cons: This might cause runtime assertions for existing code.
Background
Given the following definition:
I would expect the generated C++ code to validate the
reason
field to make sure it is a valid enum value. Or at least I'd want to have a helper that could validate it for me.Issue
We appear to use
AllValuesAreOk
as our validator forEnumView
types, for example:This leads to, well, all values being ok.
Possible fixes
I see this as a few incremental improvements depending on what we want to do.
Generate a validator
Minmal of what we should do so that users can manually validate. Something like:
Add some min and max helpers
It might be useful for quick checks to include a min and max value. Something like:
Add a proper validator
We could also generate a proper validator that will work with emboss views:
Hook the validator in
We could go further and use the validator by default.
Pros: This is the right thing to do
Cons: This might cause runtime assertions for existing code.
Something like:
The text was updated successfully, but these errors were encountered: