on master help sign in
some renaming
commit67d88d2ef5ac7ec7be2bf883491d5f94ccdfae84 authorMark Wotton <mwotton@gmail.com> dateSun 22 Nov 2020, 10:39 (6 years ago) parent2498df9
2 files changed+31−25
src/Roboservant/Types/Breakdown.hs+20−13 test/Spec.hs+11−12
src/Roboservant/Types/Breakdown.hs+20−13
@@ -2,8 +2,8 @@
22{-# LANGUAGE DataKinds #-}
33{-# LANGUAGE DerivingVia #-}
44{-# LANGUAGE StandaloneDeriving #-}
5−{-# LANGUAGE DefaultSignatures #-}
6−{-# LANGUAGE DerivingStrategies #-}
5+
6+
77{-# LANGUAGE FlexibleInstances #-}
88{-# LANGUAGE GADTs #-}
99{-# LANGUAGE PolyKinds #-}
@@ -22,6 +22,7 @@ import Data.Map.Strict (Map)
2222import Data.Maybe (fromMaybe)
2323import Data.Proxy (Proxy (..))
2424import Data.Typeable (TypeRep, Typeable, typeRep)
25+import GHC.Generics(Generic)
2526
2627data Provenance
2728 = Provenance TypeRep Int
@@ -35,9 +36,18 @@ class Typeable x => BuildFrom x where
3536
3637
3738-- | Can't be built up from parts
38−newtype Atomic x = Atomic x
39+newtype Atom x = Atom { unAtom :: x }
40+
41+-- | can be broken down and built up from generic pieces
42+newtype Compound x = Compound { unCompound :: x }
3943
40−instance Typeable x => BuildFrom (Atomic x) where
44+instance (Typeable x, Generic x) => BuildFrom (Compound x) where
45+ buildFrom = error "buildfrom"
46+
47+instance (Typeable x, Generic x) => Breakdown (Compound x) where
48+ breakdown = error "breakdown"
49+
50+instance Typeable x => BuildFrom (Atom x) where
4151 buildFrom = Map.lookup (typeRep (Proxy @x))
4252
4353
@@ -53,13 +63,13 @@ promisedDyn :: Typeable a => Dynamic -> a
5363promisedDyn = fromMaybe (error "internal error, typerep map misconstructed") . fromDynamic
5464
5565-- instance BuildFrom Bool
56−deriving via (Atomic Bool) instance BuildFrom Bool
66+deriving via (Atom Bool) instance BuildFrom Bool
5767
5868instance (Typeable x, BuildFrom x) => BuildFrom (Maybe x) where
5969 buildFrom dict = Just $ fmap toDyn <$> options
6070 where options :: NonEmpty ([Provenance], Maybe x)
6171 options = ([],Nothing) :|
62− (maybe [] NEL.toList . fmap (fmap (fmap (Just . promisedDyn @x)))
72+ (maybe [] (NEL.toList . (fmap (fmap (Just . promisedDyn @x))))
6373 $ buildFrom @x dict)
6474
6575class Breakdown x where
@@ -67,14 +77,11 @@ class Breakdown x where
6777-- default breakdown :: Typeable x => x -> NonEmpty Dynamic
6878-- breakdown = pure . toDyn
6979
70−-- | Can't break it down any further -- stuck in your teeth, maybe.
71−newtype Chewy x = Chewy { unChew :: x }
72−
73−instance Typeable a => Breakdown (Chewy a) where
74− breakdown = pure . toDyn . unChew
80+instance Typeable a => Breakdown (Atom a) where
81+ breakdown = pure . toDyn . unAtom
7582
76−deriving via (Chewy ()) instance Breakdown ()
77−deriving via (Chewy Int) instance Breakdown Int
83+deriving via (Atom ()) instance Breakdown ()
84+deriving via (Atom Int) instance Breakdown Int
7885
7986
8087--let d = toDyn x in Map.fromList [(dynTypeRep d, pure d)]
test/Spec.hs+11−12
@@ -1,4 +1,3 @@
1−
21{-# LANGUAGE OverloadedStrings #-}
32{-# LANGUAGE StandaloneDeriving #-}
43{-# LANGUAGE DerivingVia #-}
@@ -14,7 +13,7 @@ import qualified Post
1413
1514import Data.Dynamic(toDyn)
1615import qualified Roboservant as RS
17−import Roboservant(Chewy,Breakdown, BuildFrom, Atomic)
16+import Roboservant(Breakdown, BuildFrom, Atom, Compound)
1817import Test.Hspec
1918import Test.Hspec.Core.Spec
2019import Data.Void
@@ -78,24 +77,24 @@ spec = do
7877 -- RS.prop_concurrent @UnsafeIO.UnsafeApi unsafeServer []
7978
8079
81−deriving via (Chewy Foo.Foo) instance Breakdown Foo.Foo
82−deriving via (Atomic Foo.Foo) instance BuildFrom Foo.Foo
80+deriving via (Atom Foo.Foo) instance Breakdown Foo.Foo
81+deriving via (Atom Foo.Foo) instance BuildFrom Foo.Foo
8382
84−deriving via (Chewy Headers.Foo) instance Breakdown Headers.Foo
85−deriving via (Atomic Headers.Foo) instance BuildFrom Headers.Foo
83+deriving via (Atom Headers.Foo) instance Breakdown Headers.Foo
84+deriving via (Atom Headers.Foo) instance BuildFrom Headers.Foo
8685
87−deriving via (Chewy Seeded.Seed) instance Breakdown Seeded.Seed
88−deriving via (Atomic Seeded.Seed) instance BuildFrom Seeded.Seed
86+deriving via (Atom Seeded.Seed) instance Breakdown Seeded.Seed
87+deriving via (Atom Seeded.Seed) instance BuildFrom Seeded.Seed
8988
9089
9190-- instance RS.BuildFrom Seeded.Seed
9291
93−deriving via (Atomic Void) instance RS.BuildFrom Void
92+deriving via (Atom Void) instance RS.BuildFrom Void
9493
95−deriving via (Atomic Post.FooPost) instance RS.BuildFrom Post.FooPost
96−deriving via (Chewy Post.FooPost) instance RS.Breakdown Post.FooPost
94+deriving via (Atom Post.FooPost) instance RS.BuildFrom Post.FooPost
95+deriving via (Atom Post.FooPost) instance RS.Breakdown Post.FooPost
9796
98−-- deriving via (Atomic Void) instance RS.BuildFrom Void
97+-- deriving via (Atom Void) instance RS.BuildFrom Void
9998
10099-- instance RS.Breakdown Post.FooPost
101100-- instance RS.BuildFrom Post.FooPost