module Data.Text.Casing
(
GQLNameIdentifier,
namePrefix,
nameSuffixes,
NameOrigin (..),
toCamelT,
toPascalT,
toSnakeT,
toCamelG,
toPascalG,
toSnakeG,
snakeToCamel,
snakeToPascal,
fromSnake,
transformNameWith,
transformGQLSuffixWith,
transformGQLIdentifierWith,
fromTupleWith,
fromNameWith,
fromAutogeneratedName,
fromCustomName,
fromAutogeneratedTuple,
fromCustomTuple,
fromNonEmptyList,
identifierToList,
lowerFirstChar,
transformPrefixAndSuffixAndConcat,
upperFirstChar,
)
where
import Data.List (intersperse)
import Data.List.NonEmpty qualified as NE
import Data.Text qualified as T
import Hasura.Prelude
import Language.GraphQL.Draft.Syntax qualified as G
type NameWithOrigin = (G.Name, NameOrigin)
type NameSuffixWithOrigin = (G.NameSuffix, NameOrigin)
data GQLNameIdentifier = GQLNameIdentifier
{ GQLNameIdentifier -> NameWithOrigin
namePrefix :: NameWithOrigin,
GQLNameIdentifier -> [NameSuffixWithOrigin]
nameSuffixes :: [NameSuffixWithOrigin]
}
deriving (Int -> GQLNameIdentifier -> ShowS
[GQLNameIdentifier] -> ShowS
GQLNameIdentifier -> String
(Int -> GQLNameIdentifier -> ShowS)
-> (GQLNameIdentifier -> String)
-> ([GQLNameIdentifier] -> ShowS)
-> Show GQLNameIdentifier
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GQLNameIdentifier] -> ShowS
$cshowList :: [GQLNameIdentifier] -> ShowS
show :: GQLNameIdentifier -> String
$cshow :: GQLNameIdentifier -> String
showsPrec :: Int -> GQLNameIdentifier -> ShowS
$cshowsPrec :: Int -> GQLNameIdentifier -> ShowS
Show)
data NameOrigin = CustomName | AutogeneratedName
deriving (Int -> NameOrigin -> ShowS
[NameOrigin] -> ShowS
NameOrigin -> String
(Int -> NameOrigin -> ShowS)
-> (NameOrigin -> String)
-> ([NameOrigin] -> ShowS)
-> Show NameOrigin
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NameOrigin] -> ShowS
$cshowList :: [NameOrigin] -> ShowS
show :: NameOrigin -> String
$cshow :: NameOrigin -> String
showsPrec :: Int -> NameOrigin -> ShowS
$cshowsPrec :: Int -> NameOrigin -> ShowS
Show)
instance (Semigroup GQLNameIdentifier) where
GQLNameIdentifier NameWithOrigin
pref1 [NameSuffixWithOrigin]
suffs1 <> :: GQLNameIdentifier -> GQLNameIdentifier -> GQLNameIdentifier
<> GQLNameIdentifier NameWithOrigin
pref2 [NameSuffixWithOrigin]
suffs2 = NameWithOrigin -> [NameSuffixWithOrigin] -> GQLNameIdentifier
GQLNameIdentifier NameWithOrigin
pref1 ([NameSuffixWithOrigin]
suffs1 [NameSuffixWithOrigin]
-> [NameSuffixWithOrigin] -> [NameSuffixWithOrigin]
forall a. [a] -> [a] -> [a]
++ (Name -> NameSuffix) -> NameWithOrigin -> NameSuffixWithOrigin
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first Name -> NameSuffix
G.convertNameToSuffix NameWithOrigin
pref2 NameSuffixWithOrigin
-> [NameSuffixWithOrigin] -> [NameSuffixWithOrigin]
forall a. a -> [a] -> [a]
: [NameSuffixWithOrigin]
suffs2)
fromTupleWith :: NameOrigin -> (G.Name, [G.NameSuffix]) -> GQLNameIdentifier
fromTupleWith :: NameOrigin -> (Name, [NameSuffix]) -> GQLNameIdentifier
fromTupleWith NameOrigin
o (Name
pref, [NameSuffix]
suffs) = NameWithOrigin -> [NameSuffixWithOrigin] -> GQLNameIdentifier
GQLNameIdentifier (Name
pref, NameOrigin
o) ((NameSuffix -> NameSuffixWithOrigin)
-> [NameSuffix] -> [NameSuffixWithOrigin]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (,NameOrigin
o) [NameSuffix]
suffs)
fromNameWith :: NameOrigin -> G.Name -> GQLNameIdentifier
fromNameWith :: NameOrigin -> Name -> GQLNameIdentifier
fromNameWith NameOrigin
o Name
n = NameWithOrigin -> [NameSuffixWithOrigin] -> GQLNameIdentifier
GQLNameIdentifier (Name
n, NameOrigin
o) []
fromAutogeneratedName :: G.Name -> GQLNameIdentifier
fromAutogeneratedName :: Name -> GQLNameIdentifier
fromAutogeneratedName = NameOrigin -> Name -> GQLNameIdentifier
fromNameWith NameOrigin
AutogeneratedName
fromCustomName :: G.Name -> GQLNameIdentifier
fromCustomName :: Name -> GQLNameIdentifier
fromCustomName = NameOrigin -> Name -> GQLNameIdentifier
fromNameWith NameOrigin
CustomName
fromAutogeneratedTuple :: (G.Name, [G.NameSuffix]) -> GQLNameIdentifier
fromAutogeneratedTuple :: (Name, [NameSuffix]) -> GQLNameIdentifier
fromAutogeneratedTuple = NameOrigin -> (Name, [NameSuffix]) -> GQLNameIdentifier
fromTupleWith NameOrigin
AutogeneratedName
fromCustomTuple :: (G.Name, [G.NameSuffix]) -> GQLNameIdentifier
fromCustomTuple :: (Name, [NameSuffix]) -> GQLNameIdentifier
fromCustomTuple = NameOrigin -> (Name, [NameSuffix]) -> GQLNameIdentifier
fromTupleWith NameOrigin
CustomName
fromNonEmptyList :: NonEmpty NameWithOrigin -> GQLNameIdentifier
fromNonEmptyList :: NonEmpty NameWithOrigin -> GQLNameIdentifier
fromNonEmptyList NonEmpty NameWithOrigin
neList = NameWithOrigin -> [NameSuffixWithOrigin] -> GQLNameIdentifier
GQLNameIdentifier (NonEmpty NameWithOrigin -> NameWithOrigin
forall a. NonEmpty a -> a
NE.head NonEmpty NameWithOrigin
neList) ((NameWithOrigin -> NameSuffixWithOrigin)
-> [NameWithOrigin] -> [NameSuffixWithOrigin]
forall a b. (a -> b) -> [a] -> [b]
map ((Name -> NameSuffix) -> NameWithOrigin -> NameSuffixWithOrigin
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first Name -> NameSuffix
G.convertNameToSuffix) (NonEmpty NameWithOrigin -> [NameWithOrigin]
forall a. NonEmpty a -> [a]
NE.tail NonEmpty NameWithOrigin
neList))
transformNameWith :: (Text -> Text) -> G.Name -> G.Name
transformNameWith :: (Text -> Text) -> Name -> Name
transformNameWith Text -> Text
f Name
name = Name -> Maybe Name -> Name
forall a. a -> Maybe a -> a
fromMaybe Name
name (Text -> Maybe Name
G.mkName (Text -> Text
f (Name -> Text
G.unName Name
name)))
transformNameAndOriginWith :: (Text -> Text) -> NameWithOrigin -> NameWithOrigin
transformNameAndOriginWith :: (Text -> Text) -> NameWithOrigin -> NameWithOrigin
transformNameAndOriginWith Text -> Text
_ n :: NameWithOrigin
n@(Name
_, NameOrigin
CustomName) = NameWithOrigin
n
transformNameAndOriginWith Text -> Text
f (Name
name, NameOrigin
AutogeneratedName) = ((Text -> Text) -> Name -> Name
transformNameWith Text -> Text
f Name
name, NameOrigin
AutogeneratedName)
transformGQLSuffixWith :: (Text -> Text) -> G.NameSuffix -> G.NameSuffix
transformGQLSuffixWith :: (Text -> Text) -> NameSuffix -> NameSuffix
transformGQLSuffixWith Text -> Text
f NameSuffix
suffix = NameSuffix -> Maybe NameSuffix -> NameSuffix
forall a. a -> Maybe a -> a
fromMaybe NameSuffix
suffix (Text -> Maybe NameSuffix
G.mkNameSuffix (Text -> Text
f (NameSuffix -> Text
G.unNameSuffix NameSuffix
suffix)))
transformGQLSuffixAndOriginWith :: (Text -> Text) -> NameSuffixWithOrigin -> NameSuffixWithOrigin
transformGQLSuffixAndOriginWith :: (Text -> Text) -> NameSuffixWithOrigin -> NameSuffixWithOrigin
transformGQLSuffixAndOriginWith Text -> Text
f (NameSuffix
suffix, NameOrigin
orig) = ((Text -> Text) -> NameSuffix -> NameSuffix
transformGQLSuffixWith Text -> Text
f NameSuffix
suffix, NameOrigin
orig)
transformGQLIdentifierWith :: (Text -> Text) -> GQLNameIdentifier -> GQLNameIdentifier
transformGQLIdentifierWith :: (Text -> Text) -> GQLNameIdentifier -> GQLNameIdentifier
transformGQLIdentifierWith Text -> Text
f (GQLNameIdentifier NameWithOrigin
pref [NameSuffixWithOrigin]
suffs) = NameWithOrigin -> [NameSuffixWithOrigin] -> GQLNameIdentifier
GQLNameIdentifier ((Text -> Text) -> NameWithOrigin -> NameWithOrigin
transformNameAndOriginWith Text -> Text
f NameWithOrigin
pref) ((NameSuffixWithOrigin -> NameSuffixWithOrigin)
-> [NameSuffixWithOrigin] -> [NameSuffixWithOrigin]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Text -> Text) -> NameSuffixWithOrigin -> NameSuffixWithOrigin
transformGQLSuffixAndOriginWith Text -> Text
f) [NameSuffixWithOrigin]
suffs)
identifierToList :: GQLNameIdentifier -> [Text]
identifierToList :: GQLNameIdentifier -> [Text]
identifierToList (GQLNameIdentifier (Name
pref, NameOrigin
_) ([NameSuffixWithOrigin]
suffs)) = (Name -> Text
G.unName Name
pref) Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: ((NameSuffixWithOrigin -> Text) -> [NameSuffixWithOrigin] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (NameSuffix -> Text
G.unNameSuffix (NameSuffix -> Text)
-> (NameSuffixWithOrigin -> NameSuffix)
-> NameSuffixWithOrigin
-> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NameSuffixWithOrigin -> NameSuffix
forall a b. (a, b) -> a
fst) [NameSuffixWithOrigin]
suffs)
toSnakeT :: [Text] -> Text
toSnakeT :: [Text] -> Text
toSnakeT = [Text] -> Text
T.concat ([Text] -> Text) -> ([Text] -> [Text]) -> [Text] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
intersperse Text
"_"
toPascalT :: [Text] -> Text
toPascalT :: [Text] -> Text
toPascalT = [Text] -> Text
T.concat ([Text] -> Text) -> ([Text] -> [Text]) -> [Text] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Text
upperFirstChar
toCamelT :: [Text] -> Text
toCamelT :: [Text] -> Text
toCamelT ([]) = Text
""
toCamelT ((Text
x : [Text]
xs)) = (Text -> Text
lowerFirstChar Text
x) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Text] -> Text
T.concat ((Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Text
upperFirstChar [Text]
xs)
toSnakeG :: GQLNameIdentifier -> G.Name
toSnakeG :: GQLNameIdentifier -> Name
toSnakeG (GQLNameIdentifier (Name
pref, NameOrigin
_) [NameSuffixWithOrigin]
suff) = Name -> [NameSuffix] -> Name
G.addSuffixes Name
pref ((NameSuffixWithOrigin -> NameSuffix)
-> [NameSuffixWithOrigin] -> [NameSuffix]
forall a b. (a -> b) -> [a] -> [b]
map ((Text -> Text) -> NameSuffix -> NameSuffix
transformGQLSuffixWith (Text
"_" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>) (NameSuffix -> NameSuffix)
-> (NameSuffixWithOrigin -> NameSuffix)
-> NameSuffixWithOrigin
-> NameSuffix
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NameSuffixWithOrigin -> NameSuffix
forall a b. (a, b) -> a
fst) [NameSuffixWithOrigin]
suff)
toPascalG :: GQLNameIdentifier -> G.Name
toPascalG :: GQLNameIdentifier -> Name
toPascalG GQLNameIdentifier
gqlIdentifier = GQLNameIdentifier -> (Text -> Text) -> (Text -> Text) -> Name
transformPrefixAndSuffixAndConcat GQLNameIdentifier
gqlIdentifier Text -> Text
upperFirstChar Text -> Text
upperFirstChar
toCamelG :: GQLNameIdentifier -> G.Name
toCamelG :: GQLNameIdentifier -> Name
toCamelG GQLNameIdentifier
gqlIdentifier = GQLNameIdentifier -> (Text -> Text) -> (Text -> Text) -> Name
transformPrefixAndSuffixAndConcat GQLNameIdentifier
gqlIdentifier Text -> Text
lowerFirstChar Text -> Text
upperFirstChar
transformPrefixAndSuffixAndConcat :: GQLNameIdentifier -> (T.Text -> T.Text) -> (T.Text -> T.Text) -> G.Name
transformPrefixAndSuffixAndConcat :: GQLNameIdentifier -> (Text -> Text) -> (Text -> Text) -> Name
transformPrefixAndSuffixAndConcat (GQLNameIdentifier NameWithOrigin
pref [NameSuffixWithOrigin]
suff) Text -> Text
prefixTransformer Text -> Text
suffixTransformer =
Name -> [NameSuffix] -> Name
G.addSuffixes (NameWithOrigin -> Name
forall a b. (a, b) -> a
fst ((Text -> Text) -> NameWithOrigin -> NameWithOrigin
transformNameAndOriginWith Text -> Text
prefixTransformer NameWithOrigin
pref)) ((NameSuffixWithOrigin -> NameSuffix)
-> [NameSuffixWithOrigin] -> [NameSuffix]
forall a b. (a -> b) -> [a] -> [b]
map (NameSuffixWithOrigin -> NameSuffix
forall a b. (a, b) -> a
fst (NameSuffixWithOrigin -> NameSuffix)
-> (NameSuffixWithOrigin -> NameSuffixWithOrigin)
-> NameSuffixWithOrigin
-> NameSuffix
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> Text) -> NameSuffixWithOrigin -> NameSuffixWithOrigin
transformGQLSuffixAndOriginWith Text -> Text
suffixTransformer) [NameSuffixWithOrigin]
suff)
fromSnake :: Text -> [Text]
fromSnake :: Text -> [Text]
fromSnake Text
t = case Text -> Text -> [Text]
T.splitOn Text
"_" Text
t of
(Text
"" : Text
x : [Text]
xs) -> (Text
"_" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
x) Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: [Text]
xs
[Text]
bs -> [Text]
bs
snakeToPascal :: Text -> Text
snakeToPascal :: Text -> Text
snakeToPascal = [Text] -> Text
toPascalT ([Text] -> Text) -> (Text -> [Text]) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text]
fromSnake
snakeToCamel :: Text -> Text
snakeToCamel :: Text -> Text
snakeToCamel = [Text] -> Text
toCamelT ([Text] -> Text) -> (Text -> [Text]) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text]
fromSnake
lowerFirstChar :: Text -> Text
lowerFirstChar :: Text -> Text
lowerFirstChar Text
t = Text -> Text
T.toLower (Int -> Text -> Text
T.take Int
1 Text
t) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text -> Text
T.drop Int
1 Text
t
upperFirstChar :: Text -> Text
upperFirstChar :: Text -> Text
upperFirstChar Text
t = Text -> Text
T.toUpper (Int -> Text -> Text
T.take Int
1 Text
t) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text -> Text
T.drop Int
1 Text
t