module Hasura.GraphQL.Schema.NamingCase
  ( NamingCase (..),
    isGraphqlCase,
    parseNamingConventionFromText,
  )
where

import Data.Aeson qualified as Aeson
import Hasura.Incremental (Cacheable)
import Hasura.Prelude

-- | Represents the different possible type cases for fields and types, i.e.
--   @HasuraCase@ and @GraphqlCase@ (@CamelCase@ fields and @PascalCase@ types).
data NamingCase = HasuraCase | GraphqlCase
  deriving (NamingCase -> NamingCase -> Bool
(NamingCase -> NamingCase -> Bool)
-> (NamingCase -> NamingCase -> Bool) -> Eq NamingCase
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NamingCase -> NamingCase -> Bool
$c/= :: NamingCase -> NamingCase -> Bool
== :: NamingCase -> NamingCase -> Bool
$c== :: NamingCase -> NamingCase -> Bool
Eq, Int -> NamingCase -> ShowS
[NamingCase] -> ShowS
NamingCase -> String
(Int -> NamingCase -> ShowS)
-> (NamingCase -> String)
-> ([NamingCase] -> ShowS)
-> Show NamingCase
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NamingCase] -> ShowS
$cshowList :: [NamingCase] -> ShowS
show :: NamingCase -> String
$cshow :: NamingCase -> String
showsPrec :: Int -> NamingCase -> ShowS
$cshowsPrec :: Int -> NamingCase -> ShowS
Show, (forall x. NamingCase -> Rep NamingCase x)
-> (forall x. Rep NamingCase x -> NamingCase) -> Generic NamingCase
forall x. Rep NamingCase x -> NamingCase
forall x. NamingCase -> Rep NamingCase x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep NamingCase x -> NamingCase
$cfrom :: forall x. NamingCase -> Rep NamingCase x
Generic)

instance Cacheable NamingCase

instance Aeson.ToJSON NamingCase where
  toJSON :: NamingCase -> Value
toJSON NamingCase
HasuraCase = Text -> Value
Aeson.String Text
"hasura-default"
  toJSON NamingCase
GraphqlCase = Text -> Value
Aeson.String Text
"graphql-default"

instance Aeson.FromJSON NamingCase where
  parseJSON :: Value -> Parser NamingCase
parseJSON = String -> (Text -> Parser NamingCase) -> Value -> Parser NamingCase
forall a. String -> (Text -> Parser a) -> Value -> Parser a
Aeson.withText String
"NamingCase" ((Text -> Parser NamingCase) -> Value -> Parser NamingCase)
-> (Text -> Parser NamingCase) -> Value -> Parser NamingCase
forall a b. (a -> b) -> a -> b
$ \Text
s -> case Text -> Either String NamingCase
parseNamingConventionFromText Text
s of
    (Right NamingCase
nc) -> NamingCase -> Parser NamingCase
forall (f :: * -> *) a. Applicative f => a -> f a
pure NamingCase
nc
    (Left String
err) -> String -> Parser NamingCase
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
err

-- Used for both the environment variable and JSON.
parseNamingConventionFromText :: Text -> Either String NamingCase
parseNamingConventionFromText :: Text -> Either String NamingCase
parseNamingConventionFromText Text
"hasura-default" = NamingCase -> Either String NamingCase
forall a b. b -> Either a b
Right NamingCase
HasuraCase
parseNamingConventionFromText Text
"graphql-default" = NamingCase -> Either String NamingCase
forall a b. b -> Either a b
Right NamingCase
GraphqlCase
parseNamingConventionFromText Text
_ = String -> Either String NamingCase
forall a b. a -> Either a b
Left String
"naming_convention can either be \"hasura-default\" or \"graphql-default\""

isGraphqlCase :: NamingCase -> Bool
isGraphqlCase :: NamingCase -> Bool
isGraphqlCase NamingCase
GraphqlCase = Bool
True
isGraphqlCase NamingCase
_ = Bool
False