Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String: improve function names #1039

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
-- old Nix CLI is nototrious for (and that would mean to reimplement the old Nix CLI),
-- OR: https://github.com/haskell-nix/hnix/issues/172 and have some sane standart/default behaviour for (most) keys.
printer'
| isXml = out (ignoreContext . toXML) normalForm
| isJson = out (ignoreContext . mempty . toJSONNixString) normalForm
| isXml = out (getStringIgnoreContext . toXML) normalForm
| isJson = out (getStringIgnoreContext . mempty . toJSONNixString) normalForm
| isStrict = out (show . prettyNValue) normalForm
| isValues = out (show . prettyNValueProv) removeEffects
| otherwise = out (show . prettyNValue) removeEffects
Expand Down
56 changes: 28 additions & 28 deletions src/Nix/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ instance Comonad f => Eq (WValue t f m) where
WValue (NVConstant (NFloat x)) == WValue (NVConstant (NFloat y)) = x == y
WValue (NVPath x ) == WValue (NVPath y ) = x == y
WValue (NVStr x) == WValue (NVStr y) =
ignoreContext x == ignoreContext y
getStringIgnoreContext x == getStringIgnoreContext y
_ == _ = False

instance Comonad f => Ord (WValue t f m) where
Expand All @@ -154,7 +154,7 @@ instance Comonad f => Ord (WValue t f m) where
WValue (NVConstant (NFloat x)) <= WValue (NVConstant (NFloat y)) = x <= y
WValue (NVPath x ) <= WValue (NVPath y ) = x <= y
WValue (NVStr x) <= WValue (NVStr y) =
ignoreContext x <= ignoreContext y
getStringIgnoreContext x <= getStringIgnoreContext y
_ <= _ = False

-- ** Helpers
Expand Down Expand Up @@ -207,7 +207,7 @@ foldNixPath z f =
foldrM
fun
z
$ (fromInclude . ignoreContext <$> dirs)
$ (fromInclude . getStringIgnoreContext <$> dirs)
<> uriAwareSplit `whenJust` mPath
<> one (fromInclude $ "nix=" <> fromString (coerce dataDir) <> "/nix/corepkgs")
where
Expand Down Expand Up @@ -358,7 +358,7 @@ absolutePathFromValue =
NVStr ns ->
do
let
path = coerce . toString $ ignoreContext ns
path = coerce . toString $ getStringIgnoreContext ns

unless (isAbsolute path) $ throwError $ ErrorCall $ "string " <> show path <> " doesn't represent an absolute path"
pure path
Expand Down Expand Up @@ -457,7 +457,7 @@ hasAttrNix x y =
toValue $ M.member key aset

hasContextNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
hasContextNix = inHask hasContext
hasContextNix = inHask hasStringContext

getAttrNix
:: forall e t f m
Expand All @@ -479,7 +479,7 @@ unsafeDiscardOutputDependencyNix
-> m (NValue t f m)
unsafeDiscardOutputDependencyNix nv =
do
(nc, ns) <- (getStringContext &&& ignoreContext) <$> fromValue nv
(nc, ns) <- (getStringContext &&& getStringIgnoreContext) <$> fromValue nv
toValue $ mkNixString (HS.map discard nc) ns
where
discard :: StringContext -> StringContext
Expand All @@ -502,7 +502,7 @@ unsafeGetAttrPosNix nvX nvY =
maybe
(pure nvNull)
toValue
(M.lookup @VarName (coerce $ ignoreContext ns) apos)
(M.lookup @VarName (coerce $ getStringIgnoreContext ns) apos)
_xy -> throwError $ ErrorCall $ "Invalid types for builtins.unsafeGetAttrPosNix: " <> show _xy

-- This function is a bit special in that it doesn't care about the contents
Expand Down Expand Up @@ -685,7 +685,7 @@ matchNix pat str =
-- going to preserve the behavior here until it is fixed upstream.
-- Relevant issue: https://github.com/NixOS/nix/issues/2547
let
s = ignoreContext ns
s = getStringIgnoreContext ns
re = makeRegex p :: Regex
mkMatch t =
bool
Expand Down Expand Up @@ -722,7 +722,7 @@ splitNix pat str =
-- going to preserve the behavior here until it is fixed upstream.
-- Relevant issue: https://github.com/NixOS/nix/issues/2547
let
s = ignoreContext ns
s = getStringIgnoreContext ns
regex = makeRegex p :: Regex
haystack = encodeUtf8 s

