-
Notifications
You must be signed in to change notification settings - Fork 208
PHPC-359 and PHPC-801: RC, RP, and WC BSON serialization #424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -823,6 +823,49 @@ void php_phongo_read_concern_to_zval(zval *retval, const mongoc_read_concern_t * | |
} | ||
} /* }}} */ | ||
|
||
/* Prepare tagSets for BSON encoding by converting each array in the set to an | ||
* object. This ensures that empty arrays will serialize as empty documents. | ||
* | ||
* php_phongo_read_preference_tags_are_valid() handles actual validation of the | ||
* tag set structure. */ | ||
void php_phongo_read_preference_prep_tagsets(zval *tagSets TSRMLS_DC) /* {{{ */ | ||
{ | ||
HashTable *ht_data; | ||
|
||
if (Z_TYPE_P(tagSets) != IS_ARRAY) { | ||
return; | ||
} | ||
|
||
ht_data = HASH_OF(tagSets); | ||
|
||
#if PHP_VERSION_ID >= 70000 | ||
{ | ||
zval *tagSet; | ||
|
||
ZEND_HASH_FOREACH_VAL(ht_data, tagSet) { | ||
if (Z_TYPE_P(tagSet) == IS_ARRAY) { | ||
convert_to_object(tagSet); | ||
} | ||
} ZEND_HASH_FOREACH_END(); | ||
} | ||
#else | ||
{ | ||
HashPosition pos; | ||
zval **tagSet; | ||
|
||
for (zend_hash_internal_pointer_reset_ex(ht_data, &pos); | ||
zend_hash_get_current_data_ex(ht_data, (void **) &tagSet, &pos) == SUCCESS; | ||
zend_hash_move_forward_ex(ht_data, &pos)) { | ||
if (Z_TYPE_PP(tagSet) == IS_ARRAY) { | ||
convert_to_object(*tagSet); | ||
} | ||
} | ||
} | ||
#endif | ||
|
||
return; | ||
} /* }}} */ | ||
|
||
/* Checks if tags is valid to set on a mongoc_read_prefs_t. It may be null or an | ||
* array of one or more documents. */ | ||
bool php_phongo_read_preference_tags_are_valid(const bson_t *tags) /* {{{ */ | ||
|
@@ -864,10 +907,10 @@ void php_phongo_read_preference_to_zval(zval *retval, const mongoc_read_prefs_t | |
} | ||
|
||
if (!bson_empty0(tags)) { | ||
/* Use PHONGO_TYPEMAP_NATIVE_ARRAY for the root type since tags is an | ||
* array; however, inner documents and arrays can use the default. */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we be explicit about the document_type? i.e., by setting it to something like:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER; | ||
/* Use native arrays for debugging output */ | ||
state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY; | ||
state.map.document_type = PHONGO_TYPEMAP_NATIVE_ARRAY; | ||
|
||
phongo_bson_to_zval_ex(bson_get_data(tags), tags->len, &state); | ||
#if PHP_VERSION_ID >= 70000 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--TEST-- | ||
MongoDB\Driver\ReadConcern::bsonSerialize() | ||
--FILE-- | ||
<?php | ||
|
||
require_once __DIR__ . '/../utils/tools.php'; | ||
|
||
$tests = [ | ||
new MongoDB\Driver\ReadConcern(), | ||
new MongoDB\Driver\ReadConcern(MongoDB\Driver\ReadConcern::LINEARIZABLE), | ||
new MongoDB\Driver\ReadConcern(MongoDB\Driver\ReadConcern::LOCAL), | ||
new MongoDB\Driver\ReadConcern(MongoDB\Driver\ReadConcern::MAJORITY), | ||
]; | ||
|
||
foreach ($tests as $test) { | ||
echo toJSON(fromPHP($test)), "\n"; | ||
} | ||
|
||
?> | ||
===DONE=== | ||
<?php exit(0); ?> | ||
--EXPECT-- | ||
{ } | ||
{ "level" : "linearizable" } | ||
{ "level" : "local" } | ||
{ "level" : "majority" } | ||
===DONE=== |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--TEST-- | ||
MongoDB\Driver\ReadConcern::bsonSerialize() returns an object | ||
--FILE-- | ||
<?php | ||
|
||
require_once __DIR__ . '/../utils/tools.php'; | ||
|
||
$tests = [ | ||
new MongoDB\Driver\ReadConcern(), | ||
new MongoDB\Driver\ReadConcern(MongoDB\Driver\ReadConcern::LINEARIZABLE), | ||
new MongoDB\Driver\ReadConcern(MongoDB\Driver\ReadConcern::LOCAL), | ||
new MongoDB\Driver\ReadConcern(MongoDB\Driver\ReadConcern::MAJORITY), | ||
]; | ||
|
||
foreach ($tests as $test) { | ||
var_dump($test->bsonSerialize()); | ||
} | ||
|
||
?> | ||
===DONE=== | ||
<?php exit(0); ?> | ||
--EXPECTF-- | ||
object(stdClass)#%d (%d) { | ||
} | ||
object(stdClass)#%d (%d) { | ||
["level"]=> | ||
string(12) "linearizable" | ||
} | ||
object(stdClass)#%d (%d) { | ||
["level"]=> | ||
string(5) "local" | ||
} | ||
object(stdClass)#%d (%d) { | ||
["level"]=> | ||
string(8) "majority" | ||
} | ||
===DONE=== |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--TEST-- | ||
MongoDB\Driver\ReadConcern debug output | ||
--FILE-- | ||
<?php | ||
|
||
require_once __DIR__ . '/../utils/tools.php'; | ||
|
||
$tests = [ | ||
new MongoDB\Driver\ReadConcern(), | ||
new MongoDB\Driver\ReadConcern(MongoDB\Driver\ReadConcern::LINEARIZABLE), | ||
new MongoDB\Driver\ReadConcern(MongoDB\Driver\ReadConcern::LOCAL), | ||
new MongoDB\Driver\ReadConcern(MongoDB\Driver\ReadConcern::MAJORITY), | ||
]; | ||
|
||
foreach ($tests as $test) { | ||
var_dump($test); | ||
} | ||
|
||
?> | ||
===DONE=== | ||
<?php exit(0); ?> | ||
--EXPECTF-- | ||
object(MongoDB\Driver\ReadConcern)#%d (%d) { | ||
} | ||
object(MongoDB\Driver\ReadConcern)#%d (%d) { | ||
["level"]=> | ||
string(12) "linearizable" | ||
} | ||
object(MongoDB\Driver\ReadConcern)#%d (%d) { | ||
["level"]=> | ||
string(5) "local" | ||
} | ||
object(MongoDB\Driver\ReadConcern)#%d (%d) { | ||
["level"]=> | ||
string(8) "majority" | ||
} | ||
===DONE=== |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like whitespace issues here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is correct. The
for
components are all indented five spaces and theif
(and lines below) are indented with a tab, since they're within thefor
block.