-
Notifications
You must be signed in to change notification settings - Fork 208
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
PHPC-1027: Introduce classes for deprecated BSON types #700
Conversation
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) /* {{{ */ |
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.
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()
.
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.
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) /* {{{ */ |
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.
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.
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.
OK, will fix that too.
src/BSON/DBPointer.c
Outdated
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)) { |
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.
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.
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.
I had missed that. Will fix.
src/BSON/DBPointer.c
Outdated
static PHP_METHOD(DBPointer, __toString) | ||
{ | ||
php_phongo_dbpointer_t *intern; | ||
char *retval; |
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.
Alignment is off by a space here.
src/BSON/DBPointer.c
Outdated
} /* }}} */ | ||
|
||
/* {{{ proto string MongoDB\BSON\DBPointer::__toString() | ||
Return the DBPointer's namespace string. */ |
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.
"namespace string and ObjectId"
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.
+1
@@ -0,0 +1,17 @@ | |||
--TEST-- | |||
MongoDB\BSON\Javascript::jsonSerialize() return value (without scope) |
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.
I assume this was copy/pasted prematurely, since DBPointer::jsonSerialize()
isn't implemented yet (although the other jsonSerialize()
methods appear to be).
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.
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 |
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.
I don't think we'll need interfaces for any of these deprecated classes.
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.
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"} }'))); |
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.
"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.
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.
Right, I've missed this one. Will fix it.
<?php | ||
|
||
var_export(MongoDB\BSON\Symbol::__set_state([ | ||
'symbol' => "symbolValue", |
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.
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.
@derickr: I'll see your 👎 and raise you a:
tests/bson/bson-toPHP_error-005.phpt
Outdated
@@ -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 |
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.
I assume this entire test will ultimately need to be deleted.
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.
Correct.
257342d
to
ff7b852
Compare
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.
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) /* {{{ */ |
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.
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) /* {{{ */ |
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.
OK, will fix that too.
src/BSON/DBPointer.c
Outdated
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)) { |
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.
I had missed that. Will fix.
src/BSON/DBPointer.c
Outdated
} /* }}} */ | ||
|
||
/* {{{ proto string MongoDB\BSON\DBPointer::__toString() | ||
Return the DBPointer's namespace string. */ |
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.
+1
src/BSON/DBPointer.c
Outdated
*/ | ||
static PHP_METHOD(DBPointer, serialize) | ||
{ | ||
php_phongo_dbpointer_t *intern; |
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.
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) |
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.
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 |
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.
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"} }'))); |
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.
Right, I've missed this one. Will fix it.
tests/bson/bson-toPHP_error-005.phpt
Outdated
@@ -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 |
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.
Correct.
src/BSON/Symbol.c
Outdated
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) |
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.
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 :)
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.
Removed the __set_state implementation all together.
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.
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.
ff7b852
to
f259e19
Compare
src/BSON/DBPointer.c
Outdated
|
||
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); |
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.
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.
64a82bc
to
8eae9ce
Compare
8eae9ce
to
d3953f6
Compare
No description provided.