From d4e2e7c0356106c26bdb2b02f912d352ebd4063a Mon Sep 17 00:00:00 2001 From: Sebastien Braun Date: Sun, 9 Jun 2019 20:26:57 +0200 Subject: [PATCH 1/2] Allow th-abstraction 0.3 --- package.yaml | 2 +- src/Data/Aeson/TypeScript/TH.hs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/package.yaml b/package.yaml index 3293791..096c637 100644 --- a/package.yaml +++ b/package.yaml @@ -33,7 +33,7 @@ dependencies: - mtl - template-haskell - text -- th-abstraction < 0.3 +- th-abstraction < 0.4 - unordered-containers library: diff --git a/src/Data/Aeson/TypeScript/TH.hs b/src/Data/Aeson/TypeScript/TH.hs index 4d75b8e..d9298aa 100644 --- a/src/Data/Aeson/TypeScript/TH.hs +++ b/src/Data/Aeson/TypeScript/TH.hs @@ -197,9 +197,15 @@ deriveTypeScript options name = do 1 -> [ConT ''T] n -> take (length datatypeVars) [ConT ''T1, ConT ''T2, ConT ''T3, ConT ''T4, ConT ''T5, ConT ''T6, ConT ''T7, ConT ''T8, ConT ''T9, ConT ''T10] +#if MIN_VERSION_th_abstraction(0,3,0) + let subMap = M.fromList $ zip (catMaybes $ fmap getFreeVariableName datatypeInstTypes) templateVarsToUse + let fullyQualifiedDatatypeInfo = (datatypeInfo {datatypeInstTypes = templateVarsToUse + , datatypeCons = fmap (applySubstitution subMap) datatypeCons}) +#else let subMap = M.fromList $ zip (catMaybes $ fmap getFreeVariableName datatypeVars) templateVarsToUse let fullyQualifiedDatatypeInfo = (datatypeInfo {datatypeVars = templateVarsToUse , datatypeCons = fmap (applySubstitution subMap) datatypeCons}) +#endif getTypeFn <- getTypeExpression fullyQualifiedDatatypeInfo >>= \expr -> return $ FunD 'getTypeScriptType [Clause [WildP] (NormalB expr) []] getDeclarationFn <- getDeclarationFunctionBody options name fullyQualifiedDatatypeInfo let fullyGenericInstance = mkInstance [] (AppT (ConT ''TypeScript) (ConT name)) [getTypeFn, getDeclarationFn] @@ -207,7 +213,11 @@ deriveTypeScript options name = do otherInstances <- case length datatypeVars > 0 of True -> do otherGetTypeFn <- getTypeExpression datatypeInfo >>= \expr -> return $ FunD 'getTypeScriptType [Clause [WildP] (NormalB expr) []] +#if MIN_VERSION_th_abstraction(0,3,0) + return [mkInstance (fmap getDatatypePredicate datatypeInstTypes) (AppT (ConT ''TypeScript) (foldl (\x y -> AppT x y) (ConT name) datatypeInstTypes)) [otherGetTypeFn]] +#else return [mkInstance (fmap getDatatypePredicate datatypeVars) (AppT (ConT ''TypeScript) (foldl (\x y -> AppT x y) (ConT name) datatypeVars)) [otherGetTypeFn]] +#endif False -> return [] return $ fullyGenericInstance : otherInstances @@ -297,9 +307,15 @@ assembleInterfaceDeclaration options constructorName genericVariables members = -- | Get an expression to be used for getTypeScriptType. -- For datatypes of kind * this is easy, since we can just evaluate the string literal in TH. -- For higher-kinded types, we need to make an expression which evaluates the template types and fills it in. +#if MIN_VERSION_th_abstraction(0,3,0) +getTypeExpression :: DatatypeInfo -> Q Exp +getTypeExpression (DatatypeInfo {datatypeInstTypes=[], ..}) = return $ stringE $ getTypeName datatypeName +getTypeExpression (DatatypeInfo {datatypeInstTypes=vars, ..}) = do +#else getTypeExpression :: DatatypeInfo -> Q Exp getTypeExpression (DatatypeInfo {datatypeVars=[], ..}) = return $ stringE $ getTypeName datatypeName getTypeExpression (DatatypeInfo {datatypeVars=vars, ..}) = do +#endif let baseName = stringE $ getTypeName datatypeName let typeNames = ListE [getTypeAsStringExp typ | typ <- vars] let headType = AppE (VarE 'head) typeNames From 61a856b78bae158b3376fa4314fd13558a817c36 Mon Sep 17 00:00:00 2001 From: Sebastien Braun Date: Sun, 9 Jun 2019 23:53:48 +0200 Subject: [PATCH 2/2] Add Travis for nightly that has th-abstraction 0.3 --- .travis.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.travis.yml b/.travis.yml index 91941c7..bd0965f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -110,6 +110,12 @@ matrix: compiler: ": #stack 8.6.4" addons: {apt: {packages: [libgmp-dev]}} + # To test against th-abstraction 0.3, which is not contained in + # earlier snapshots: + - env: BUILD=stack ARGS="--resolver nightly-2019-06-09" + compiler: ": #stack nightly-2019-06-09" + addons: {apt: {packages: [libgmp-dev]}} + # Nightly builds are allowed to fail - env: BUILD=stack ARGS="--resolver nightly" compiler: ": #stack nightly"