-
Notifications
You must be signed in to change notification settings - Fork 164
Json Schema: Add enum value descriptions #610
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
Open
AndrewNolte
wants to merge
1
commit into
getml:main
Choose a base branch
from
AndrewNolte:json-schema-enum-descriptions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| #include <gtest/gtest.h> | ||
| #include <rfl.hpp> | ||
| #include <rfl/json.hpp> | ||
| #include <string> | ||
|
|
||
| namespace test_enum_descriptions { | ||
|
|
||
| // Define an enum with descriptions | ||
| enum class Color { red, green, blue }; | ||
|
|
||
| // An enum without descriptions for comparison | ||
| enum class Size { small, medium, large }; | ||
|
|
||
| struct Config { | ||
| Color color; | ||
| Size size; | ||
| }; | ||
|
|
||
| } // namespace test_enum_descriptions | ||
|
|
||
| // Specialize enum_descriptions to provide descriptions for Color values | ||
| template <> | ||
| struct rfl::config::enum_descriptions<test_enum_descriptions::Color> { | ||
| static constexpr bool has_descriptions = true; | ||
| static constexpr std::string_view get(test_enum_descriptions::Color value) { | ||
| switch (value) { | ||
| case test_enum_descriptions::Color::red: | ||
| return "The color red"; | ||
| case test_enum_descriptions::Color::green: | ||
| return "The color green"; | ||
| case test_enum_descriptions::Color::blue: | ||
| return "The color blue"; | ||
| default: | ||
| return ""; | ||
| } | ||
|
Comment on lines
+33
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
| }; | ||
|
|
||
| namespace test_enum_descriptions { | ||
|
|
||
| TEST(json, test_enum_descriptions_schema) { | ||
| const auto json_schema = rfl::json::to_schema<Config>(); | ||
|
|
||
| // The schema should contain oneOf with const/description for Color | ||
| EXPECT_TRUE(json_schema.find("\"oneOf\"") != std::string::npos) | ||
| << "Expected oneOf for described enum. Schema: " << json_schema; | ||
| EXPECT_TRUE(json_schema.find("\"const\":\"red\"") != std::string::npos) | ||
| << "Expected const for red. Schema: " << json_schema; | ||
| EXPECT_TRUE(json_schema.find("\"description\":\"The color red\"") != | ||
| std::string::npos) | ||
| << "Expected description for red. Schema: " << json_schema; | ||
| EXPECT_TRUE(json_schema.find("\"const\":\"green\"") != std::string::npos) | ||
| << "Expected const for green. Schema: " << json_schema; | ||
| EXPECT_TRUE(json_schema.find("\"const\":\"blue\"") != std::string::npos) | ||
| << "Expected const for blue. Schema: " << json_schema; | ||
|
|
||
| // Size should still use regular enum format | ||
| EXPECT_TRUE(json_schema.find("\"enum\":[\"small\",\"medium\",\"large\"]") != | ||
| std::string::npos) | ||
| << "Expected regular enum for Size. Schema: " << json_schema; | ||
| } | ||
|
|
||
| TEST(json, test_enum_descriptions_read_write) { | ||
| // Verify that read/write still works correctly with described enums | ||
| const Config config{.color = Color::green, .size = Size::medium}; | ||
|
|
||
| const auto json_string = rfl::json::write(config); | ||
| EXPECT_EQ(json_string, R"({"color":"green","size":"medium"})"); | ||
|
|
||
| const auto parsed = rfl::json::read<Config>(json_string); | ||
| EXPECT_TRUE(parsed.has_value()) << "Failed to parse: " << parsed.error().what(); | ||
| EXPECT_EQ(parsed.value().color, Color::green); | ||
| EXPECT_EQ(parsed.value().size, Size::medium); | ||
| } | ||
|
|
||
| } // namespace test_enum_descriptions | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Using
defaultin aswitchover an enum can suppress useful compiler warnings if a new enumerator is added but not handled in theswitch. By removing thedefaultcase and having areturnstatement after theswitch, you encourage developers to explicitly handle all enum values. The compiler will warn them if they miss one, leading to more maintainable code.