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

Deduplicate string literals. #1355

Merged
merged 7 commits into from
Mar 2, 2024

Conversation

BillyONeal
Copy link
Member

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.

…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.
Copy link
Contributor

@ras0219-msft ras0219-msft left a 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
args.parser.parse_option(JsonIdManifestRoot, StabilityTag::Experimental, args.manifest_root_dir);
args.parser.parse_option(SwitchManifestRoot, StabilityTag::Experimental, args.manifest_root_dir);

RegistryConfigDeserializer::NAME,
RegistryConfigDeserializer::LOCATION,
};
JsonIdKind, JsonIdBaseline, JsonIdPath, JsonIdRepository, JsonIdReference, JsonIdName, JsonIdLocation};
Copy link
Contributor

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)
Copy link
Contributor

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)

Copy link
Member Author

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);
Copy link
Contributor

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 StringLiterals?

Suggested change
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{});

Copy link
Member Author

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)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See also #1362

@@ -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);
Copy link
Contributor

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped the id :)

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));
Copy link
Contributor

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)
@BillyONeal BillyONeal merged commit 05f88ed into microsoft:main Mar 2, 2024
5 checks passed
@BillyONeal BillyONeal deleted the deduplicate-string-literals branch March 2, 2024 02:35
BillyONeal added a commit to BillyONeal/vcpkg-tool that referenced this pull request Mar 2, 2024
Followup to microsoft#1355

Also renames the JSON documents that talk to tests and to artifacts to follow the convention used elsewhere.
@Thomas1664
Copy link
Contributor

Thomas1664 commented Mar 18, 2024

@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 .to_string() to make it work which adds clutter. Furthermore, this causes merge conflicts with every open PR. I would argue this actually causes more typos by picking the wrong string literal.

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants