Skip to content

Commit 78fb5e7

Browse files
authored
Merge pull request #1500 from tweag/rev-types
Add *RevisionIx newtypes
2 parents f361075 + c3d6b20 commit 78fb5e7

8 files changed

Lines changed: 54 additions & 32 deletions

File tree

src/Distribution/Server/Features/Core/State.hs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE DeriveDataTypeable, TypeFamilies, TemplateHaskell, BangPatterns #-}
2+
{-# LANGUAGE FlexibleContexts #-}
23

34
module Distribution.Server.Features.Core.State (
45
-- * DB state
@@ -33,6 +34,7 @@ import Distribution.Server.Users.Types (UserId, UserName(..), UserInfo(..))
3334
import Distribution.Server.Users.Users (Users, lookupUserId)
3435
import Distribution.Server.Framework.MemSize
3536

37+
import Data.Coerce (Coercible, coerce)
3638
import Data.Acid (Query, Update, makeAcidic)
3739
import Data.SafeCopy (Migrate(..), base, extension, deriveSafeCopy)
3840
import Control.Monad.Reader
@@ -103,7 +105,7 @@ addPackage2 pkgid cabalfile uploadinfo@(timestamp, uid) username mtarball = do
103105
Nothing -> do
104106
let !pkginfo = mkPackageInfo pkgid cabalfile uploadinfo mtarball
105107
pkgindex' = PackageIndex.insert pkginfo pkgindex
106-
!pkgentry = CabalFileEntry pkgid 0 timestamp uid username
108+
!pkgentry = CabalFileEntry pkgid (MetadataRevIx 0) timestamp uid username
107109
updatelog' = fmap (Seq.|> pkgentry) updatelog
108110
State.put $! PackagesState pkgindex' updatelog'
109111
return (Just pkginfo)
@@ -116,7 +118,7 @@ addPackage3 !pkginfo (timestamp,uid) username entries = do
116118
Just _ -> return False
117119
Nothing -> do
118120
let pkgindex' = PackageIndex.insert pkginfo pkgindex
119-
!pkgentry = CabalFileEntry (pkgInfoId pkginfo) 0 timestamp uid username
121+
!pkgentry = CabalFileEntry (pkgInfoId pkginfo) (MetadataRevIx 0) timestamp uid username
120122
updatelog' = fmap (\ul -> foldr (\e s -> s Seq.|> e) ul (pkgentry:entries)) updatelog
121123
State.put $! PackagesState pkgindex' updatelog'
122124
return True
@@ -160,7 +162,7 @@ addPackageRevision2 pkgid cabalfile uploadinfo@(timestamp, uid) username = do
160162
`Vec.snoc` (cabalfile, uploadinfo)
161163
}
162164
pkgindex' = PackageIndex.insert pkginfo' pkgindex
163-
newrevision = Vec.length (pkgMetadataRevisions pkginfo)
165+
newrevision = MetadataRevIx $ Vec.length (pkgMetadataRevisions pkginfo)
164166
!pkgentry = CabalFileEntry pkgid newrevision timestamp uid username
165167
updatelog' = fmap (Seq.|> pkgentry) updatelog
166168
State.put $! PackagesState pkgindex' updatelog'
@@ -172,7 +174,7 @@ addPackageRevision2 pkgid cabalfile uploadinfo@(timestamp, uid) username = do
172174
pkgTarballRevisions = Vec.empty
173175
}
174176
pkgindex' = PackageIndex.insert pkginfo pkgindex
175-
!pkgentry = CabalFileEntry pkgid 0 timestamp uid username
177+
!pkgentry = CabalFileEntry pkgid (MetadataRevIx 0) timestamp uid username
176178
updatelog' = fmap (Seq.|> pkgentry) updatelog
177179
State.put $! PackagesState pkgindex' updatelog'
178180
return (Nothing, pkginfo)
@@ -279,11 +281,11 @@ initialUpdateLog oldExtras users pkgs =
279281
where
280282
pkgId = pkgInfoId pkgInfo
281283

282-
entryCabal :: PackageId -> (Int, (a, UploadInfo)) -> TarIndexEntry
284+
entryCabal :: PackageId -> (MetadataRevIx, (a, UploadInfo)) -> TarIndexEntry
283285
entryCabal pkgId (revNo, (_cabalFile, (timestamp, uid))) =
284286
CabalFileEntry pkgId revNo timestamp uid (uidToName uid)
285287

286-
entryTUF :: PackageId -> (Int, (a, UploadInfo)) -> TarIndexEntry
288+
entryTUF :: PackageId -> (TarballRevIx, (a, UploadInfo)) -> TarIndexEntry
287289
entryTUF pkgId (revNo, (_tarball, (timestamp, _uid))) =
288290
MetadataEntry pkgId revNo timestamp
289291

