Skip to content

Commit 88346f1

Browse files
Googlercopybara-github
authored andcommitted
Change proto.encode_text() to omit struct fields with None values.
```bzl # omit 'bar' field proto.encode_text(struct(foo=1, bar=None)) ``` PiperOrigin-RevId: 511759002 Change-Id: I65034b9da59039429fb55f2a3205f98d47ad38f0
1 parent 9961a75 commit 88346f1

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/main/java/com/google/devtools/build/lib/packages/StarlarkLibrary.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ static final class Proto implements StarlarkValue {
9393
+ "The data structure must be recursively composed of strings, ints, floats, or"
9494
+ " bools, or structs, sequences, and dicts of these types.\n"
9595
+ "<p>A struct is converted to a message. Fields are emitted in name order.\n"
96+
+ "Each struct field whose value is None is ignored.\n"
9697
+ "<p>A sequence (such as a list or tuple) is converted to a repeated field.\n"
9798
+ "Its elements must not be sequences or dicts.\n"
9899
+ "<p>A dict is converted to a repeated field of messages with fields named 'key'"
@@ -108,9 +109,10 @@ static final class Proto implements StarlarkValue {
108109
+ "# field: 1\n"
109110
+ "# field: 2\n"
110111
+ "# field: 3\n\n"
111-
+ "proto.encode_text(struct(field='text'))\n"
112+
+ "proto.encode_text(struct(field='text', ignored_field=None))\n"
112113
+ "# field: \"text\"\n\n"
113-
+ "proto.encode_text(struct(field=struct(inner_field='text')))\n"
114+
+ "proto.encode_text(struct(field=struct(inner_field='text',"
115+
+ " ignored_field=None)))\n"
114116
+ "# field {\n"
115117
+ "# inner_field: \"text\"\n"
116118
+ "# }\n\n"
@@ -206,6 +208,9 @@ private void field(String name, Object v) throws EvalException {
206208
}
207209

208210
// non-repeated field
211+
if (v == Starlark.NONE) {
212+
return;
213+
}
209214
fieldElement(name, v);
210215
}
211216

src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleClassFunctionsTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,16 @@ public void testSimpleTextMessages() throws Exception {
13211321
"}");
13221322
}
13231323

1324+
@Test
1325+
public void testNoneStructValue() throws Exception {
1326+
checkTextMessage(
1327+
"proto.encode_text(struct(a=1, b=None, nested=struct(c=2, d=None)))",
1328+
"a: 1",
1329+
"nested {",
1330+
" c: 2",
1331+
"}");
1332+
}
1333+
13241334
@Test
13251335
public void testProtoFieldsOrder() throws Exception {
13261336
checkTextMessage("struct(d=4, b=2, c=3, a=1).to_proto()", "a: 1", "b: 2", "c: 3", "d: 4");

0 commit comments

Comments
 (0)