Expand Down Expand Up @@ -935,7 +935,7 @@ dirOfNix nvdir =
unsafeDiscardStringContextNix
:: MonadNix e t f m => NValue t f m -> m (NValue t f m)
unsafeDiscardStringContextNix =
inHask (mkNixStringWithoutContext . ignoreContext)
inHask (mkNixStringWithoutContext . getStringIgnoreContext)

-- | Evaluate `a` to WHNF to collect its topmost effect.
seqNix
Expand Down Expand Up @@ -1082,7 +1082,7 @@ replaceStringsNix tfrom tto ts =
where
formMatchReplaceTailInfo (m, r) = (m, r, Text.drop (Text.length m) input)

fromKeysToValsMap = zip (ignoreContext <$> fromKeys) toVals
fromKeysToValsMap = zip (getStringIgnoreContext <$> fromKeys) toVals

-- Not passing args => It is constant that gets embedded into `go` => It is simple `go` tail recursion
passOneChar =
Expand Down Expand Up @@ -1117,7 +1117,7 @@ replaceStringsNix tfrom tto ts =
updatedOutput = output <> replacement
updatedCtx = ctx <> replacementCtx

replacement = Builder.fromText $ ignoreContext replacementNS
replacement = Builder.fromText $ getStringIgnoreContext replacementNS
replacementCtx = getStringContext replacementNS

-- The bug modifies the content => bug demands `pass` to be a real function =>
Expand All @@ -1129,7 +1129,7 @@ replaceStringsNix tfrom tto ts =
(\(c, i) -> go updatedCtx i $ output <> Builder.singleton c) -- If there are chars - pass one char & continue
(Text.uncons input) -- chip first char

toValue $ go (getStringContext string) (ignoreContext string) mempty
toValue $ go (getStringContext string) (getStringIgnoreContext string) mempty

