purescript-bridge 0.10.0.0 → 0.10.1.0
raw patch · 4 files changed
+57/−7 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Language.PureScript.Bridge.Printer: sumTypeToTypeDecls :: SumType PureScript -> Text
Files
- purescript-bridge.cabal +1/−1
- src/Language/PureScript/Bridge/Printer.hs +19/−5
- test/Spec.hs +31/−1
- test/TestData.hs +6/−0
purescript-bridge.cabal view
@@ -10,7 +10,7 @@ -- PVP summary: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change-version: 0.10.0.0+version: 0.10.1.0 -- A short (one-line) description of the package. synopsis: Generate PureScript data types from Haskell data types
src/Language/PureScript/Bridge/Printer.hs view
@@ -82,13 +82,22 @@ typeList = T.intercalate ", " (Set.toList (importTypes l)) sumTypeToText :: SumType 'PureScript -> Text-sumTypeToText st@(SumType t cs) = (T.unlines $- "data " <> typeInfoToText True t <> " ="- : " " <> T.intercalate "\n | " (map (constructorToText 4) cs)- : [ "\nderive instance generic" <> _typeName t <> " :: " <> genericConstrains <> genericInstance t ])- <> "\n" <> sep <> "\n" <> sumTypeToPrismsAndLenses st <> sep+sumTypeToText st =+ sumTypeToTypeDecls st+ <> "\n"+ <> sep+ <> "\n"+ <> sumTypeToPrismsAndLenses st+ <> sep where sep = T.replicate 80 "-"++sumTypeToTypeDecls :: SumType 'PureScript -> Text+sumTypeToTypeDecls st@(SumType t cs) = T.unlines $+ dataOrNewtype cs <> " " <> typeInfoToText True t <> " ="+ : " " <> T.intercalate "\n | " (map (constructorToText 4) cs)+ : [ "\nderive instance generic" <> _typeName t <> " :: " <> genericConstrains <> genericInstance t ]+ where genericInstance = ("Generic " <>) . typeInfoToText False genericConstrains | stpLength == 0 = mempty@@ -101,6 +110,11 @@ bracketWrap x = "(" <> x <> ")" sumTypeParameters = filter isTypeParam . Set.toList $ getUsedTypes st isTypeParam typ = _typeName typ `elem` map _typeName (_typeParameters t)+ dataOrNewtype [constr]+ | either isSingletonList (const True) (_sigValues constr) = "newtype"+ dataOrNewtype _ = "data"+ isSingletonList [_] = True+ isSingletonList _ = False sumTypeToPrismsAndLenses :: SumType 'PureScript -> Text sumTypeToPrismsAndLenses st = sumTypeToPrisms st <> sumTypeToLenses st
test/Spec.hs view
@@ -176,4 +176,34 @@ , "" ] in (barLenses <> recTypeLenses) `shouldBe` txt-+ it "tests generation of newtypes for record data type" $+ let recType = bridgeSumType (buildBridge defaultBridge) (mkSumType (Proxy :: Proxy (SingleRecord A B)))+ recTypeText = sumTypeToTypeDecls recType+ txt = T.unlines [ "newtype SingleRecord a b ="+ , " SingleRecord {"+ , " _a :: a"+ , " , _b :: b"+ , " , c :: String"+ , " }"+ , ""+ , "derive instance genericSingleRecord :: (Generic a, Generic b) => Generic (SingleRecord a b)"+ ]+ in recTypeText `shouldBe` txt+ it "tests generation of newtypes for haskell newtype" $+ let recType = bridgeSumType (buildBridge defaultBridge) (mkSumType (Proxy :: Proxy SomeNewtype))+ recTypeText = sumTypeToTypeDecls recType+ txt = T.unlines [ "newtype SomeNewtype ="+ , " SomeNewtype Int"+ , ""+ , "derive instance genericSomeNewtype :: Generic SomeNewtype"+ ]+ in recTypeText `shouldBe` txt+ it "tests generation of newtypes for haskell data type with one argument" $+ let recType = bridgeSumType (buildBridge defaultBridge) (mkSumType (Proxy :: Proxy SingleValueConstr))+ recTypeText = sumTypeToTypeDecls recType+ txt = T.unlines [ "newtype SingleValueConstr ="+ , " SingleValueConstr Int"+ , ""+ , "derive instance genericSingleValueConstr :: Generic SingleValueConstr"+ ]+ in recTypeText `shouldBe` txt
test/TestData.hs view
@@ -50,6 +50,12 @@ , c :: String } deriving(Generic, Typeable, Show) +newtype SomeNewtype = SomeNewtype Int+ deriving (Generic, Typeable, Show)++data SingleValueConstr = SingleValueConstr Int+ deriving (Generic, Typeable, Show)+ a :: HaskellType a = mkTypeInfo (Proxy :: Proxy (Either String Int))