-
Notifications
You must be signed in to change notification settings - Fork 272
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
Deduplicate string literals. #1355
Deduplicate string literals. #1355
Conversation
…hese constants being separately declared variables is to avoid typos. By that standard, they should not be declared wherever they are used, they should be declared in one place and reused. This change does that. See also microsoft#1329 (comment) Note that the json constants in the recursive environment blob were renamed to be consistent with other JSON identifiers, but because this is our exe communicating with the same exe this is OK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Through commands.add.cpp and From commands.z-extract.cpp)
StabilityTag::Standard, | ||
args.host_triplet, | ||
msg::format(msgSpecifyHostArch, msg::env_var = format_environment_variable("VCPKG_DEFAULT_HOST_TRIPLET"))); | ||
args.parser.parse_option(MANIFEST_ROOT_DIR_ARG, StabilityTag::Experimental, args.manifest_root_dir); | ||
args.parser.parse_option(BUILDTREES_ROOT_DIR_ARG, | ||
args.parser.parse_option(JsonIdManifestRoot, StabilityTag::Experimental, args.manifest_root_dir); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
args.parser.parse_option(JsonIdManifestRoot, StabilityTag::Experimental, args.manifest_root_dir); | |
args.parser.parse_option(SwitchManifestRoot, StabilityTag::Experimental, args.manifest_root_dir); |
src/vcpkg/configuration.cpp
Outdated
RegistryConfigDeserializer::NAME, | ||
RegistryConfigDeserializer::LOCATION, | ||
}; | ||
JsonIdKind, JsonIdBaseline, JsonIdPath, JsonIdRepository, JsonIdReference, JsonIdName, JsonIdLocation}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit suggestion: trailing comma.
|
||
if (kind == KIND_BUILTIN) | ||
if (kind == JsonIdBuiltin) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consideration: should these semantically be JsonValueBuiltin
instead, since they aren't keys? (As-is also LGTM)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I originally had JsonFieldXxx
but this convinced me to do JsonId
instead because they were sometimes values.
}; | ||
|
||
const std::string empty; | ||
for (auto&& kv : VCPKG_OPTIONS) | ||
{ | ||
const std::string& variable_value = Util::value_or_default(cmakevars, kv.first, empty); | ||
const auto& variable_value = Util::value_or_default(cmakevars, kv.first.to_string(), empty); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The lack of types makes me uncertain about temporary lifetime extension etc. Perhaps this can change to simply be StringLiteral
s?
const auto& variable_value = Util::value_or_default(cmakevars, kv.first.to_string(), empty); | |
StringLiteral variable_value = Util::value_or_default(cmakevars, kv.first, StringLiteral{}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not possible because cmakevars
is const std::unordered_map<std::string, std::string>&
, and unordered_map
is not transparent. (If it were std::map<std::string, std::string, std::less<>>
we could)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See also #1362
src/vcpkg/commands.build.cpp
Outdated
@@ -1165,8 +1141,8 @@ namespace vcpkg | |||
const auto& triplet_abi = paths.get_triplet_info(pre_build_info, toolset); | |||
abi_info.triplet_abi.emplace(triplet_abi); | |||
const auto& triplet_canonical_name = action.spec.triplet().canonical_name(); | |||
abi_tag_entries.emplace_back("triplet", triplet_canonical_name); | |||
abi_tag_entries.emplace_back("triplet_abi", triplet_abi); | |||
abi_tag_entries.emplace_back(AbiTagIdTriplet, triplet_canonical_name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are these AbiTagIdXYZ
but below we have AbiTagCMake
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dropped the id :)
src/vcpkg/commands.set-installed.cpp
Outdated
detector.insert("name", Json::Value::string("vcpkg")); | ||
detector.insert("url", Json::Value::string("https://github.com/microsoft/vcpkg")); | ||
detector.insert("version", Json::Value::string("1.0.0")); | ||
detector.insert(JsonIdName, Json::Value::string(JsonIdVcpkg)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JsonIdVcpkg
in this case seems more like an "unrelated constant" like the URL and version below.
…terals # Conflicts: # src/vcpkg/commands.add-version.cpp # src/vcpkg/commands.set-installed.cpp # src/vcpkg/registries.cpp # src/vcpkg/sourceparagraph.cpp # src/vcpkg/versiondeserializers.cpp
--------------------------------------------- D:\vcpkg-tool\include\vcpkg\base\contractual-constants.h(438, 36) D:\vcpkg-tool\src\vcpkg\commands.build.cpp(1138, 38) Renamed 'AbiTagIdTripletAbi' to 'AbiTagTripletAbi': --------------------------------------------------- D:\vcpkg-tool\include\vcpkg\base\contractual-constants.h(439, 36) D:\vcpkg-tool\src\vcpkg\commands.build.cpp(1139, 38)
Followup to microsoft#1355 Also renames the JSON documents that talk to tests and to artifacts to follow the convention used elsewhere.
@BillyONeal IMO the possible benefit of avoiding typos isn't worth it: By making string literals a concrete type, they can't easily be fed into things expecting strings. One has to use I think you should focus on more important tasks rather than making life harder for everyone contributing (assuming making life harder for everyone isn't an important task for you). |
In discussion with @ras0219-msft, Robert argues that the purpose of these constants being separately declared variables is to avoid typos. By that standard, they should not be declared wherever they are used, they should be declared in one place and reused. This change does that.
See also #1329 (comment)
Note that the json constants in the recursive environment blob were renamed to be consistent with other JSON identifiers, but because this is our exe communicating with the same exe this is OK.