Skip to content

Commit

Permalink
Support lockfile serialization of Starlark tuples in MODULE.bazel files
Browse files Browse the repository at this point in the history
Fixes bazelbuild#20116

Closes bazelbuild#20129.

PiperOrigin-RevId: 581276198
Change-Id: I66259e273802e94a09ff1f862001a0109cc623e6
  • Loading branch information
fmeum authored and copybara-github committed Nov 10, 2023
1 parent 79300a3 commit 04d81db
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ private JsonElement serializeObject(Object obj) {
jsonObject.add(serializeObjToString(entry.getKey()), serializeObject(entry.getValue()));
}
return jsonObject;
} else if (obj instanceof StarlarkList) {
} else if (obj instanceof Iterable) {
// ListType supports any kind of Iterable, including Tuples and StarlarkLists. All of them
// are converted to an equivalent StarlarkList during deserialization.
JsonArray jsonArray = new JsonArray();
for (Object item : (StarlarkList<?>) obj) {
for (Object item : (Iterable<?>) obj) {
jsonArray.add(serializeObject(item));
}
return jsonArray;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.starlark.java.eval.Mutability;
import net.starlark.java.eval.StarlarkInt;
import net.starlark.java.eval.StarlarkList;
import net.starlark.java.eval.Tuple;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -89,4 +90,24 @@ public void testAttributeValuesAdapter() throws IOException {
assertThat(jsonString).contains(":\"Hello String\"");
assertThat((Map<?, ?>) attributeValues.attributes()).containsExactlyEntriesIn(builtDict);
}

@Test
public void testTuple() throws IOException {
Dict.Builder<String, Object> dict = new Dict.Builder<>();
dict.put("Tuple", Tuple.of("bzl", "mod"));

Dict<String, Object> builtDict = dict.buildImmutable();
AttributeValuesAdapter attrAdapter = new AttributeValuesAdapter();
String jsonString;
try (StringWriter stringWriter = new StringWriter()) {
attrAdapter.write(new JsonWriter(stringWriter), AttributeValues.create(builtDict));
jsonString = stringWriter.toString();
}
AttributeValues attributeValues;
try (StringReader stringReader = new StringReader(jsonString)) {
attributeValues = attrAdapter.read(new JsonReader(stringReader));
}
assertThat((Map<?, ?>) attributeValues.attributes())
.containsExactly("Tuple", StarlarkList.of(Mutability.IMMUTABLE, "bzl", "mod"));
}
}

0 comments on commit 04d81db

Please sign in to comment.