Skip to content

Commit ac19194

Browse files
committed
PHPC-606: Translate strings from mongoc_server_description_type()
1 parent c795d20 commit ac19194

File tree

3 files changed

+69
-17
lines changed

3 files changed

+69
-17
lines changed

php_phongo.c

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ ZEND_DECLARE_MODULE_GLOBALS(mongodb)
8181
#endif
8282
#endif
8383

84+
php_phongo_server_description_type_map_t
85+
php_phongo_server_description_type_map[PHONGO_SERVER_DESCRIPTION_TYPES] = {
86+
{ PHONGO_SERVER_UNKNOWN, "Unknown" },
87+
{ PHONGO_SERVER_STANDALONE, "Standalone" },
88+
{ PHONGO_SERVER_MONGOS, "Mongos" },
89+
{ PHONGO_SERVER_POSSIBLE_PRIMARY, "PossiblePrimary" },
90+
{ PHONGO_SERVER_RS_PRIMARY, "RSPrimary" },
91+
{ PHONGO_SERVER_RS_SECONDARY, "RSSecondary" },
92+
{ PHONGO_SERVER_RS_ARBITER, "RSArbiter" },
93+
{ PHONGO_SERVER_RS_OTHER, "RSOther" },
94+
{ PHONGO_SERVER_RS_GHOST, "RSGhost" },
95+
};
96+
8497
/* {{{ phongo_std_object_handlers */
8598
zend_object_handlers phongo_std_object_handlers;
8699

@@ -1275,6 +1288,20 @@ void php_phongo_objectid_new_from_oid(zval *object, const bson_oid_t *oid TSRMLS
12751288
bson_oid_to_string(oid, intern->oid);
12761289
} /* }}} */
12771290

