on master help sign in
Merge pull request #25 from avnik/no-generic-flag

No generic flag

commit3d0e2c231a39b8d5f33981e0645eeb72cc03b62b authorkana <andrew4chrome@gmail.com> dateFri 29 Apr 2022, 19:11 (4 years ago) parent94ab29fd185bd4
3 files changed+17−7
jrec.cabal+10−1 src/JRec.hs+3 src/JRec/Internal.hs+4−6
jrec.cabal+10−1
@@ -24,6 +24,13 @@ flag with-aeson
2424 -- with instances by default
2525 default: True
2626
27+flag with-generics
28+ description: Enable GHC.Generics instances
29+ -- don't let the solver fiddle with the flag
30+ manual: True
31+ -- with instances by default
32+ default: True
33+
2734-- A common stanza to share with tests, so that ghcid (bin/test) will reload
2835-- instantly when the library sources change (without us having to restart it).
2936common library-common
@@ -81,13 +88,15 @@ library
8188 JRec.Field
8289 if flag(with-aeson)
8390 cpp-options: -DWITH_AESON
91+ if flag(with-generics)
92+ cpp-options: -DWITH_GENERICS
8493
8594test-suite jrec-test
8695 import: library-common
8796 type: exitcode-stdio-1.0
8897 hs-source-dirs: test, src
8998 main-is: Spec.hs
90− cpp-options: -DWITH_AESON
99+ cpp-options: -DWITH_AESON -DWITH_GENERICS
91100 build-depends:
92101 base,
93102 hspec,
src/JRec.hs+3
@@ -1,3 +1,4 @@
1+{-# LANGUAGE CPP #-}
12{-# LANGUAGE GADTs #-}
23{-# LANGUAGE PatternSynonyms #-}
34{-# LANGUAGE TypeApplications #-}
@@ -128,6 +129,7 @@ type RecordRep fields =
128129 (Sels fields)
129130 )
130131
132+#ifdef WITH_GENERICS
131133instance
132134 (R.FromNative (RecordRep fields) fields, R.ToNative (RecordRep fields) fields) =>
133135 Generic (Rec fields)
@@ -137,6 +139,7 @@ instance
137139 RecordRep fields
138140 from r = R.toNative' r
139141 to rep = R.fromNative' rep
142+#endif
140143
141144----------------------------------------------------------------------------
142145-- generic-lens
src/JRec/Internal.hs+4−6
@@ -32,6 +32,7 @@ import qualified Control.Monad.State as S
3232import Data.Aeson
3333import Data.Aeson.Types (Parser)
3434import Data.Constraint
35+import Data.List (intercalate)
3536import Data.Maybe (catMaybes)
3637import Data.Proxy
3738import qualified Data.Text as T
@@ -42,8 +43,6 @@ import GHC.OverloadedLabels
4243import GHC.Prim
4344import GHC.ST (ST (..), runST)
4445import GHC.TypeLits
45−import Generic.Data (gshowsPrec)
46−import Generic.Data.Internal.Show (GShow)
4746import qualified Optics.Lens as OL
4847import Unsafe.Coerce
4948import Prelude
@@ -102,13 +101,12 @@ defaultJSONOptions = JSONOptions {fieldTransform = id}
102101type role Rec representational
103102
104103instance
105− ( RecApply lts lts Show,
106− GShow Proxy (Rep (Rec lts)),
107− Generic (Rec lts)
104+ ( RecApply lts lts Show
108105 ) =>
109106 Show (Rec lts)
110107 where
111− show = (\x -> if null x then "{}" else x) . unwords . drop 1 . words . flip (gshowsPrec 0) ""
108+ show rec = "{" ++ formatted ++ "}"
109+ where formatted = intercalate ", " $ map (\(k, v) -> k ++ " = " ++ v) $ showRec rec
112110
113111instance RecEq lts lts => Eq (Rec lts) where
114112 (==) (a :: Rec lts) (b :: Rec lts) = recEq a b (Proxy :: Proxy lts)