Skip to content

Commit 4df67fd

Browse files
committed
Report vector type as enum in debug output
1 parent 8fe7457 commit 4df67fd

File tree

5 files changed

+69
-39
lines changed

5 files changed

+69
-39
lines changed

src/BSON/Binary.c

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,16 @@ static HashTable* php_phongo_binary_get_debug_info(zend_object* object, int* is_
299299

300300
zend_hash_str_update(props, "vector", sizeof("vector") - 1, &vector);
301301

302-
zval vector_type;
302+
zval vector_type;
303+
zend_object* vector_type_case = phongo_bson_vector_type_to_case(phongo_binary_get_vector_type(intern));
303304

304-
ZVAL_LONG(&vector_type, phongo_binary_get_vector_type(intern));
305+
// The vector should always be valid by this point, but check for an error
306+
if (!vector_type_case) {
307+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Binary vector data is invalid");
308+
return props;
309+
}
310+
311+
ZVAL_OBJ_COPY(&vector_type, vector_type_case);
305312
zend_hash_str_update(props, "vectorType", sizeof("vectorType") - 1, &vector_type);
306313
}
307314

@@ -496,7 +503,7 @@ static PHP_METHOD(MongoDB_BSON_Binary, fromVector)
496503
Z_PARAM_OBJ_OF_CLASS(type, php_phongo_vectortype_ce)
497504
PHONGO_PARSE_PARAMETERS_END();
498505

