types::b_codewscope element::get_codewscope() const { CITER; uint32_t code_len; const uint8_t* scope_ptr; uint32_t scope_len; const char* code = bson_iter_codewscope(&iter, &code_len, &scope_len, &scope_ptr); document::view view(scope_ptr, scope_len); return types::b_codewscope{string_or_literal{code, code_len}, view}; }
types::b_codewscope element::get_codewscope() const { BSONCXX_TYPE_CHECK(k_codewscope); BSONCXX_CITER; uint32_t code_len; const uint8_t* scope_ptr; uint32_t scope_len; const char* code = bson_iter_codewscope(&iter, &code_len, &scope_len, &scope_ptr); document::view view(scope_ptr, scope_len); return types::b_codewscope{stdx::string_view{code, code_len}, view}; }
void bsonToMongoCodeWithScope(bson_iter_t* iter, Array* output) { uint32_t length; uint32_t scope_len; const uint8_t* scope; const char* code = bson_iter_codewscope(iter, &length, &scope_len, &scope); bson_t bson; bson_init_static(&bson, scope, scope_len); Array scopeArray = Array::Create(); bsonToVariant(&bson, &scopeArray); bsonToObject(iter, output, &s_MongoCode, make_packed_array(String(code, length, CopyString), scopeArray) ); }
static void test_bson_append_code_with_scope (void) { const bson_uint8_t *scope_buf = NULL; bson_uint32_t scopelen = 0; bson_uint32_t len = 0; bson_iter_t iter; bson_bool_t r; const char *code = NULL; bson_t *b; bson_t *b2; bson_t *scope; /* Test with empty bson, which converts to just CODE type. */ b = bson_new(); scope = bson_new(); assert(bson_append_code_with_scope(b, "code", -1, "var a = {};", scope)); b2 = get_bson("test30.bson"); assert_bson_equal(b, b2); r = bson_iter_init_find(&iter, b, "code"); assert(r); assert(BSON_ITER_HOLDS_CODE(&iter)); /* Not codewscope */ bson_destroy(b); bson_destroy(b2); bson_destroy(scope); /* Test with non-empty scope */ b = bson_new(); scope = bson_new(); assert(bson_append_utf8(scope, "foo", -1, "bar", -1)); assert(bson_append_code_with_scope(b, "code", -1, "var a = {};", scope)); b2 = get_bson("test31.bson"); assert_bson_equal(b, b2); r = bson_iter_init_find(&iter, b, "code"); assert(r); assert(BSON_ITER_HOLDS_CODEWSCOPE(&iter)); code = bson_iter_codewscope(&iter, &len, &scopelen, &scope_buf); assert(len == 11); assert(scopelen == scope->len); assert(!strcmp(code, "var a = {};")); bson_destroy(b); bson_destroy(b2); bson_destroy(scope); }