diff --git a/src/main/java/com/google/devtools/build/lib/packages/StarlarkLibrary.java b/src/main/java/com/google/devtools/build/lib/packages/StarlarkLibrary.java index 7462726a44eb8b..7ebde67a035104 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/StarlarkLibrary.java +++ b/src/main/java/com/google/devtools/build/lib/packages/StarlarkLibrary.java @@ -93,6 +93,7 @@ static final class Proto implements StarlarkValue { + "The data structure must be recursively composed of strings, ints, floats, or" + " bools, or structs, sequences, and dicts of these types.\n" + "

A struct is converted to a message. Fields are emitted in name order.\n" + + "Each struct field whose value is None is ignored.\n" + "

A sequence (such as a list or tuple) is converted to a repeated field.\n" + "Its elements must not be sequences or dicts.\n" + "

A dict is converted to a repeated field of messages with fields named 'key'" @@ -108,9 +109,10 @@ static final class Proto implements StarlarkValue { + "# field: 1\n" + "# field: 2\n" + "# field: 3\n\n" - + "proto.encode_text(struct(field='text'))\n" + + "proto.encode_text(struct(field='text', ignored_field=None))\n" + "# field: \"text\"\n\n" - + "proto.encode_text(struct(field=struct(inner_field='text')))\n" + + "proto.encode_text(struct(field=struct(inner_field='text'," + + " ignored_field=None)))\n" + "# field {\n" + "# inner_field: \"text\"\n" + "# }\n\n" @@ -206,6 +208,9 @@ private void field(String name, Object v) throws EvalException { } // non-repeated field + if (v == Starlark.NONE) { + return; + } fieldElement(name, v); } diff --git a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleClassFunctionsTest.java b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleClassFunctionsTest.java index 08f295a75e6d1b..40d88f269c2c5f 100644 --- a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleClassFunctionsTest.java +++ b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleClassFunctionsTest.java @@ -1321,6 +1321,16 @@ public void testSimpleTextMessages() throws Exception { "}"); } + @Test + public void testNoneStructValue() throws Exception { + checkTextMessage( + "proto.encode_text(struct(a=1, b=None, nested=struct(c=2, d=None)))", + "a: 1", + "nested {", + " c: 2", + "}"); + } + @Test public void testProtoFieldsOrder() throws Exception { checkTextMessage("struct(d=4, b=2, c=3, a=1).to_proto()", "a: 1", "b: 2", "c: 3", "d: 4");