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

fix: properly support spaces in string values #27

Merged
merged 2 commits into from
Feb 24, 2021
Merged

Conversation

alexander-fenster
Copy link
Contributor

Some values can have spaces in them. For such strings, buildozer print returns a "string in quotes", but to write them back, we "must\ quote\ spaces". Weird it is.

@alexander-fenster alexander-fenster requested a review from a team as a code owner February 24, 2021 00:39
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Feb 24, 2021
@viacheslav-rostovtsev
Copy link
Member

please just add comments everywhere explaining why this stuff is being done
somebody possibly you will look at if (value.charAt(0) == '"' && value.charAt(value.length() - 1) == '"') and be like 'but why?'

Copy link
Contributor

@vam-google vam-google left a comment

Choose a reason for hiding this comment

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

LGTM, with few minor comments

@@ -92,7 +95,8 @@ public String getAttribute(Path bazelBuildFile, String target, String attribute)
// immediately.
public void setAttribute(Path bazelBuildFile, String target, String attribute, String value)
throws IOException {
execute(bazelBuildFile, String.format("set %s \"%s\"", attribute, value), target);
final String escapedValue = value.replace(" ", "\\ ");
execute(bazelBuildFile, String.format("set %s \"%s\"", attribute, escapedValue), target);
Copy link
Contributor

Choose a reason for hiding this comment

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

minor: just inlining replace call here would be just fine.

On a side note, it feels like setAttribute(), addAttribute(), batchSetAttribute(), batchAddAttribute() could become a one method, with an argument maybe.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You know what, we don't need non-batch methods at all. We don't use them anywhere so YAGNI. They are terribly slow since they execute buildozer for each invocation.

@@ -82,6 +82,9 @@ public String getAttribute(Path bazelBuildFile, String target, String attribute)
if (value.equals("(missing)")) {
return null;
}
if (value.charAt(0) == '"' && value.charAt(value.length() - 1) == '"') {
value = value.substring(1, value.length() - 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

This does not seem to be related to the spaces issue. How did this thing work before and why is it needed?
Minor: another lazy option of doign it coudl be value.startsWith("\"") && value.endsWith("\""), though the currenv version is more "performant" as it works directly with characters (pretending like it matters here...).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is related. I will add a comment :) The thing is:

fenster@fenster ws:~/googleapis$ buildozer 'print ruby_cloud_title' //google/api/servicecontrol/v1:servicecontrol_ruby_gapic
"Service Control API V1"
fenster@fenster ws:~/googleapis$ buildozer 'print name' //google/api/servicecontrol/v1:servicecontrol_ruby_gapic
servicecontrol_ruby_gapic

So it adds quotes if the string has spaces inside. We don't need those quotes, so I'm stripping them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added comment

@alexander-fenster alexander-fenster merged commit ff1d244 into main Feb 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants