Search in sources :

Example 1 with ReplacingDocumentFieldNameValidator

use of com.mongodb.internal.validator.ReplacingDocumentFieldNameValidator in project mongo-java-driver by mongodb.

the class UpdateMessage method encodeMessageBodyWithMetadata.

@Override
protected EncodingMetadata encodeMessageBodyWithMetadata(final BsonOutput bsonOutput) {
    // reserved
    bsonOutput.writeInt32(0);
    bsonOutput.writeCString(getCollectionName());
    int flags = 0;
    if (updateRequest.isUpsert()) {
        flags |= 1;
    }
    if (updateRequest.isMulti()) {
        flags |= 2;
    }
    bsonOutput.writeInt32(flags);
    int firstDocumentStartPosition = bsonOutput.getPosition();
    addDocument(updateRequest.getFilter(), bsonOutput, new NoOpFieldNameValidator());
    if (updateRequest.getType() == REPLACE && updateRequest.getUpdateValue().isDocument()) {
        addDocument(updateRequest.getUpdateValue().asDocument(), bsonOutput, new ReplacingDocumentFieldNameValidator());
    } else {
        int bufferPosition = bsonOutput.getPosition();
        BsonValue update = updateRequest.getUpdateValue();
        if (update.isDocument()) {
            addDocument(update.asDocument(), bsonOutput, new UpdateFieldNameValidator());
        } else {
            throw new IllegalArgumentException("Invalid update filter in update request. The filter must be a document.");
        }
        if (bsonOutput.getPosition() == bufferPosition + 5) {
            throw new IllegalArgumentException("Invalid BSON document for an update");
        }
    }
    return new EncodingMetadata(firstDocumentStartPosition);
}
Also used : NoOpFieldNameValidator(com.mongodb.internal.validator.NoOpFieldNameValidator) ReplacingDocumentFieldNameValidator(com.mongodb.internal.validator.ReplacingDocumentFieldNameValidator) UpdateFieldNameValidator(com.mongodb.internal.validator.UpdateFieldNameValidator) BsonValue(org.bson.BsonValue)

Example 2 with ReplacingDocumentFieldNameValidator

use of com.mongodb.internal.validator.ReplacingDocumentFieldNameValidator in project mongo-java-driver by mongodb.

the class FindAndReplaceOperation method getFieldNameValidator.

@Override
protected FieldNameValidator getFieldNameValidator() {
    Map<String, FieldNameValidator> map = new HashMap<String, FieldNameValidator>();
    map.put("update", new ReplacingDocumentFieldNameValidator());
    return new MappedFieldNameValidator(new NoOpFieldNameValidator(), map);
}
Also used : NoOpFieldNameValidator(com.mongodb.internal.validator.NoOpFieldNameValidator) ReplacingDocumentFieldNameValidator(com.mongodb.internal.validator.ReplacingDocumentFieldNameValidator) HashMap(java.util.HashMap) MappedFieldNameValidator(com.mongodb.internal.validator.MappedFieldNameValidator) BsonString(org.bson.BsonString) NoOpFieldNameValidator(com.mongodb.internal.validator.NoOpFieldNameValidator) ReplacingDocumentFieldNameValidator(com.mongodb.internal.validator.ReplacingDocumentFieldNameValidator) MappedFieldNameValidator(com.mongodb.internal.validator.MappedFieldNameValidator) FieldNameValidator(org.bson.FieldNameValidator)

Aggregations

NoOpFieldNameValidator (com.mongodb.internal.validator.NoOpFieldNameValidator)2 ReplacingDocumentFieldNameValidator (com.mongodb.internal.validator.ReplacingDocumentFieldNameValidator)2 MappedFieldNameValidator (com.mongodb.internal.validator.MappedFieldNameValidator)1 UpdateFieldNameValidator (com.mongodb.internal.validator.UpdateFieldNameValidator)1 HashMap (java.util.HashMap)1 BsonString (org.bson.BsonString)1 BsonValue (org.bson.BsonValue)1 FieldNameValidator (org.bson.FieldNameValidator)1