use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.
the class MorphiaQuery method getQueryDocument.
Document getQueryDocument() {
DocumentWriter writer = new DocumentWriter(mapper, seedQuery);
document(writer, () -> {
EncoderContext context = EncoderContext.builder().build();
for (Filter filter : filters) {
filter.encode(datastore, writer, context);
}
});
Document query = writer.getDocument();
if (mapper.isMappable(getEntityClass())) {
mapper.updateQueryWithDiscriminators(mapper.getEntityModel(getEntityClass()), query);
}
return query;
}
use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.
the class SetEntityOperator method toTarget.
@Override
public OperationTarget toTarget(PathTarget pathTarget) {
return new OperationTarget(null, value()) {
@Override
@SuppressWarnings("unchecked")
public Object encode(Datastore datastore) {
Object value = value();
EntityModel entityModel = datastore.getMapper().getEntityModel(value.getClass());
PropertyModel versionProperty = entityModel.getVersionProperty();
if (versionProperty == null) {
return super.encode(datastore);
}
Codec<Object> codec = datastore.getCodecRegistry().get((Class<Object>) value.getClass());
DocumentWriter writer = new DocumentWriter(datastore.getMapper());
codec.encode(writer, value, EncoderContext.builder().build());
Document document = writer.getDocument();
document.remove(versionProperty.getMappedName());
return document;
}
};
}
use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.
the class ExpressionHelper method document.
public static Document document(Mapper mapper, Document seed, Consumer<BsonWriter> body) {
DocumentWriter writer = new DocumentWriter(mapper, seed);
writer.writeStartDocument();
body.accept(writer);
writer.writeEndDocument();
return writer.getDocument();
}
use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.
the class TestDocumentWriter method arrays.
@Test
public void arrays() {
DocumentWriter writer = new DocumentWriter(getMapper());
document(writer, () -> {
array(writer, "stuff", () -> {
writer.writeString("hello");
writer.writeInt32(42);
});
writer.writeName("next");
writer.writeString("something simple");
});
Assert.assertEquals(writer.getDocument(), new Document("stuff", asList("hello", 42)).append("next", "something simple"));
}
use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.
the class TestDocumentWriter method nestedArrays.
@Test
public void nestedArrays() {
DocumentWriter writer = new DocumentWriter(getMapper());
document(writer, () -> {
array(writer, "top", () -> {
array(writer, () -> {
writer.writeInt32(1);
writer.writeInt32(2);
writer.writeInt32(3);
document(writer, () -> {
writer.writeString("nested", "string");
});
});
});
});
Document top = new Document("top", of(of(1, 2, 3, new Document("nested", "string"))));
Assert.assertEquals(top, writer.getDocument());
}
Aggregations