Skip to content

PHPC-1491: Add ReadPreference::getModeString #1062

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 3 commits into from
Dec 6, 2019

Conversation

alcaeus
Copy link
Member

@alcaeus alcaeus commented Nov 15, 2019

@alcaeus alcaeus requested a review from jmikola November 15, 2019 09:55
@alcaeus alcaeus self-assigned this Nov 15, 2019
return;
}

PHONGO_RETURN_STRING(php_phongo_readpreference_get_mode_string(mongoc_read_prefs_get_mode(intern->read_preference)));
Copy link
Member Author

Choose a reason for hiding this comment

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

php_phongo_readpreference_get_mode_string returns NULL when encountering an unknown value for read preference. The Server Selection spec specifies that driver MUST support the 5 modes, but doesn't limit the possible modes for read preferences.

While I suppose that PHONGO_RETURN_STRING (or rather zend_string_init) can handle the NULL value, I'm not sure if returning an empty string is the preferred option here - we may rather want to fall back to returning the int value (as string for type safety).

Copy link
Member

Choose a reason for hiding this comment

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

I would be more concerned about passing NULL to strlen() in the ZVAL_STRING macro:

I believe strlen(NULL) is undefined in most implementations:

In practice, I don't think it's possible for the read preference to have a different mode unless it was added to the enum and the PHP driver was not updated to handle it. That said, a defensive solution may be to simply throw an exception here if php_phongo_readpreference_get_mode_string returns NULL and include the numeric code in the message.

Copy link
Member

@jmikola jmikola Dec 4, 2019

Choose a reason for hiding this comment

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

I was just reminded of https://github.com/mongodb/mongo-php-driver/pull/1061/files#r345720246. For consistency, perhaps you'd rather throw from php_phongo_readpreference_get_mode_string directly instead of getModeString()?

@@ -733,6 +755,12 @@ void php_phongo_readpreference_init_ce(INIT_FUNC_ARGS) /* {{{ */
zend_declare_class_constant_long(php_phongo_readpreference_ce, ZEND_STRL("RP_NEAREST"), MONGOC_READ_NEAREST TSRMLS_CC);
zend_declare_class_constant_long(php_phongo_readpreference_ce, ZEND_STRL("NO_MAX_STALENESS"), MONGOC_NO_MAX_STALENESS TSRMLS_CC);
zend_declare_class_constant_long(php_phongo_readpreference_ce, ZEND_STRL("SMALLEST_MAX_STALENESS_SECONDS"), MONGOC_SMALLEST_MAX_STALENESS_SECONDS TSRMLS_CC);

zend_declare_class_constant_string(php_phongo_readpreference_ce, ZEND_STRL("PRIMARY"), PHONGO_READ_PRIMARY TSRMLS_CC);
Copy link
Member Author

Choose a reason for hiding this comment

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

As in #1061, I'm exposing constants here. Since the class name is ReadPreference, I decided to avoid using any prefix. This also resolves a naming conflict with the RP_ constants that expose the integer values for read preference modes.

I'm not too happy about having two very similar constants (RP_PRIMARY and PRIMARY) in the same class, as this may confuse users. We don't need to expose these values as constants now and may opt to only do that in 2.0 when the RP_ constants are dropped.

Copy link
Member

Choose a reason for hiding this comment

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

I'm OK with adding the constants now, as it's consistent with what we're already doing in ReadConcern. If we don't already have tickets to deprecate and remove the RP_ constants, feel free to create those.

Copy link
Member Author

Choose a reason for hiding this comment

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

Deprecation is tracked in PHPC-1489. I've added tests for all constants, including int ones for good measure.

@jmikola
Copy link
Member

jmikola commented Dec 4, 2019

I'd also suggest one additional test for the new constants, similar to I suggest adding one more test for these constants, similar to https://github.com/mongodb/mongo-php-driver/blob/master/tests/readConcern/readconcern-constants.phpt. Feel free to add the numeric constants if you like, but I'm not too concerned about those.

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.

New test LGTM but I think the change to php_phongo_readpreference_get_mode_string (throwing for unexpected enum value) is still needed.

@alcaeus
Copy link
Member Author

alcaeus commented Dec 5, 2019

Oh, my apologies, I totally missed that message. php_phongo_readpreference_get_mode_string now throws in the default path.

alcaeus added a commit that referenced this pull request Dec 6, 2019
@alcaeus alcaeus merged commit 24de9ca into mongodb:master Dec 6, 2019
@alcaeus alcaeus deleted the phpc-1491 branch December 6, 2019 10: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