packages feed

telegram-bot-api-7.4 / src / Telegram / Bot / API / Internal / Utils.hs

{-# LANGUAGE FlexibleContexts #-}{-# LANGUAGE FlexibleInstances #-}{-# LANGUAGE PolyKinds #-}{-# LANGUAGE ScopedTypeVariables #-}{-# LANGUAGE TypeFamilies #-}{-# LANGUAGE TypeOperators #-}{-# LANGUAGE TypeSynonymInstances #-}{-# LANGUAGE CPP #-}module Telegram.Bot.API.Internal.Utils whereimport Control.Applicative ((<|>))import Data.Aeson (FromJSON(..), ToJSON(..), Value(..), GToJSON, GFromJSON, genericToJSON, genericParseJSON, Zero)import Data.Aeson.TH (deriveJSON)import Data.Aeson.Types (Options(..), defaultOptions, Parser, Pair)import Data.Char (isUpper, toUpper, toLower)import Data.List (intercalate)import GHC.Genericsimport Language.Haskell.THimport Servant.Multipart.API (MultipartData(MultipartData), Input)import Telegram.Bot.API.Internal.TH ()#if MIN_VERSION_aeson(2,0,0)import qualified Data.Aeson.KeyMap as Map#elseimport qualified Data.HashMap.Strict as Map#endifderiveJSON' :: Name -> Q [Dec]deriveJSON' name = deriveJSON (jsonOptions (nameBase name)) namegtoJSON :: forall a d f. (Generic a, GToJSON Zero (Rep a), Rep a ~ D1 d f, Datatype d)  => a -> ValuegtoJSON = genericToJSON (jsonOptions (datatypeName (Proxy3 :: Proxy3 d f a)))gparseJSON :: forall a d f. (Generic a, GFromJSON Zero (Rep a), Rep a ~ D1 d f, Datatype d)  => Value -> Parser agparseJSON = genericParseJSON (jsonOptions (datatypeName (Proxy3 :: Proxy3 d f a)))genericSomeToJSON :: (Generic a, GSomeJSON (Rep a)) => a -> ValuegenericSomeToJSON = gsomeToJSON . fromgenericSomeParseJSON :: (Generic a, GSomeJSON (Rep a)) => Value -> Parser agenericSomeParseJSON = fmap to . gsomeParseJSONdata Proxy3 d f a = Proxy3jsonOptions :: String -> OptionsjsonOptions tname = defaultOptions  { fieldLabelModifier     = snakeFieldModifier tname  , constructorTagModifier = snakeFieldModifier tname  , omitNothingFields      = True  }snakeFieldModifier :: String -> String -> StringsnakeFieldModifier xs ys = wordsToSnake (stripCommonPrefixWords xs ys)camelWords :: String -> [String]camelWords "" = []camelWords s  = case us of      (_:_:_) -> us : camelWords restLs      _       -> (us ++ ls) : camelWords rest  where   (us, restLs) = span  isUpper s   (ls, rest)   = break isUpper restLsstripCommonPrefix :: Eq a => [a] -> [a] -> [a]stripCommonPrefix (x:xs) (y:ys) | x == y = stripCommonPrefix xs ysstripCommonPrefix _ ys = yswordsToCamel :: [String] -> StringwordsToCamel [] = ""wordsToCamel (w:ws) = map toLower w ++ concatMap capitalise wswordsToSnake :: [String] -> StringwordsToSnake = intercalate "_" . map (map toLower)capitalise :: String -> Stringcapitalise (c:s) = toUpper c : scapitalise "" = ""stripCommonPrefixWords :: String -> String -> [String]stripCommonPrefixWords xs ys =  stripCommonPrefix (camelWords xs) (camelWords (capitalise ys))class GSomeJSON f where  gsomeToJSON :: f p -> Value  gsomeParseJSON :: Value -> Parser (f p)instance GSomeJSON f => GSomeJSON (D1 d f) where  gsomeToJSON (M1 x) = gsomeToJSON x  gsomeParseJSON js = M1 <$> gsomeParseJSON jsinstance (ToJSON a, FromJSON a) => GSomeJSON (C1 c (S1 s (K1 i a))) where  gsomeToJSON (M1 (M1 (K1 x))) = toJSON x  gsomeParseJSON js = (M1 . M1 . K1) <$> parseJSON jsinstance (GSomeJSON f, GSomeJSON g) => GSomeJSON (f :+: g) where  gsomeToJSON (L1 x) = gsomeToJSON x  gsomeToJSON (R1 y) = gsomeToJSON y  gsomeParseJSON js      = L1 <$> gsomeParseJSON js    <|> R1 <$> gsomeParseJSON jsaddJsonFields :: Value -> [Pair] -> ValueaddJsonFields (Object obj) pairs = Object $  Map.union obj $ Map.fromList (filter (not . isNull) pairs)addJsonFields x _ = xisNull :: Pair -> BoolisNull (_, Null) = TrueisNull _ = FalseaddMultipartFields :: [Input] -> MultipartData tag -> MultipartData tagaddMultipartFields newFields (MultipartData currenFields files)      = MultipartData (newFields <> currenFields) files