Skip to content

Commit b61086f

Browse files
committed
refactor: factor out marshaling a Struct
This is done 3 times: 1. High-level properties 2. Map<String, Object> 3. Custom records
1 parent aba5094 commit b61086f

2 files changed

Lines changed: 22 additions & 43 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ public void testNestedProperties_insertMany() throws IOException {
486486
Property.integer("buildingNr"),
487487
Property.bool("isOneWay")))));
488488

489-
var buildings = client.collections.use("nsBuildings");
489+
var buildings = client.collections.use(nsBuildings);
490490

491491
Map<String, Object> house_1 = Map.of("address", Map.of(
492492
"street", "Burggasse",

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

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -130,25 +130,9 @@ public static <T> void buildObject(WeaviateProtoBatch.BatchObject.Builder object
130130
}
131131
}
132132

133-
var properties = WeaviateProtoBatch.BatchObject.Properties.newBuilder();
134-
var nonRef = com.google.protobuf.Struct.newBuilder();
135133
var singleRef = new ArrayList<WeaviateProtoBatch.BatchObject.SingleTargetRefProps>();
136134
var multiRef = new ArrayList<WeaviateProtoBatch.BatchObject.MultiTargetRefProps>();
137135

138-
collection
139-
.propertiesReader(insert.properties()).readProperties()
140-
.entrySet().stream().forEach(entry -> {
141-
try {
142-
var protoValue = marshalValue(entry.getValue());
143-
if (protoValue == null) {
144-
return;
145-
}
146-
nonRef.putFields(entry.getKey(), protoValue);
147-
} catch (IllegalArgumentException e) {
148-
throw new IllegalArgumentException("marshal property " + entry.getKey(), e);
149-
}
150-
});
151-
152136
insert.references()
153137
.entrySet().stream().forEach(entry -> {
154138
var references = entry.getValue();
@@ -173,16 +157,16 @@ public static <T> void buildObject(WeaviateProtoBatch.BatchObject.Builder object
173157
}
174158
});
175159

176-
properties
160+
var nonRef = marshalStruct(collection.propertiesReader(insert.properties()).readProperties());
161+
object.setProperties(WeaviateProtoBatch.BatchObject.Properties.newBuilder()
177162
.setNonRefProperties(nonRef)
178163
.addAllSingleTargetRefProps(singleRef)
179-
.addAllMultiTargetRefProps(multiRef);
180-
181-
object.setProperties(properties);
164+
.addAllMultiTargetRefProps(multiRef));
182165

183166
Debug.printProto(object);
184167
}
185168

169+
@SuppressWarnings("unchecked")
186170
private static com.google.protobuf.Value marshalValue(Object value) {
187171
var protoValue = com.google.protobuf.Value.newBuilder();
188172

@@ -282,35 +266,30 @@ private static com.google.protobuf.Value marshalValue(Object value) {
282266
protoValue.setListValue(com.google.protobuf.ListValue.newBuilder()
283267
.addAllValues(values)
284268
.build());
285-
} else if (value instanceof Map<?, ?> v) {
286-
var protoNested = com.google.protobuf.Struct.newBuilder();
287-
v.entrySet().stream()
288-
.forEach(nested -> {
289-
var nestedValue = marshalValue(nested.getValue());
290-
if (nestedValue == null) {
291-
return;
292-
}
293-
protoNested.putFields((String) nested.getKey(), nestedValue);
294-
});
295-
protoValue.setStructValue(protoNested);
269+
} else if (value instanceof Map<?, ?> properties) {
270+
protoValue.setStructValue(marshalStruct((Map<String, Object>) properties));
296271
} else if (value instanceof Record r) {
297-
@SuppressWarnings("unchecked")
298272
CollectionDescriptor<? super Record> recordDescriptor = (CollectionDescriptor<? super Record>) CollectionDescriptor
299273
.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);
274+
var properties = recordDescriptor.propertiesReader(r).readProperties();
275+
protoValue.setStructValue(marshalStruct(properties));
310276
} else {
311277
throw new IllegalArgumentException("data type " + value.getClass() + " is not supported");
312278
}
313279

314280
return protoValue.build();
315281
}
282+
283+
private static com.google.protobuf.Struct marshalStruct(Map<String, Object> properties) {
284+
var struct = com.google.protobuf.Struct.newBuilder();
285+
properties.entrySet().stream()
286+
.forEach(entry -> {
287+
var nestedValue = marshalValue(entry.getValue());
288+
if (nestedValue == null) {
289+
return;
290+
}
291+
struct.putFields((String) entry.getKey(), nestedValue);
292+
});
293+
return struct.build();
294+
}
316295
}

0 commit comments

Comments
 (0)