1291+
php_phongo_server_description_type_t php_phongo_server_description_type(mongoc_server_description_t *sd)
1292+
{
1293+
const char* name = mongoc_server_description_type(sd);
1294+
int i;
1295+
1296+
for (i = 0; i < PHONGO_SERVER_DESCRIPTION_TYPES; i++) {
1297+
if (!strcmp(name, php_phongo_server_description_type_map[i].name)) {
1298+
return php_phongo_server_description_type_map[i].type;
1299+
}
1300+
}
1301+
1302+
return PHONGO_SERVER_UNKNOWN;
1303+
}
1304+
12781305
void php_phongo_server_to_zval(zval *retval, mongoc_server_description_t *sd) /* {{{ */
12791306
{
12801307
mongoc_host_list_t *host = mongoc_server_description_host(sd);
@@ -1285,10 +1312,10 @@ void php_phongo_server_to_zval(zval *retval, mongoc_server_description_t *sd) /*
12851312

12861313
ADD_ASSOC_STRING(retval, "host", host->host);
12871314
ADD_ASSOC_LONG_EX(retval, "port", host->port);
1288-
ADD_ASSOC_LONG_EX(retval, "type", sd->type);
1289-
ADD_ASSOC_BOOL_EX(retval, "is_primary", sd->type == MONGOC_SERVER_RS_PRIMARY);
1290-
ADD_ASSOC_BOOL_EX(retval, "is_secondary", sd->type == MONGOC_SERVER_RS_SECONDARY);
1291-
ADD_ASSOC_BOOL_EX(retval, "is_arbiter", sd->type == MONGOC_SERVER_RS_ARBITER);
1315+
ADD_ASSOC_LONG_EX(retval, "type", php_phongo_server_description_type(sd));
1316+
ADD_ASSOC_BOOL_EX(retval, "is_primary", !strcmp(mongoc_server_description_type(sd), php_phongo_server_description_type_map[PHONGO_SERVER_RS_PRIMARY].name));
1317+
ADD_ASSOC_BOOL_EX(retval, "is_secondary", !strcmp(mongoc_server_description_type(sd), php_phongo_server_description_type_map[PHONGO_SERVER_RS_SECONDARY].name));
1318+
ADD_ASSOC_BOOL_EX(retval, "is_arbiter", !strcmp(mongoc_server_description_type(sd), php_phongo_server_description_type_map[PHONGO_SERVER_RS_ARBITER].name));
12921319
ADD_ASSOC_BOOL_EX(retval, "is_hidden", bson_iter_init_find_case(&iter, is_master, "hidden") && bson_iter_as_bool(&iter));
12931320
ADD_ASSOC_BOOL_EX(retval, "is_passive", bson_iter_init_find_case(&iter, is_master, "passive") && bson_iter_as_bool(&iter));
12941321

php_phongo.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,29 @@ ZEND_END_MODULE_GLOBALS(mongodb)
7070

7171
#include "php_phongo_classes.h"
7272

73+
/* This enum is necessary since mongoc_server_description_type_t is private and
74+
* we need to translate strings returned by mongoc_server_description_type() to
75+
* Server integer constants. */
76+
typedef enum {
77+
PHONGO_SERVER_UNKNOWN = 0,
78+
PHONGO_SERVER_STANDALONE = 1,
79+
PHONGO_SERVER_MONGOS = 2,
80+
PHONGO_SERVER_POSSIBLE_PRIMARY = 3,
81+
PHONGO_SERVER_RS_PRIMARY = 4,
82+
PHONGO_SERVER_RS_SECONDARY = 5,
83+
PHONGO_SERVER_RS_ARBITER = 6,
84+
PHONGO_SERVER_RS_OTHER = 7,
85+
PHONGO_SERVER_RS_GHOST = 8,
86+
PHONGO_SERVER_DESCRIPTION_TYPES = 9,
87+
} php_phongo_server_description_type_t;
88+
89+
typedef struct {
90+
php_phongo_server_description_type_t type;
91+
const char *name;
92+
} php_phongo_server_description_type_map_t;
93+
94+
extern php_phongo_server_description_type_map_t php_phongo_server_description_type_map[];
95+
7396
typedef enum {
7497
PHONGO_ERROR_INVALID_ARGUMENT = 1,
7598
PHONGO_ERROR_RUNTIME = 2,
@@ -130,6 +153,8 @@ const mongoc_read_prefs_t* phongo_read_preference_from_zval(zval *zread_prefe
130153
const mongoc_write_concern_t* phongo_write_concern_from_zval (zval *zwrite_concern TSRMLS_DC);
131154
const php_phongo_query_t* phongo_query_from_zval (zval *zquery TSRMLS_DC);
132155

156+
php_phongo_server_description_type_t php_phongo_server_description_type(mongoc_server_description_t *sd);
157+
133158
void php_phongo_server_to_zval(zval *retval, mongoc_server_description_t *sd);
134159
void php_phongo_read_concern_to_zval(zval *retval, const mongoc_read_concern_t *read_concern);
135160
void php_phongo_read_preference_to_zval(zval *retval, const mongoc_read_prefs_t *read_prefs);

src/MongoDB/Server.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ PHP_METHOD(Server, getType)
301301
}
302302

303303
if ((sd = mongoc_client_get_server_description(intern->client, intern->server_id))) {
304-
RETVAL_LONG(sd->type);
304+
RETVAL_LONG(php_phongo_server_description_type(sd));
305305
mongoc_server_description_destroy(sd);
306306
return;
307307
}
@@ -325,7 +325,7 @@ PHP_METHOD(Server, isPrimary)
325325
}
326326

327327
if ((sd = mongoc_client_get_server_description(intern->client, intern->server_id))) {
328-
RETVAL_BOOL(sd->type == MONGOC_SERVER_RS_PRIMARY);
328+
RETVAL_BOOL(!strcmp(mongoc_server_description_type(sd), php_phongo_server_description_type_map[PHONGO_SERVER_RS_PRIMARY].name));
329329
mongoc_server_description_destroy(sd);
330330
return;
331331
}
@@ -349,7 +349,7 @@ PHP_METHOD(Server, isSecondary)
349349
}
350350

351351
if ((sd = mongoc_client_get_server_description(intern->client, intern->server_id))) {
352-
RETVAL_BOOL(sd->type == MONGOC_SERVER_RS_SECONDARY);
352+
RETVAL_BOOL(!strcmp(mongoc_server_description_type(sd), php_phongo_server_description_type_map[PHONGO_SERVER_RS_SECONDARY].name));
353353
mongoc_server_description_destroy(sd);
354354
return;
355355
}
@@ -373,7 +373,7 @@ PHP_METHOD(Server, isArbiter)
373373
}
374374