@@ -295,8 +297,8 @@ initialUpdateLog oldExtras users pkgs =
295297
entryTimestamp (MetadataEntry _ _ timestamp ) = timestamp
296298
entryTimestamp (ExtraEntry _ _ timestamp ) = timestamp
297299

298-
vecToList :: Vec.Vector a -> [(Int, a)]
299-
vecToList = zip [0..] . Vec.toList
300+
vecToList :: Coercible Int ix => Vec.Vector a -> [(ix, a)]
301+
vecToList = coerce . zip [(0 :: Int)..] . Vec.toList
300302

301303
------------------------------------------------------------------------------
302304

src/Distribution/Server/Features/PackageInfoJSON.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import qualified Distribution.Server.Framework as Framework
3838
import Distribution.Server.Features.Core (CoreFeature(..),
3939
CoreResource(..))
4040
import qualified Distribution.Server.Features.PreferredVersions as Preferred
41-
import Distribution.Server.Packages.Types (CabalFileText(..), pkgSpecificRevision, pkgLatestRevision, pkgMaxRevision, pkgNumRevisions)
41+
import Distribution.Server.Packages.Types (CabalFileText(..), MetadataRevIx(..), pkgSpecificRevision, pkgLatestRevision, pkgMaxRevision, pkgNumRevisions)
4242

4343
import Distribution.Utils.ShortText (fromShortText)
4444
import Data.Foldable (toList)
@@ -55,7 +55,7 @@ data PackageBasicDescription = PackageBasicDescription
5555
, pbd_description :: !T.Text
5656
, pbd_author :: !T.Text
5757
, pbd_homepage :: !T.Text
58-
, pbd_metadata_revision :: !Int
58+
, pbd_metadata_revision :: !MetadataRevIx
5959
, pbd_uploaded_at :: !UTCTime
6060
} deriving (Eq, Show)
6161

@@ -69,7 +69,7 @@ data PackageBasicDescriptionDTO = PackageBasicDescriptionDTO
6969
, description :: !T.Text
7070
, author :: !T.Text
7171
, homepage :: !T.Text
72-
, metadata_revision :: !Int
72+
, metadata_revision :: !MetadataRevIx
7373
, uploaded_at :: !UTCTime
7474
, uploader :: !UserName
7575
} deriving (Eq, Show)
@@ -173,7 +173,7 @@ getBasicDescription
173173
:: UTCTime
174174
-- ^ Time of upload
175175
-> CabalFileText
176-
-> Int
176+
-> MetadataRevIx
177177
-- ^ Metadata revision. This will be added to the resulting
178178
-- @PackageBasicDescription@
179179
-> Either String PackageBasicDescription
@@ -225,7 +225,7 @@ servePackageBasicDescription
225225
-> Framework.ServerPartE Framework.Response
226226
servePackageBasicDescription resource userFeature preferred dpath = do
227227

228-
let metadataRev :: Maybe Int = lookup "revision" dpath >>= Framework.fromReqURI
228+
let metadataRev :: Maybe MetadataRevIx = lookup "revision" dpath >>= Framework.fromReqURI
229229

230230
pkgid@(PackageIdentifier name version) <- packageInPath resource dpath
231231
guardValidPackageName resource name
@@ -238,7 +238,7 @@ servePackageBasicDescription resource userFeature preferred dpath = do
238238

239239
fetchDescr
240240
:: PackageIdentifier
241-
-> Maybe Int
241+
-> Maybe MetadataRevIx
242242
-> Framework.ServerPartE Framework.Response
243243
fetchDescr pkgid metadataRev = do
244244
guardValidPackageId resource pkgid

src/Distribution/Server/Features/Security.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ initSecurityFeature env = do
8585
case pkgLatestTarball pkgInfo of
8686
Nothing -> []
8787
Just (_tarball, (uploadTime, _uploadUserId), latestRev) ->
88-
[MetadataEntry (pkgInfoId pkgInfo) latestRev uploadTime]
88+
[MetadataEntry (pkgInfoId pkgInfo) (TarballRevIx latestRev) uploadTime]
8989

9090
-- | The main security feature
9191
--

src/Distribution/Server/Packages/Index.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import Distribution.Server.Framework.MemSize
1717

1818
import Distribution.Server.Packages.Types
1919
( CabalFileText(..), PkgInfo(..)
20+
, TarballRevIx, MetadataRevIx
2021
, pkgSpecificRevision
2122
, pkgLatestCabalFileText, pkgLatestUploadInfo
2223
)
@@ -57,7 +58,7 @@ data TarIndexEntry =
5758
-- can also be changed (this is used during mirroring, for instance).
5859
--
5960
-- The UTCTime and userName are used as file metadata in the tarball.
60-
CabalFileEntry !PackageId !RevisionNo !UTCTime !UserId !UserName
61+
CabalFileEntry !PackageId !MetadataRevIx !UTCTime !UserId !UserName
6162

