diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 00000000..e4bee2b5 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,28 @@ +name: Publish to pub.dev + +on: + release: + types: [published] + +jobs: + publish: + runs-on: ubuntu-latest + + permissions: + id-token: write + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Setup Dart + uses: dart-lang/setup-dart@v1 + + - name: Install dependencies + run: dart pub get + + - name: Verify package + run: dart pub publish --dry-run + + - name: Publish package + run: dart pub publish --force \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c9b3184..c3e6f74f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 2.1.1 + +- Updated switch statements to Switch expressions. +- Fix: `BackgroundTypeType.chatTheme` typo + # 2.1.0 - 🤖 Bot API 8.1 diff --git a/lib/src/telegram/models/abstracts/background_fill.dart b/lib/src/telegram/models/abstracts/background_fill.dart index e13327a9..bef4893b 100644 --- a/lib/src/telegram/models/abstracts/background_fill.dart +++ b/lib/src/telegram/models/abstracts/background_fill.dart @@ -8,16 +8,12 @@ abstract class BackgroundFill { /// Creates a new [BackgroundFill] object. /// This method decides which [BackgroundFill] subclass to use based on the [type] field. factory BackgroundFill.fromJson(Map json) { - switch (json['type']) { - case 'solid': - return BackgroundFillSolid.fromJson(json); - case 'gradient': - return BackgroundFillGradient.fromJson(json); - case 'freeform_gradient': - return BackgroundFillFreeformGradient.fromJson(json); - default: - throw ArgumentError('Invalid background fill type'); - } + return switch (BackgroundFillType.fromJson(json['type'])) { + BackgroundFillType.solid => BackgroundFillSolid.fromJson(json), + BackgroundFillType.gradient => BackgroundFillGradient.fromJson(json), + BackgroundFillType.freeformGradient => + BackgroundFillFreeformGradient.fromJson(json), + }; } /// Creates a new [BackgroundFill] object from JSON. diff --git a/lib/src/telegram/models/abstracts/background_type_fill.dart b/lib/src/telegram/models/abstracts/background_type_fill.dart index a5192ab7..96ecbf88 100644 --- a/lib/src/telegram/models/abstracts/background_type_fill.dart +++ b/lib/src/telegram/models/abstracts/background_type_fill.dart @@ -8,18 +8,12 @@ abstract class BackgroundType { /// Creates a new [BackgroundType] object. /// This method decides which [BackgroundType] subclass to use based on the [type] field. static BackgroundType create(Map json) { - switch (json['type']) { - case 'fill': - return BackgroundTypeFill.fromJson(json); - case 'wallpaper': - return BackgroundTypeWallpaper.fromJson(json); - case 'pattern': - return BackgroundTypePattern.fromJson(json); - case 'chat_theme': - return BackgroundTypeChatTheme.fromJson(json); - default: - throw ArgumentError('Invalid background type'); - } + return switch (BackgroundTypeType.fromJson(json['type'])) { + BackgroundTypeType.fill => BackgroundTypeFill.fromJson(json), + BackgroundTypeType.wallpaper => BackgroundTypeWallpaper.fromJson(json), + BackgroundTypeType.pattern => BackgroundTypePattern.fromJson(json), + BackgroundTypeType.chatTheme => BackgroundTypeChatTheme.fromJson(json), + }; } /// Creates a new [BackgroundType] object from JSON. diff --git a/lib/src/telegram/models/abstracts/chat_boost_source.dart b/lib/src/telegram/models/abstracts/chat_boost_source.dart index e46d3db1..e3f89d97 100644 --- a/lib/src/telegram/models/abstracts/chat_boost_source.dart +++ b/lib/src/telegram/models/abstracts/chat_boost_source.dart @@ -16,20 +16,10 @@ abstract class ChatBoostSource { /// Creates a `ChatBoostSource` object from a JSON object. factory ChatBoostSource.fromJson(Map json) { - switch (json['type']) { - case 'premium': - return ChatBoostSourcePremium.fromJson(json); - case 'gift_code': - return ChatBoostSourceGiftCode.fromJson(json); - case 'giveaway': - return ChatBoostSourceGiveaway.fromJson(json); - default: - throw TeleverseException( - "Invalid value '${json['type']}' for ChatBoostSourceType.", - description: - 'The given value does not match any ChatBoostSourceType.', - type: TeleverseExceptionType.invalidParameter, - ); - } + return switch (ChatBoostSourceType.fromJson(json['type'])) { + ChatBoostSourceType.premium => ChatBoostSourcePremium.fromJson(json), + ChatBoostSourceType.giftCode => ChatBoostSourceGiftCode.fromJson(json), + ChatBoostSourceType.giveaway => ChatBoostSourceGiveaway.fromJson(json), + }; } } diff --git a/lib/src/telegram/models/abstracts/chat_member.dart b/lib/src/telegram/models/abstracts/chat_member.dart index e310d07f..69567793 100644 --- a/lib/src/telegram/models/abstracts/chat_member.dart +++ b/lib/src/telegram/models/abstracts/chat_member.dart @@ -21,26 +21,14 @@ abstract class ChatMember implements WithUser { /// Creates a new [ChatMember] object from a JSON object. /// This method decides which [ChatMember] subclass to use based on the [status] field. static ChatMember fromJson(Map json) { - switch (json['status'] as String) { - case 'creator': - return ChatMemberOwner.fromJson(json); - case 'administrator': - return ChatMemberAdministrator.fromJson(json); - case 'member': - return ChatMemberMember.fromJson(json); - case 'restricted': - return ChatMemberRestricted.fromJson(json); - case 'left': - return ChatMemberLeft.fromJson(json); - case 'kicked': - return ChatMemberBanned.fromJson(json); - default: - throw TeleverseException( - 'Unknown ChatMember status: ${json['status']}', - description: 'The given status does not match any ChatMemberStatus.', - type: TeleverseExceptionType.invalidParameter, - ); - } + return switch (ChatMemberStatus.fromJson(json['status'])) { + ChatMemberStatus.creator => ChatMemberOwner.fromJson(json), + ChatMemberStatus.administrator => ChatMemberAdministrator.fromJson(json), + ChatMemberStatus.member => ChatMemberMember.fromJson(json), + ChatMemberStatus.restricted => ChatMemberRestricted.fromJson(json), + ChatMemberStatus.left => ChatMemberLeft.fromJson(json), + ChatMemberStatus.kicked => ChatMemberBanned.fromJson(json), + }; } /// Converts a [ChatMember] to a [Map] for JSON encoding. diff --git a/lib/src/telegram/models/abstracts/menu_button.dart b/lib/src/telegram/models/abstracts/menu_button.dart index 12ac58cc..75877f74 100644 --- a/lib/src/telegram/models/abstracts/menu_button.dart +++ b/lib/src/telegram/models/abstracts/menu_button.dart @@ -10,10 +10,10 @@ abstract class MenuButton { /// Creates a new [MenuButton] object. /// This method decides which [MenuButton] subclass to use based on the [type] field. static MenuButton create(Map json) { - return switch (json['type']) { - 'commands' => MenuButtonCommands.fromJson(json), - 'web_app' => MenuButtonWebApp.fromJson(json), - _ => MenuButtonDefault.fromJson(json), + return switch (MenuButtonType.fromJson(json['type'])) { + MenuButtonType.commands => MenuButtonCommands.fromJson(json), + MenuButtonType.webApp => MenuButtonWebApp.fromJson(json), + MenuButtonType.defaultButton => MenuButtonDefault.fromJson(json), }; } diff --git a/lib/src/telegram/models/abstracts/message_origin.dart b/lib/src/telegram/models/abstracts/message_origin.dart index f731307a..370a959f 100644 --- a/lib/src/telegram/models/abstracts/message_origin.dart +++ b/lib/src/telegram/models/abstracts/message_origin.dart @@ -21,23 +21,12 @@ abstract class MessageOrigin { /// Creates a new [MessageOrigin] instance from a JSON object. factory MessageOrigin.fromJson(Map json) { - switch (json['type']) { - case 'user': - return MessageOriginUser.fromJson(json); - case 'hidden_user': - return MessageOriginHiddenUser.fromJson(json); - case 'chat': - return MessageOriginChat.fromJson(json); - case 'channel': - return MessageOriginChannel.fromJson(json); - default: - throw TeleverseException( - "Unknown message origin type", - description: - "The given JSON object does not match any MessageOrigin type.", - type: TeleverseExceptionType.invalidParameter, - ); - } + return switch (MessageOriginType.from(json['type'])) { + MessageOriginType.user => MessageOriginUser.fromJson(json), + MessageOriginType.hiddenUser => MessageOriginHiddenUser.fromJson(json), + MessageOriginType.chat => MessageOriginChat.fromJson(json), + MessageOriginType.channel => MessageOriginChannel.fromJson(json), + }; } /// Converts [MessageOrigin] instance to a JSON object. diff --git a/lib/src/telegram/models/abstracts/paid_media.dart b/lib/src/telegram/models/abstracts/paid_media.dart index a8e4b501..973b7a66 100644 --- a/lib/src/telegram/models/abstracts/paid_media.dart +++ b/lib/src/telegram/models/abstracts/paid_media.dart @@ -9,11 +9,10 @@ abstract class PaidMedia { /// Creates a new [PaidMedia] object. /// This method decides which [PaidMedia] subclass to use based on the [type] field. static PaidMedia fromJson(Map json) { - return switch (json['type']) { - 'preview' => PaidMediaPreview.fromJson(json), - 'photo' => PaidMediaPhoto.fromJson(json), - 'video' => PaidMediaVideo.fromJson(json), - _ => throw ArgumentError('Invalid paid media type') + return switch (PaidMediaType.fromJson(json['type'])) { + PaidMediaType.preview => PaidMediaPreview.fromJson(json), + PaidMediaType.photo => PaidMediaPhoto.fromJson(json), + PaidMediaType.video => PaidMediaVideo.fromJson(json), }; } diff --git a/lib/src/telegram/models/abstracts/revenue_withdrawal_state.dart b/lib/src/telegram/models/abstracts/revenue_withdrawal_state.dart index 7fa8adaa..0b9704f8 100644 --- a/lib/src/telegram/models/abstracts/revenue_withdrawal_state.dart +++ b/lib/src/telegram/models/abstracts/revenue_withdrawal_state.dart @@ -9,16 +9,14 @@ abstract class RevenueWithdrawalState { /// Creates a new [RevenueWithdrawalState] object. /// This method decides which [RevenueWithdrawalState] subclass to use based on the [type] field. static RevenueWithdrawalState fromJson(Map json) { - switch (RevenueWithdrawalStateType.fromJson(json['type'])) { - case RevenueWithdrawalStateType.pending: - return RevenueWithdrawalStatePending.fromJson(json); - case RevenueWithdrawalStateType.succeeded: - return RevenueWithdrawalStateSucceeded.fromJson(json); - case RevenueWithdrawalStateType.failed: - return RevenueWithdrawalStateFailed.fromJson(json); - default: - throw ArgumentError('Invalid revenue withdrawal state type'); - } + return switch (RevenueWithdrawalStateType.fromJson(json['type'])) { + RevenueWithdrawalStateType.pending => + RevenueWithdrawalStatePending.fromJson(json), + RevenueWithdrawalStateType.succeeded => + RevenueWithdrawalStateSucceeded.fromJson(json), + RevenueWithdrawalStateType.failed => + RevenueWithdrawalStateFailed.fromJson(json), + }; } /// Creates a new [RevenueWithdrawalState] object from JSON. diff --git a/lib/src/telegram/models/abstracts/transaction_partner.dart b/lib/src/telegram/models/abstracts/transaction_partner.dart index 3ab1dc2d..363a4175 100644 --- a/lib/src/telegram/models/abstracts/transaction_partner.dart +++ b/lib/src/telegram/models/abstracts/transaction_partner.dart @@ -9,12 +9,17 @@ abstract class TransactionPartner { /// Creates a new [TransactionPartner] object. /// This method decides which [TransactionPartner] subclass to use based on the [type] field. static TransactionPartner fromJson(Map json) { - return switch (json['type']) { - 'fragment' => TransactionPartnerFragment.fromJson(json), - 'user' => TransactionPartnerUser.fromJson(json), - 'other' => TransactionPartnerOther.fromJson(json), - 'telegram_ads' => TransactionPartnerTelegramAds.fromJson(json), - _ => throw ArgumentError('Invalid transaction partner type') + return switch (TransactionPartnerType.fromJson(json['type'])) { + TransactionPartnerType.fragment => + TransactionPartnerFragment.fromJson(json), + TransactionPartnerType.user => TransactionPartnerUser.fromJson(json), + TransactionPartnerType.other => TransactionPartnerOther.fromJson(json), + TransactionPartnerType.telegramAds => + TransactionPartnerTelegramAds.fromJson(json), + TransactionPartnerType.affiliateProgram => + TransactionPartnerAffiliateProgram.fromJson(json), + TransactionPartnerType.telegramApi => + TransactionPartnerTelegramApi.fromJson(json), }; } diff --git a/lib/src/types/background_fill_type.dart b/lib/src/types/background_fill_type.dart index 15b108fd..32bbd423 100644 --- a/lib/src/types/background_fill_type.dart +++ b/lib/src/types/background_fill_type.dart @@ -9,7 +9,7 @@ enum BackgroundFillType { gradient("gradient"), /// The background is a freeform gradient that rotates after every message in the chat. - freeformGradient("freeformGradient"), + freeformGradient("freeform_gradient"), ; /// The value of this enum. @@ -18,16 +18,7 @@ enum BackgroundFillType { /// Constructs a new [BackgroundFillType]. const BackgroundFillType(this.value); - factory BackgroundFillType.fromString(String val) { - switch (val) { - case "solid": - return solid; - case "gradient": - return gradient; - case "freeform_gradient": - return freeformGradient; - default: - throw ArgumentError("That's not a possible value"); - } + factory BackgroundFillType.fromJson(String json) { + return values.singleWhere((e) => e.value == json); } } diff --git a/lib/src/types/background_type_type.dart b/lib/src/types/background_type_type.dart index a36e5aed..37acd542 100644 --- a/lib/src/types/background_type_type.dart +++ b/lib/src/types/background_type_type.dart @@ -12,7 +12,7 @@ enum BackgroundTypeType { pattern("pattern"), /// The background is taken directly from a built-in chat theme. - chatTheme("chatTheme"), + chatTheme("chat_theme"), ; /// The value of this enum. @@ -20,4 +20,11 @@ enum BackgroundTypeType { /// Constructs a new [BackgroundFillType]. const BackgroundTypeType(this.value); + + /// Creates the type object from passed type string. + static BackgroundTypeType fromJson(String json) { + return values.firstWhere((e) { + return e.value == json; + }); + } } diff --git a/pubspec.yaml b/pubspec.yaml index 42b6b1b8..474830b1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: televerse description: Televerse lets you create your own efficient Telegram bots with ease in Dart. Supports latest Telegram Bot API - 8.1! -version: 2.1.0 +version: 2.1.1 homepage: https://televerse.xooniverse.com repository: https://github.com/xooniverse/televerse topics: