2022-06-21 14:11:08 +03:00
|
|
|
{-# LANGUAGE TemplateHaskellQuotes #-}
|
2022-03-16 03:39:21 +03:00
|
|
|
|
2020-10-29 03:04:21 +03:00
|
|
|
module Data.Text.NonEmpty
|
2021-09-24 01:56:37 +03:00
|
|
|
( NonEmptyText,
|
|
|
|
mkNonEmptyTextUnsafe,
|
|
|
|
mkNonEmptyText,
|
|
|
|
unNonEmptyText,
|
|
|
|
nonEmptyText,
|
2022-04-18 22:43:00 +03:00
|
|
|
nonEmptyTextQQ,
|
2021-09-24 01:56:37 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Data.Aeson
|
|
|
|
import Data.Text qualified as T
|
|
|
|
import Data.Text.Extended
|
|
|
|
import Database.PG.Query qualified as Q
|
2022-04-18 22:43:00 +03:00
|
|
|
import Hasura.Prelude hiding (lift)
|
|
|
|
import Language.Haskell.TH.Quote (QuasiQuoter (..))
|
|
|
|
import Language.Haskell.TH.Syntax (Lift, Q, TExp, lift)
|
2021-09-24 01:56:37 +03:00
|
|
|
import Test.QuickCheck qualified as QC
|
|
|
|
|
|
|
|
newtype NonEmptyText = NonEmptyText {unNonEmptyText :: Text}
|
2020-10-29 03:04:21 +03:00
|
|
|
deriving (Show, Eq, Ord, Hashable, ToJSON, ToJSONKey, Lift, Q.ToPrepArg, ToTxt, Generic, NFData)
|
|
|
|
|
2021-07-30 10:54:50 +03:00
|
|
|
instance QC.Arbitrary NonEmptyText where
|
2020-10-29 03:04:21 +03:00
|
|
|
arbitrary = NonEmptyText . T.pack <$> QC.listOf1 (QC.elements alphaNumerics)
|
|
|
|
|
|
|
|
mkNonEmptyText :: Text -> Maybe NonEmptyText
|
2021-09-24 01:56:37 +03:00
|
|
|
mkNonEmptyText "" = Nothing
|
2020-10-29 03:04:21 +03:00
|
|
|
mkNonEmptyText text = Just $ NonEmptyText text
|
|
|
|
|
|
|
|
mkNonEmptyTextUnsafe :: Text -> NonEmptyText
|
|
|
|
mkNonEmptyTextUnsafe = NonEmptyText
|
|
|
|
|
|
|
|
parseNonEmptyText :: MonadFail m => Text -> m NonEmptyText
|
2021-02-23 20:37:27 +03:00
|
|
|
parseNonEmptyText text = mkNonEmptyText text `onNothing` fail "empty string not allowed"
|
2020-10-29 03:04:21 +03:00
|
|
|
|
|
|
|
nonEmptyText :: Text -> Q (TExp NonEmptyText)
|
2021-09-24 01:56:37 +03:00
|
|
|
nonEmptyText = parseNonEmptyText >=> \text -> [||text||]
|
2020-10-29 03:04:21 +03:00
|
|
|
|
2022-04-18 22:43:00 +03:00
|
|
|
-- | Construct 'NonEmptyText' literals at compile-time via quasiquotation.
|
|
|
|
nonEmptyTextQQ :: QuasiQuoter
|
|
|
|
nonEmptyTextQQ =
|
|
|
|
QuasiQuoter {quoteExp, quotePat, quoteType, quoteDec}
|
|
|
|
where
|
|
|
|
quotePat _ = error "nonEmptyTextQQ does not support quoting patterns"
|
|
|
|
quoteType _ = error "nonEmptyTextQQ does not support quoting types"
|
|
|
|
quoteDec _ = error "nonEmptyTextQQ does not support quoting declarations"
|
|
|
|
quoteExp s = case mkNonEmptyText (T.pack s) of
|
|
|
|
Just result -> lift result
|
|
|
|
Nothing -> fail "empty string not allowed"
|
|
|
|
|
2020-10-29 03:04:21 +03:00
|
|
|
instance FromJSON NonEmptyText where
|
|
|
|
parseJSON = withText "String" parseNonEmptyText
|
|
|
|
|
|
|
|
instance FromJSONKey NonEmptyText where
|
|
|
|
fromJSONKey = FromJSONKeyTextParser parseNonEmptyText
|
|
|
|
|
|
|
|
instance Q.FromCol NonEmptyText where
|
2021-09-24 01:56:37 +03:00
|
|
|
fromCol bs =
|
|
|
|
mkNonEmptyText <$> Q.fromCol bs
|
|
|
|
>>= maybe (Left "empty string not allowed") Right
|