on master help sign in
buildfrom with derivingvia
commita74abfd311a75c4919f5076a1df166477f9da2e1 authorMark Wotton <mwotton@gmail.com> dateFri 27 Nov 2020, 16:16 (6 years ago) parent0b3434a
2 files changed+12−18
src/Roboservant/Types/Breakdown.hs+10−11 test/Spec.hs+2−7
src/Roboservant/Types/Breakdown.hs+10−11
@@ -1,5 +1,8 @@
11{-# LANGUAGE DefaultSignatures #-}
22{-# LANGUAGE DerivingStrategies #-}
3+{-# LANGUAGE StandaloneDeriving #-}
4+{-# LANGUAGE DerivingVia #-}
5+
36
47module Roboservant.Types.Breakdown where
58
@@ -8,6 +11,7 @@ import Data.List.NonEmpty (NonEmpty (..))
811import qualified Data.List.NonEmpty as NEL
912import Data.Maybe (fromMaybe)
1013import Data.Typeable (Typeable)
14+import Roboservant.Types.Internal
1115
1216-- | only use this when we are using the internal typerep map.
1317promisedDyn :: Typeable a => Dynamic -> a
@@ -15,20 +19,15 @@ promisedDyn = fromMaybe (error "internal error, typerep map misconstructed") . f
1519
1620class Breakdown x where
1721 breakdown :: x -> NonEmpty Dynamic
18− default breakdown :: Typeable x => x -> NonEmpty Dynamic
19− breakdown = pure . toDyn
20−
21−-- | Can't break it down any further -- stuck in your teeth, maybe.
22−newtype Chewy x = Chewy {unChew :: x}
22+-- default breakdown :: Typeable x => x -> NonEmpty Dynamic
23+-- breakdown = pure . toDyn
2324
24−instance Breakdown () where
25− breakdown = pure . toDyn
2625
27−instance Breakdown Int where
28− breakdown = pure . toDyn
26+instance Typeable x => Breakdown (Atom x) where
27+ breakdown = pure . toDyn . unAtom
2928
30−instance Typeable x => Breakdown (Chewy x) where
31− breakdown x = pure (toDyn x)
29+deriving via (Atom ()) instance Breakdown ()
30+deriving via (Atom Int) instance Breakdown Int
3231
3332instance (Typeable a, Breakdown a) => Breakdown [a] where
3433 breakdown x = toDyn x :| mconcat (map (NEL.toList . breakdown) x)
test/Spec.hs+2−7
@@ -64,13 +64,8 @@ spec = do
6464 -- RS.prop_concurrent @UnsafeIO.UnsafeApi unsafeServer []
6565
6666
67−instance RS.Breakdown Foo.Foo where
68− breakdown = pure . toDyn
69−
70−
71−instance RS.Breakdown Headers.Foo where
72− breakdown = pure . toDyn
73−
67+deriving via (RS.Atom Foo.Foo) instance RS.Breakdown Foo.Foo
68+deriving via (RS.Atom Headers.Foo) instance RS.Breakdown Headers.Foo
7469
7570deriving via (RS.Atom Void) instance RS.BuildFrom Void
7671deriving via (RS.Atom Foo.Foo) instance RS.BuildFrom Foo.Foo