Skip to content

Commit

Permalink
Merge pull request #185 from fizruk/fix-optional-fields-manual-parsers
Browse files Browse the repository at this point in the history
Parse Maybe fields optionally
  • Loading branch information
swamp-agr authored May 26, 2024
2 parents 1fbbea1 + 30b7a52 commit 7ec78ba
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -321,30 +321,30 @@ instance FromJSON InlineQueryResult where
(o .: "type" :: Parser Text) >>= \case
"article" -> InlineQueryResultArticle
<$> parseJSON (Object o)
<*> o .: "url"
<*> o .: "hide_url"
<*> o .:? "url"
<*> o .:? "hide_url"
"photo" -> parseFileId o "photo_file_id" >>= \case
Nothing -> InlineQueryResultPhoto
<$> parseJSON (Object o) -- generic thumbnail
<*> o .: "photo_url"
<*> o .: "photo_width"
<*> o .: "photo_height"
<*> o .:? "photo_width"
<*> o .:? "photo_height"
Just fileId -> InlineQueryResultCachedPhoto <$> parseJSON (Object o) <*> pure fileId
"gif" -> parseFileId o "gif_file_id" >>= \case
Nothing -> InlineQueryResultGif
<$> parseJSON (Object o) -- generic thumbnail
<*> o .: "gif_url"
<*> o .: "gif_width"
<*> o .: "gif_height"
<*> o .: "gif_duration"
<*> o .:? "gif_width"
<*> o .:? "gif_height"
<*> o .:? "gif_duration"
Just fileId -> InlineQueryResultCachedGif <$> parseJSON (Object o) <*> pure fileId
"mpeg4_gif" -> parseFileId o "mpeg4_file_id" >>= \case
Nothing -> InlineQueryResultMpeg4Gif
<$> parseJSON (Object o) -- generic thumbnail
<*> o .: "mpeg4_url"
<*> o .: "mpeg4_width"
<*> o .: "mpeg4_height"
<*> o .: "mpeg4_duration"
<*> o .:? "mpeg4_width"
<*> o .:? "mpeg4_height"
<*> o .:? "mpeg4_duration"
Just fileId -> InlineQueryResultCachedMpeg4Gif
<$> parseJSON (Object o)
<*> pure fileId
Expand All @@ -353,24 +353,24 @@ instance FromJSON InlineQueryResult where
<$> parseJSON (Object o)
<*> o .: "video_url"
<*> o .: "mime_type"
<*> o .: "video_width"
<*> o .: "video_height"
<*> o .: "video_duration"
<*> o .:? "video_width"
<*> o .:? "video_height"
<*> o .:? "video_duration"
Just fileId -> InlineQueryResultCachedVideo
<$> parseJSON (Object o)
<*> pure fileId
"audio" -> parseFileId o "audio_file_id" >>= \case
Nothing -> InlineQueryResultAudio
<$> parseJSON (Object o)
<*> o .: "audio_url"
<*> o .: "performer"
<*> o .: "duration"
<*> o .:? "performer"
<*> o .:? "duration"
Just fileId -> InlineQueryResultCachedAudio <$> parseJSON (Object o) <*> pure fileId
"voice" -> parseFileId o "voice_file_id" >>= \case
Nothing -> InlineQueryResultVoice
<$> parseJSON (Object o)
<*> o .: "voice_url"
<*> o .: "voice_duration"
<*> o .:? "voice_duration"
Just fileId -> InlineQueryResultCachedVoice <$> parseJSON (Object o) <*> pure fileId
"document" -> parseFileId o "document_file_id" >>= \case
Nothing -> InlineQueryResultDocument
Expand All @@ -382,25 +382,25 @@ instance FromJSON InlineQueryResult where
<$> parseJSON (Object o)
<*> o .: "latitude"
<*> o .: "longitude"
<*> o .: "horizontal_accuracy"
<*> o .: "live_period"
<*> o .: "heading"
<*> o .: "proximity_alert_radius"
<*> o .:? "horizontal_accuracy"
<*> o .:? "live_period"
<*> o .:? "heading"
<*> o .:? "proximity_alert_radius"
"venue" -> InlineQueryResultVenue
<$> parseJSON (Object o)
<*> o .: "latitude"
<*> o .: "longitude"
<*> o .: "address"
<*> o .: "foursquare_id"
<*> o .: "foursquare_type"
<*> o .: "google_place_id"
<*> o .: "google_place_type"
<*> o .:? "foursquare_id"
<*> o .:? "foursquare_type"
<*> o .:? "google_place_id"
<*> o .:? "google_place_type"
"contact" -> InlineQueryResultContact
<$> parseJSON (Object o)
<*> o .: "phone_number"
<*> o .: "first_name"
<*> o .: "last_name"
<*> o .: "vcard"
<*> o .:? "last_name"
<*> o .:? "vcard"
"game" -> InlineQueryResultGame
<$> parseJSON (Object o)
<*> o .: "game_short_name"
Expand Down
10 changes: 5 additions & 5 deletions telegram-bot-api/src/Telegram/Bot/API/Types/BackgroundType.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{-# LANGUAGE OverloadedStrings #-}
module Telegram.Bot.API.Types.BackgroundType where

import Data.Aeson (FromJSON (..), ToJSON (..), Value (..), (.=), (.:), withObject)
import Data.Aeson (FromJSON (..), ToJSON (..), Value (..), (.=), (.:), (.:?), withObject)
import Data.Aeson.Types (Parser)
import Data.Text (Text)
import GHC.Generics (Generic)
Expand Down Expand Up @@ -82,15 +82,15 @@ instance FromJSON BackgroundType where
<$> o .: "type"
<*> o .: "document"
<*> o .: "dark_theme_dimming"
<*> o .: "is_blurred"
<*> o .: "is_moving"
<*> o .:? "is_blurred"
<*> o .:? "is_moving"
"pattern" -> BackgroundTypePattern
<$> o .: "type"
<*> o .: "document"
<*> o .: "fill"
<*> o .: "intensity"
<*> o .: "is_inverted"
<*> o .: "is_moving"
<*> o .:? "is_inverted"
<*> o .:? "is_moving"
"chat_theme" -> BackgroundTypeChatTheme
<$> o .: "type"
<*> o .: "theme_name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{-# LANGUAGE OverloadedStrings #-}
module Telegram.Bot.API.Types.ChatBoostSource where

import Data.Aeson (FromJSON (..), ToJSON (..), KeyValue ((.=)), Value (..), withObject, (.:))
import Data.Aeson (FromJSON (..), ToJSON (..), KeyValue ((.=)), Value (..), withObject, (.:), (.:?))
import Data.Aeson.Types (Parser)
import Data.Text (Text)
import GHC.Generics (Generic)
Expand All @@ -22,6 +22,7 @@ import Telegram.Bot.API.Types.User
-- * ChatBoostSourcePremium
-- * ChatBoostSourceGiftCode
-- * ChatBoostSourceGiveaway
--
data ChatBoostSource
-- | The boost was obtained by subscribing to Telegram Premium or by gifting a Telegram Premium subscription to another user.
= ChatBoostSourcePremium
Expand Down Expand Up @@ -68,6 +69,6 @@ instance FromJSON ChatBoostSource where
"giveaway" -> ChatBoostSourceGiveaway
<$> o .: "source"
<*> o .: "giveaway_message_id"
<*> o .: "user"
<*> o .: "is_unclaimed"
<*> o .:? "user"
<*> o .:? "is_unclaimed"
t -> fail $ Text.unpack ("Unknown source: " <> t)
6 changes: 3 additions & 3 deletions telegram-bot-api/src/Telegram/Bot/API/Types/MessageOrigin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{-# LANGUAGE OverloadedStrings #-}
module Telegram.Bot.API.Types.MessageOrigin where

import Data.Aeson (FromJSON (..), ToJSON (..), Value (..), (.=), (.:), withObject)
import Data.Aeson (FromJSON (..), ToJSON (..), Value (..), (.=), (.:), (.:?), withObject)
import Data.Aeson.Types (Parser)
import Data.Text (Text)
import Data.Time.Clock.POSIX (POSIXTime)
Expand Down Expand Up @@ -86,12 +86,12 @@ instance FromJSON MessageOrigin where
<$> o .: "type"
<*> o .: "date"
<*> o .: "sender_chat"
<*> o .: "author_signature"
<*> o .:? "author_signature"
"channel" -> MessageOriginChannel
<$> o .: "type"
<*> o .: "date"
<*> o .: "chat"
<*> o .: "message_id"
<*> o .: "author_signature"
<*> o .:? "author_signature"
t -> fail $ Text.unpack ("Unknown type: " <> t)

0 comments on commit 7ec78ba

Please sign in to comment.