module Data.SqlCommenter
( sqlCommenterGoogle,
sqlCommenterStandard,
Attribute,
)
where
import Data.List.NonEmpty qualified as NE
import Data.Text.Extended (commaSeparated)
import Hasura.Prelude
import Hasura.QueryTags
import Network.URI.Encode qualified as URI
sqlCommenterGoogle :: QueryTagsAttributes -> QueryTagsComment
QueryTagsAttributes
qtAttributes
| [(Text, Text)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Text, Text)]
attributes = QueryTagsComment
emptyQueryTagsComment
| Bool
otherwise = Text -> QueryTagsComment
QueryTagsComment (Text -> QueryTagsComment) -> Text -> QueryTagsComment
forall a b. (a -> b) -> a -> b
$ Text -> Text
forall {a}. (Semigroup a, IsString a) => a -> a
createSQLComment (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ NonEmpty (Text, Text) -> Text
generateCommentTags ([(Text, Text)] -> NonEmpty (Text, Text)
forall a. HasCallStack => [a] -> NonEmpty a
NE.fromList [(Text, Text)]
attributes)
where
createSQLComment :: a -> a
createSQLComment a
comment = a
" /* " a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
comment a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
" */"
attributes :: [(Text, Text)]
attributes = QueryTagsAttributes -> [(Text, Text)]
_unQueryTagsAttributes QueryTagsAttributes
qtAttributes
sqlCommenterStandard :: QueryTagsAttributes -> QueryTagsComment
sqlCommenterStandard :: QueryTagsAttributes -> QueryTagsComment
sqlCommenterStandard QueryTagsAttributes
qtAttributes
| [(Text, Text)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Text, Text)]
attributes = QueryTagsComment
emptyQueryTagsComment
| Bool
otherwise = Text -> QueryTagsComment
QueryTagsComment (Text -> QueryTagsComment) -> Text -> QueryTagsComment
forall a b. (a -> b) -> a -> b
$ Text -> Text
forall {a}. (Semigroup a, IsString a) => a -> a
createSQLComment (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ NonEmpty (Text, Text) -> Text
forall {t}.
(ToTxt t, Semigroup t, IsString t) =>
NonEmpty (t, t) -> Text
generateComment ([(Text, Text)] -> NonEmpty (Text, Text)
forall a. HasCallStack => [a] -> NonEmpty a
NE.fromList [(Text, Text)]
attributes)
where
generateComment :: NonEmpty (t, t) -> Text
generateComment NonEmpty (t, t)
attr = [t] -> Text
forall t (f :: * -> *). (ToTxt t, Foldable f) => f t -> Text
commaSeparated [t
k t -> t -> t
forall a. Semigroup a => a -> a -> a
<> t
"=" t -> t -> t
forall a. Semigroup a => a -> a -> a
<> t
v | (t
k, t
v) <- NonEmpty (t, t) -> [(t, t)]
forall a. NonEmpty a -> [a]
NE.toList NonEmpty (t, t)
attr]
createSQLComment :: a -> a
createSQLComment a
comment = a
" /* " a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
comment a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
" */"
attributes :: [(Text, Text)]
attributes = QueryTagsAttributes -> [(Text, Text)]
_unQueryTagsAttributes QueryTagsAttributes
qtAttributes
generateCommentTags :: NE.NonEmpty Attribute -> Text
NonEmpty (Text, Text)
attributes =
let
encoded :: NonEmpty (Text, Text)
encoded = ((Text, Text) -> (Text, Text))
-> NonEmpty (Text, Text) -> NonEmpty (Text, Text)
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
NE.map (Text, Text) -> (Text, Text)
urlEncodePair NonEmpty (Text, Text)
attributes
sorted :: NonEmpty (Text, Text)
sorted = NonEmpty (Text, Text) -> NonEmpty (Text, Text)
forall a. Ord a => NonEmpty a -> NonEmpty a
NE.sort NonEmpty (Text, Text)
encoded
serialized :: Text
serialized = NonEmpty (Text, Text) -> Text
forall {t}.
(ToTxt t, Semigroup t, IsString t) =>
NonEmpty (t, t) -> Text
serializePairs NonEmpty (Text, Text)
sorted
in Text
serialized
where
urlEncodePair :: (Text, Text) -> (Text, Text)
urlEncodePair (Text
k, Text
v) = (Text -> Text
URI.encodeText Text
k, Text -> Text
URI.encodeText Text
v)
serializePairs :: NonEmpty (t, t) -> Text
serializePairs NonEmpty (t, t)
pairs = [t] -> Text
forall t (f :: * -> *). (ToTxt t, Foldable f) => f t -> Text
commaSeparated [t
k t -> t -> t
forall a. Semigroup a => a -> a -> a
<> t
"='" t -> t -> t
forall a. Semigroup a => a -> a -> a
<> t
v t -> t -> t
forall a. Semigroup a => a -> a -> a
<> t
"'" | (t
k, t
v) <- NonEmpty (t, t) -> [(t, t)]
forall a. NonEmpty a -> [a]
NE.toList NonEmpty (t, t)
pairs]