Skip to content

Commit

Permalink
Merge pull request #85 from uffizio/feature-generic-show
Browse files Browse the repository at this point in the history
Feature: generic `Show`
  • Loading branch information
eskimor authored Sep 24, 2023
2 parents 797c1d5 + 65613fd commit d947035
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/Language/PureScript/Bridge/Printer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ moduleToText settings m =
<> _genericsImports settings
<> _argonautCodecsImports settings
<> _foreignImports settings
<> if any (\ (SumType _ _ requestedInstances) -> Show `elem` requestedInstances) (psTypes m)
then [ImportLine "Data.Show.Generic" Nothing (Set.fromList ["genericShow"])]
else [ ]
allImports = Map.elems $ mergeImportLines otherImports (psImportLines m)

_genericsImports :: Switches.Settings -> [ImportLine]
Expand Down Expand Up @@ -261,6 +264,10 @@ instances settings st@(SumType t _ is) = map go is
constraintsInner = T.intercalate ", " $ map instances sumTypeParameters
instances params = genericInstance settings params <> ", " <> decodeJsonInstance params <> ", " <> decodeJsonFieldInstance params

Check warning on line 265 in src/Language/PureScript/Bridge/Printer.hs

View workflow job for this annotation

GitHub Actions / linux (9.2.4, 3.6)

This binding for ‘instances’ shadows the existing binding

Check warning on line 265 in src/Language/PureScript/Bridge/Printer.hs

View workflow job for this annotation

GitHub Actions / linux (8.6.5, 3.6)

This binding for ‘instances’ shadows the existing binding

Check warning on line 265 in src/Language/PureScript/Bridge/Printer.hs

View workflow job for this annotation

GitHub Actions / linux (8.8.4, 3.6)

This binding for ‘instances’ shadows the existing binding

Check warning on line 265 in src/Language/PureScript/Bridge/Printer.hs

View workflow job for this annotation

GitHub Actions / linux (8.10.4, 3.6)

This binding for ‘instances’ shadows the existing binding

Check warning on line 265 in src/Language/PureScript/Bridge/Printer.hs

View workflow job for this annotation

GitHub Actions / linux (9.0.2, 3.6)

This binding for ‘instances’ shadows the existing binding
bracketWrap x = "(" <> x <> ")"
go Show = T.unlines
[ T.unwords ["instance", "show" <> _typeName t, "", "Show", typeInfoToText False t, "where"]
, " " <> T.unwords ["show", "value", "=", "genericShow", "value"]
]
go i = "derive instance " <> T.toLower c <> _typeName t <> " :: " <> extras i <> c <> " " <> typeInfoToText False t <> postfix i
where
c = T.pack $ show i
Expand Down
6 changes: 5 additions & 1 deletion src/Language/PureScript/Bridge/SumType.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module Language.PureScript.Bridge.SumType
, sumTypeConstructors
, recLabel
, recValue
, showing
) where

import Control.Lens hiding (from, to)
Expand Down Expand Up @@ -67,7 +68,7 @@ mkSumType p = SumType (mkTypeInfo p) constructors (Encode : Decode : EncodeJson
constructors = gToConstructors (from (undefined :: t))

-- | Purescript typeclass instances that can be generated for your Haskell types.
data Instance = Encode | EncodeJson | Decode | DecodeJson | Generic | Newtype | Eq | Ord
data Instance = Encode | EncodeJson | Decode | DecodeJson | Generic | Newtype | Eq | Ord | Show
deriving (Eq, Show)

{- | The Purescript typeclass `Newtype` might be derivable if the original
Expand All @@ -91,6 +92,9 @@ equal _ (SumType ti dc is) = SumType ti dc . nub $ Eq : is
order :: Ord a => Proxy a -> SumType t -> SumType t
order _ (SumType ti dc is) = SumType ti dc . nub $ Eq : Ord : is

showing :: Show a => Proxy a -> SumType t -> SumType t
showing _ (SumType ti dc is) = SumType ti dc . nub $ Show : is

data DataConstructor (lang :: Language) = DataConstructor
{ _sigConstructor :: !Text
-- ^ e.g. `Left`/`Right` for `Either`
Expand Down
9 changes: 6 additions & 3 deletions test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ allTests = do
in bst `shouldBe` ti
it "tests with custom type Foo" $
let prox = Proxy :: Proxy Foo
bst = bridgeSumType (buildBridge defaultBridge) (order prox $ mkSumType prox)
bst = bridgeSumType (buildBridge defaultBridge) (showing prox . order prox $ mkSumType prox)
st =
SumType
TypeInfo {_typePackage = "", _typeModule = "TestData", _typeName = "Foo", _typeParameters = []}
Expand All @@ -57,11 +57,11 @@ allTests = do
]
}
]
[Eq, Ord, Encode, Decode, EncodeJson, DecodeJson, Generic]
[Show, Eq, Ord, Encode, Decode, EncodeJson, DecodeJson, Generic]
in bst `shouldBe` st
it "tests generation of for custom type Foo" $
let prox = Proxy :: Proxy Foo
recType = bridgeSumType (buildBridge defaultBridge) (order prox $ mkSumType prox)
recType = bridgeSumType (buildBridge defaultBridge) (showing prox . order prox $ mkSumType prox)
recTypeText = sumTypeToText defaultSettings recType
txt =
T.stripEnd $
Expand All @@ -71,6 +71,9 @@ allTests = do
, " | Bar Int"
, " | FooBar Int String"
, ""
, "instance showFoo ∷ Show Foo where"
, " show value = genericShow value"
, ""
, "derive instance eqFoo :: Eq Foo"
, "derive instance ordFoo :: Ord Foo"
, "instance encodeJsonFoo :: EncodeJson Foo where"
Expand Down

0 comments on commit d947035

Please sign in to comment.