Skip to content

Commit

Permalink
Make points a type family
Browse files Browse the repository at this point in the history
  • Loading branch information
raduom committed Nov 26, 2022
1 parent b7ac5b2 commit d3cb5f5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion rewindable-index/src/RewindableIndex/Storable.hs
Expand Up @@ -62,7 +62,7 @@ import GHC.Generics (Generic)
-}
data family StorableEvent h

data family StorablePoint h
type family StorablePoint h

data family StorableQuery h

Expand Down
16 changes: 10 additions & 6 deletions rewindable-index/test/RewindableIndex/Spec/Sqlite.hs
Expand Up @@ -42,14 +42,18 @@ import RewindableIndex.Storable qualified as Storable
-- in the handler type, that they need to implement the storable interface.
newtype Handle = Handle Sql.Connection

-- StorablePoints can be either Point (intergers) or the Genesis block.
data instance StorablePoint Handle =
-- This point for this test will be representated by a point on some blockchain where
-- slot numbers are identified by integers.
data Point =
Point Int
| Genesis
deriving (Eq, Show, Generic)

-- StorablePoints can be either Point (intergers) or the Genesis block.
type instance StorablePoint Handle = Point

-- We should be able to order points.
instance Ord (StorablePoint Handle) where
instance Ord Point where
Genesis <= _ = True
(Point x) <= (Point y) = x <= y
_ <= _ = False
Expand Down Expand Up @@ -79,7 +83,7 @@ newtype instance StorableResult Handle = Result
deriving newtype (Eq, Ord)

-- We can extract points from events in the expected way.
instance HasPoint (StorableEvent Handle) (StorablePoint Handle) where
instance HasPoint (StorableEvent Handle) Point where
getPoint (Event point _) = point

data Config = Config
Expand All @@ -104,15 +108,15 @@ instance ToRow (StorableEvent Handle) where
instance FromRow (StorableEvent Handle) where
fromRow = Event <$> field <*> field

instance FromField (StorablePoint Handle) where
instance FromField Point where
fromField f =
fromField f <&> \p ->
-- encode -1 as the genesis block.
if p == -1
then Genesis
else Point p

instance ToField (StorablePoint Handle) where
instance ToField Point where
-- encode -1 as the genesis block.
toField Genesis = toField (-1 :: Int)
toField (Point p) = toField p
Expand Down

0 comments on commit d3cb5f5

Please sign in to comment.