6263
-- | Package metadata
6364
--
@@ -69,16 +70,14 @@ data TarIndexEntry =
6970
-- Although we do not currently allow to change the upload time for package
7071
-- tarballs, but I'm not sure why not (TODO) and it's conceivable we may
7172
-- change this, so we record the original upload time.
72-
| MetadataEntry !PackageId !RevisionNo !UTCTime
73+
| MetadataEntry !PackageId !TarballRevIx !UTCTime
7374

7475
-- | Additional entries that we add to the tarball
7576
--
7677
-- This is currently used for @preferred-versions@.
7778
| ExtraEntry !FilePath !LazyByteString !UTCTime
7879
deriving (Eq, Show)
7980

80-
type RevisionNo = Int
81-
8281
instance MemSize TarIndexEntry where
8382
memSize (CabalFileEntry a b c d e) = memSize5 a b c d e
8483
memSize (MetadataEntry a b c) = memSize3 a b c

src/Distribution/Server/Packages/Metadata.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import qualified Hackage.Security.TUF.FileMap as Sec.FileMap
2424
--
2525
-- Revisions numbers count from 0; we use the revision number as is for the
2626
-- TUF file version.
27-
computePkgMetadata :: PkgInfo -- ^ Package
28-
-> Int -- ^ Tarball revision
27+
computePkgMetadata :: PkgInfo -- ^ Package
28+
-> TarballRevIx -- ^ Tarball revision
2929
-> (FilePath, BS.Lazy.ByteString)
3030
computePkgMetadata pkg revNo = (inIndexPkgMetadata pkgId, raw)
3131
where
@@ -35,9 +35,9 @@ computePkgMetadata pkg revNo = (inIndexPkgMetadata pkgId, raw)
3535
signed = Sec.withSignatures' [] targets
3636
raw = Sec.renderJSON_NoLayout signed
3737

