Skip to content

Commit 20f4cce

Browse files
committed
[#45] Add newtype Id with phantom type parameter
Resolves #45
1 parent 3b481ba commit 20f4cce

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed

issue-wanted.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ library
7070
IW.Config
7171

7272
-- Core Modules
73+
IW.Core.Id
7374
IW.Core.Issue
75+
IW.Core.Repo
7476

7577
-- Database
7678
IW.Db

src/IW/Core/Id.hs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
2+
3+
{-# LANGUAGE AllowAmbiguousTypes #-}
4+
5+
module IW.Core.Id
6+
( Id (..)
7+
, AnyId
8+
, castId
9+
) where
10+
11+
import Data.Type.Equality (type (==))
12+
13+
14+
-- | Wrapper for integer id. Contains phantom type parameter for increased
15+
-- type-safety.
16+
newtype Id a = Id { unId :: Int }
17+
deriving stock (Show, Generic)
18+
deriving newtype (Eq, Ord, FromField, ToField, FromJSON, ToJSON)
19+
20+
-- | When we don't care about type of 'Id' but don't want to deal with type variables.
21+
type AnyId = Id ()
22+
23+
-- | Unsafe cast of 'Id'. Implementation uses smart trick to enforce usage
24+
-- always with @TypeApplications@.
25+
castId :: forall to from to' . ((to == to') ~ 'True) => Id from -> Id to'
26+
castId (Id a) = Id a

src/IW/Core/Issue.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
module IW.Core.Issue
44
( Issue (..)
55
) where
6+
7+
import IW.Core.Id (Id)
8+
import IW.Core.Repo (Repo)
9+
610

711
-- | Data type representing a GitHub issue.
812
data Issue = Issue
9-
{ issueId :: Int
13+
{ issueId :: Id Issue
1014
, issueNumber :: Int
1115
, issueTitle :: Text
1216
, issueBody :: Text
1317
, issueUrl :: Text
14-
, issueRepoId :: Int
18+
, issueRepoId :: Id Repo
1519
} deriving stock (Generic, Show, Eq)
1620
deriving anyclass (FromRow, ToRow)
1721
deriving (FromJSON, ToJSON)

src/IW/Core/Repo.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{-# LANGUAGE DeriveAnyClass #-}
2+
3+
module IW.Core.Repo
4+
( Repo (..)
5+
) where
6+
7+
import IW.Core.Id (Id)
8+
9+
10+
-- | Data type representing a GitHub repository.
11+
data Repo = Repo

src/IW/Db/Issue.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module IW.Db.Issue
66
( getIssues
77
, getIssueById
88
, getIssuesByLabel
9+
, insertIssues
910
) where
1011

1112
import IW.App (WithError)

0 commit comments

Comments
 (0)