Skip to content

Commit aba5094

Browse files
committed
feat: support writing records as nested properties
1 parent df8b0c7 commit aba5094

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

src/it/java/io/weaviate/integration/DataITest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,12 @@ public void testDataTypes() throws IOException {
468468

469469
}
470470

471+
record Address(
472+
String street,
473+
@io.weaviate.client6.v1.api.collections.annotations.Property("building_nr") int buildingNr,
474+
@io.weaviate.client6.v1.api.collections.annotations.Property("isOneWay") boolean oneWay) {
475+
}
476+
471477
@Test
472478
public void testNestedProperties_insertMany() throws IOException {
473479
// Arrange
@@ -486,10 +492,10 @@ public void testNestedProperties_insertMany() throws IOException {
486492
"street", "Burggasse",
487493
"building_nr", 51,
488494
"isOneWay", true));
489-
Map<String, Object> house_2 = Map.of("address", Map.of(
490-
"street", "Port Mariland St.",
491-
"building_nr", 111,
492-
"isOneWay", false));
495+
Map<String, Object> house_2 = Map.of("address", new Address(
496+
"Port Mariland St.",
497+
111,
498+
false));
493499

494500
// Act
495501
var result = buildings.data.insertMany(house_1, house_2);

src/main/java/io/weaviate/client6/v1/api/collections/data/InsertManyRequest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,20 @@ private static com.google.protobuf.Value marshalValue(Object value) {
293293
protoNested.putFields((String) nested.getKey(), nestedValue);
294294
});
295295
protoValue.setStructValue(protoNested);
296+
} else if (value instanceof Record r) {
297+
@SuppressWarnings("unchecked")
298+
CollectionDescriptor<? super Record> recordDescriptor = (CollectionDescriptor<? super Record>) CollectionDescriptor
299+
.ofClass(r.getClass());
300+
var protoRecord = com.google.protobuf.Struct.newBuilder();
301+
recordDescriptor.propertiesReader(r).readProperties().entrySet().stream()
302+
.forEach(recordField -> {
303+
var nestedValue = marshalValue(recordField.getValue());
304+
if (nestedValue == null) {
305+
return;
306+
}
307+
protoRecord.putFields((String) recordField.getKey(), nestedValue);
308+
});
309+
protoValue.setStructValue(protoRecord);
296310
} else {
297311
throw new IllegalArgumentException("data type " + value.getClass() + " is not supported");
298312
}

0 commit comments

Comments
 (0)