375375
if ((sd = mongoc_client_get_server_description(intern->client, intern->server_id))) {
376-
RETVAL_BOOL(sd->type == MONGOC_SERVER_RS_ARBITER);
376+
RETVAL_BOOL(!strcmp(mongoc_server_description_type(sd), php_phongo_server_description_type_map[PHONGO_SERVER_RS_ARBITER].name));
377377
mongoc_server_description_destroy(sd);
378378
return;
379379
}
@@ -622,15 +622,15 @@ PHP_MINIT_FUNCTION(Server)
622622
php_phongo_handler_server.offset = XtOffsetOf(php_phongo_server_t, std);
623623
#endif
624624

625-
zend_declare_class_constant_long(php_phongo_server_ce, ZEND_STRL("TYPE_UNKNOWN"), MONGOC_SERVER_UNKNOWN TSRMLS_CC);
626-
zend_declare_class_constant_long(php_phongo_server_ce, ZEND_STRL("TYPE_STANDALONE"), MONGOC_SERVER_STANDALONE TSRMLS_CC);
627-
zend_declare_class_constant_long(php_phongo_server_ce, ZEND_STRL("TYPE_MONGOS"), MONGOC_SERVER_MONGOS TSRMLS_CC);
628-
zend_declare_class_constant_long(php_phongo_server_ce, ZEND_STRL("TYPE_POSSIBLE_PRIMARY"), MONGOC_SERVER_POSSIBLE_PRIMARY TSRMLS_CC);
629-
zend_declare_class_constant_long(php_phongo_server_ce, ZEND_STRL("TYPE_RS_PRIMARY"), MONGOC_SERVER_RS_PRIMARY TSRMLS_CC);
630-
zend_declare_class_constant_long(php_phongo_server_ce, ZEND_STRL("TYPE_RS_SECONDARY"), MONGOC_SERVER_RS_SECONDARY TSRMLS_CC);
631-
zend_declare_class_constant_long(php_phongo_server_ce, ZEND_STRL("TYPE_RS_ARBITER"), MONGOC_SERVER_RS_ARBITER TSRMLS_CC);
632-
zend_declare_class_constant_long(php_phongo_server_ce, ZEND_STRL("TYPE_RS_OTHER"), MONGOC_SERVER_RS_OTHER TSRMLS_CC);
633-
zend_declare_class_constant_long(php_phongo_server_ce, ZEND_STRL("TYPE_RS_GHOST"), MONGOC_SERVER_RS_GHOST TSRMLS_CC);
625+
zend_declare_class_constant_long(php_phongo_server_ce, ZEND_STRL("TYPE_UNKNOWN"), PHONGO_SERVER_UNKNOWN TSRMLS_CC);
626+
zend_declare_class_constant_long(php_phongo_server_ce, ZEND_STRL("TYPE_STANDALONE"), PHONGO_SERVER_STANDALONE TSRMLS_CC);
627+
zend_declare_class_constant_long(php_phongo_server_ce, ZEND_STRL("TYPE_MONGOS"), PHONGO_SERVER_MONGOS TSRMLS_CC);
628+
zend_declare_class_constant_long(php_phongo_server_ce, ZEND_STRL("TYPE_POSSIBLE_PRIMARY"), PHONGO_SERVER_POSSIBLE_PRIMARY TSRMLS_CC);
629+
zend_declare_class_constant_long(php_phongo_server_ce, ZEND_STRL("TYPE_RS_PRIMARY"), PHONGO_SERVER_RS_PRIMARY TSRMLS_CC);
630+
zend_declare_class_constant_long(php_phongo_server_ce, ZEND_STRL("TYPE_RS_SECONDARY"), PHONGO_SERVER_RS_SECONDARY TSRMLS_CC);
631+
zend_declare_class_constant_long(php_phongo_server_ce, ZEND_STRL("TYPE_RS_ARBITER"), PHONGO_SERVER_RS_ARBITER TSRMLS_CC);
632+
zend_declare_class_constant_long(php_phongo_server_ce, ZEND_STRL("TYPE_RS_OTHER"), PHONGO_SERVER_RS_OTHER TSRMLS_CC);
633+
zend_declare_class_constant_long(php_phongo_server_ce, ZEND_STRL("TYPE_RS_GHOST"), PHONGO_SERVER_RS_GHOST TSRMLS_CC);
634634

635635

636636
return SUCCESS;

0 commit comments

Comments
 (0)