feat(eventsub): handle multi enum constant keys (#6078)

This commit is contained in:
pajlada
2025-03-15 11:57:03 +01:00
committed by GitHub
parent d2ed3bbae7
commit caeb28dd9a
6 changed files with 23 additions and 1 deletions
@@ -16,6 +16,10 @@ class CommentCommands:
# If set, `name_transform` will do nothing
name_change: Optional[str] = None
# Whether the enum constant should look for additional keys
# `name_transform` is not applied to these
extra_enum_constant_names: list[str] = []
# Don't fail when an optional object exists and its data is bad
dont_fail_on_deserialization: bool = False
@@ -31,6 +35,7 @@ class CommentCommands:
if parent is not None:
self.name_transform = parent.name_transform
self.name_change = parent.name_change
self.extra_enum_constant_names = parent.extra_enum_constant_names
self.dont_fail_on_deserialization = parent.dont_fail_on_deserialization
self.tag = parent.tag
self.inner_root = parent.inner_root
@@ -52,6 +57,8 @@ class CommentCommands:
match command:
case "json_rename":
self.name_change = value
case "json_extra_enum_constant_names":
self.extra_enum_constant_names = [v.strip() for v in value.split(",")]
case "json_dont_fail_on_deserialization":
self.dont_fail_on_deserialization = bool(value.lower() == "true")
case "json_transform":
@@ -18,12 +18,14 @@ class EnumConstant:
) -> None:
self.name = name
self.json_name = name
self.json_extra_enum_constant_names: list[str] = []
self.tag: Optional[str] = None
self.dont_fail_on_deserialization: bool = False
def apply_comment_commands(self, comment_commands: CommentCommands) -> None:
self.json_name = comment_commands.apply_name_transform(self.json_name)
self.json_extra_enum_constant_names = comment_commands.extra_enum_constant_names
self.tag = comment_commands.tag
self.dont_fail_on_deserialization = comment_commands.dont_fail_on_deserialization
@@ -13,6 +13,11 @@ boost::json::result_for<{{enum.full_name}}, boost::json::value>::type tag_invoke
if (eString == "{{constant.json_name}}"sv) {
return {{enum.full_name}}::{{constant.name}};
}
{%- for extra_name in constant.json_extra_enum_constant_names -%}
if (eString == "{{extra_name}}"sv) {
return {{enum.full_name}}::{{constant.name}};
}
{%- endfor -%}
{%- endfor %}
{%- if enum.default -%}
@@ -16,8 +16,12 @@ enum class Status : std::uint8_t {
/// default=Unknown
enum class Type : std::uint8_t {
Unknown,
/// The user was marked as Low Trust by a moderator
/// json_extra_enum_constant_names=manually_added
Manual,
/// The user was detected as a ban evader
BanEvaderDetector,
/// The user is banned in a channel the channel shares bans with
SharedChannelBan
};
@@ -53,6 +53,10 @@ boost::json::result_for<Type, boost::json::value>::type tag_invoke(
{
return Type::Manual;
}
if (eString == "manually_added"sv)
{
return Type::Manual;
}
if (eString == "ban_evader_detector"sv)
{
return Type::BanEvaderDetector;