38-
pkgTarballTargets :: Int -> PackageIdentifier -> PkgTarball -> Sec.Targets
38+
pkgTarballTargets :: TarballRevIx -> PackageIdentifier -> PkgTarball -> Sec.Targets
3939
pkgTarballTargets revNo pkgId pkgTarball = Sec.Targets {
40-
targetsVersion = Sec.FileVersion (fromIntegral revNo)
40+
targetsVersion = Sec.FileVersion (fromIntegral $ getTarballRevIx revNo)
4141
, targetsExpires = Sec.expiresNever
4242
, targetsTargets = Sec.FileMap.fromList [
4343
(inRepoPkgTarGz pkgId, secFileInfo pkgTarballGz)

src/Distribution/Server/Packages/Render.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ data PackageRender = PackageRender {
8787
rendChangeLog :: Maybe (FilePath, ETag, TarEntryOffset, FilePath),
8888
rendReadme :: Maybe (FilePath, ETag, TarEntryOffset, FilePath),
8989
rendUploadInfo :: (UTCTime, Maybe UserInfo),
90-
rendUpdateInfo :: Maybe (Int, UTCTime, Maybe UserInfo),
90+
rendUpdateInfo :: Maybe (MetadataRevIx, UTCTime, Maybe UserInfo),
9191
rendPkgUri :: String,
9292
rendFlags :: [PackageFlag],
9393
-- rendOther contains other useful fields which are merely strings, possibly empty
@@ -127,7 +127,7 @@ doPackageRender users info = PackageRender
127127
, rendUpdateInfo = let maxrevision = pkgMaxRevision info
128128
(utime, uid) = pkgLatestUploadInfo info
129129
uinfo = Users.lookupUserId uid users
130-
in if maxrevision > 0
130+
in if maxrevision > MetadataRevIx 0
131131
then Just (maxrevision, utime, uinfo)
132132
else Nothing
133133
, rendPkgUri = pkgUri

src/Distribution/Server/Packages/Types.hs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
{-# LANGUAGE DerivingStrategies #-}
12
{-# LANGUAGE GeneralizedNewtypeDeriving, DeriveDataTypeable,
23
StandaloneDeriving, TemplateHaskell, TypeFamilies,
34
RecordWildCards #-}
5+
46
-----------------------------------------------------------------------------
57
-- |
68
-- Module : Distribution.Server.Packages.Types
@@ -17,6 +19,7 @@ module Distribution.Server.Packages.Types where
1719

1820
import Distribution.Server.Prelude
1921

22+
import Distribution.Server.Framework (FromReqURI(..))
2023
import Distribution.Server.Users.Types (UserId(..))
2124
import Distribution.Server.Framework.BlobStorage (BlobId, BlobId_v0, BlobStorage)
2225
import Distribution.Server.Framework.Instances (PackageIdentifier_v0)
@@ -34,6 +37,7 @@ import Distribution.PackageDescription
3437
import Distribution.PackageDescription.Parsec
3538
( parseGenericPackageDescription, runParseResult )
3639

40+
import Data.Aeson (ToJSON)
3741
import Data.Serialize (Serialize)
3842
import Data.ByteString (StrictByteString)
3943
import Data.ByteString.Lazy (LazyByteString)
@@ -158,6 +162,22 @@ instance Package PkgInfo where
158162
Utility
159163
-------------------------------------------------------------------------------}
160164

165+
newtype MetadataRevIx = MetadataRevIx { getMetadataRevIx :: Int }
166+
deriving newtype (Eq, Ord, Show, MemSize, Read, FromReqURI, ToJSON, Serialize)
167+
168+
instance SafeCopy MetadataRevIx where
169+
getCopy = contain Serialize.get
170+
putCopy = contain . Serialize.put
171+
errorTypeName _ = "MetadataRevIx"
172+
173+
newtype TarballRevIx = TarballRevIx { getTarballRevIx :: Int }
174+
deriving newtype (Eq, Ord, Show, MemSize, Read, FromReqURI, ToJSON, Serialize)
175+
176+
instance SafeCopy TarballRevIx where
177+
getCopy = contain Serialize.get
178+
putCopy = contain . Serialize.put
179+
errorTypeName _ = "TarballRevIx"
180+
161181
cabalFileString :: CabalFileText -> String
162182
cabalFileString = unpackUTF8Strict . cabalFileByteString
163183

@@ -176,14 +196,14 @@ pkgOriginalUploadUser = snd . pkgOriginalUploadInfo
176196
pkgLatestRevision :: PkgInfo -> (CabalFileText, UploadInfo)
177197
pkgLatestRevision = Vec.last . pkgMetadataRevisions
178198

179-
pkgSpecificRevision :: PkgInfo -> Int -> Maybe (CabalFileText, UploadInfo)
180-
pkgSpecificRevision pkg revno = pkgMetadataRevisions pkg Vec.!? revno
199+
pkgSpecificRevision :: PkgInfo -> MetadataRevIx -> Maybe (CabalFileText, UploadInfo)
200+
pkgSpecificRevision pkg (MetadataRevIx revno) = pkgMetadataRevisions pkg Vec.!? revno
181201

182202
pkgAllRevisionsCabalFiles :: PkgInfo -> [CabalFileText]
183203
pkgAllRevisionsCabalFiles = fmap fst . Vec.toList . pkgMetadataRevisions
184204

185-
pkgSpecificTarball :: PkgInfo -> Int -> Maybe (PkgTarball, UploadInfo)
186-
pkgSpecificTarball pkg revno = pkgTarballRevisions pkg Vec.!? revno
205+
pkgSpecificTarball :: PkgInfo -> TarballRevIx -> Maybe (PkgTarball, UploadInfo)
206+
pkgSpecificTarball pkg (TarballRevIx revno) = pkgTarballRevisions pkg Vec.!? revno
187207

188208
pkgAllTarballs :: PkgInfo -> [(PkgTarball, UploadInfo)]
189209
pkgAllTarballs = Vec.toList . pkgTarballRevisions
@@ -206,8 +226,8 @@ pkgLatestUploadUser = snd . pkgLatestUploadInfo
206226
pkgNumRevisions :: PkgInfo -> Int
207227
pkgNumRevisions = Vec.length . pkgMetadataRevisions
208228

209-
pkgMaxRevision :: PkgInfo -> Int
210-
pkgMaxRevision = subtract 1 . pkgNumRevisions
229+
pkgMaxRevision :: PkgInfo -> MetadataRevIx
230+
pkgMaxRevision = MetadataRevIx . subtract 1 . pkgNumRevisions
211231

212232
-- | The latest tarball for a package (if any)
213233
--
@@ -360,3 +380,4 @@ instance Migrate PkgInfo where
360380
}
361381

362382
deriveSafeCopy 4 'extension ''PkgInfo
383+

src/Distribution/Server/Pages/PackageFromTemplate.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ packagePageTemplate render
269269
"" -> "None provided"
270270
x -> x
271271

272-
renderUpdateInfo :: Int -> UTCTime -> Maybe UserInfo -> Html
272+
renderUpdateInfo :: MetadataRevIx -> UTCTime -> Maybe UserInfo -> Html
273273
renderUpdateInfo revisionNo utime uinfo =
274274
anchor ! [href revisionsURL] << ("Revision " +++ show revisionNo)
275275
+++ " made " +++

0 commit comments

Comments
 (0)