diff --git a/src/main/java/com/google/devtools/build/lib/packages/StructImpl.java b/src/main/java/com/google/devtools/build/lib/packages/StructImpl.java index 2916911b8e4d1c..626c24ab28575e 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/StructImpl.java +++ b/src/main/java/com/google/devtools/build/lib/packages/StructImpl.java @@ -17,17 +17,13 @@ import com.google.common.base.Objects; import com.google.common.collect.Ordering; import com.google.devtools.build.lib.starlarkbuildapi.core.StructApi; -import com.google.protobuf.TextFormat; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Map; import javax.annotation.Nullable; -import net.starlark.java.eval.Dict; import net.starlark.java.eval.EvalException; import net.starlark.java.eval.Printer; import net.starlark.java.eval.Starlark; -import net.starlark.java.eval.StarlarkInt; import net.starlark.java.eval.Structure; /** @@ -147,87 +143,6 @@ private Object getValueOrNull(String name) { } } - @Override - public String toProto() throws EvalException { - return Proto.INSTANCE.encodeText(this); - } - - /** - * Escapes the given string for use in proto/JSON string. - * - *
This escapes double quotes, backslashes, and newlines.
- */
- private static String escapeDoubleQuotesAndBackslashesAndNewlines(String string) {
- return TextFormat.escapeDoubleQuotesAndBackslashes(string).replace("\n", "\\n");
- }
-
- @Override
- public String toJson() throws EvalException {
- StringBuilder sb = new StringBuilder();
- printJson(this, sb, "struct field", null);
- return sb.toString();
- }
-
- private static void printJson(Object value, StringBuilder sb, String container, String key)
- throws EvalException {
- if (value == Starlark.NONE) {
- sb.append("null");
- } else if (value instanceof Structure) {
- sb.append("{");
-
- String join = "";
- for (String field : ((Structure) value).getFieldNames()) {
- sb.append(join);
- join = ",";
- appendJSONStringLiteral(sb, field);
- sb.append(':');
- printJson(((Structure) value).getValue(field), sb, "struct field", field);
- }
- sb.append("}");
- } else if (value instanceof Dict) {
- sb.append("{");
- String join = "";
- for (Map.Entry, ?> entry : ((Dict, ?>) value).entrySet()) {
- sb.append(join);
- join = ",";
- if (!(entry.getKey() instanceof String)) {
- throw Starlark.errorf(
- "Keys must be a string but got a %s for %s%s",
- Starlark.type(entry.getKey()), container, key != null ? " '" + key + "'" : "");
- }
- appendJSONStringLiteral(sb, (String) entry.getKey());
- sb.append(':');
- printJson(entry.getValue(), sb, "dict value", String.valueOf(entry.getKey()));
- }
- sb.append("}");
- } else if (value instanceof List) {
- sb.append("[");
- String join = "";
- for (Object item : ((List>) value)) {
- sb.append(join);
- join = ",";
- printJson(item, sb, "list element in struct field", key);
- }
- sb.append("]");
- } else if (value instanceof String) {
- appendJSONStringLiteral(sb, (String) value);
- } else if (value instanceof StarlarkInt || value instanceof Boolean) {
- sb.append(value);
- } else {
- throw Starlark.errorf(
- "Invalid text format, expected a struct, a string, a bool, or an int but got a %s for"
- + " %s%s",
- Starlark.type(value), container, key != null ? " '" + key + "'" : "");
- }
- }
-
- private static void appendJSONStringLiteral(StringBuilder out, String s) {
- out.append('"');
- out.append(
- escapeDoubleQuotesAndBackslashesAndNewlines(s).replace("\r", "\\r").replace("\t", "\\t"));
- out.append('"');
- }
-
@Override
public String toString() {
return Starlark.repr(this);
diff --git a/src/main/java/com/google/devtools/build/lib/packages/semantics/BuildLanguageOptions.java b/src/main/java/com/google/devtools/build/lib/packages/semantics/BuildLanguageOptions.java
index 9269e0a22c97d2..73cf13a2f02fec 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/semantics/BuildLanguageOptions.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/semantics/BuildLanguageOptions.java
@@ -378,18 +378,6 @@ public final class BuildLanguageOptions extends OptionsBase {
+ " and 1 cpu.")
public boolean experimentalActionResourceSet;
- @Option(
- name = "incompatible_struct_has_no_methods",
- defaultValue = "true",
- documentationCategory = OptionDocumentationCategory.STARLARK_SEMANTICS,
- effectTags = {OptionEffectTag.BUILD_FILE_SEMANTICS},
- metadataTags = {OptionMetadataTag.INCOMPATIBLE_CHANGE},
- help =
- "Disables the to_json and to_proto methods of struct, which pollute the struct field"
- + " namespace. Instead, use json.encode or json.encode_indent for JSON, or"
- + " proto.encode_text for textproto.")
- public boolean incompatibleStructHasNoMethods;
-
@Option(
name = "incompatible_always_check_depset_elements",
defaultValue = "true",
@@ -798,7 +786,6 @@ public StarlarkSemantics toStarlarkSemantics() {
.setBool(INCOMPATIBLE_NO_PACKAGE_DISTRIBS, incompatibleNoPackageDistribs)
.setBool(INCOMPATIBLE_NO_RULE_OUTPUTS_PARAM, incompatibleNoRuleOutputsParam)
.setBool(INCOMPATIBLE_RUN_SHELL_COMMAND_STRING, incompatibleRunShellCommandString)
- .setBool(INCOMPATIBLE_STRUCT_HAS_NO_METHODS, incompatibleStructHasNoMethods)
.setBool(StarlarkSemantics.PRINT_TEST_MARKER, internalStarlarkFlagTestCanary)
.setBool(
INCOMPATIBLE_DO_NOT_SPLIT_LINKING_CMDLINE, incompatibleDoNotSplitLinkingCmdline)
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaPluginInfo.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaPluginInfo.java
index 8be5b5b8e51dc1..4979bd9bcaf730 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaPluginInfo.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaPluginInfo.java
@@ -205,16 +205,6 @@ private JavaPluginData disableAnnotationProcessing() {
// Preserve data, which may be used by Error Prone plugins.
data());
}
-
- @Override
- public String toProto() throws EvalException {
- throw Starlark.errorf("unsupported method");
- }
-
- @Override
- public String toJson() throws EvalException {
- throw Starlark.errorf("unsupported method");
- }
}
public static JavaPluginInfo mergeWithoutJavaOutputs(JavaPluginInfo a, JavaPluginInfo b) {
diff --git a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/core/StructApi.java b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/core/StructApi.java
index a5828dbf393b3f..4ebbdf93510908 100644
--- a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/core/StructApi.java
+++ b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/core/StructApi.java
@@ -16,7 +16,6 @@
import com.google.devtools.build.docgen.annot.DocCategory;
import com.google.devtools.build.docgen.annot.StarlarkConstructor;
-import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions;
import net.starlark.java.annot.Param;
import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.annot.StarlarkMethod;
@@ -35,62 +34,6 @@
+ "equal if they have the same fields and if corresponding field values are equal.")
public interface StructApi extends StarlarkValue {
- @StarlarkMethod(
- name = "to_proto",
- doc =
- "Creates a text message from the struct parameter. This method only works if all "
- + "struct elements (recursively) are strings, ints, booleans, "
- + "other structs or dicts or lists of these types. "
- + "Quotes and new lines in strings are escaped. "
- + "Struct keys are iterated in the sorted order. "
- + "Examples:
" - + "struct(key=123).to_proto()\n# key: 123\n\n" - + "struct(key=True).to_proto()\n# key: true\n\n" - + "struct(key=[1, 2, 3]).to_proto()\n# key: 1\n# key: 2\n# key: 3\n\n" - + "struct(key='text').to_proto()\n# key: \"text\"\n\n" - + "struct(key=struct(inner_key='text')).to_proto()\n" - + "# key {\n# inner_key: \"text\"\n# }\n\n" - + "struct(key=[struct(inner_key=1), struct(inner_key=2)]).to_proto()\n" - + "# key {\n# inner_key: 1\n# }\n# key {\n# inner_key: 2\n# }\n\n" - + "struct(key=struct(inner_key=struct(inner_inner_key='text'))).to_proto()\n" - + "# key {\n# inner_key {\n# inner_inner_key: \"text\"\n# }\n# }\n\n" - + "struct(foo={4: 3, 2: 1}).to_proto()\n" - + "# foo: {\n" - + "# key: 4\n" - + "# value: 3\n" - + "# }\n" - + "# foo: {\n" - + "# key: 2\n" - + "# value: 1\n" - + "# }\n" - + "" - + "
Deprecated: use proto.encode_text(x) instead.",
- disableWithFlag = BuildLanguageOptions.INCOMPATIBLE_STRUCT_HAS_NO_METHODS)
- String toProto() throws EvalException;
-
- @StarlarkMethod(
- name = "to_json",
- doc =
- "Creates a JSON string from the struct parameter. This method only works if all "
- + "struct elements (recursively) are strings, ints, booleans, other structs, a "
- + "list of these types or a dictionary with string keys and values of these types. "
- + "Quotes and new lines in strings are escaped. "
- + "Examples:
" - + "struct(key=123).to_json()\n# {\"key\":123}\n\n" - + "struct(key=True).to_json()\n# {\"key\":true}\n\n" - + "struct(key=[1, 2, 3]).to_json()\n# {\"key\":[1,2,3]}\n\n" - + "struct(key='text').to_json()\n# {\"key\":\"text\"}\n\n" - + "struct(key=struct(inner_key='text')).to_json()\n" - + "# {\"key\":{\"inner_key\":\"text\"}}\n\n" - + "struct(key=[struct(inner_key=1), struct(inner_key=2)]).to_json()\n" - + "# {\"key\":[{\"inner_key\":1},{\"inner_key\":2}]}\n\n" - + "struct(key=struct(inner_key=struct(inner_inner_key='text'))).to_json()\n" - + "# {\"key\":{\"inner_key\":{\"inner_inner_key\":\"text\"}}}\n." - + "
Deprecated: instead, use json.encode(x) or json.encode_indent(x), which work"
- + " for values other than structs and do not pollute the struct field namespace. ",
- disableWithFlag = BuildLanguageOptions.INCOMPATIBLE_STRUCT_HAS_NO_METHODS)
- String toJson() throws EvalException;
-
/** Callable Provider for new struct objects. */
@StarlarkBuiltin(name = "Provider", documented = false, doc = "")
interface StructProviderApi extends ProviderApi {
diff --git a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/java/JavaOutputApi.java b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/java/JavaOutputApi.java
index 0460367798eeb1..412dee25d3416d 100644
--- a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/java/JavaOutputApi.java
+++ b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/java/JavaOutputApi.java
@@ -19,8 +19,6 @@
import com.google.devtools.build.lib.starlarkbuildapi.core.StructApi;
import javax.annotation.Nullable;
import net.starlark.java.annot.StarlarkMethod;
-import net.starlark.java.eval.EvalException;
-import net.starlark.java.eval.Starlark;
import net.starlark.java.eval.StarlarkSemantics;
/** A tuple of a java classes jar and its associated source and interface archives. */
@@ -118,14 +116,4 @@ public interface JavaOutputApi