Skip to content

PHPC-1027: Introduce classes for deprecated BSON types #700

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

Merged

Conversation

derickr
Copy link
Contributor

@derickr derickr commented Dec 11, 2017

No description provided.

@derickr derickr requested a review from jmikola December 11, 2017 17:06
php_phongo.c Outdated
@@ -2377,6 +2377,33 @@ void php_phongo_new_regex_from_regex_and_options(zval *object, const char *patte
qsort((void *) intern->flags, intern->flags_len, 1, php_phongo_regex_compare_flags);
} /* }}} */

void php_phongo_new_symbol_from_symbol(int init, zval *object, const char *symbol, size_t symbol_len TSRMLS_DC) /* {{{ */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two questions about the init argument. It is apparently treated as a boolean, so bool would be more appropriate than int; however, I only see this function called when init is 1, so does it and the conditional below really serve a purpose? The other php_phongo_new_X functions don't take such an argument an always call object_init_ex().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of them must have done, as I've copied it :-)

I'll change it.

php_phongo.c Outdated
intern->symbol_len = symbol_len;
} /* }}} */

void php_phongo_new_dbpointer_from_dbpointer(int init, zval *object, const char *namespace, size_t namespace_len, const bson_oid_t *oid TSRMLS_DC) /* {{{ */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is only going to be one php_phongo_new_X function per deprecated type, I think we can omit the "from_X" suffix for conciseness. It appears to be a historical relic, as newer functions such as php_phongo_new_decimal128() do not have a "from" component in the name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, will fix that too.

zval *namespace, *oid;

