Skip to content

Commit 9e2ef07

Browse files
committed
String: flip mkNixSting{,WithSingleTonContext}
1 parent 7128046 commit 9e2ef07

File tree

5 files changed

+72
-73
lines changed

5 files changed

+72
-73
lines changed

src/Nix/Builtins.hs

Lines changed: 61 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ replaceStringsNix tfrom tto ts =
10431043

10441044
-- 2021-02-18: NOTE: rly?: toStrict . toLazyText
10451045
-- Maybe `text-builder`, `text-show`?
1046-
finish ctx output = mkNixString (toStrict $ Builder.toLazyText output) ctx
1046+
finish ctx output = mkNixString ctx (toStrict $ Builder.toLazyText output)
10471047

10481048
replace (key, replacementNS, unprocessedInput) = replaceWithNixBug unprocessedInput updatedOutput
10491049

@@ -1141,7 +1141,7 @@ toFileNix name s =
11411141
storepath = coerce (fromString @Text) mres
11421142
sc = StringContext DirectPath storepath
11431143

1144-
toValue $ mkNixStringWithSingletonContext storepath sc
1144+
toValue $ mkNixStringWithSingletonContext sc storepath
11451145

11461146
toPathNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
11471147
toPathNix = inHask @Path id
@@ -1699,15 +1699,12 @@ getRecursiveSizeNix = fmap (mkNVConstant . NInt . fromIntegral) . recursiveSize
16991699

17001700
getContextNix
17011701
:: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
1702-
getContextNix v =
1703-
do
1704-
v' <- demand v
1705-
case v' of
1706-
(NVStr ns) -> do
1707-
let context = getNixLikeContext $ toNixLikeContext $ getContext ns
1708-
valued :: AttrSet (NValue t f m) <- traverseToValue context
1709-
pure $ mkNVSet mempty valued
1710-
x -> throwError $ ErrorCall $ "Invalid type for builtins.getContext: " <> show x
1702+
getContextNix =
1703+
\case
1704+
(NVStr ns) ->
1705+
mkNVSet mempty <$> traverseToValue (getNixLikeContext $ toNixLikeContext $ getContext ns)
1706+
x -> throwError $ ErrorCall $ "Invalid type for builtins.getContext: " <> show x
1707+
<=< demand
17111708

17121709
appendContextNix
17131710
:: forall e t f m
@@ -1723,62 +1720,63 @@ appendContextNix tx ty =
17231720
case (x, y) of
17241721
(NVStr ns, NVSet _ attrs) ->
17251722
do
1726-
newContextValues <- traverse getPathNOuts attrs
1723+
let
1724+
getPathNOuts :: NValue t f m -> m NixLikeContextValue
1725+
getPathNOuts tx =
1726+
do
1727+
x <- demand tx
17271728

1728-
toValue $ addContext ns newContextValues
1729+
case x of
1730+
NVSet _ atts ->
1731+
do
1732+
-- TODO: Fail for unexpected keys.
1733+
1734+
let
1735+
getK :: VarName -> m Bool
1736+
getK k =
1737+
maybe
1738+
(pure False)
1739+
(fromValue <=< demand)
1740+
$ M.lookup k atts
1741+
1742+
getOutputs :: m [Text]
1743+
getOutputs =
1744+
maybe
1745+
stub
1746+
(\ touts ->
1747+
do
1748+
outs <- demand touts
1749+
1750+
case outs of
1751+
NVList vs -> traverse (fmap ignoreContext . fromValue) vs
1752+
_x -> throwError $ ErrorCall $ "Invalid types for context value outputs in builtins.appendContext: " <> show _x
1753+
)
1754+
(M.lookup "outputs" atts)
1755+
1756+
path <- getK "path"
1757+
allOutputs <- getK "allOutputs"
1758+
1759+
NixLikeContextValue path allOutputs <$> getOutputs
1760+
1761+
_x -> throwError $ ErrorCall $ "Invalid types for context value in builtins.appendContext: " <> show _x
1762+
addContext :: HashMap VarName NixLikeContextValue -> NixString
1763+
addContext newContextValues =
1764+
mkNixString
1765+
(fromNixLikeContext $
1766+
NixLikeContext $
1767+
M.unionWith
1768+
(<>)
1769+
newContextValues
1770+
$ getNixLikeContext $
1771+
toNixLikeContext $
1772+
getContext ns
1773+
)
1774+
$ ignoreContext ns
17291775

1730-
_xy -> throwError $ ErrorCall $ "Invalid types for builtins.appendContext: " <> show _xy
1776+
toValue . addContext =<< traverse getPathNOuts attrs
17311777

