on master help sign in
buildfrom with derivingvia
a74abfdMark Wotton
src/Roboservant/Types/Breakdown.hs+10−11
@@ -1,5 +1,8 @@
1{-# LANGUAGE DefaultSignatures #-}
1{-# LANGUAGE DefaultSignatures #-}
2{-# LANGUAGE DerivingStrategies #-}
2{-# LANGUAGE DerivingStrategies #-}
3+{-# LANGUAGE StandaloneDeriving #-}
4+{-# LANGUAGE DerivingVia #-}
5+
3
6
4module Roboservant.Types.Breakdown where
7module Roboservant.Types.Breakdown where
5
8
@@ -8,6 +11,7 @@ import Data.List.NonEmpty (NonEmpty (..))
8import qualified Data.List.NonEmpty as NEL
11import qualified Data.List.NonEmpty as NEL
9import Data.Maybe (fromMaybe)
12import Data.Maybe (fromMaybe)
10import Data.Typeable (Typeable)
13import Data.Typeable (Typeable)
14+import Roboservant.Types.Internal
11
15
12-- | only use this when we are using the internal typerep map.
16-- | only use this when we are using the internal typerep map.
13promisedDyn :: Typeable a => Dynamic -> a
17promisedDyn :: Typeable a => Dynamic -> a
@@ -15,20 +19,15 @@ promisedDyn = fromMaybe (error "internal error, typerep map misconstructed") . f
15
19
16class Breakdown x where
20class Breakdown x where
17 breakdown :: x -> NonEmpty Dynamic
21 breakdown :: x -> NonEmpty Dynamic
18− default breakdown :: Typeable x => x -> NonEmpty Dynamic
22+-- default breakdown :: Typeable x => x -> NonEmpty Dynamic
19− breakdown = pure . toDyn
23+-- breakdown = pure . toDyn
20−
21−-- | Can't break it down any further -- stuck in your teeth, maybe.
22−newtype Chewy x = Chewy {unChew :: x}
23
24
24−instance Breakdown () where
25− breakdown = pure . toDyn
26
25
27−instance Breakdown Int where
26+instance Typeable x => Breakdown (Atom x) where
28− breakdown = pure . toDyn
27+ breakdown = pure . toDyn . unAtom
29
28
30−instance Typeable x => Breakdown (Chewy x) where
29+deriving via (Atom ()) instance Breakdown ()
31− breakdown x = pure (toDyn x)
30+deriving via (Atom Int) instance Breakdown Int
32
31
33instance (Typeable a, Breakdown a) => Breakdown [a] where
32instance (Typeable a, Breakdown a) => Breakdown [a] where
34 breakdown x = toDyn x :| mconcat (map (NEL.toList . breakdown) x)
33 breakdown x = toDyn x :| mconcat (map (NEL.toList . breakdown) x)
test/Spec.hs+2−7
@@ -64,13 +64,8 @@ spec = do
64 -- RS.prop_concurrent @UnsafeIO.UnsafeApi unsafeServer []
64 -- RS.prop_concurrent @UnsafeIO.UnsafeApi unsafeServer []
65
65
66
66
67−instance RS.Breakdown Foo.Foo where
67+deriving via (RS.Atom Foo.Foo) instance RS.Breakdown Foo.Foo
68− breakdown = pure . toDyn
69−
70−
71−instance RS.Breakdown Headers.Foo where
68+deriving via (RS.Atom Headers.Foo) instance RS.Breakdown Headers.Foo
72− breakdown = pure . toDyn
73−
74
69
75deriving via (RS.Atom Void) instance RS.BuildFrom Void
70deriving via (RS.Atom Void) instance RS.BuildFrom Void
76deriving via (RS.Atom Foo.Foo) instance RS.BuildFrom Foo.Foo
71deriving via (RS.Atom Foo.Foo) instance RS.BuildFrom Foo.Foo