{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# OPTIONS_GHC -Wno-type-defaults #-}
{-# OPTIONS_GHC -fno-warn-partial-type-signatures #-}

{-
  TemplateHaskell utility functions to create encoders and decoders
-}

module Data.Registry.Aeson.TH.TH where

import Control.Monad.Fail
import Data.List (elemIndex)
import Data.Registry.Aeson.TH.ThOptions
import Language.Haskell.TH
import Language.Haskell.TH.Syntax
import Protolude hiding (Type)

indexConstructorTypes :: [Type] -> [Type] -> Q [(Type, Int)]
indexConstructorTypes :: [Type] -> [Type] -> Q [(Type, Int)]
indexConstructorTypes [Type]
allTypes [Type]
constructorTypes =
  forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for [Type]
constructorTypes forall a b. (a -> b) -> a -> b
$ \Type
t ->
    case forall a. Eq a => a -> [a] -> Maybe Int
elemIndex Type
t [Type]
allTypes of
      Just Int
n -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (Type
t, Int
n)
      Maybe Int
Nothing -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$ String
"the type " forall a. Semigroup a => a -> a -> a
<> forall a b. (Show a, StringConv String b) => a -> b
show Type
t forall a. Semigroup a => a -> a -> a
<> String
" cannot be found in the list of all types " forall a. Semigroup a => a -> a -> a
<> forall a b. (Show a, StringConv String b) => a -> b
show [Type]
allTypes

-- | Get the types of all the fields of a constructor
typesOf :: Con -> Q [Type]
typesOf :: Con -> Q [Type]
typesOf (NormalC Name
_ [BangType]
types) = forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [BangType]
types)
typesOf (RecC Name
_ [VarBangType]
types) = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ (\(Name
_, Bang
_, Type
t) -> Type
t) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [VarBangType]
types
typesOf Con
other = do
  forall (m :: * -> *). Quasi m => Bool -> String -> m ()
qReport Bool
True (String
"we can only create encoders / decoders for normal constructors and records, got: " forall a. Semigroup a => a -> a -> a
<> forall a b. (Show a, StringConv String b) => a -> b
show Con
other)
  forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"encoders / decoders creation failed"

-- | Get the name of a constructor
nameOf :: Con -> Q Name
nameOf :: Con -> Q Name
nameOf (NormalC Name
n [BangType]
_) = forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
nameOf (RecC Name
n [VarBangType]
_) = forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
nameOf Con
other = do
  forall (m :: * -> *). Quasi m => Bool -> String -> m ()
qReport Bool
True (String
"we can only create encoders / decoders for normal constructors and records, got: " forall a. Semigroup a => a -> a -> a
<> forall a b. (Show a, StringConv String b) => a -> b
show Con
other)
  forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"encoders / decoders creation failed"

-- | Get the list of names of a constructor
fieldsOf :: Con -> Q [Name]
fieldsOf :: Con -> Q [Name]
fieldsOf (NormalC Name
_ [BangType]
_) = forall (f :: * -> *) a. Applicative f => a -> f a
pure []
fieldsOf (RecC Name
_ [VarBangType]
types) = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ (\(Name
f, Bang
_, Type
_) -> Name
f) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [VarBangType]
types
fieldsOf Con
other = do
  forall (m :: * -> *). Quasi m => Bool -> String -> m ()
qReport Bool
True (String
"we can only create encoders / decoders for normal constructors and records, got: " forall a. Semigroup a => a -> a -> a
<> forall a b. (Show a, StringConv String b) => a -> b
show Con
other)
  forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"encoders / decoders creation failed"

-- | Remove the module name from a qualified name
makeName :: ThOptions -> Name -> Name
makeName :: ThOptions -> Name -> Name
makeName ThOptions
options = String -> Name
mkName forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. ConvertText a b => a -> b
toS forall b c a. (b -> c) -> (a -> b) -> a -> c
. ThOptions -> Text -> Text
modifyTypeName ThOptions
options forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Show a, StringConv String b) => a -> b
show

-- | Return the name of a given type with a modified name based on options
getSimpleTypeName :: ThOptions -> Type -> Name
getSimpleTypeName :: ThOptions -> Type -> Name
getSimpleTypeName ThOptions
options (ForallT [TyVarBndr Specificity]
_ [Type]
_ Type
ty) = ThOptions -> Type -> Name
getSimpleTypeName ThOptions
options Type
ty
getSimpleTypeName ThOptions
options (VarT Name
name) = ThOptions -> Name -> Name
makeName ThOptions
options Name
name
getSimpleTypeName ThOptions
options (ConT Name
name) = ThOptions -> Name -> Name
makeName ThOptions
options Name
name
getSimpleTypeName ThOptions
options (TupleT Int
n) = ThOptions -> Name -> Name
makeName ThOptions
options forall a b. (a -> b) -> a -> b
$ Int -> Name
tupleTypeName Int
n
getSimpleTypeName ThOptions
options Type
ArrowT = ThOptions -> Name -> Name
makeName ThOptions
options ''(->)
getSimpleTypeName ThOptions
options Type
ListT = ThOptions -> Name -> Name
makeName ThOptions
options ''[]
getSimpleTypeName ThOptions
options (AppT Type
t1 Type
t2) = String -> Name
mkName (forall a b. (Show a, StringConv String b) => a -> b
show (ThOptions -> Type -> Name
getSimpleTypeName ThOptions
options Type
t1) forall a. Semigroup a => a -> a -> a
<> String
" " forall a. Semigroup a => a -> a -> a
<> forall a b. (Show a, StringConv String b) => a -> b
show (ThOptions -> Type -> Name
getSimpleTypeName ThOptions
options Type
t2))
getSimpleTypeName ThOptions
options (SigT Type
t Type
_) = ThOptions -> Type -> Name
getSimpleTypeName ThOptions
options Type
t
getSimpleTypeName ThOptions
options (UnboxedTupleT Int
n) = ThOptions -> Name -> Name
makeName ThOptions
options forall a b. (a -> b) -> a -> b
$ Int -> Name
unboxedTupleTypeName Int
n
getSimpleTypeName ThOptions
_ Type
t = forall a. HasCallStack => Text -> a
panic forall a b. (a -> b) -> a -> b
$ Text
"getSimpleTypeName: Unknown type: " forall a. Semigroup a => a -> a -> a
<> forall a b. (Show a, StringConv String b) => a -> b
show Type
t