Skip to content

Commit 7f9c7fb

Browse files
committed
remote: add/derive instances for StoreRequest
1 parent e0456e3 commit 7f9c7fb

File tree

3 files changed

+87
-6
lines changed

3 files changed

+87
-6
lines changed

hnix-store-remote/hnix-store-remote.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,15 @@ library
9999
base >=4.12 && <5
100100
, hnix-store-core >= 0.8 && <0.9
101101
, hnix-store-nar >= 0.1
102+
, hnix-store-tests >= 0.1
102103
, attoparsec
103104
, bytestring
104105
, cereal
105106
, containers
106107
, crypton
107108
, data-default-class
108109
, dependent-sum > 0.7 && < 1
110+
, dependent-sum-template > 0.1.1 && < 0.3
109111
, generic-arbitrary < 1.1
110112
, hashable
111113
, text

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@
33
{-# OPTIONS_GHC -Wno-orphans #-}
44
module System.Nix.Store.Remote.Arbitrary where
55

6+
import Data.Some (Some(Some))
7+
import System.Nix.Arbitrary ()
8+
import System.Nix.Store.Remote.GADT
69
import System.Nix.Store.Remote.Types
710

8-
import Test.QuickCheck (Arbitrary(..))
11+
import Test.QuickCheck (Arbitrary(..), oneof)
912
import Test.QuickCheck.Arbitrary.Generic (GenericArbitrary(..))
1013
import Test.QuickCheck.Instances ()
1114

15+
deriving via GenericArbitrary CheckMode
16+
instance Arbitrary CheckMode
17+
18+
deriving via GenericArbitrary SubstituteMode
19+
instance Arbitrary SubstituteMode
20+
1221
deriving via GenericArbitrary ProtoVersion
1322
instance Arbitrary ProtoVersion
1423

@@ -43,3 +52,30 @@ deriving via GenericArbitrary Logger
4352

4453
deriving via GenericArbitrary Verbosity
4554
instance Arbitrary Verbosity
55+
56+
instance Arbitrary (Some StoreRequest) where
57+
arbitrary = oneof
58+
[ Some <$> (AddToStore <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary)
59+
, Some <$> (AddTextToStore <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary)
60+
, Some <$> (AddSignatures <$> arbitrary <*> arbitrary)
61+
, Some . AddIndirectRoot <$> arbitrary
62+
, Some . AddTempRoot <$> arbitrary
63+
, Some <$> (BuildPaths <$> arbitrary <*> arbitrary)
64+
, Some <$> (BuildDerivation <$> arbitrary <*> arbitrary <*> arbitrary)
65+
, Some . EnsurePath <$> arbitrary
66+
, pure $ Some FindRoots
67+
, Some . IsValidPath <$> arbitrary
68+
, Some <$> (QueryValidPaths <$> arbitrary <*> arbitrary)
69+
, pure $ Some QueryAllValidPaths
70+
, Some . QuerySubstitutablePaths <$> arbitrary
71+
, Some . QueryPathInfo <$> arbitrary
72+
, Some . QueryReferrers <$> arbitrary
73+
, Some . QueryValidDerivers <$> arbitrary
74+
, Some . QueryDerivationOutputs <$> arbitrary
75+
, Some . QueryDerivationOutputNames <$> arbitrary
76+
, Some . QueryPathFromHashPart <$> arbitrary
77+
, Some . QueryMissing <$> arbitrary
78+
, pure $ Some OptimiseStore
79+
, pure $ Some SyncWithGC
80+
, Some <$> (VerifyStore <$> arbitrary <*> arbitrary)
81+
]

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

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
{-# language GADTs #-}
22
{-# language Rank2Types #-}
3+
{-# LANGUAGE TemplateHaskell #-}
34

45
module System.Nix.Store.Remote.GADT
56
( StoreRequest(..)
67
) where
78

8-
import Control.Monad.IO.Class (MonadIO)
99
import Data.ByteString (ByteString)
10+
import Data.GADT.Compare.TH (deriveGEq, deriveGCompare)
11+
import Data.GADT.Show.TH (deriveGShow)
1012
import Data.HashSet (HashSet)
1113
import Data.Kind (Type)
1214
import Data.Map (Map)
1315
import Data.Set (Set)
1416
import Data.Text (Text)
15-
import Data.Some (Some)
17+
import Data.Some (Some(Some))
1618

1719
import System.Nix.Build (BuildMode, BuildResult)
1820
import System.Nix.Derivation (Derivation)
1921
import System.Nix.DerivedPath (DerivedPath)
2022
import System.Nix.Hash (HashAlgo)
21-
import System.Nix.Nar (NarSource)
2223
import System.Nix.Store.Types (RepairMode)
2324
import System.Nix.StorePath (StorePath, StorePathName, StorePathHashPart)
2425
import System.Nix.StorePath.Metadata (Metadata)
@@ -30,8 +31,16 @@ data StoreRequest :: Type -> Type where
3031
AddToStore
3132
:: StorePathName -- ^ Name part of the newly created @StorePath@
3233
-> Bool -- ^ Add target directory recursively
33-
-> Some HashAlgo
34-
-> (forall m . MonadIO m => NarSource m) -- ^ provide nar stream
34+
-> Some HashAlgo -- ^ Nar hashing algorithm
35+
-- -> (forall m . MonadIO m => NarSource m) -- ^ provide nar stream
36+
-- Not part of StoreRequest
37+
-- as it would require StoreRequest (m :: Type -> Type) :: Type -> Type
38+
-- for which we cannot derive anything
39+
--
40+
-- Also the thing is the only special case
41+
-- and it is always sent *after* the other
42+
-- information so it can be handled
43+
-- separately after that. Hopefully.
3544
-> RepairMode -- ^ Only used by local store backend
3645
-> StoreRequest StorePath
3746

@@ -148,3 +157,37 @@ data StoreRequest :: Type -> Type where
148157
:: CheckMode
149158
-> RepairMode
150159
-> StoreRequest Bool
160+
161+
deriving instance Eq (StoreRequest a)
162+
deriving instance Show (StoreRequest a)
163+
164+
deriveGEq ''StoreRequest
165+
deriveGCompare ''StoreRequest
166+
deriveGShow ''StoreRequest
167+
168+
instance {-# OVERLAPPING #-} Eq (Some StoreRequest) where
169+
Some (AddToStore a b c d) == Some (AddToStore a' b' c' d') = (a, b, c, d) == (a', b', c', d')
170+
Some (AddTextToStore a b c d) == Some (AddTextToStore a' b' c' d') = (a, b, c, d) == (a', b', c', d')
171+
Some (AddSignatures a b) == Some (AddSignatures a' b') = (a, b) == (a', b')
172+
Some (AddIndirectRoot a) == Some (AddIndirectRoot a') = a == a'
173+
Some (AddTempRoot a) == Some (AddTempRoot a') = a == a'
174+
Some (BuildPaths a b) == Some (BuildPaths a' b') = (a, b) == (a', b')
175+
Some (BuildDerivation a b c) == Some (BuildDerivation a' b' c') = (a, b, c) == (a', b', c')
176+
Some (EnsurePath a) == Some (EnsurePath a') = a == a'
177+
Some (FindRoots) == Some (FindRoots) = True
178+
Some (IsValidPath a) == Some (IsValidPath a') = a == a'
179+
Some (QueryValidPaths a b) == Some (QueryValidPaths a' b') = (a, b) == (a', b')
180+
Some QueryAllValidPaths == Some QueryAllValidPaths = True
181+
Some (QuerySubstitutablePaths a) == Some (QuerySubstitutablePaths a') = a == a'
182+
Some (QueryPathInfo a) == Some (QueryPathInfo a') = a == a'
183+
Some (QueryReferrers a) == Some (QueryReferrers a') = a == a'
184+
Some (QueryValidDerivers a) == Some (QueryValidDerivers a') = a == a'
185+
Some (QueryDerivationOutputs a) == Some (QueryDerivationOutputs a') = a == a'
186+
Some (QueryDerivationOutputNames a) == Some (QueryDerivationOutputNames a') = a == a'
187+
Some (QueryPathFromHashPart a) == Some (QueryPathFromHashPart a') = a == a'
188+
Some (QueryMissing a) == Some (QueryMissing a') = a == a'
189+
Some OptimiseStore == Some OptimiseStore = True
190+
Some SyncWithGC == Some SyncWithGC = True
191+
Some (VerifyStore a b) == Some (VerifyStore a' b') = (a, b) == (a', b')
192+
193+
_ == _ = False

0 commit comments

Comments
 (0)