Skip to content

Commit c3dc389

Browse files
c-mitacopybara-github
authored andcommitted
Remove --incompatible_struct_has_no_methods and associated struct methods.
Removes the disabled struct.to_json() and struct.to_proto() and the flag that could re-enable them. bazelbuild#19465 RELNOTES[inc]: Removed --incompatible_struct_has_no_methods PiperOrigin-RevId: 622844911 Change-Id: Ie6a9b42a0ea6f45bfca40bd59a4ea6163214bebc
1 parent a7c588c commit c3dc389

File tree

8 files changed

+0
-207
lines changed

8 files changed

+0
-207
lines changed

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

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,13 @@
1717
import com.google.common.base.Objects;
1818
import com.google.common.collect.Ordering;
1919
import com.google.devtools.build.lib.starlarkbuildapi.core.StructApi;
20-
import com.google.protobuf.TextFormat;
2120
import java.util.ArrayList;
2221
import java.util.Collections;
2322
import java.util.List;
24-
import java.util.Map;
2523
import javax.annotation.Nullable;
26-
import net.starlark.java.eval.Dict;
2724
import net.starlark.java.eval.EvalException;
2825
import net.starlark.java.eval.Printer;
2926
import net.starlark.java.eval.Starlark;
30-
import net.starlark.java.eval.StarlarkInt;
3127
import net.starlark.java.eval.Structure;
3228