499-
switch (phongo_bson_vector_type_from_name(Z_STRVAL_P(zend_enum_fetch_case_name(type)))) {
506+
switch (phongo_bson_vector_type_from_case(type)) {
500507
case PHONGO_BSON_VECTOR_TYPE_FLOAT32:
501508
phongo_binary_init_vector_from_float32_array(intern, vector);
502509
return;
@@ -545,7 +552,7 @@ static PHP_METHOD(MongoDB_BSON_Binary, getVectorType)
545552
RETURN_THROWS();
546553
}
547554

548-
const char* type_case = phongo_bson_vector_type_to_name(phongo_binary_get_vector_type(Z_BINARY_OBJ_P(getThis())));
555+
const char* type_case = phongo_bson_vector_type_to_name(phongo_binary_get_vector_type(intern));
549556

550557
// The vector should always be valid by this point, but check for an error
551558
if (!type_case) {
@@ -558,43 +565,48 @@ static PHP_METHOD(MongoDB_BSON_Binary, getVectorType)
558565

559566
static void phongo_binary_get_vector_as_array(const php_phongo_binary_t* intern, zval* return_value)
560567
{
561-
phongo_bson_vector_type_t type = phongo_binary_get_vector_type(intern);
562-
563-
// The vector should always be valid by this point, but check for an error
564-
if (type == PHONGO_BSON_VECTOR_TYPE_UNKNOWN) {
565-
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Binary vector data is invalid");
566-
RETURN_THROWS();
567-
}
568-
569568
bson_t tmp_doc = BSON_INITIALIZER;
570569

571-
if (type == PHONGO_BSON_VECTOR_TYPE_INT8) {
572-
bson_vector_int8_const_view_t view;
570+
switch (phongo_binary_get_vector_type(intern)) {
571+
case PHONGO_BSON_VECTOR_TYPE_INT8: {
572+
bson_vector_int8_const_view_t view;
573573

574-
if (!bson_vector_int8_const_view_init(&view, (const uint8_t*) intern->data, intern->data_len) ||
575-
!BSON_APPEND_ARRAY_FROM_VECTOR_INT8(&tmp_doc, "vector", view)) {
576-
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Failed to convert binary vector data to an array");
577-
bson_destroy(&tmp_doc);
578-
RETURN_THROWS();
574+
if (!bson_vector_int8_const_view_init(&view, (const uint8_t*) intern->data, intern->data_len) ||
575+
!BSON_APPEND_ARRAY_FROM_VECTOR_INT8(&tmp_doc, "vector", view)) {
576+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Failed to convert binary vector data to an array");
577+
bson_destroy(&tmp_doc);
578+
RETURN_THROWS();
579+
}
580+
581+
break;
579582
}
580-
} else if (type == PHONGO_BSON_VECTOR_TYPE_FLOAT32) {
581-
bson_vector_float32_const_view_t view;
583+
case PHONGO_BSON_VECTOR_TYPE_FLOAT32: {
584+
bson_vector_float32_const_view_t view;
582585

583-
if (!bson_vector_float32_const_view_init(&view, (const uint8_t*) intern->data, intern->data_len) ||
584-
!BSON_APPEND_ARRAY_FROM_VECTOR_FLOAT32(&tmp_doc, "vector", view)) {
585-
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Failed to convert binary vector data to an array");
586-
bson_destroy(&tmp_doc);
587-
RETURN_THROWS();
586+
if (!bson_vector_float32_const_view_init(&view, (const uint8_t*) intern->data, intern->data_len) ||
587+
!BSON_APPEND_ARRAY_FROM_VECTOR_FLOAT32(&tmp_doc, "vector", view)) {
588+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Failed to convert binary vector data to an array");
589+
bson_destroy(&tmp_doc);
590+
RETURN_THROWS();
591+
}
592+
593+
break;
588594
}
589-
} else if (type == PHONGO_BSON_VECTOR_TYPE_PACKED_BIT) {
590-
bson_vector_packed_bit_const_view_t view;
595+
case PHONGO_BSON_VECTOR_TYPE_PACKED_BIT: {
596+
bson_vector_packed_bit_const_view_t view;
591597

592-
if (!bson_vector_packed_bit_const_view_init(&view, (const uint8_t*) intern->data, intern->data_len) ||
593-
!BSON_APPEND_ARRAY_FROM_VECTOR_PACKED_BIT(&tmp_doc, "vector", view)) {
594-
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Failed to convert binary vector data to an array");
595-
bson_destroy(&tmp_doc);
596-
RETURN_THROWS();
598+
if (!bson_vector_packed_bit_const_view_init(&view, (const uint8_t*) intern->data, intern->data_len) ||
599+
!BSON_APPEND_ARRAY_FROM_VECTOR_PACKED_BIT(&tmp_doc, "vector", view)) {
600+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Failed to convert binary vector data to an array");
601+
bson_destroy(&tmp_doc);
602+
RETURN_THROWS();
603+
}
604+
605+
break;
597606
}
607+
default:
608+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Binary vector data is invalid");
609+
RETURN_THROWS();
598610
}
599611

600612
bson_iter_t iter;
@@ -647,5 +659,5 @@ static PHP_METHOD(MongoDB_BSON_Binary, toArray)
647659
RETURN_THROWS();
648660
}
649661

650-
phongo_binary_get_vector_as_array(Z_BINARY_OBJ_P(getThis()), return_value);
662+
phongo_binary_get_vector_as_array(intern, return_value);
651663
}

src/BSON/VectorType.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ const char* phongo_bson_vector_type_to_name(phongo_bson_vector_type_t type)
5454
}
5555
}
5656

57+
phongo_bson_vector_type_t phongo_bson_vector_type_from_case(zend_object* case_obj)
58+
{
59+
return phongo_bson_vector_type_from_name(Z_STRVAL_P(zend_enum_fetch_case_name(case_obj)));
60+
}
61+
62+
zend_object* phongo_bson_vector_type_to_case(phongo_bson_vector_type_t type)
63+
{
64+
const char* name = phongo_bson_vector_type_to_name(type);
65+
66+
if (!name) {
67+
return NULL;
68+
}
69+
70+
return zend_enum_get_case_cstr(php_phongo_vectortype_ce, name);
71+
}
72+
5773
void php_phongo_vectortype_init_ce(INIT_FUNC_ARGS)
5874
{
5975
php_phongo_vectortype_ce = register_class_MongoDB_BSON_VectorType();

src/BSON/VectorType.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,7 @@ typedef enum {
3030

3131
phongo_bson_vector_type_t phongo_bson_vector_type_from_name(const char* name);
3232
const char* phongo_bson_vector_type_to_name(phongo_bson_vector_type_t type);
33+
phongo_bson_vector_type_t phongo_bson_vector_type_from_case(zend_object* case_obj);
34+
zend_object* phongo_bson_vector_type_to_case(phongo_bson_vector_type_t type);
3335

3436
#endif /* PHONGO_BSON_VECTORTYPE_H */

tests/bson/bson-binary-fromVector-001.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ object(MongoDB\BSON\Binary)#%d (%d) {
2828
float(-0.5)
2929
}
3030
["vectorType"]=>
31-
int(39)
31+
enum(MongoDB\BSON\VectorType::Float32)
3232
}
3333
object(MongoDB\BSON\Binary)#%d (%d) {
3434
["data"]=>
@@ -47,7 +47,7 @@ object(MongoDB\BSON\Binary)#%d (%d) {
4747
int(127)
4848
}
4949
["vectorType"]=>
50-
int(3)
50+
enum(MongoDB\BSON\VectorType::Int8)
5151
}
5252
object(MongoDB\BSON\Binary)#%d (%d) {
5353
["data"]=>
@@ -66,6 +66,6 @@ object(MongoDB\BSON\Binary)#%d (%d) {
6666
int(0)
6767
}
6868
["vectorType"]=>
69-
int(16)
69+
enum(MongoDB\BSON\VectorType::PackedBit)
7070
}
7171
===DONE===

tests/bson/bson-binary-serialization-002.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ object(MongoDB\BSON\Binary)#%d (%d) {
112112
int(4)
113113
}
114114
["vectorType"]=>
115-
int(3)
115+
enum(MongoDB\BSON\VectorType::Int8)
116116
}
117117
string(70) "O:19:"MongoDB\BSON\Binary":2:{s:4:"data";s:6:"%a";s:4:"type";i:9;}"
118118
object(MongoDB\BSON\Binary)#%d (%d) {
@@ -132,7 +132,7 @@ object(MongoDB\BSON\Binary)#%d (%d) {
132132
int(4)
133133
}
134134
["vectorType"]=>
135-
int(3)
135+
enum(MongoDB\BSON\VectorType::Int8)
136136
}
137137

138138
===DONE===

0 commit comments

Comments
 (0)