1732-
where
1733-
getPathNOuts tx =
1734-
do
1735-
x <- demand tx
1778+
_xy -> throwError $ ErrorCall $ "Invalid types for builtins.appendContext: " <> show _xy
17361779

1737-
case x of
1738-
NVSet _ attrs->
1739-
do
1740-
-- TODO: Fail for unexpected keys.
1741-
1742-
let
1743-
getK k =
1744-
maybe
1745-
(pure False)
1746-
(fromValue <=< demand)
1747-
(M.lookup k attrs)
1748-
1749-
getOutputs =
1750-
maybe
1751-
stub
1752-
(\ touts ->
1753-
do
1754-
outs <- demand touts
1755-
1756-
case outs of
1757-
NVList vs -> traverse (fmap ignoreContext . fromValue) vs
1758-
_x -> throwError $ ErrorCall $ "Invalid types for context value outputs in builtins.appendContext: " <> show _x
1759-
)
1760-
(M.lookup "outputs" attrs)
1761-
1762-
path <- getK "path"
1763-
allOutputs <- getK "allOutputs"
1764-
1765-
NixLikeContextValue path allOutputs <$> getOutputs
1766-
1767-
_x -> throwError $ ErrorCall $ "Invalid types for context value in builtins.appendContext: " <> show _x
1768-
1769-
addContext ns newContextValues =
1770-
mkNixString
1771-
(ignoreContext ns)
1772-
(fromNixLikeContext $
1773-
NixLikeContext $
1774-
M.unionWith
1775-
(<>)
1776-
newContextValues
1777-
(getNixLikeContext $
1778-
toNixLikeContext $
1779-
getContext ns
1780-
)
1781-
)
17821780

17831781
nixVersionNix :: MonadNix e t f m => m (NValue t f m)
17841782
nixVersionNix = toValue $ mkNixStringWithoutContext "2.3"

src/Nix/Convert.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ instance ( Convertible e t f m
218218
\case
219219
NVStr' ns -> pure $ pure ns
220220
NVPath' p ->
221-
(\path -> pure $ (`mkNixStringWithSingletonContext` StringContext DirectPath path) path ) . fromString . coerce <$>
221+
(\path -> pure $ mkNixStringWithSingletonContext (StringContext DirectPath path) path) . fromString . coerce <$>
222222
addPath p
223223
NVSet' _ s ->
224224
maybe

src/Nix/Effects/Derivation.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,13 @@ defaultDerivationStrict val = do
289289
let
290290
outputsWithContext =
291291
Map.mapWithKey
292-
(\out (coerce -> path) -> mkNixStringWithSingletonContext path $ StringContext (DerivationOutput out) drvPath)
292+
(\out (coerce -> path) -> mkNixStringWithSingletonContext (StringContext (DerivationOutput out) drvPath) path)
293293
(outputs drv')
294-
drvPathWithContext = mkNixStringWithSingletonContext drvPath $ StringContext AllOutputs drvPath
294+
drvPathWithContext = mkNixStringWithSingletonContext (StringContext AllOutputs drvPath) drvPath
295295
attrSet = mkNVStr <$> M.fromList (("drvPath", drvPathWithContext) : Map.toList outputsWithContext)
296296
-- TODO: Add location information for all the entries.
297297
-- here --v
298-
pure $ mkNVSet mempty (M.mapKeys coerce attrSet)
298+
pure $ mkNVSet mempty $ M.mapKeys coerce attrSet
299299

300300
where
301301

src/Nix/String.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,13 @@ mkNixStringWithoutContext = NixString mempty
130130

131131
-- | Create NixString using a singleton context
132132
mkNixStringWithSingletonContext
133-
:: VarName -> StringContext -> NixString
134-
mkNixStringWithSingletonContext s c = NixString (one c) (coerce @VarName @Text s)
133+
:: StringContext -> VarName -> NixString
134+
mkNixStringWithSingletonContext c s = NixString (one c) (coerce @VarName @Text s)
135135

136136
-- | Create NixString from a Text and context
137-
mkNixString :: Text -> S.HashSet StringContext -> NixString
138-
mkNixString t = (`NixString` t)
137+
mkNixString
138+
:: S.HashSet StringContext -> Text -> NixString
139+
mkNixString = NixString
139140

140141

141142
-- ** Checkers

src/Nix/String/Coerce.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,5 @@ coercePathToNixString =
133133
. (CopyToStore ==)
134134
where
135135
storePathToNixString :: StorePath -> NixString
136-
storePathToNixString =
137-
(mkNixStringWithSingletonContext <*> StringContext DirectPath) . fromString . coerce
136+
storePathToNixString (fromString . coerce -> sp) =
137+
(mkNixStringWithSingletonContext . StringContext DirectPath) sp sp

0 commit comments

Comments
 (0)