3329
/**
@@ -147,87 +143,6 @@ private Object getValueOrNull(String name) {
147143
}
148144
}
149145

150-
@Override
151-
public String toProto() throws EvalException {
152-
return Proto.INSTANCE.encodeText(this);
153-
}
154-
155-
/**
156-
* Escapes the given string for use in proto/JSON string.
157-
*
158-
* <p>This escapes double quotes, backslashes, and newlines.
159-
*/
160-
private static String escapeDoubleQuotesAndBackslashesAndNewlines(String string) {
161-
return TextFormat.escapeDoubleQuotesAndBackslashes(string).replace("\n", "\\n");
162-
}
163-
164-
@Override
165-
public String toJson() throws EvalException {
166-
StringBuilder sb = new StringBuilder();
167-
printJson(this, sb, "struct field", null);
168-
return sb.toString();
169-
}
170-
171-
private static void printJson(Object value, StringBuilder sb, String container, String key)
172-
throws EvalException {
173-
if (value == Starlark.NONE) {
174-
sb.append("null");
175-
} else if (value instanceof Structure) {
176-
sb.append("{");
177-
178-
String join = "";
179-
for (String field : ((Structure) value).getFieldNames()) {
180-
sb.append(join);
181-
join = ",";
182-
appendJSONStringLiteral(sb, field);
183-
sb.append(':');
184-
printJson(((Structure) value).getValue(field), sb, "struct field", field);
185-
}
186-
sb.append("}");
187-
} else if (value instanceof Dict) {
188-
sb.append("{");
189-
String join = "";
190-
for (Map.Entry<?, ?> entry : ((Dict<?, ?>) value).entrySet()) {
191-
sb.append(join);
192-
join = ",";
193-
if (!(entry.getKey() instanceof String)) {
194-
throw Starlark.errorf(
195-
"Keys must be a string but got a %s for %s%s",
196-
Starlark.type(entry.getKey()), container, key != null ? " '" + key + "'" : "");
197-
}
198-
appendJSONStringLiteral(sb, (String) entry.getKey());
199-
sb.append(':');
200-
printJson(entry.getValue(), sb, "dict value", String.valueOf(entry.getKey()));
201-
}
202-
sb.append("}");
203-
} else if (value instanceof List) {
204-
sb.append("[");
205-
String join = "";
206-
for (Object item : ((List<?>) value)) {
207-
sb.append(join);
208-
join = ",";
209-
printJson(item, sb, "list element in struct field", key);
210-
}
211-
sb.append("]");
212-
} else if (value instanceof String) {
213-
appendJSONStringLiteral(sb, (String) value);
214-
} else if (value instanceof StarlarkInt || value instanceof Boolean) {
215-
sb.append(value);
216-
} else {
217-
throw Starlark.errorf(
218-
"Invalid text format, expected a struct, a string, a bool, or an int but got a %s for"
219-
+ " %s%s",
220-
Starlark.type(value), container, key != null ? " '" + key + "'" : "");
221-
}
222-
}
223-
224-
private static void appendJSONStringLiteral(StringBuilder out, String s) {
225-
out.append('"');
226-
out.append(
227-
escapeDoubleQuotesAndBackslashesAndNewlines(s).replace("\r", "\\r").replace("\t", "\\t"));
228-
out.append('"');
229-
}
230-
231146
@Override
232147
public String toString() {
233148
return Starlark.repr(this);

src/main/java/com/google/devtools/build/lib/packages/semantics/BuildLanguageOptions.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -378,18 +378,6 @@ public final class BuildLanguageOptions extends OptionsBase {
378378
+ " and 1 cpu.")
379379
public boolean experimentalActionResourceSet;
380380

381-
@Option(
382-
name = "incompatible_struct_has_no_methods",
383-
defaultValue = "true",
384-
documentationCategory = OptionDocumentationCategory.STARLARK_SEMANTICS,
385-
effectTags = {OptionEffectTag.BUILD_FILE_SEMANTICS},
386-
metadataTags = {OptionMetadataTag.INCOMPATIBLE_CHANGE},
387-
help =
388-
"Disables the to_json and to_proto methods of struct, which pollute the struct field"
389-
+ " namespace. Instead, use json.encode or json.encode_indent for JSON, or"
390-
+ " proto.encode_text for textproto.")
391-
public boolean incompatibleStructHasNoMethods;
392-
393381
@Option(
394382
name = "incompatible_always_check_depset_elements",
395383
defaultValue = "true",
@@ -798,7 +786,6 @@ public StarlarkSemantics toStarlarkSemantics() {
798786
.setBool(INCOMPATIBLE_NO_PACKAGE_DISTRIBS, incompatibleNoPackageDistribs)
799787
.setBool(INCOMPATIBLE_NO_RULE_OUTPUTS_PARAM, incompatibleNoRuleOutputsParam)
800788
.setBool(INCOMPATIBLE_RUN_SHELL_COMMAND_STRING, incompatibleRunShellCommandString)
801-
.setBool(INCOMPATIBLE_STRUCT_HAS_NO_METHODS, incompatibleStructHasNoMethods)
802789
.setBool(StarlarkSemantics.PRINT_TEST_MARKER, internalStarlarkFlagTestCanary)
803790
.setBool(
804791
INCOMPATIBLE_DO_NOT_SPLIT_LINKING_CMDLINE, incompatibleDoNotSplitLinkingCmdline)

src/main/java/com/google/devtools/build/lib/rules/java/JavaPluginInfo.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,6 @@ private JavaPluginData disableAnnotationProcessing() {
205205
// Preserve data, which may be used by Error Prone plugins.
206206
data());
207207
}
208-
209-
@Override
210-
public String toProto() throws EvalException {
211-
throw Starlark.errorf("unsupported method");
212-
}
213-
214-
@Override
215-
public String toJson() throws EvalException {
216-
throw Starlark.errorf("unsupported method");
217-
}
218208
}
219209

220210
public static JavaPluginInfo mergeWithoutJavaOutputs(JavaPluginInfo a, JavaPluginInfo b) {

src/main/java/com/google/devtools/build/lib/starlarkbuildapi/core/StructApi.java

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import com.google.devtools.build.docgen.annot.DocCategory;
1818
import com.google.devtools.build.docgen.annot.StarlarkConstructor;
19-
import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions;
2019
import net.starlark.java.annot.Param;
2120
import net.starlark.java.annot.StarlarkBuiltin;
2221
import net.starlark.java.annot.StarlarkMethod;
@@ -35,62 +34,6 @@
3534
+ "equal if they have the same fields and if corresponding field values are equal.")
3635
public interface StructApi extends StarlarkValue {
3736

38-
@StarlarkMethod(
39-
name = "to_proto",
40-
doc =
41-
"Creates a text message from the struct parameter. This method only works if all "
42-
+ "struct elements (recursively) are strings, ints, booleans, "
43-
+ "other structs or dicts or lists of these types. "
44-
+ "Quotes and new lines in strings are escaped. "
45-
+ "Struct keys are iterated in the sorted order. "
46-
+ "Examples:<br><pre class=language-python>"
47-
+ "struct(key=123).to_proto()\n# key: 123\n\n"
48-
+ "struct(key=True).to_proto()\n# key: true\n\n"
49-
+ "struct(key=[1, 2, 3]).to_proto()\n# key: 1\n# key: 2\n# key: 3\n\n"
50-
+ "struct(key='text').to_proto()\n# key: \"text\"\n\n"
51-
+ "struct(key=struct(inner_key='text')).to_proto()\n"
52-
+ "# key {\n# inner_key: \"text\"\n# }\n\n"
53-
+ "struct(key=[struct(inner_key=1), struct(inner_key=2)]).to_proto()\n"
54-
+ "# key {\n# inner_key: 1\n# }\n# key {\n# inner_key: 2\n# }\n\n"
55-
+ "struct(key=struct(inner_key=struct(inner_inner_key='text'))).to_proto()\n"
56-
+ "# key {\n# inner_key {\n# inner_inner_key: \"text\"\n# }\n# }\n\n"
57-
+ "struct(foo={4: 3, 2: 1}).to_proto()\n"
58-
+ "# foo: {\n"
59-
+ "# key: 4\n"
60-
+ "# value: 3\n"
61-
+ "# }\n"
62-
+ "# foo: {\n"
63-
+ "# key: 2\n"
64-
+ "# value: 1\n"
65-
+ "# }\n"
66-
+ "</pre>"
67-
+ "<p>Deprecated: use proto.encode_text(x) instead.",
68-
disableWithFlag = BuildLanguageOptions.INCOMPATIBLE_STRUCT_HAS_NO_METHODS)
69-
String toProto() throws EvalException;
70-
71-
@StarlarkMethod(
72-
name = "to_json",
73-
doc =
74-
"Creates a JSON string from the struct parameter. This method only works if all "
75-
+ "struct elements (recursively) are strings, ints, booleans, other structs, a "
76-
+ "list of these types or a dictionary with string keys and values of these types. "
77-
+ "Quotes and new lines in strings are escaped. "
78-
+ "Examples:<br><pre class=language-python>"
79-
+ "struct(key=123).to_json()\n# {\"key\":123}\n\n"
80-
+ "struct(key=True).to_json()\n# {\"key\":true}\n\n"
81-
+ "struct(key=[1, 2, 3]).to_json()\n# {\"key\":[1,2,3]}\n\n"
82-
+ "struct(key='text').to_json()\n# {\"key\":\"text\"}\n\n"
83-
+ "struct(key=struct(inner_key='text')).to_json()\n"
84-
+ "# {\"key\":{\"inner_key\":\"text\"}}\n\n"
85-
+ "struct(key=[struct(inner_key=1), struct(inner_key=2)]).to_json()\n"
86-
+ "# {\"key\":[{\"inner_key\":1},{\"inner_key\":2}]}\n\n"
87-
+ "struct(key=struct(inner_key=struct(inner_inner_key='text'))).to_json()\n"
88-
+ "# {\"key\":{\"inner_key\":{\"inner_inner_key\":\"text\"}}}\n</pre>."
89-
+ "<p>Deprecated: instead, use json.encode(x) or json.encode_indent(x), which work"
90-
+ " for values other than structs and do not pollute the struct field namespace. ",
91-
disableWithFlag = BuildLanguageOptions.INCOMPATIBLE_STRUCT_HAS_NO_METHODS)
92-
String toJson() throws EvalException;
93-
9437
/** Callable Provider for new struct objects. */
9538
@StarlarkBuiltin(name = "Provider", documented = false, doc = "")
9639
interface StructProviderApi extends ProviderApi {

src/main/java/com/google/devtools/build/lib/starlarkbuildapi/java/JavaOutputApi.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import com.google.devtools.build.lib.starlarkbuildapi.core.StructApi;
2020
import javax.annotation.Nullable;
2121
import net.starlark.java.annot.StarlarkMethod;
22-
import net.starlark.java.eval.EvalException;
23-
import net.starlark.java.eval.Starlark;
2422
import net.starlark.java.eval.StarlarkSemantics;
2523

2624
/** A tuple of a java classes jar and its associated source and interface archives. */
@@ -118,14 +116,4 @@ public interface JavaOutputApi<FileT extends FileApi> extends StructApi {
118116
structField = true)
119117
@Nullable
120118
Depset getSrcJarsStarlark(StarlarkSemantics semantics);
121-
122-
@Override
123-
default String toProto() throws EvalException {
124-
throw Starlark.errorf("unsupported method");
125-
}
126-
127-
@Override
128-
default String toJson() throws EvalException {
129-
throw Starlark.errorf("unsupported method");
130-
}
131119
}

src/main/java/com/google/devtools/build/skydoc/fakebuildapi/FakeStructApi.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,6 @@ public FakeStructApi() {
4141
this(ImmutableMap.of());
4242
}
4343

44-
@Override
45-
public String toProto() throws EvalException {
46-
return "";
47-
}
48-
49-
@Override
50-
public String toJson() throws EvalException {
51-
return "";
52-
}
53-
5444
/**
5545
* Wraps {@link Structure#getValue(String)}, returning null in cases where {@link EvalException}
5646
* would have been thrown.

src/test/java/com/google/devtools/build/lib/packages/semantics/ConsistencyTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ private static BuildLanguageOptions buildRandomOptions(Random rand) throws Excep
146146
"--incompatible_no_package_distribs=" + rand.nextBoolean(),
147147
"--incompatible_no_rule_outputs_param=" + rand.nextBoolean(),
148148
"--incompatible_run_shell_command_string=" + rand.nextBoolean(),
149-
"--incompatible_struct_has_no_methods=" + rand.nextBoolean(),
150149
"--incompatible_require_linker_input_cc_api=" + rand.nextBoolean(),
151150
"--incompatible_use_cc_configure_from_rules_cc=" + rand.nextBoolean(),
152151
"--incompatible_unambiguous_label_stringification=" + rand.nextBoolean(),
@@ -196,7 +195,6 @@ private static StarlarkSemantics buildRandomSemantics(Random rand) {
196195
.setBool(BuildLanguageOptions.INCOMPATIBLE_NO_PACKAGE_DISTRIBS, rand.nextBoolean())
197196
.setBool(BuildLanguageOptions.INCOMPATIBLE_NO_RULE_OUTPUTS_PARAM, rand.nextBoolean())
198197
.setBool(BuildLanguageOptions.INCOMPATIBLE_RUN_SHELL_COMMAND_STRING, rand.nextBoolean())
199-
.setBool(BuildLanguageOptions.INCOMPATIBLE_STRUCT_HAS_NO_METHODS, rand.nextBoolean())
200198
.setBool(BuildLanguageOptions.INCOMPATIBLE_REQUIRE_LINKER_INPUT_CC_API, rand.nextBoolean())
201199
.setBool(
202200
BuildLanguageOptions.INCOMPATIBLE_USE_CC_CONFIGURE_FROM_RULES_CC, rand.nextBoolean())

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,16 +1507,6 @@ private void checkTextMessage(String from, String... lines) throws Exception {
15071507
assertThat(result).isEqualTo(expect);
15081508
}
15091509

1510-
@Test
1511-
public void testStructRestrictedOverrides() throws Exception {
1512-
setBuildLanguageOptions("--incompatible_struct_has_no_methods=false");
1513-
ev.checkEvalErrorContains(
1514-
"cannot override built-in struct function 'to_json'", "struct(to_json='foo')");
1515-
1516-
ev.checkEvalErrorContains(
1517-
"cannot override built-in struct function 'to_proto'", "struct(to_proto='foo')");
1518-
}
1519-
15201510
@Test
15211511
public void testSimpleTextMessages() throws Exception {
15221512
checkTextMessage("proto.encode_text(struct(name='value'))", "name: \"value\"");
@@ -1664,16 +1654,8 @@ private void checkJson(String from, String expected) throws Exception {
16641654

16651655
@Test
16661656
public void testStarlarkJsonModule() throws Exception {
1667-
// struct.to_json is deprecated.
1668-
// java.starlark.net's json module is its replacement.
1669-
setBuildLanguageOptions("--incompatible_struct_has_no_methods=false");
16701657
checkJson("json.encode(struct(name=True))", "{\"name\":true}");
16711658
checkJson("json.encode([1, 2])", "[1,2]"); // works for non-structs too
1672-
checkJson("str(dir(struct()))", "[\"to_json\", \"to_proto\"]");
1673-
1674-
setBuildLanguageOptions("--incompatible_struct_has_no_methods=true");
1675-
ev.checkEvalErrorContains("no field or method 'to_json'", "struct(name=True).to_json()");
1676-
checkJson("str(dir(struct()))", "[]"); // no to_{json,proto}
16771659
}
16781660

16791661
@Test

0 commit comments

Comments
 (0)