-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Let module extensions track calls to Label()
#20742
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,9 +23,12 @@ | |
import com.google.auto.value.AutoValue; | ||
import com.google.common.base.Preconditions; | ||
import com.google.common.base.Splitter; | ||
import com.google.common.collect.ImmutableTable; | ||
import com.google.common.collect.Table; | ||
import com.google.devtools.build.lib.bazel.bzlmod.Version.ParseException; | ||
import com.google.devtools.build.lib.cmdline.Label; | ||
import com.google.devtools.build.lib.cmdline.LabelSyntaxException; | ||
import com.google.devtools.build.lib.cmdline.RepositoryName; | ||
import com.google.devtools.build.lib.vfs.Path; | ||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
|
@@ -40,8 +43,10 @@ | |
import java.io.IOException; | ||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.util.Arrays; | ||
import java.util.Base64; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
import javax.annotation.Nullable; | ||
import net.starlark.java.syntax.Location; | ||
|
||
|
@@ -92,7 +97,7 @@ public ModuleKey read(JsonReader jsonReader) throws IOException { | |
}; | ||
|
||
public static final TypeAdapter<Label> LABEL_TYPE_ADAPTER = | ||
new TypeAdapter<Label>() { | ||
new TypeAdapter<>() { | ||
@Override | ||
public void write(JsonWriter jsonWriter, Label label) throws IOException { | ||
jsonWriter.value(label.getUnambiguousCanonicalForm()); | ||
|
@@ -104,6 +109,19 @@ public Label read(JsonReader jsonReader) throws IOException { | |
} | ||
}; | ||
|
||
public static final TypeAdapter<RepositoryName> REPOSITORY_NAME_TYPE_ADAPTER = | ||
new TypeAdapter<>() { | ||
@Override | ||
public void write(JsonWriter jsonWriter, RepositoryName repoName) throws IOException { | ||
jsonWriter.value(repoName.getName()); | ||
} | ||
|
||
@Override | ||
public RepositoryName read(JsonReader jsonReader) throws IOException { | ||
return RepositoryName.createUnvalidated(jsonReader.nextString()); | ||
} | ||
}; | ||
|
||
public static final TypeAdapter<ModuleExtensionId> MODULE_EXTENSION_ID_TYPE_ADAPTER = | ||
new TypeAdapter<>() { | ||
@Override | ||
|
@@ -283,6 +301,67 @@ public Optional<T> read(JsonReader jsonReader) throws IOException { | |
} | ||
} | ||
|
||
/** | ||
* Converts Guava tables into a JSON array of 3-tuples (one per cell). Each 3-tuple is a JSON | ||
* array itself (rowKey, columnKey, value). For example, a JSON snippet could be: {@code | ||
* [ ["row1", "col1", "value1"], ["row2", "col2", "value2"], ... ]} | ||
*/ | ||
public static final TypeAdapterFactory IMMUTABLE_TABLE = | ||
new TypeAdapterFactory() { | ||
@Nullable | ||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { | ||
if (typeToken.getRawType() != ImmutableTable.class) { | ||
return null; | ||
} | ||
Type type = typeToken.getType(); | ||
if (!(type instanceof ParameterizedType)) { | ||
return null; | ||
} | ||
Type[] typeArgs = ((ParameterizedType) typeToken.getType()).getActualTypeArguments(); | ||
if (typeArgs.length != 3) { | ||
return null; | ||
} | ||
var rowTypeAdapter = (TypeAdapter<Object>) gson.getAdapter(TypeToken.get(typeArgs[0])); | ||
var colTypeAdapter = (TypeAdapter<Object>) gson.getAdapter(TypeToken.get(typeArgs[1])); | ||
var valTypeAdapter = (TypeAdapter<Object>) gson.getAdapter(TypeToken.get(typeArgs[2])); | ||
if (rowTypeAdapter == null || colTypeAdapter == null || valTypeAdapter == null) { | ||
return null; | ||
} | ||
return (TypeAdapter<T>) new TypeAdapter<ImmutableTable<Object, Object, Object>>() { | ||
@Override | ||
public void write(JsonWriter jsonWriter, ImmutableTable<Object, Object, Object> t) | ||
throws IOException { | ||
jsonWriter.beginArray(); | ||
for (Table.Cell<Object, Object, Object> cell : t.cellSet()) { | ||
jsonWriter.beginArray(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is my confusion with the Javadoc: there aren't any tuples here (are tuples even a json concept?), just an array of arrays. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tuples are not JSON concepts. I wanted to emphasize that each "sub-array" has exactly 3 elements, so conceptually a 3-tuple. |
||
rowTypeAdapter.write(jsonWriter, cell.getRowKey()); | ||
colTypeAdapter.write(jsonWriter, cell.getColumnKey()); | ||
valTypeAdapter.write(jsonWriter, cell.getValue()); | ||
jsonWriter.endArray(); | ||
} | ||
jsonWriter.endArray(); | ||
} | ||
|
||
@Override | ||
public ImmutableTable<Object, Object, Object> read(JsonReader jsonReader) | ||
throws IOException { | ||
var builder = ImmutableTable.builder(); | ||
jsonReader.beginArray(); | ||
while (jsonReader.peek() != JsonToken.END_ARRAY) { | ||
jsonReader.beginArray(); | ||
builder.put(rowTypeAdapter.read(jsonReader), colTypeAdapter.read(jsonReader), | ||
valTypeAdapter.read(jsonReader)); | ||
jsonReader.endArray(); | ||
} | ||
jsonReader.endArray(); | ||
return builder.buildOrThrow(); | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
/** | ||
* A variant of {@link Location} that converts the absolute path to the root module file to a | ||
* constant and back. | ||
|
@@ -371,8 +450,10 @@ public static Gson createLockFileGson(Path moduleFilePath) { | |
.registerTypeAdapterFactory(IMMUTABLE_BIMAP) | ||
.registerTypeAdapterFactory(IMMUTABLE_SET) | ||
.registerTypeAdapterFactory(OPTIONAL) | ||
.registerTypeAdapterFactory(IMMUTABLE_TABLE) | ||
.registerTypeAdapterFactory(new LocationTypeAdapterFactory(moduleFilePath)) | ||
.registerTypeAdapter(Label.class, LABEL_TYPE_ADAPTER) | ||
.registerTypeAdapter(RepositoryName.class, REPOSITORY_NAME_TYPE_ADAPTER) | ||
.registerTypeAdapter(Version.class, VERSION_TYPE_ADAPTER) | ||
.registerTypeAdapter(ModuleKey.class, MODULE_KEY_TYPE_ADAPTER) | ||
.registerTypeAdapter(ModuleExtensionId.class, MODULE_EXTENSION_ID_TYPE_ADAPTER) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand this comment: can you provide a small example JSON to clarify?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added a small snippet.