Skip to content

Commit 3576273

Browse files
committed
core,json: wrap (DerivationOutput OutputName, Realisation) tuple into newtype
1 parent 2bdd171 commit 3576273

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

hnix-store-core/src/System/Nix/Realisation.hs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module System.Nix.Realisation (
88
, derivationOutputBuilder
99
, derivationOutputParser
1010
, Realisation(..)
11+
, RealisationWithId(..)
1112
) where
1213

1314
import Crypto.Hash (Digest)
@@ -80,8 +81,7 @@ derivationOutputBuilder outputName DerivationOutput{..} =
8081
--
8182
-- realisationId is ommited since it is a key
8283
-- of type @DerivationOutput OutputName@ so
83-
-- we will use a tuple like @(DerivationOutput OutputName, Realisation)@
84-
-- instead.
84+
-- we will use @RealisationWithId@ newtype
8585
data Realisation = Realisation
8686
{ realisationOutPath :: StorePath
8787
-- ^ Output path
@@ -90,3 +90,14 @@ data Realisation = Realisation
9090
, realisationDependencies :: Map (DerivationOutput OutputName) StorePath
9191
-- ^ Dependent realisations required for this one to be valid
9292
} deriving (Eq, Generic, Ord, Show)
93+
94+
-- | For wire protocol
95+
--
96+
-- We store this normalized in @Build.buildResultBuiltOutputs@
97+
-- as @Map (DerivationOutput OutputName) Realisation@
98+
-- but wire protocol needs it de-normalized so we
99+
-- need a special (From|To)JSON instances for it
100+
newtype RealisationWithId = RealisationWithId
101+
{ unRealisationWithId :: (DerivationOutput OutputName, Realisation)
102+
}
103+
deriving (Eq, Generic, Ord, Show)

hnix-store-json/src/System/Nix/JSON.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Data.Aeson
1313
import Deriving.Aeson
1414
import System.Nix.Base (BaseEncoding(NixBase32))
1515
import System.Nix.OutputName (OutputName)
16-
import System.Nix.Realisation (DerivationOutput, Realisation)
16+
import System.Nix.Realisation (DerivationOutput, Realisation, RealisationWithId(..))
1717
import System.Nix.Signature (Signature)
1818
import System.Nix.StorePath (StoreDir(..), StorePath, StorePathName, StorePathHashPart)
1919

@@ -159,18 +159,18 @@ deriving
159159
instance FromJSON Realisation
160160

161161
-- For a keyed version of Realisation
162-
-- we use (DerivationOutput OutputName, Realisation)
162+
-- we use RealisationWithId (DerivationOutput OutputName, Realisation)
163163
-- instead of Realisation.id :: (DerivationOutput OutputName)
164164
-- field.
165-
instance {-# OVERLAPPING #-} ToJSON (DerivationOutput OutputName, Realisation) where
166-
toJSON (drvOut, r) =
165+
instance ToJSON RealisationWithId where
166+
toJSON (RealisationWithId (drvOut, r)) =
167167
case toJSON r of
168168
Object o -> Object $ Data.Aeson.KeyMap.insert "id" (toJSON drvOut) o
169169
_ -> error "absurd"
170170

171-
instance {-# OVERLAPPING #-} FromJSON (DerivationOutput OutputName, Realisation) where
171+
instance FromJSON RealisationWithId where
172172
parseJSON v@(Object o) = do
173173
r <- parseJSON @Realisation v
174174
drvOut <- o .: "id"
175-
pure (drvOut, r)
175+
pure (RealisationWithId (drvOut, r))
176176
parseJSON x = fail $ "Expected Object but got " ++ show x

hnix-store-remote/src/System/Nix/Store/Remote/Serializer.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ import System.Nix.DerivedPath (DerivedPath(..), ParseOutputsError)
131131
import System.Nix.Hash (HashAlgo(..))
132132
import System.Nix.JSON ()
133133
import System.Nix.OutputName (OutputName)
134-
import System.Nix.Realisation (DerivationOutputError, Realisation(..))
134+
import System.Nix.Realisation (DerivationOutputError, Realisation(..), RealisationWithId(..))
135135
import System.Nix.Signature (Signature, NarSignature)
136136
import System.Nix.Store.Types (FileIngestionMethod(..), RepairMode(..))
137137
import System.Nix.StorePath (HasStoreDir(..), InvalidNameError, InvalidPathError, StorePath, StorePathHashPart, StorePathName)
@@ -1398,7 +1398,7 @@ derivationOutputTyped = mapErrorS ReplySError_DerivationOutput $
13981398
realisation :: NixSerializer r ReplySError Realisation
13991399
realisation = mapErrorS ReplySError_Realisation json
14001400

1401-
realisationWithId :: NixSerializer r ReplySError (System.Nix.Realisation.DerivationOutput OutputName, Realisation)
1401+
realisationWithId :: NixSerializer r ReplySError RealisationWithId
14021402
realisationWithId = mapErrorS ReplySError_RealisationWithId json
14031403

14041404
-- *** BuildResult
@@ -1434,7 +1434,7 @@ buildResult = Serializer
14341434
then
14351435
pure
14361436
. Data.Map.Strict.fromList
1437-
. map (\(_, (a, b)) -> (a, b))
1437+
. map (\(_, RealisationWithId (a, b)) -> (a, b))
14381438
. Data.Map.Strict.toList
14391439
<$> getS (mapS derivationOutputTyped realisationWithId)
14401440
else pure Nothing
@@ -1453,7 +1453,7 @@ buildResult = Serializer
14531453
Control.Monad.when (protoVersion_minor pv >= 28)
14541454
$ putS (mapS derivationOutputTyped realisationWithId)
14551455
$ Data.Map.Strict.fromList
1456-
$ map (\(a, b) -> (a, (a, b)))
1456+
$ map (\(a, b) -> (a, RealisationWithId (a, b)))
14571457
$ Data.Map.Strict.toList
14581458
$ Data.Maybe.fromMaybe mempty buildResultBuiltOutputs
14591459
}

0 commit comments

Comments
 (0)