@Override public CodeWithScope createCode(String code, Document scope) { if (scope instanceof DocumentEditor) { scope = ((DocumentEditor) scope).unwrap(); } return new CodeWithScope(code, scope); }
@Override public CodeWithScope createCode(String code, Document scope) { if (scope instanceof DocumentEditor) { scope = ((DocumentEditor) scope).unwrap(); } return new CodeWithScope(code, scope); }
@Test public void shouldRoundTripSimpleBsonObjectWithCodeWithScope() { Document scope = new BasicDocument("baz", "bam", "bak", "bat"); input = new BasicDocument("foo", new CodeWithScope("bar", scope)); assertRoundtrip(input); }
@Test public void shouldCorrectlyWriteCodeWithScope() { BasicDocument scope = new BasicDocument(); scope.put("firstName", "Jack"); scope.put("lastName", "Riley"); CodeWithScope code = new CodeWithScope("name", scope); String actual = writer.write(code); String expected = "{ \"$code\" : \"name\" , \"$scope\" : { \"firstName\" : \"Jack\" , \"lastName\" : \"Riley\" } }"; assertSame(expected, actual); }
@Test public void shouldParseDocumentWithCodeAndSscope() throws Exception { Document scope = (Document)parser("{ \"foo\" : 32 }").parseValue(); CodeWithScope obj = new CodeWithScope("foo", scope); // String str = writer.write(obj); // print = true; // print(str); value = parser(writer.write(obj)).parseValue(); assertEquals(obj, value); }
protected void write( String name, CodeWithScope value, BsonDataOutput output ) { output.writeByte(Type.JAVASCRIPT_WITH_SCOPE); writeCString(name, output); // Write the size for the CodeWithScope; we'll come back to this after we write the object ... int arraySizePosition = output.size(); output.writeInt(0); // Write the code & scope ... writeString(value.getCode(), output); write(null, value.getScope(), output); // Determine the number of bytes written in the array, and overwrite the value we wrote earlier .. int arraySize = output.size() - arraySizePosition; output.writeInt(arraySizePosition, arraySize); }
protected void write( String name, CodeWithScope value, BsonDataOutput output ) { output.writeByte(Type.JAVASCRIPT_WITH_SCOPE); writeCString(name, output); // Write the size for the CodeWithScope; we'll come back to this after we write the object ... int arraySizePosition = output.size(); output.writeInt(0); // Write the code & scope ... writeString(value.getCode(), output); write(null, value.getScope(), output); // Determine the number of bytes written in the array, and overwrite the value we wrote earlier .. int arraySize = output.size() - arraySizePosition; output.writeInt(arraySizePosition, arraySize); }