if ((namespace = zend_hash_str_find(props, "namespace", sizeof("namespace")-1)) && Z_TYPE_P(namespace) == IS_STRING &&
(oid = zend_hash_str_find(props, "oid", sizeof("oid")-1)) && Z_TYPE_P(oid) == IS_OBJECT && instanceof_function(Z_OBJCE_P(oid), php_phongo_objectid_interface_ce TSRMLS_CC)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Objections to mixing tabs and spaces not withstanding, current code format uses tabs for block indents and spaces to align multi-line parens (e.g. if conditions). This is using only tabs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had missed that. Will fix.

static PHP_METHOD(DBPointer, __toString)
{
php_phongo_dbpointer_t *intern;
char *retval;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alignment is off by a space here.

} /* }}} */

/* {{{ proto string MongoDB\BSON\DBPointer::__toString()
Return the DBPointer's namespace string. */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"namespace string and ObjectId"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@@ -0,0 +1,17 @@
--TEST--
MongoDB\BSON\Javascript::jsonSerialize() return value (without scope)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this was copy/pasted prematurely, since DBPointer::jsonSerialize() isn't implemented yet (although the other jsonSerialize() methods appear to be).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and this is also what causes my current crash. It's required as part of one of the interfaces.

@@ -0,0 +1,14 @@
--TEST--
MongoDB\BSON\JavascriptInterface is implemented by MongoDB\BSON\Javascript
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we'll need interfaces for any of these deprecated classes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know. I haven't removed this test yet, as I had done for Symbol and Undefined.

--FILE--
<?php

var_dump(MongoDB\BSON\toPHP(MongoDB\BSON\fromJSON('{ "undefined": {"$symbol": "val1"} }')) == MongoDB\BSON\toPHP(MongoDB\BSON\fromJSON('{ "undefined": {"$symbol": "val1"} }')));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"undefined" is a curious choice here. Something like "x" or "foo" might be more concise, and not cause any confusion with the actual Undefined type.

Edit: I noticed you used "symbol" in a later test. That certainly works.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I've missed this one. Will fix it.

<?php

var_export(MongoDB\BSON\Symbol::__set_state([
'symbol' => "symbolValue",
Copy link
Member

@jmikola jmikola Dec 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tab alert.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@derickr: I'll see your 👎 and raise you a:

@@ -3,9 +3,7 @@ MongoDB\BSON\toPHP(): BSON decoding ignores unsupported BSON types
--FILE--
<?php
$tests = [
pack('VCa*xx', 10, 0x06, 'foo'), // undefined
pack('VCa*xVa*xx12x', 37, 0x0C, 'foo', 11, 'collection'), // DBPointer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this entire test will ultimately need to be deleted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct.

@derickr derickr force-pushed the PHPC-1027-add-classes-for-deprecated-bson-types branch from 257342d to ff7b852 Compare December 12, 2017 12:55
@derickr derickr changed the title [WIP] PHPC-1027: Introduce classes for deprecated BSON types PHPC-1027: Introduce classes for deprecated BSON types Dec 12, 2017
Copy link
Contributor Author

@derickr derickr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated for all the comments, and finished DBPointer.

Only questions I have is whether you think the serialisation formats are good.

php_phongo.c Outdated
@@ -2377,6 +2377,33 @@ void php_phongo_new_regex_from_regex_and_options(zval *object, const char *patte
qsort((void *) intern->flags, intern->flags_len, 1, php_phongo_regex_compare_flags);
} /* }}} */

void php_phongo_new_symbol_from_symbol(int init, zval *object, const char *symbol, size_t symbol_len TSRMLS_DC) /* {{{ */
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of them must have done, as I've copied it :-)

I'll change it.

php_phongo.c Outdated
intern->symbol_len = symbol_len;
} /* }}} */

void php_phongo_new_dbpointer_from_dbpointer(int init, zval *object, const char *namespace, size_t namespace_len, const bson_oid_t *oid TSRMLS_DC) /* {{{ */
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, will fix that too.

zval *namespace, *oid;

if ((namespace = zend_hash_str_find(props, "namespace", sizeof("namespace")-1)) && Z_TYPE_P(namespace) == IS_STRING &&
(oid = zend_hash_str_find(props, "oid", sizeof("oid")-1)) && Z_TYPE_P(oid) == IS_OBJECT && instanceof_function(Z_OBJCE_P(oid), php_phongo_objectid_interface_ce TSRMLS_CC)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had missed that. Will fix.

} /* }}} */

/* {{{ proto string MongoDB\BSON\DBPointer::__toString()
Return the DBPointer's namespace string. */
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

*/
static PHP_METHOD(DBPointer, serialize)
{
php_phongo_dbpointer_t *intern;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was off in other BSON class files too, I think. Didn't want to change them though just for the sake of it, but in new code it should done good.

@@ -0,0 +1,17 @@
--TEST--
MongoDB\BSON\Javascript::jsonSerialize() return value (without scope)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and this is also what causes my current crash. It's required as part of one of the interfaces.

@@ -0,0 +1,14 @@
--TEST--
MongoDB\BSON\JavascriptInterface is implemented by MongoDB\BSON\Javascript
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know. I haven't removed this test yet, as I had done for Symbol and Undefined.

--FILE--
<?php

var_dump(MongoDB\BSON\toPHP(MongoDB\BSON\fromJSON('{ "undefined": {"$symbol": "val1"} }')) == MongoDB\BSON\toPHP(MongoDB\BSON\fromJSON('{ "undefined": {"$symbol": "val1"} }')));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I've missed this one. Will fix it.

@@ -3,9 +3,7 @@ MongoDB\BSON\toPHP(): BSON decoding ignores unsupported BSON types
--FILE--
<?php
$tests = [
pack('VCa*xx', 10, 0x06, 'foo'), // undefined
pack('VCa*xVa*xx12x', 37, 0x0C, 'foo', 11, 'collection'), // DBPointer
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct.

ZEND_END_ARG_INFO()

static zend_function_entry php_phongo_symbol_me[] = {
PHP_ME(Symbol, __set_state, ai_Symbol___set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to implement this method instead of disabling it as we do the constructor?

With this gone, that would leave unserialize() as the only way for users to manually create these deprecated objects. I'm not suggesting we remove unserialize(), as folks should be able to round-trip these classes through PHP serialization :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the __set_state implementation all together.

Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe DBPointer::__toString() is missing, but other things look good.

Now that these types exist, I think we can enable some of the deprecated tests in the BSON corpus. I created PHPC-1065 to track that.

@derickr derickr force-pushed the PHPC-1027-add-classes-for-deprecated-bson-types branch from ff7b852 to f259e19 Compare December 12, 2017 15:08

array_init_size(return_value, 2);
ADD_ASSOC_STRINGL(return_value, "$ref", intern->ref, intern->ref_len);
ADD_ASSOC_STRING(return_value, "$id", intern->id);
Copy link
Member

@jmikola jmikola Dec 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized that the extended JSON spec calls for: {"$dbPointer": {"$ref": <namespace as a string>, "$id": ObjectId}} to avoid confusion with DBRef notation. Can we wrap this in an outer array with a $dbPointer key?

The spec also shows { "$oid": "57e193d7a9cc81b4027498b1" } for the value of the $id field, so I think that string should be wrapped as well.

@derickr derickr force-pushed the PHPC-1027-add-classes-for-deprecated-bson-types branch 2 times, most recently from 64a82bc to 8eae9ce Compare December 13, 2017 17:07
@derickr derickr force-pushed the PHPC-1027-add-classes-for-deprecated-bson-types branch from 8eae9ce to d3953f6 Compare December 14, 2017 10:49
@derickr derickr merged commit d3953f6 into mongodb:master Dec 14, 2017
derickr added a commit that referenced this pull request Dec 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants