Skip to content

->[to|from]Array() -> [to|from]PHP(). "root" typemapping. Default "root" to stdclass, not array #56

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
merged 8 commits into from
Jul 15, 2015

Conversation

jmikola
Copy link
Member

@jmikola jmikola commented Jul 6, 2015

Rename BSON\to|from]Array() to BSON[to|from]PHP()

Support setting the "root" (container) value to explicit type.
->setTypemap(["root" => "array"]);

Use stdClass, not array, for top-level containers by default

@jmikola jmikola force-pushed the phpc-319 branch 6 times, most recently from e250088 to 2ad506f Compare July 8, 2015 00:12
@@ -73,7 +73,7 @@ object(MongoDB\Driver\Cursor)#%d (%d) {
["mode"]=>
int(1)
["tags"]=>
array(0) {
object(stdClass)#%d (0) {
Copy link
Member Author

Choose a reason for hiding this comment

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

@bjori: When mongoc_read_prefs_t is initialized, its tags are also initialized to an empty BSON document. In turn, php_phongo_read_preference_to_zval() will do:

if (read_prefs->tags.len) {
    php_phongo_bson_state  state = PHONGO_BSON_STATE_INITIALIZER;

    MAKE_STD_ZVAL(state.zchild);
    bson_to_zval(bson_get_data(&read_prefs->tags), read_prefs->tags.len, &state);
    add_assoc_zval_ex(retval, ZEND_STRS("tags"), state.zchild);
} else {
    add_assoc_null_ex(retval, ZEND_STRS("tags"));
}

That else condition will really never be reached. Would you rather I change the condition to read_prefs->tags.len > 5 and add a comment that we only dump it if it's a non-empty document? Otherwise, I can leave this as-is.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think all the else append null checks were all added at the same time (dozens of them) after I realized not everything was initialized everytime, so var_dump()ing had the tendency to crash.

I don't think I ever checked individual cases, but instead always added a fallback path.

I'd say in this particular case, tags = null would look weird. tags = array() looks best.

Why is this stdClass now? that looks weird?

Copy link
Member Author

Choose a reason for hiding this comment

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

Why is this stdClass now? that looks weird?

I'm changing php_phongo_read_preference_to_zval(), Server::getTags(), Server::getInfo(), and php_phongo_server_to_zval() to use native array decoding, as we discussed earlier.

@jmikola jmikola force-pushed the phpc-319 branch 2 times, most recently from 4dd8f9e to 3d04657 Compare July 8, 2015 18:41
@jmikola jmikola changed the title [WIP] PHPC-319: Convert top-level documents to stdClass by default PHPC-319: Convert top-level documents to stdClass by default Jul 8, 2015
*
* TODO: decide if native array/object should be allowed to override this,
* in which case we only supersede PHONGO_TYPEMAP_NONE.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

no code todo please. They are destined to rot.
If its realistic, add a ticket. If not. swallow it :]

Copy link
Member Author

Choose a reason for hiding this comment

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

Created PHPC-329, so I'll remove this.

@bjori
Copy link
Contributor

bjori commented Jul 8, 2015

I've fixed the failing test on travis and am kicking the builds now

@jmikola jmikola force-pushed the phpc-319 branch 2 times, most recently from 06ca6a8 to 7df7934 Compare July 11, 2015 04:36
Update tests to expect stdClass for documents. Debug handlers and the Server getter methods will still decode documents as native arrays.
@jmikola
Copy link
Member Author

jmikola commented Jul 11, 2015

@bjori: I cleaned up the cases where I replaced (%d) expectations with hard-coded numbers, but I wasn't about to do a global search-and-replace in this PR. Feel free to create a ticket to do this later. I'm not sure if you just wanted stdClass and array dumps covered, or also things like string lengths.

@jmikola
Copy link
Member Author

jmikola commented Jul 11, 2015

A few issues still remaining:

  • PHPC-329: Only allow ODM behavior to supersede unspecified type map (not array/stdClass/custom mode)
  • PHPC-330: Include __pclass in bsonUnserialize() when possible
  • PHPC-331: Throw UnexpectedValueExceptions for unsupported (de)serialization cases

@@ -957,7 +979,7 @@ void php_phongo_bson_typemap_to_state(zval *typemap, php_phongo_bson_typemap *ma
map->array_type = PHONGO_TYPEMAP_NATIVE_OBJECT;
} else {
map->array_type = PHONGO_TYPEMAP_CLASS;
array_ce = zend_fetch_class(classname, classname_len, ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
zend_class_entry *array_ce = zend_fetch_class(classname, classname_len, ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
Copy link
Contributor

Choose a reason for hiding this comment

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

Variable declarations need to be done first.
This won't compile on VS.. And I thought this was part of our --enable-developer-flags (-Wdeclaration-after-statement).

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch. I surely must have missed this stderr output when building locally.

@bjori
Copy link
Contributor

bjori commented Jul 13, 2015

LGTM

  • Fix the build
  • Confirm no new build warnings
  • Confirm tests/bson/bson-encode-002.phpt

@bjori bjori changed the title PHPC-319: Convert top-level documents to stdClass by default ->[to|from]Array() -> [to|from]PHP(). "root" typemapping. Default "root" to stdclass, not array Jul 13, 2015
@jmikola
Copy link
Member Author

jmikola commented Jul 14, 2015

Rebased the PHPC-315 commit to fix the build warnings, and also adjusted my build scripts to I should catch stderr output more easily in the future.

I left an explanation of why I changed the bson-encode-002.phpt test, too. Hopefully that's acceptable.

@bjori
Copy link
Contributor

bjori commented Jul 15, 2015

oklgtm

@jmikola jmikola merged commit 1edcd14 into mongodb:master Jul 15, 2015
jmikola added a commit that referenced this pull request Jul 15, 2015
@jmikola jmikola deleted the phpc-319 branch July 15, 2015 19:36
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