@Override public BsonJavaScript encode(Code object) { return new BsonJavaScript(object.getCode()); }
private List<Document> createValidDocuments() throws Exception { DateFormat format = getUTCDateFormat(); List<Document> documents = new ArrayList<>(); documents.add( new Document("double_field", 1.23) .append("string_field", "embulk") .append("array_field", Arrays.asList(1, 2, 3)) .append("binary_field", new BsonBinary(("test").getBytes("UTF-8"))) .append("boolean_field", true) .append("datetime_field", format.parse("2015-01-27T10:23:49.000Z")) .append("null_field", null) .append("regex_field", new BsonRegularExpression(".+?")) .append("javascript_field", new BsonJavaScript("var s = \"javascript\";")) .append("int32_field", 1) .append("timestamp_field", new BsonTimestamp(1463991177, 4)) .append("int64_field", new BsonInt64(314159265)) .append("document_field", new Document("k", true)) .append("symbol_field", new Symbol("symbol")) ); documents.add( new Document("boolean_field", false) .append("int32_field", 2) .append("document_field", new Document("k", 1)) ); documents.add( new Document("int32_field", 3) .append("document_field", new Document("k", 1.23)) ); documents.add( new Document("int32_field", 4) .append("document_field", new Document("k", "v")) ); documents.add( new Document("int32_field", 5) .append("document_field", new Document("k", format.parse("2015-02-02T23:13:45.000Z"))) ); return documents; }
public static boolean checkType(Optional<BsonValue> o, String type) { if (!o.isPresent() && !"null".equals(type) && !"notnull".equals(type)) { return false; } switch (type.toLowerCase().trim()) { case "null": return !o.isPresent(); case "notnull": return o.isPresent(); case "object": return o.get().isDocument(); case "array": return o.get().isArray(); case "string": return o.get().isString(); case "number": return o.get().isNumber(); case "boolean": return o.get().isBoolean(); case "objectid": return o.get().isObjectId(); case "objectidstring": return o.get().isString() && ObjectId.isValid(o.get().asString().getValue()); case "date": return o.get().isDateTime(); case "timestamp": return o.get().isTimestamp(); case "maxkey": return o.get() instanceof BsonMaxKey; case "minkey": return o.get() instanceof BsonMinKey; case "symbol": return o.get().isSymbol(); case "code": return o.get() instanceof BsonJavaScript; default: return false; } }