Skip to content

Commit 548c183

Browse files
committed
Refactor typemap struct
1 parent 5e26792 commit 548c183

File tree

5 files changed

+51
-51
lines changed

5 files changed

+51
-51
lines changed

src/MongoDB/BulkWrite.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static void php_phongo_bulkwrite_extract_id(bson_t* doc, zval** return_value)
4040
php_phongo_bson_state state;
4141

4242
PHONGO_BSON_INIT_STATE(state);
43-
state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
43+
state.map.root.type = PHONGO_TYPEMAP_NATIVE_ARRAY;
4444

4545
if (!php_phongo_bson_to_zval_ex(doc, &state)) {
4646
goto cleanup;

src/MongoDB/ReadPreference.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ static HashTable* php_phongo_readpreference_get_properties_hash(phongo_compat_ob
458458
/* Use PHONGO_TYPEMAP_NATIVE_ARRAY for the root type since tags is an
459459
* array; however, inner documents and arrays can use the default. */
460460
PHONGO_BSON_INIT_STATE(state);
461-
state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
461+
state.map.root.type = PHONGO_TYPEMAP_NATIVE_ARRAY;
462462

463463
if (!php_phongo_bson_to_zval_ex(tags, &state)) {
464464
zval_ptr_dtor(&state.zchild);

src/MongoDB/WriteResult.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ static PHP_METHOD(MongoDB_Driver_WriteResult, getUpsertedIds)
230230
/* Use PHONGO_TYPEMAP_NATIVE_ARRAY for the root type so we can
231231
* easily access the "index" and "_id" fields. */
232232
PHONGO_BSON_INIT_STATE(state);
233-
state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
233+
state.map.root.type = PHONGO_TYPEMAP_NATIVE_ARRAY;
234234

235235
if (!BSON_ITER_HOLDS_DOCUMENT(&child)) {
236236
continue;

src/phongo_bson.c

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -747,19 +747,19 @@ static php_phongo_field_path_map_element* map_find_field_path_entry(php_phongo_b
747747
return NULL;
748748
}
749749

750-
static void php_phongo_handle_field_path_entry_for_compound_type(php_phongo_bson_state* state, php_phongo_bson_typemap_types* type, zend_class_entry** ce)
750+
static void php_phongo_handle_field_path_entry_for_compound_type(php_phongo_bson_state* state, php_phongo_bson_typemap_element* element)
751751
{
752752
php_phongo_field_path_map_element* entry = map_find_field_path_entry(state);
753753

754754
if (entry) {
755-
switch (entry->node_type) {
755+
switch (entry->node.type) {
756756
case PHONGO_TYPEMAP_NATIVE_ARRAY:
757757
case PHONGO_TYPEMAP_NATIVE_OBJECT:
758-
*type = entry->node_type;
758+
element->type = entry->node.type;
759759
break;
760760
case PHONGO_TYPEMAP_CLASS:
761-
*type = entry->node_type;
762-
*ce = entry->node_ce;
761+
element->type = entry->node.type;
762+
element->class = entry->node.class;
763763
break;
764764
default:
765765
/* Do nothing - pacify compiler */
@@ -836,15 +836,15 @@ static bool php_phongo_bson_visit_document(const bson_iter_t* iter ARG_UNUSED, c
836836
if (!bson_iter_visit_all(&child, &php_bson_visitors, &state) && !child.err_off) {
837837
/* Check for entries in the fieldPath type map key, and use them to
838838
* override the default ones for this type */
839-
php_phongo_handle_field_path_entry_for_compound_type(&state, &state.map.document_type, &state.map.document);
839+
php_phongo_handle_field_path_entry_for_compound_type(&state, &state.map.document);
840840

841841
/* If php_phongo_bson_visit_binary() finds an ODM class, it should
842842
* supersede a default type map and named document class. */
843-
if (state.odm && state.map.document_type == PHONGO_TYPEMAP_NONE) {
844-
state.map.document_type = PHONGO_TYPEMAP_CLASS;
843+
if (state.odm && state.map.document.type == PHONGO_TYPEMAP_NONE) {
844+
state.map.document.type = PHONGO_TYPEMAP_CLASS;
845845
}
846846

847-
switch (state.map.document_type) {
847+
switch (state.map.document.type) {
848848
case PHONGO_TYPEMAP_NATIVE_ARRAY:
849849
if (((php_phongo_bson_state*) data)->is_visiting_array) {
850850
add_next_index_zval(retval, &state.zchild);
@@ -855,7 +855,7 @@ static bool php_phongo_bson_visit_document(const bson_iter_t* iter ARG_UNUSED, c
855855

856856
case PHONGO_TYPEMAP_CLASS: {
857857
zval obj;
858-
zend_class_entry* obj_ce = state.odm ? state.odm : state.map.document;
858+
zend_class_entry* obj_ce = state.odm ? state.odm : state.map.document.class;
859859

860860
#if PHP_VERSION_ID >= 80100
861861
/* Enums require special handling for instantiation */
@@ -959,13 +959,13 @@ static bool php_phongo_bson_visit_array(const bson_iter_t* iter ARG_UNUSED, cons
959959
if (!bson_iter_visit_all(&child, &php_bson_visitors, &state) && !child.err_off) {
960960
/* Check for entries in the fieldPath type map key, and use them to
961961
* override the default ones for this type */
962-
php_phongo_handle_field_path_entry_for_compound_type(&state, &state.map.array_type, &state.map.array);
962+
php_phongo_handle_field_path_entry_for_compound_type(&state, &state.map.array);
963963

964-
switch (state.map.array_type) {
964+
switch (state.map.array.type) {
965965
case PHONGO_TYPEMAP_CLASS: {
966966
zval obj;
967967

968-
object_init_ex(&obj, state.map.array);
968+
object_init_ex(&obj, state.map.array.class);
969969
zend_call_method_with_1_params(PHONGO_COMPAT_OBJ_P(&obj), NULL, NULL, BSON_UNSERIALIZE_FUNC_NAME, NULL, &state.zchild);
970970
if (((php_phongo_bson_state*) data)->is_visiting_array) {
971971
add_next_index_zval(retval, &obj);
@@ -1047,7 +1047,7 @@ bool php_phongo_bson_value_to_zval(const bson_value_t* value, zval* zv)
10471047
bool retval = false;
10481048

10491049
PHONGO_BSON_INIT_STATE(state);
1050-
state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY;
1050+
state.map.root.type = PHONGO_TYPEMAP_NATIVE_ARRAY;
10511051

10521052
bson_append_value(&bson, "data", 4, value);
10531053
if (!php_phongo_bson_to_zval_ex(&bson, &state)) {
@@ -1119,18 +1119,18 @@ bool php_phongo_bson_to_zval_ex(const bson_t* b, php_phongo_bson_state* state)
11191119

11201120
/* If php_phongo_bson_visit_binary() finds an ODM class, it should supersede
11211121
* a default type map and named root class. */
1122-
if (state->odm && state->map.root_type == PHONGO_TYPEMAP_NONE) {
1123-
state->map.root_type = PHONGO_TYPEMAP_CLASS;
1122+
if (state->odm && state->map.root.type == PHONGO_TYPEMAP_NONE) {
1123+
state->map.root.type = PHONGO_TYPEMAP_CLASS;
11241124
}
11251125

1126-
switch (state->map.root_type) {
1126+
switch (state->map.root.type) {
11271127
case PHONGO_TYPEMAP_NATIVE_ARRAY:
11281128
/* Nothing to do here */
11291129
break;
11301130

11311131
case PHONGO_TYPEMAP_CLASS: {
11321132
zval obj;
1133-
zend_class_entry* obj_ce = state->odm ? state->odm : state->map.root;
1133+
zend_class_entry* obj_ce = state->odm ? state->odm : state->map.root.class;
11341134

11351135
#if PHP_VERSION_ID >= 80100
11361136
/* Enums require special handling for instantiation */
@@ -1258,7 +1258,7 @@ static zend_class_entry* php_phongo_bson_state_fetch_class(const char* classname
12581258
/* Parses a BSON type (i.e. array, document, or root). On success, the type and
12591259
* type_ce output arguments will be assigned and true will be returned;
12601260
* otherwise, false is returned and an exception is thrown. */
1261-
static bool php_phongo_bson_state_parse_type(zval* options, const char* name, php_phongo_bson_typemap_types* type, zend_class_entry** type_ce)
1261+
static bool php_phongo_bson_state_parse_type(zval* options, const char* name, php_phongo_bson_typemap_element* element)
12621262
{
12631263
char* classname;
12641264
int classname_len;
@@ -1272,14 +1272,14 @@ static bool php_phongo_bson_state_parse_type(zval* options, const char* name, ph
12721272
}
12731273

12741274
if (!strcasecmp(classname, "array")) {
1275-
*type = PHONGO_TYPEMAP_NATIVE_ARRAY;
1276-
*type_ce = NULL;
1275+
element->type = PHONGO_TYPEMAP_NATIVE_ARRAY;
1276+
element->class = NULL;
12771277
} else if (!strcasecmp(classname, "stdclass") || !strcasecmp(classname, "object")) {
1278-
*type = PHONGO_TYPEMAP_NATIVE_OBJECT;
1279-
*type_ce = NULL;
1278+
element->type = PHONGO_TYPEMAP_NATIVE_OBJECT;
1279+
element->class = NULL;
12801280
} else {
1281-
if ((*type_ce = php_phongo_bson_state_fetch_class(classname, classname_len, php_phongo_unserializable_ce))) {
1282-
*type = PHONGO_TYPEMAP_CLASS;
1281+
if ((element->class = php_phongo_bson_state_fetch_class(classname, classname_len, php_phongo_unserializable_ce))) {
1282+
element->type = PHONGO_TYPEMAP_CLASS;
12831283
} else {
12841284
retval = false;
12851285
}
@@ -1293,10 +1293,10 @@ static bool php_phongo_bson_state_parse_type(zval* options, const char* name, ph
12931293
return retval;
12941294
}
12951295

1296-
static void field_path_map_element_set_info(php_phongo_field_path_map_element* element, php_phongo_bson_typemap_types type, zend_class_entry* ce)
1296+
static void field_path_map_element_set_info(php_phongo_field_path_map_element* element, php_phongo_bson_typemap_element* typemap_element)
12971297
{
1298-
element->node_type = type;
1299-
element->node_ce = ce;
1298+
element->node.type = typemap_element->type;
1299+
element->node.class = typemap_element->class;
13001300
}
13011301

13021302
static void map_add_field_path_element(php_phongo_bson_typemap* map, php_phongo_field_path_map_element* element)
@@ -1326,7 +1326,7 @@ static void field_path_map_element_dtor(php_phongo_field_path_map_element* eleme
13261326
efree(element);
13271327
}
13281328

1329-
static bool php_phongo_bson_state_add_field_path(php_phongo_bson_typemap* map, char* field_path_original, php_phongo_bson_typemap_types type, zend_class_entry* ce)
1329+
static bool php_phongo_bson_state_add_field_path(php_phongo_bson_typemap* map, char* field_path_original, php_phongo_bson_typemap_element* typemap_element)
13301330
{
13311331
char* ptr = NULL;
13321332
char* segment_end = NULL;
@@ -1367,7 +1367,7 @@ static bool php_phongo_bson_state_add_field_path(php_phongo_bson_typemap* map, c
13671367
/* Add the last (or single) element */
13681368
php_phongo_field_path_push(field_path_map_element->entry, ptr, PHONGO_FIELD_PATH_ITEM_NONE);
13691369

1370-
field_path_map_element_set_info(field_path_map_element, type, ce);
1370+
field_path_map_element_set_info(field_path_map_element, typemap_element);
13711371
map_add_field_path_element(map, field_path_map_element);
13721372

13731373
return true;
@@ -1414,8 +1414,7 @@ static bool php_phongo_bson_state_parse_fieldpaths(zval* typemap, php_phongo_bso
14141414

14151415
ZEND_HASH_FOREACH_KEY_VAL(ht_data, num_key, string_key, property)
14161416
{
1417-
zend_class_entry* map_ce = NULL;
1418-
php_phongo_bson_typemap_types map_type;
1417+
php_phongo_bson_typemap_element element;
14191418

14201419
if (!string_key) {
14211420
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "The 'fieldPaths' element is not an associative array");
@@ -1427,11 +1426,11 @@ static bool php_phongo_bson_state_parse_fieldpaths(zval* typemap, php_phongo_bso
14271426
return false;
14281427
}
14291428

1430-
if (!php_phongo_bson_state_parse_type(fieldpaths, ZSTR_VAL(string_key), &map_type, &map_ce)) {
1429+
if (!php_phongo_bson_state_parse_type(fieldpaths, ZSTR_VAL(string_key), &element)) {
14311430
return false;
14321431
}
14331432

1434-
if (!php_phongo_bson_state_add_field_path(map, ZSTR_VAL(string_key), map_type, map_ce)) {
1433+
if (!php_phongo_bson_state_add_field_path(map, ZSTR_VAL(string_key), &element)) {
14351434
return false;
14361435
}
14371436
}
@@ -1449,9 +1448,9 @@ bool php_phongo_bson_typemap_to_state(zval* typemap, php_phongo_bson_typemap* ma
14491448
return true;
14501449
}
14511450

1452-
if (!php_phongo_bson_state_parse_type(typemap, "array", &map->array_type, &map->array) ||
1453-
!php_phongo_bson_state_parse_type(typemap, "document", &map->document_type, &map->document) ||
1454-
!php_phongo_bson_state_parse_type(typemap, "root", &map->root_type, &map->root) ||
1451+
if (!php_phongo_bson_state_parse_type(typemap, "array", &map->array) ||
1452+
!php_phongo_bson_state_parse_type(typemap, "document", &map->document) ||
1453+
!php_phongo_bson_state_parse_type(typemap, "root", &map->root) ||
14551454
!php_phongo_bson_state_parse_fieldpaths(typemap, map)) {
14561455

14571456
/* Exception should already have been thrown */

src/phongo_bson.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,19 @@ typedef enum {
4848
} php_phongo_bson_typemap_types;
4949

5050
typedef struct {
51-
php_phongo_field_path* entry;
52-
php_phongo_bson_typemap_types node_type;
53-
zend_class_entry* node_ce;
51+
php_phongo_bson_typemap_types type;
52+
zend_class_entry* class;
53+
} php_phongo_bson_typemap_element;
54+
55+
typedef struct {
56+
php_phongo_field_path* entry;
57+
php_phongo_bson_typemap_element node;
5458
} php_phongo_field_path_map_element;
5559

5660
typedef struct {
57-
php_phongo_bson_typemap_types document_type;
58-
zend_class_entry* document;
59-
php_phongo_bson_typemap_types array_type;
60-
zend_class_entry* array;
61-
php_phongo_bson_typemap_types root_type;
62-
zend_class_entry* root;
61+
php_phongo_bson_typemap_element document;
62+
php_phongo_bson_typemap_element array;
63+
php_phongo_bson_typemap_element root;
6364
struct {
6465
php_phongo_field_path_map_element** map;
6566
size_t allocated_size;
@@ -83,8 +84,8 @@ typedef struct {
8384
#define PHONGO_BSON_INIT_DEBUG_STATE(s) \
8485
do { \
8586
memset(&(s), 0, sizeof(php_phongo_bson_state)); \
86-
s.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY; \
87-
s.map.document_type = PHONGO_TYPEMAP_NATIVE_ARRAY; \
87+
s.map.root.type = PHONGO_TYPEMAP_NATIVE_ARRAY; \
88+
s.map.document.type = PHONGO_TYPEMAP_NATIVE_ARRAY; \
8889
} while (0)
8990

9091
char* php_phongo_field_path_as_string(php_phongo_field_path* field_path);

0 commit comments

Comments
 (0)