removeAttrsNix
:: forall e t f m
Expand Down Expand Up @@ -1185,7 +1185,7 @@ toFileNix name s =
mres <-
toFile_
(coerce $ toString name')
(ignoreContext s')
(getStringIgnoreContext s')

let
storepath = coerce (fromString @Text) mres
Expand All @@ -1203,7 +1203,7 @@ pathExistsNix nvpath =
toValue =<<
case path of
NVPath p -> doesPathExist p
NVStr ns -> doesPathExist $ coerce $ toString $ ignoreContext ns
NVStr ns -> doesPathExist $ coerce $ toString $ getStringIgnoreContext ns
_v -> throwError $ ErrorCall $ "builtins.pathExists: expected path, got " <> show _v

isPathNix
Expand Down Expand Up @@ -1257,7 +1257,7 @@ isFunctionNix nv =

throwNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
throwNix =
throwError . ErrorCall . toString . ignoreContext
throwError . ErrorCall . toString . getStringIgnoreContext
<=< coerceStringlikeToNixString CopyToStore

-- | Implementation of Nix @import@ clause.
Expand Down Expand Up @@ -1392,7 +1392,7 @@ lessThanNix ta tb =
(NFloat a, NInt b) -> pure $ a < fromInteger b
(NFloat a, NFloat b) -> pure $ a < b
_ -> badType
(NVStr a, NVStr b) -> pure $ ignoreContext a < ignoreContext b
(NVStr a, NVStr b) -> pure $ getStringIgnoreContext a < getStringIgnoreContext b
_ -> badType

-- | Helper function, generalization of @concat@ operations.
Expand Down Expand Up @@ -1544,7 +1544,7 @@ placeHolderNix p =
bytes :: NixString -> ByteString
bytes = encodeUtf8 . body

body = ignoreContext
body = getStringIgnoreContext

readFileNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
readFileNix = toValue <=< Nix.Render.readFile <=< absolutePathFromValue <=< demand
Expand All @@ -1563,7 +1563,7 @@ findFileNix nvaset nvfilepath =
case (aset, filePath) of
(NVList x, NVStr ns) ->
do
mres <- findPath @t @f @m x $ coerce $ toString $ ignoreContext ns
mres <- findPath @t @f @m x $ coerce $ toString $ getStringIgnoreContext ns

pure $ mkNVPath mres

Expand Down Expand Up @@ -1702,7 +1702,7 @@ traceNix
-> m (NValue t f m)
traceNix msg action =
do
traceEffect @t @f @m . toString . ignoreContext =<< fromValue msg
traceEffect @t @f @m . toString . getStringIgnoreContext =<< fromValue msg
pure action

-- Please, can function remember fail context
Expand All @@ -1722,7 +1722,7 @@ execNix xs =
-- 2018-11-19: NOTE: Still need to do something with the context here
-- See prim_exec in nix/src/libexpr/primops.cc
-- Requires the implementation of EvalState::realiseContext
exec $ ignoreContext <$> xs'
exec $ getStringIgnoreContext <$> xs'

fetchurlNix
:: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
Expand All @@ -1749,7 +1749,7 @@ fetchurlNix =
maybe
(throwError $ ErrorCall "builtins.fetchurl: unsupported arguments to url")
pure
(getStringNoContext ns)
(getStringIfNoContext ns)

partitionNix
:: forall e t f m
Expand Down Expand Up @@ -1791,8 +1791,8 @@ currentTimeNix =
derivationStrictNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
derivationStrictNix = derivationStrict

getRecursiveSizeNix :: (MonadIntrospect m, Applicative f) => a -> m (NValue t f m)
getRecursiveSizeNix = fmap (mkNVConstant . NInt . fromIntegral) . recursiveSize
valueSizeNix :: (MonadIntrospect m, Applicative f) => a -> m (NValue t f m)
valueSizeNix = fmap (mkNVConstant . NInt . fromIntegral) . recursiveSize

getContextNix
:: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
Expand Down Expand Up @@ -1845,7 +1845,7 @@ appendContextNix tx ty =
outs <- demand touts

case outs of
NVList vs -> traverse (fmap ignoreContext . fromValue) vs
NVList vs -> traverse (fmap getStringIgnoreContext . fromValue) vs
_x -> throwError $ ErrorCall $ "Invalid types for context value outputs in builtins.appendContext: " <> show _x
)
(M.lookup "outputs" atts)
Expand All @@ -1868,7 +1868,7 @@ appendContextNix tx ty =
toNixLikeContext $
getStringContext ns
)
$ ignoreContext ns
$ getStringIgnoreContext ns

toValue . addContext =<< traverse getPathNOuts attrs

Expand Down Expand Up @@ -1980,7 +1980,7 @@ builtinsList =
, add Normal "splitVersion" splitVersionNix
, add0 Normal "storeDir" (pure $ mkNVStrWithoutContext "/nix/store")
--, add Normal "storePath" storePath
, add' Normal "stringLength" (arity1 $ Text.length . ignoreContext)
, add' Normal "stringLength" (arity1 $ Text.length . getStringIgnoreContext)
, add' Normal "sub" (arity2 ((-) @Integer))
, add' Normal "substring" substringNix
, add Normal "tail" tailNix
Expand All @@ -1994,7 +1994,7 @@ builtinsList =
, add Normal "unsafeDiscardOutputDependency" unsafeDiscardOutputDependencyNix
, add Normal "unsafeDiscardStringContext" unsafeDiscardStringContextNix
, add2 Normal "unsafeGetAttrPos" unsafeGetAttrPosNix
, add Normal "valueSize" getRecursiveSizeNix
, add Normal "valueSize" valueSizeNix
]
where

Expand Down
6 changes: 3 additions & 3 deletions src/Nix/Convert.hs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ instance Convertible e t f m
fromValueMay =
pure .
\case
NVStr' ns -> encodeUtf8 <$> getStringNoContext ns
NVStr' ns -> encodeUtf8 <$> getStringIfNoContext ns
_ -> mempty

fromValue = fromMayToValue $ TString mempty
Expand All @@ -249,7 +249,7 @@ instance Convertible e t f m
fromValueMay =
pure .
\case
NVStr' ns -> getStringNoContext ns
NVStr' ns -> getStringIfNoContext ns
_ -> mempty

