Skip to content

Commit a94383f

Browse files
chiragramanicopybara-github
authored andcommitted
Add jsonproto option to query --output flag
Addresses #17627 Closes #18187. PiperOrigin-RevId: 530859727 Change-Id: Ie12e6c783448def4b891055647cfe568f7b71b14
1 parent 96517c5 commit a94383f

File tree

5 files changed

+79
-3
lines changed

5 files changed

+79
-3
lines changed

src/main/java/com/google/devtools/build/lib/query2/query/output/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ java_library(
3838
"//third_party:guava",
3939
"//third_party:jsr305",
4040
"//third_party/protobuf:protobuf_java",
41+
"//third_party/protobuf:protobuf_java_util",
4142
],
4243
)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2023 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package com.google.devtools.build.lib.query2.query.output;
15+
16+
import com.google.devtools.build.lib.cmdline.RepositoryMapping;
17+
import com.google.devtools.build.lib.packages.Target;
18+
import com.google.devtools.build.lib.query2.engine.OutputFormatterCallback;
19+
import com.google.protobuf.util.JsonFormat;
20+
import java.io.IOException;
21+
import java.io.OutputStream;
22+
import java.nio.charset.StandardCharsets;
23+
24+
/**
25+
* An output formatter that outputs a protocol buffer json representation of a query result and
26+
* outputs the json to the output print stream.
27+
*/
28+
public class JSONProtoOutputFormatter extends ProtoOutputFormatter {
29+
@Override
30+
public String getName() {
31+
return "jsonproto";
32+
}
33+
34+
private final JsonFormat.Printer jsonPrinter = JsonFormat.printer();
35+
36+
@Override
37+
public OutputFormatterCallback<Target> createPostFactoStreamCallback(
38+
final OutputStream out, final QueryOptions options, RepositoryMapping mainRepoMapping) {
39+
return new OutputFormatterCallback<Target>() {
40+
@Override
41+
public void processOutput(Iterable<Target> partialResult)
42+
throws IOException, InterruptedException {
43+
for (Target target : partialResult) {
44+
out.write(
45+
jsonPrinter.print(toTargetProtoBuffer(target)).getBytes(StandardCharsets.UTF_8));
46+
}
47+
}
48+
};
49+
}
50+
}

src/main/java/com/google/devtools/build/lib/query2/query/output/OutputFormatters.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public static ImmutableList<OutputFormatter> getDefaultFormatters() {
3737
new PackageOutputFormatter(),
3838
new LocationOutputFormatter(),
3939
new GraphOutputFormatter(),
40+
new JSONProtoOutputFormatter(),
4041
new XmlOutputFormatter(),
4142
new ProtoOutputFormatter(),
4243
new StreamedProtoOutputFormatter());

src/main/java/com/google/devtools/build/lib/query2/query/output/QueryOptions.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ public OrderOutputConverter() {
3939
documentationCategory = OptionDocumentationCategory.QUERY,
4040
effectTags = {OptionEffectTag.TERMINAL_OUTPUT},
4141
help =
42-
"The format in which the query results should be printed. Allowed values for query are: "
43-
+ "build, graph, label, label_kind, location, maxrank, minrank, package, proto, xml.")
42+
"The format in which the query results should be printed. Allowed values for query are:"
43+
+ " build, graph, jsonproto, label, label_kind, location, maxrank, minrank, package,"
44+
+ " proto, xml.")
4445
public String outputFormat;
4546

4647
@Option(

src/test/shell/integration/bazel_query_test.sh

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ genquery(name='q',
668668
opts = ["--output=blargh"],)
669669
EOF
670670

671-
local expected_error_msg="in genquery rule //starfruit:q: Invalid output format 'blargh'. Valid values are: label, label_kind, build, minrank, maxrank, package, location, graph, xml, proto"
671+
local expected_error_msg="in genquery rule //starfruit:q: Invalid output format 'blargh'. Valid values are: label, label_kind, build, minrank, maxrank, package, location, graph, jsonproto, xml, proto"
672672
bazel build //starfruit:q >& $TEST_log && fail "Expected failure"
673673
expect_log "$expected_error_msg"
674674
}
@@ -1098,4 +1098,27 @@ EOF
10981098
expect_log "//pkg3:t4"
10991099
}
11001100

1101+
function test_basic_query_jsonproto() {
1102+
local pkg="${FUNCNAME[0]}"
1103+
mkdir -p "$pkg" || fail "mkdir -p $pkg"
1104+
cat > "$pkg/BUILD" <<'EOF'
1105+
genrule(
1106+
name = "bar",
1107+
srcs = ["dummy.txt"],
1108+
outs = ["bar_out.txt"],
1109+
cmd = "echo unused > $(OUTS)",
1110+
)
1111+
EOF
1112+
bazel query --output=jsonproto --noimplicit_deps "//$pkg:bar" > output 2> "$TEST_log" \
1113+
|| fail "Expected success"
1114+
cat output >> "$TEST_log"
1115+
1116+
# Verify that the appropriate attributes were included.
1117+
assert_contains "\"ruleClass\": \"genrule\"" output
1118+
assert_contains "\"name\": \"//$pkg:bar\"" output
1119+
assert_contains "\"ruleInput\": \[\"//$pkg:dummy.txt\"\]" output
1120+
assert_contains "\"ruleOutput\": \[\"//$pkg:bar_out.txt\"\]" output
1121+
assert_contains "echo unused" output
1122+
}
1123+
11011124
run_suite "${PRODUCT_NAME} query tests"

0 commit comments

Comments
 (0)