Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

TjazVracko
Copy link
Collaborator

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

  • My code follows the style guidelines as defined by IRNAS.
  • I have performed a self-review of my code.
  • My changes generate no new warnings.
  • I added/updated source code documentation for all newly added or changed functions.
  • I updated all customer-facing technical documentation.

After-review steps

  • I will merge PR by myself.

Copy link
Collaborator Author

@github-actions github-actions bot added the pull request Pull request, added automatically by CI. label Jan 13, 2025
@TjazVracko TjazVracko force-pushed the feature/validation branch 3 times, most recently from 018f0e1 to 666a1c4 Compare January 13, 2025 09:25
Copy link

Coverage after merging feature/validation into main will be

24.21%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
library/protocol/binary
   user_settings_protocol_binary.c83.33%72.97%100%87.18%113, 116–118, 118, 118–119, 121–124, 135–136, 173–174, 66–67, 71–72, 80
library/user_settings
   user_settings.c46.44%28.19%72%60.82%100, 107, 107, 110, 114, 131–132, 137–138, 138, 138, 138, 138–141, 141, 141, 141, 141–142, 145, 145, 168, 176–177, 177, 177, 177, 177–178, 183–184, 184, 184, 184, 184–185, 190–191, 191, 191, 191, 191–192, 197–198, 198, 198, 198, 198–199, 209–211, 218–219, 227, 233–234, 234, 234, 234, 234–235, 240–241, 241, 241, 241, 241–242, 247–248, 248, 248, 248, 248–249, 259, 259, 259, 265, 265, 265, 265, 271, 271, 271, 271, 278–279, 279, 279, 279, 279–280, 287, 287, 287, 287, 299–300, 300, 300, 300, 300–301, 306–307, 307, 307, 307, 307–308, 314, 316, 316, 316, 318–319, 319, 319, 321, 326, 326, 326, 329, 329, 329, 344, 350, 350, 360–361, 361, 361, 361, 361–362, 368–369, 369, 369, 369, 369–370, 378, 384, 384, 384, 384, 390, 390, 398, 398, 398, 398, 408–409, 409, 409, 409, 409–410, 415–416, 416, 416, 416, 416–417, 422–423, 423, 423, 423, 423–424, 433–434, 441, 443, 443, 443, 449, 451, 451, 451–452, 454, 456, 458, 458, 458, 460–461, 461, 461, 463–464, 469, 469, 469, 47, 472, 472, 472, 48, 480, 488, 488, 488, 496, 499, 506, 509, 516, 52, 523, 523, 523–524, 524, 524–525, 527, 53, 530, 533, 535, 535, 535, 537–538, 538, 538, 540, 545, 548, 555–556, 562, 565, 567, 567, 567, 569–570, 570, 570, 572, 577, 577, 577, 58, 580, 580, 580, 587, 59, 59, 59, 59, 59, 592, 594, 594, 594, 596–597, 597, 597, 599, 60, 600, 604, 607, 61, 612, 614, 614, 614, 616–617, 617, 617, 619, 62, 62, 62, 62, 62, 620, 623, 623, 623, 626, 626, 626, 632, 634, 634, 634, 636–637, 637, 637, 639, 64, 643, 645, 645, 645, 647–648, 648, 648, 650, 653, 655, 655, 655, 657–658, 658, 658, 660, 663, 665, 665, 665, 667–668, 668, 668, 670, 675, 675, 675, 678, 678, 678, 685, 685, 685, 688, 688, 688, 693, 695, 695, 695, 697–698, 698, 698, 700, 705, 705, 705, 708, 708, 708, 715, 718, 725, 725, 725, 728, 728, 728, 769, 771, 771, 771, 773–774, 774, 774, 776–777, 779, 781, 781, 781, 783–784, 784, 784, 786–787, 791, 791, 791, 794, 794, 794, 801, 801, 801, 804, 804, 804, 811, 83–84, 88–89, 94–95, 95, 95, 95, 95–98, 98, 98, 98, 98
   user_settings_json.c45.99%34.75%100%52.44%100–101, 103, 106, 114, 127–128, 128, 128, 128, 128–129, 133–134, 143–144, 160, 160, 160, 160, 160, 160, 160, 160, 162, 165, 169–171, 173–175, 181–183, 185–187, 189–191, 193–195, 197–199, 216–217, 217, 217, 217, 217, 221–222, 222, 222,

Comment on lines +345 to +372
/**
* @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);
Copy link
Collaborator

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 be parsed 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 the user_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_*)

Copy link
Collaborator

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 to validator, so verb to noun change, it makes a bit more sense to me, but in any case it is clear what this does.

library/user_settings/user_settings.c Show resolved Hide resolved
Comment on lines 87 to +90
us->default_is_set = 0;
us->has_changed_recently = 0;
us->on_change_cb = NULL;
us->validate_cb = NULL;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Re: lines +78 to +90]

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__-->

Copy link
Collaborator Author

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

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pull request Pull request, added automatically by CI.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Settings validation
2 participants