fromValue = fromMayToValue $ TString mempty
Expand All @@ -262,7 +262,7 @@ instance ( Convertible e t f m
fromValueMay =
\case
NVPath' p -> pure $ pure $ coerce p
NVStr' ns -> pure $ coerce . toString <$> getStringNoContext ns
NVStr' ns -> pure $ coerce . toString <$> getStringIfNoContext ns
NVSet' _ s ->
maybe
stub
Expand Down
6 changes: 3 additions & 3 deletions src/Nix/Effects/Basic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ findPathBy finder ls name =
tryPath path $
whenJust
(\ nsPfx ->
let pfx = ignoreContext nsPfx in
let pfx = getStringIgnoreContext nsPfx in
pure $ coerce $ toString pfx `whenFalse` Text.null pfx
)
mns
Expand Down Expand Up @@ -191,7 +191,7 @@ fetchTarball =
-> m (NValue t f m)
fetchFromString msha =
\case
NVStr ns -> fetch (ignoreContext ns) msha
NVStr ns -> fetch (getStringIgnoreContext ns) msha
v -> throwError $ ErrorCall $ "builtins.fetchTarball: Expected URI or string, got " <> show v

{- jww (2018-04-11): This should be written using pipes in another module
Expand All @@ -216,7 +216,7 @@ fetchTarball =
do
nsSha <- fromValue =<< demand v

let sha = ignoreContext nsSha
let sha = getStringIgnoreContext nsSha

nixInstantiateExpr $
"builtins.fetchTarball { " <> "url = \"" <> uri <> "\"; " <> "sha256 = \"" <> sha <> "\"; }"
Expand Down
2 changes: 1 addition & 1 deletion src/Nix/Effects/Derivation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ buildDerivationWithContext drvAttrs = do
maybe
(lift $ throwError $ ErrorCall $ "The string " <> show ns <> " is not allowed to have a context.")
pure
(getStringNoContext ns)
(getStringIfNoContext ns)

assertNonNull :: MonadNix e t f m => Text -> WithStringContextT m Text
assertNonNull t = do
Expand Down
2 changes: 1 addition & 1 deletion src/Nix/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ evalSetterKeyName =
\case
StaticKey k -> pure $ pure k
DynamicKey k ->
coerce . ignoreContext <<$>> runAntiquoted "\n" assembleString (fromValueMay =<<) k
coerce . getStringIgnoreContext <<$>> runAntiquoted "\n" assembleString (fromValueMay =<<) k

assembleString
:: forall v m
Expand Down
6 changes: 3 additions & 3 deletions src/Nix/Exec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mkNVStrWithProvenance
-> NixString
-> NValue t f m
mkNVStrWithProvenance scopes span x =
addProvenance (Provenance scopes . NStrAnnF span . DoubleQuoted . one . Plain . ignoreContext $ x) $ mkNVStr x
addProvenance (Provenance scopes . NStrAnnF span . DoubleQuoted . one . Plain . getStringIgnoreContext $ x) $ mkNVStr x

mkNVPathWithProvenance
:: MonadCited t f m
Expand Down Expand Up @@ -434,7 +434,7 @@ execBinaryOpForced scope span op lval rval =
maybe
(throwError $ ErrorCall "A string that refers to a store path cannot be appended to a path.") -- data/nix/src/libexpr/eval.cc:1412
(\ rs2 -> mkPathP <$> toAbsolutePath @t @f (ls <> coerce (toString rs2)))
(getStringNoContext rs)
(getStringIfNoContext rs)
(NVPath ls, NVPath rs) -> mkPathP <$> toAbsolutePath @t @f (ls <> rs)

(ls@NVSet{}, NVStr rs) ->
Expand Down Expand Up @@ -513,7 +513,7 @@ fromStringNoContext ns =
maybe
(throwError $ ErrorCall $ "expected string with no context, but got " <> show ns)
pure
(getStringNoContext ns)
(getStringIfNoContext ns)

addTracing
::( MonadNix e t f m
Expand Down
4 changes: 2 additions & 2 deletions src/Nix/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ valueToExpr = iterNValueByDiscardWith thk (Fix . phi)

phi :: NValue' t f m NExpr -> NExprF NExpr
phi (NVConstant' a ) = NConstant a
phi (NVStr' ns ) = NStr $ DoubleQuoted $ one $ Plain $ ignoreContext ns
phi (NVStr' ns ) = NStr $ DoubleQuoted $ one $ Plain $ getStringIgnoreContext ns
phi (NVList' l ) = NList l
phi (NVSet' p s) = NSet mempty
[ NamedVar (one $ StaticKey k) v (fromMaybe nullPos $ (`M.lookup` p) k)
Expand Down Expand Up @@ -423,7 +423,7 @@ printNix =
where
phi :: NValue' t f m Text -> Text
phi (NVConstant' a ) = atomText a
phi (NVStr' ns) = "\"" <> escapeString (ignoreContext ns) <> "\""
phi (NVStr' ns) = "\"" <> escapeString (getStringIgnoreContext ns) <> "\""
phi (NVList' l ) = "[ " <> unwords l <> " ]"
phi (NVSet' _ s) =
"{ " <>
Expand Down
Loading