@Override public void gotCodeWScope(final String name, final String code, final Object scope) { _put(name, new CodeWScope(code, (BSONObject) scope)); }
@Test public void testWhereWithInvalidStringQuery() { getDs().save(new PhotoWithKeywords()); final CodeWScope hasKeyword = new CodeWScope("keywords != null", new BasicDBObject()); try { // must fail assertNotNull(getDs().find(PhotoWithKeywords.class).where(hasKeyword.getCode()) .find(new FindOptions().limit(1)) .next()); fail("Invalid javascript magically isn't invalid anymore?"); } catch (MongoInternalException e) { // fine } catch (MongoException e) { // fine } }
/** * Tests if {@code JavaScript} objects can be deserialized * @throws Exception if something goes wrong */ @Test public void javascript() throws Exception { Code code = new Code("code"); TC.J obj = generateAndParse(code, TC.J.class); assertEquals(code.getCode(), obj.obj.getCode()); CodeWScope codews = new CodeWScope("code", new BasicBSONObject("key", "value")); obj = generateAndParse(codews, TC.J.class); assertEquals(code.getCode(), obj.obj.getCode()); assertEquals(codews.getScope().toMap(), obj.obj.getScope()); }
private void pickleCode(final Code code, final OutputStream out, final Pickler pickler) throws IOException { out.write(Opcodes.GLOBAL); out.write("bson.code\nCode\n".getBytes()); pickler.save(code.getCode()); out.write(Opcodes.TUPLE1); out.write(Opcodes.NEWOBJ); // PyMongo's bson.code.Code always has a scope, even it if is empty. out.write(Opcodes.EMPTY_DICT); putBinstring("_Code__scope", out); if (code instanceof CodeWithScope) { pickler.save(((CodeWithScope) code).getScope()); } else if (code instanceof CodeWScope) { pickler.save(((CodeWScope) code).getScope().toMap()); } else { out.write(Opcodes.EMPTY_DICT); } out.write(Opcodes.SETITEM); out.write(Opcodes.BUILD); }
@Override public int hashCode() { return getCode().hashCode() ^ scope.hashCode(); } }
private CodeWScope readCodeWScope(final BsonReader reader, final DecoderContext decoderContext, final List<String> path) { return new CodeWScope(reader.readJavaScriptWithScope(), readDocument(reader, decoderContext, path)); }
/** * Encodes a field to a {@link org.bson.BsonType#JAVASCRIPT_WITH_SCOPE} value. * * @param name the field name * @param codeWScope the value */ protected void putCodeWScope(final String name, final CodeWScope codeWScope) { putName(name); bsonWriter.writeJavaScriptWithScope(codeWScope.getCode()); putObject(codeWScope.getScope()); }
} else if (b.containsField("$code")) { if (b.containsField("$scope")) { o = new CodeWScope((String) b.get("$code"), (DBObject) b.get("$scope")); } else { o = new Code((String) b.get("$code"));
@Test public void testWhereCodeWScopeQuery() { getDs().save(new PhotoWithKeywords(new Keyword("california"), new Keyword("nevada"), new Keyword("arizona"))); // CodeWScope hasKeyword = new CodeWScope("for (kw in this.keywords) { if(kw.keyword == kwd) return true; } return false; // ", new BasicDBObject("kwd","california")); final CodeWScope hasKeyword = new CodeWScope("this.keywords != null", new BasicDBObject()); assertNotNull(getDs().find(PhotoWithKeywords.class).where(hasKeyword) .find(new FindOptions().limit(1)) .next()); }
return new Symbol(reader.readSymbol()); case JAVASCRIPT_WITH_SCOPE: return new CodeWScope(reader.readJavaScriptWithScope(), (BSONObject) readJavaScriptWithScopeDocument(reader)); case INT32: return reader.readInt32();
public void gotCodeWScope(String name, String code, Object scope) { _put(name, new CodeWScope(code, (BSONObject) scope)); }
private CodeWScope readCodeWScope(final BsonReader reader, final DecoderContext decoderContext, final List<String> path) { return new CodeWScope(reader.readJavaScriptWithScope(), readDocument(reader, decoderContext, path)); }