-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(validation): Add validation callback support #64
base: main
Are you sure you want to change the base?
Conversation
This stack of pull requests is managed by Graphite. Learn more about stacking. |
018f0e1
to
666a1c4
Compare
666a1c4
to
6f0c976
Compare
/** | ||
* @brief Set the validate callback for changes to a specific setting | ||
* | ||
* The provided function is called when the provided setting is updated to a new value. | ||
* If the callback returns false, the setting is not updated. | ||
* | ||
* This will assert if no setting with the provided key exists. | ||
* If the key input for this function is unknown to the application (i.e. parsed from user), then | ||
* it should first be checked with user_settings_exists_with_key(). | ||
* | ||
* @param[in] key The key of the setting to set the callback on | ||
* @param[in] validate_cb The callback function. NULL to disable validation. | ||
*/ | ||
void user_settings_set_validate_cb_with_key(char *key, user_settings_validate_t validate_cb); | ||
|
||
/** | ||
* @brief Set the validate callback for changes to a specific setting | ||
* | ||
* This behaves the same as user_settings_set_validate_cb_with_key() | ||
* | ||
* This will assert if no setting with the provided ID exists. | ||
* If the ID input for this function is unknown to the application (i.e. parsed from user), then | ||
* it should first be checked with user_settings_exists_with_id(). | ||
* | ||
* @param[in] id The ID of the setting to set the callback on | ||
* @param[in] validate_cb The callback function. NULL to disable validation. | ||
*/ | ||
void user_settings_set_validate_cb_with_id(uint16_t id, user_settings_validate_t validate_cb); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two nits:
- the
parsed from user
could beparsed from user input
- In doxygen for the
user_settings_set_validate_cb_with_id
you could completely remove the third paragraph (about the asserts), the doxygen for theuser_settings_set_validate_cb_with_key
should then be written in a more generic manner (eg. user_settings_exists_with_key -> user_settings_exists_with_*)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another nit:
validate
could be changed tovalidator
, so verb to noun change, it makes a bit more sense to me, but in any case it is clear what this does.
us->default_is_set = 0; | ||
us->has_changed_recently = 0; | ||
us->on_change_cb = NULL; | ||
us->validate_cb = NULL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: wouldn't be enough to just memset to zero the struct (as it is already done) and then just set non-zero values? Below should be enough:
struct user_setting *us = mem;
memset(us, 0, sizeof(struct user_setting));
us->id = id;
us->key = (char *)key;
us->type = type;
us->max_size = size;
```<!--__GRAPHITE_HTML_TAG_START__--><span class='graphite__hidden'><br/><br/>See this comment inline on <a href="https://app.graphite.dev/github/pr/IRNAS/irnas-usersettings-lib/64?utm_source=unchanged-line-comment">Graphite</a>.</span>
<!--__GRAPHITE_HTML_TAG_END__-->
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup.
Blame past me :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:D
Description
Adds support for settings validation.
Each setting can now have an attached validation callback.
The callback is called whenever the setting is changed, and it can reject the change by returning
false
.The PR also adds tests for the new feature and updates the
callbacks
sample.Closes #47
Areas of interest for the reviewer
All
Checklist
After-review steps