{-# LANGUAGE BangPatterns, DefaultSignatures, LambdaCase,
ScopedTypeVariables #-}
module Frames.ColumnTypeable where
import Control.Monad (MonadPlus)
import Data.Maybe (fromMaybe)
import Data.Readable (Readable(fromText))
import Data.Typeable (Proxy(..), typeRep, Typeable)
import qualified Data.Text as T
import Data.Int (Int32, Int64)
import Data.Vinyl.Functor (Const(..))
import Language.Haskell.TH
data Parsed a = Possibly a | Definitely a deriving (Eq, Ord, Show)
parsedValue :: Parsed a -> a
parsedValue (Possibly a) = a
parsedValue (Definitely a) = a
instance Functor Parsed where
fmap f (Possibly x) = Possibly (f x)
fmap f (Definitely x) = Definitely (f x)
class Parseable a where
parse :: MonadPlus m => T.Text -> m (Parsed a)
default parse :: (Readable a, MonadPlus m)
=> T.Text -> m (Parsed a)
parse = fmap Definitely . fromText
{-# INLINE parse #-}
parseCombine :: MonadPlus m => Parsed a -> Parsed a -> m (Parsed a)
default parseCombine :: MonadPlus m => Parsed a -> Parsed a -> m (Parsed a)
parseCombine = const . return
representableAsType :: Parsed a -> Const (Either (String -> Q [Dec]) Type) a
default
representableAsType :: Typeable a
=> Parsed a -> Const (Either (String -> Q [Dec]) Type) a
representableAsType =
const (Const (Right (ConT (mkName (show (typeRep (Proxy :: Proxy a)))))))
discardConfidence :: Parsed a -> a
discardConfidence (Possibly x) = x
discardConfidence (Definitely x) = x
parse' :: (MonadPlus m, Parseable a) => T.Text -> m a
parse' = fmap discardConfidence . parse
parseIntish :: (Readable a, MonadPlus f) => T.Text -> f (Parsed a)
parseIntish t =
Definitely <$> fromText (fromMaybe t (T.stripSuffix (T.pack ".0") t))
instance Parseable Bool where
instance Parseable Int where
parse = parseIntish
instance Parseable Int32 where
parse = parseIntish
instance Parseable Int64 where
parse = parseIntish
instance Parseable Integer where
parse = parseIntish
instance Parseable Float where
instance Parseable Double where
parse = fmap Definitely . fromText . T.filter (/= ',')
instance Parseable T.Text where
class ColumnTypeable a where
colType :: a -> Either (String -> Q [Dec]) Type
inferType :: T.Text -> a