Introduce OutputName and OutputNameView type aliases

Hopefully they make the code easier to understand!
This commit is contained in:
John Ericson 2023-08-25 09:53:12 -04:00
parent 0a6ac133cf
commit 2f5d3da806
11 changed files with 45 additions and 33 deletions

View file

@ -1843,7 +1843,7 @@ static void prim_outputOf(EvalState & state, const PosIdx pos, Value * * args, V
{ {
SingleDerivedPath drvPath = state.coerceToSingleDerivedPath(pos, *args[0], "while evaluating the first argument to builtins.outputOf"); SingleDerivedPath drvPath = state.coerceToSingleDerivedPath(pos, *args[0], "while evaluating the first argument to builtins.outputOf");
std::string_view outputName = state.forceStringNoCtx(*args[1], pos, "while evaluating the second argument to builtins.outputOf"); OutputNameView outputName = state.forceStringNoCtx(*args[1], pos, "while evaluating the second argument to builtins.outputOf");
state.mkSingleDerivedPathString( state.mkSingleDerivedPathString(
SingleDerivedPath::Built { SingleDerivedPath::Built {

View file

@ -2955,7 +2955,7 @@ bool LocalDerivationGoal::isReadDesc(int fd)
} }
StorePath LocalDerivationGoal::makeFallbackPath(std::string_view outputName) StorePath LocalDerivationGoal::makeFallbackPath(OutputNameView outputName)
{ {
return worker.store.makeStorePath( return worker.store.makeStorePath(
"rewrite:" + std::string(drvPath.to_string()) + ":name:" + std::string(outputName), "rewrite:" + std::string(drvPath.to_string()) + ":name:" + std::string(outputName),

View file

@ -297,7 +297,7 @@ struct LocalDerivationGoal : public DerivationGoal
* @todo Add option to randomize, so we can audit whether our * @todo Add option to randomize, so we can audit whether our
* rewrites caught everything * rewrites caught everything
*/ */
StorePath makeFallbackPath(std::string_view outputName); StorePath makeFallbackPath(OutputNameView outputName);
}; };
} }

View file

@ -12,7 +12,7 @@
namespace nix { namespace nix {
std::optional<StorePath> DerivationOutput::path(const Store & store, std::string_view drvName, std::string_view outputName) const std::optional<StorePath> DerivationOutput::path(const Store & store, std::string_view drvName, OutputNameView outputName) const
{ {
return std::visit(overloaded { return std::visit(overloaded {
[](const DerivationOutput::InputAddressed & doi) -> std::optional<StorePath> { [](const DerivationOutput::InputAddressed & doi) -> std::optional<StorePath> {
@ -36,7 +36,7 @@ std::optional<StorePath> DerivationOutput::path(const Store & store, std::string
} }
StorePath DerivationOutput::CAFixed::path(const Store & store, std::string_view drvName, std::string_view outputName) const StorePath DerivationOutput::CAFixed::path(const Store & store, std::string_view drvName, OutputNameView outputName) const
{ {
return store.makeFixedOutputPathFromCA( return store.makeFixedOutputPathFromCA(
outputPathName(drvName, outputName), outputPathName(drvName, outputName),
@ -466,7 +466,7 @@ bool isDerivation(std::string_view fileName)
} }
std::string outputPathName(std::string_view drvName, std::string_view outputName) { std::string outputPathName(std::string_view drvName, OutputNameView outputName) {
std::string res { drvName }; std::string res { drvName };
if (outputName != "out") { if (outputName != "out") {
res += "-"; res += "-";
@ -810,7 +810,7 @@ void writeDerivation(Sink & out, const Store & store, const BasicDerivation & dr
} }
std::string hashPlaceholder(const std::string_view outputName) std::string hashPlaceholder(const OutputNameView outputName)
{ {
// FIXME: memoize? // FIXME: memoize?
return "/" + hashString(htSHA256, concatStrings("nix-output:", outputName)).to_string(Base32, false); return "/" + hashString(htSHA256, concatStrings("nix-output:", outputName)).to_string(Base32, false);
@ -963,7 +963,7 @@ void Derivation::checkInvariants(Store & store, const StorePath & drvPath) const
const Hash impureOutputHash = hashString(htSHA256, "impure"); const Hash impureOutputHash = hashString(htSHA256, "impure");
nlohmann::json DerivationOutput::toJSON( nlohmann::json DerivationOutput::toJSON(
const Store & store, std::string_view drvName, std::string_view outputName) const const Store & store, std::string_view drvName, OutputNameView outputName) const
{ {
nlohmann::json res = nlohmann::json::object(); nlohmann::json res = nlohmann::json::object();
std::visit(overloaded { std::visit(overloaded {
@ -990,7 +990,7 @@ nlohmann::json DerivationOutput::toJSON(
DerivationOutput DerivationOutput::fromJSON( DerivationOutput DerivationOutput::fromJSON(
const Store & store, std::string_view drvName, std::string_view outputName, const Store & store, std::string_view drvName, OutputNameView outputName,
const nlohmann::json & _json, const nlohmann::json & _json,
const ExperimentalFeatureSettings & xpSettings) const ExperimentalFeatureSettings & xpSettings)
{ {

View file

@ -55,7 +55,7 @@ struct DerivationOutput
* @param drvName The name of the derivation this is an output of, without the `.drv`. * @param drvName The name of the derivation this is an output of, without the `.drv`.
* @param outputName The name of this output. * @param outputName The name of this output.
*/ */
StorePath path(const Store & store, std::string_view drvName, std::string_view outputName) const; StorePath path(const Store & store, std::string_view drvName, OutputNameView outputName) const;
GENERATE_CMP(CAFixed, me->ca); GENERATE_CMP(CAFixed, me->ca);
}; };
@ -132,19 +132,19 @@ struct DerivationOutput
* the safer interface provided by * the safer interface provided by
* BasicDerivation::outputsAndOptPaths * BasicDerivation::outputsAndOptPaths
*/ */
std::optional<StorePath> path(const Store & store, std::string_view drvName, std::string_view outputName) const; std::optional<StorePath> path(const Store & store, std::string_view drvName, OutputNameView outputName) const;
nlohmann::json toJSON( nlohmann::json toJSON(
const Store & store, const Store & store,
std::string_view drvName, std::string_view drvName,
std::string_view outputName) const; OutputNameView outputName) const;
/** /**
* @param xpSettings Stop-gap to avoid globals during unit tests. * @param xpSettings Stop-gap to avoid globals during unit tests.
*/ */
static DerivationOutput fromJSON( static DerivationOutput fromJSON(
const Store & store, const Store & store,
std::string_view drvName, std::string_view drvName,
std::string_view outputName, OutputNameView outputName,
const nlohmann::json & json, const nlohmann::json & json,
const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings); const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
}; };
@ -405,7 +405,7 @@ bool isDerivation(std::string_view fileName);
* This is usually <drv-name>-<output-name>, but is just <drv-name> when * This is usually <drv-name>-<output-name>, but is just <drv-name> when
* the output name is "out". * the output name is "out".
*/ */
std::string outputPathName(std::string_view drvName, std::string_view outputName); std::string outputPathName(std::string_view drvName, OutputNameView outputName);
/** /**
@ -499,7 +499,7 @@ void writeDerivation(Sink & out, const Store & store, const BasicDerivation & dr
* own outputs without needing to use the hash of a derivation in * own outputs without needing to use the hash of a derivation in
* itself, making the hash near-impossible to calculate. * itself, making the hash near-impossible to calculate.
*/ */
std::string hashPlaceholder(const std::string_view outputName); std::string hashPlaceholder(const OutputNameView outputName);
extern const Hash impureOutputHash; extern const Hash impureOutputHash;

View file

@ -167,7 +167,7 @@ void drvRequireExperiment(
SingleDerivedPath::Built SingleDerivedPath::Built::parse( SingleDerivedPath::Built SingleDerivedPath::Built::parse(
const Store & store, ref<SingleDerivedPath> drv, const Store & store, ref<SingleDerivedPath> drv,
std::string_view output, OutputNameView output,
const ExperimentalFeatureSettings & xpSettings) const ExperimentalFeatureSettings & xpSettings)
{ {
drvRequireExperiment(*drv, xpSettings); drvRequireExperiment(*drv, xpSettings);
@ -179,7 +179,7 @@ SingleDerivedPath::Built SingleDerivedPath::Built::parse(
DerivedPath::Built DerivedPath::Built::parse( DerivedPath::Built DerivedPath::Built::parse(
const Store & store, ref<SingleDerivedPath> drv, const Store & store, ref<SingleDerivedPath> drv,
std::string_view outputsS, OutputNameView outputsS,
const ExperimentalFeatureSettings & xpSettings) const ExperimentalFeatureSettings & xpSettings)
{ {
drvRequireExperiment(*drv, xpSettings); drvRequireExperiment(*drv, xpSettings);

View file

@ -42,7 +42,7 @@ struct SingleDerivedPath;
*/ */
struct SingleDerivedPathBuilt { struct SingleDerivedPathBuilt {
ref<SingleDerivedPath> drvPath; ref<SingleDerivedPath> drvPath;
std::string output; OutputName output;
/** /**
* Get the store path this is ultimately derived from (by realising * Get the store path this is ultimately derived from (by realising
@ -71,7 +71,7 @@ struct SingleDerivedPathBuilt {
*/ */
static SingleDerivedPathBuilt parse( static SingleDerivedPathBuilt parse(
const Store & store, ref<SingleDerivedPath> drvPath, const Store & store, ref<SingleDerivedPath> drvPath,
std::string_view outputs, OutputNameView outputs,
const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings); const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
nlohmann::json toJSON(Store & store) const; nlohmann::json toJSON(Store & store) const;

View file

@ -11,7 +11,7 @@ std::string DownstreamPlaceholder::render() const
DownstreamPlaceholder DownstreamPlaceholder::unknownCaOutput( DownstreamPlaceholder DownstreamPlaceholder::unknownCaOutput(
const StorePath & drvPath, const StorePath & drvPath,
std::string_view outputName, OutputNameView outputName,
const ExperimentalFeatureSettings & xpSettings) const ExperimentalFeatureSettings & xpSettings)
{ {
xpSettings.require(Xp::CaDerivations); xpSettings.require(Xp::CaDerivations);
@ -25,7 +25,7 @@ DownstreamPlaceholder DownstreamPlaceholder::unknownCaOutput(
DownstreamPlaceholder DownstreamPlaceholder::unknownDerivation( DownstreamPlaceholder DownstreamPlaceholder::unknownDerivation(
const DownstreamPlaceholder & placeholder, const DownstreamPlaceholder & placeholder,
std::string_view outputName, OutputNameView outputName,
const ExperimentalFeatureSettings & xpSettings) const ExperimentalFeatureSettings & xpSettings)
{ {
xpSettings.require(Xp::DynamicDerivations); xpSettings.require(Xp::DynamicDerivations);

View file

@ -58,7 +58,7 @@ public:
*/ */
static DownstreamPlaceholder unknownCaOutput( static DownstreamPlaceholder unknownCaOutput(
const StorePath & drvPath, const StorePath & drvPath,
std::string_view outputName, OutputNameView outputName,
const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings); const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
/** /**
@ -72,7 +72,7 @@ public:
*/ */
static DownstreamPlaceholder unknownDerivation( static DownstreamPlaceholder unknownDerivation(
const DownstreamPlaceholder & drvPlaceholder, const DownstreamPlaceholder & drvPlaceholder,
std::string_view outputName, OutputNameView outputName,
const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings); const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
/** /**

View file

@ -13,24 +13,36 @@
namespace nix { namespace nix {
/**
* An (owned) output name. Just a type alias used to make code more
* readible.
*/
typedef std::string OutputName;
/**
* A borrowed output name. Just a type alias used to make code more
* readible.
*/
typedef std::string_view OutputNameView;
struct OutputsSpec { struct OutputsSpec {
/** /**
* A non-empty set of outputs, specified by name * A non-empty set of outputs, specified by name
*/ */
struct Names : std::set<std::string> { struct Names : std::set<OutputName> {
using std::set<std::string>::set; using std::set<OutputName>::set;
/* These need to be "inherited manually" */ /* These need to be "inherited manually" */
Names(const std::set<std::string> & s) Names(const std::set<OutputName> & s)
: std::set<std::string>(s) : std::set<OutputName>(s)
{ assert(!empty()); } { assert(!empty()); }
/** /**
* Needs to be "inherited manually" * Needs to be "inherited manually"
*/ */
Names(std::set<std::string> && s) Names(std::set<OutputName> && s)
: std::set<std::string>(s) : std::set<OutputName>(s)
{ assert(!empty()); } { assert(!empty()); }
/* This set should always be non-empty, so we delete this /* This set should always be non-empty, so we delete this
@ -57,7 +69,7 @@ struct OutputsSpec {
*/ */
OutputsSpec() = delete; OutputsSpec() = delete;
bool contains(const std::string & output) const; bool contains(const OutputName & output) const;
/** /**
* Create a new OutputsSpec which is the union of this and that. * Create a new OutputsSpec which is the union of this and that.

View file

@ -34,7 +34,7 @@ struct DrvOutput {
/** /**
* The name of the output. * The name of the output.
*/ */
std::string outputName; OutputName outputName;
std::string to_string() const; std::string to_string() const;
@ -84,7 +84,7 @@ struct Realisation {
* Since these are the outputs of a single derivation, we know the * Since these are the outputs of a single derivation, we know the
* output names are unique so we can use them as the map key. * output names are unique so we can use them as the map key.
*/ */
typedef std::map<std::string, Realisation> SingleDrvOutputs; typedef std::map<OutputName, Realisation> SingleDrvOutputs;
/** /**
* Collection type for multiple derivations' outputs' `Realisation`s. * Collection type for multiple derivations' outputs' `Realisation`s.
@ -146,7 +146,7 @@ public:
MissingRealisation(DrvOutput & outputId) MissingRealisation(DrvOutput & outputId)
: MissingRealisation(outputId.outputName, outputId.strHash()) : MissingRealisation(outputId.outputName, outputId.strHash())
{} {}
MissingRealisation(std::string_view drv, std::string outputName) MissingRealisation(std::string_view drv, OutputName outputName)
: Error( "cannot operate on output '%s' of the " : Error( "cannot operate on output '%s' of the "
"unbuilt derivation '%s'", "unbuilt derivation '%s'",
outputName, outputName,