Skip to content

Commit 8fe7457

Browse files
committed
Relocate VectorType enum defs and revise test cases
1 parent 6ffd63d commit 8fe7457

File tree

7 files changed

+107
-57
lines changed

7 files changed

+107
-57
lines changed

src/BSON/Binary.c

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "phongo_error.h"
2525
#include "Binary.h"
2626
#include "Binary_arginfo.h"
27+
#include "VectorType.h"
2728

2829
zend_class_entry* php_phongo_binary_ce;
2930

@@ -45,7 +46,7 @@ static bool php_phongo_binary_init(php_phongo_binary_t* intern, const char* data
4546
return false;
4647
}
4748

48-
if ((type == BSON_SUBTYPE_VECTOR) && phongo_binary_get_vector_type_from_data((const uint8_t*) data, data_len) == PHONGO_BSON_VECTOR_TYPE_INVALID) {
49+
if ((type == BSON_SUBTYPE_VECTOR) && phongo_binary_get_vector_type_from_data((const uint8_t*) data, data_len) == PHONGO_BSON_VECTOR_TYPE_UNKNOWN) {
4950
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Binary vector data is invalid");
5051
return false;
5152
}
@@ -459,11 +460,11 @@ static void phongo_binary_init_vector_from_packed_bit_array(php_phongo_binary_t*
459460
ZEND_HASH_FOREACH_VAL_IND(vector, val)
460461
{
461462
if (Z_TYPE_P(val) != IS_LONG && Z_TYPE_P(val) != IS_TRUE && Z_TYPE_P(val) != IS_FALSE) {
462-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected vector[%zu] to be an integer or boolean, %s given", i, zend_zval_type_name(val));
463+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected vector[%zu] to be 0, 1, or a boolean, %s given", i, zend_zval_type_name(val));
463464
return;
464465
}
465466

466-
if (Z_TYPE_P(val) == IS_LONG && (Z_LVAL_P(val) < 0 || Z_LVAL_P(val) > 1)) {
467+
if (Z_TYPE_P(val) == IS_LONG && Z_LVAL_P(val) != 0 && Z_LVAL_P(val) != 1) {
467468
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected vector[%zu] to be 0 or 1, %" PHONGO_LONG_FORMAT " given", i, Z_LVAL_P(val));
468469
return;
469470
}
@@ -484,8 +485,8 @@ static void phongo_binary_init_vector_from_packed_bit_array(php_phongo_binary_t*
484485

485486
static PHP_METHOD(MongoDB_BSON_Binary, fromVector)
486487
{
487-
HashTable* vector;
488-
zend_object* type;
488+
HashTable* vector;
489+
zend_object* type;
489490

490491
object_init_ex(return_value, php_phongo_binary_ce);
491492
php_phongo_binary_t* intern = Z_BINARY_OBJ_P(return_value);
@@ -495,25 +496,20 @@ static PHP_METHOD(MongoDB_BSON_Binary, fromVector)
495496
Z_PARAM_OBJ_OF_CLASS(type, php_phongo_vectortype_ce)
496497
PHONGO_PARSE_PARAMETERS_END();
497498

498-
zval *type_name = zend_enum_fetch_case_name(type);
499-
500-
if (zend_string_equals_literal(Z_STR_P(type_name), "Float32")) {
501-
phongo_binary_init_vector_from_float32_array(intern, vector);
502-
return;
503-
}
504-
505-
if (zend_string_equals_literal(Z_STR_P(type_name), "Int8")) {
506-
phongo_binary_init_vector_from_int8_array(intern, vector);
507-
return;
508-
}
509-
510-
if (zend_string_equals_literal(Z_STR_P(type_name), "PackedBit")) {
511-
phongo_binary_init_vector_from_packed_bit_array(intern, vector);
512-
return;
499+
switch (phongo_bson_vector_type_from_name(Z_STRVAL_P(zend_enum_fetch_case_name(type)))) {
500+
case PHONGO_BSON_VECTOR_TYPE_FLOAT32:
501+
phongo_binary_init_vector_from_float32_array(intern, vector);
502+
return;
503+
case PHONGO_BSON_VECTOR_TYPE_INT8:
504+
phongo_binary_init_vector_from_int8_array(intern, vector);
505+
return;
506+
case PHONGO_BSON_VECTOR_TYPE_PACKED_BIT:
507+
phongo_binary_init_vector_from_packed_bit_array(intern, vector);
508+
return;
509+
default:
510+
phongo_throw_exception(PHONGO_ERROR_LOGIC, "Unsupported binary vector type: %s", Z_STRVAL_P(zend_enum_fetch_case_name(type)));
511+
RETURN_THROWS();
513512
}
514-
515-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Unsupported binary vector type: %s", Z_STR_P(type_name));
516-
RETURN_THROWS();
517513
}
518514

519515
static phongo_bson_vector_type_t phongo_binary_get_vector_type_from_data(const uint8_t* data, uint32_t data_len)
@@ -530,7 +526,7 @@ static phongo_bson_vector_type_t phongo_binary_get_vector_type_from_data(const u
530526
return PHONGO_BSON_VECTOR_TYPE_PACKED_BIT;
531527
}
532528

533-
return PHONGO_BSON_VECTOR_TYPE_INVALID;
529+
return PHONGO_BSON_VECTOR_TYPE_UNKNOWN;
534530
}
535531

536532
static phongo_bson_vector_type_t phongo_binary_get_vector_type(const php_phongo_binary_t* intern)
@@ -549,23 +545,12 @@ static PHP_METHOD(MongoDB_BSON_Binary, getVectorType)
549545
RETURN_THROWS();
550546
}
551547

552-
phongo_bson_vector_type_t type = phongo_binary_get_vector_type(Z_BINARY_OBJ_P(getThis()));
553-
const char *type_case;
548+
const char* type_case = phongo_bson_vector_type_to_name(phongo_binary_get_vector_type(Z_BINARY_OBJ_P(getThis())));
554549

555-
switch (type) {
556-
case PHONGO_BSON_VECTOR_TYPE_FLOAT32:
557-
type_case = "Float32";
558-
break;
559-
case PHONGO_BSON_VECTOR_TYPE_INT8:
560-
type_case = "Int8";
561-
break;
562-
case PHONGO_BSON_VECTOR_TYPE_PACKED_BIT:
563-
type_case = "PackedBit";
564-
break;
565-
default:
566-
// The vector should always be valid by this point, but check for an error
567-
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Binary vector data is invalid");
568-
RETURN_THROWS();
550+
// The vector should always be valid by this point, but check for an error
551+
if (!type_case) {
552+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Binary vector data is invalid");
553+
RETURN_THROWS();
569554
}
570555

571556
RETVAL_OBJ_COPY(zend_enum_get_case_cstr(php_phongo_vectortype_ce, type_case));
@@ -576,7 +561,7 @@ static void phongo_binary_get_vector_as_array(const php_phongo_binary_t* intern,
576561
phongo_bson_vector_type_t type = phongo_binary_get_vector_type(intern);
577562

578563
// The vector should always be valid by this point, but check for an error
579-
if (type == PHONGO_BSON_VECTOR_TYPE_INVALID) {
564+
if (type == PHONGO_BSON_VECTOR_TYPE_UNKNOWN) {
580565
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Binary vector data is invalid");
581566
RETURN_THROWS();
582567
}

src/BSON/Binary.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@
1919

2020
#define PHONGO_BINARY_UUID_SIZE 16
2121

22-
typedef enum {
23-
PHONGO_BSON_VECTOR_TYPE_INVALID = 0,
24-
PHONGO_BSON_VECTOR_TYPE_INT8 = 0x03,
25-
PHONGO_BSON_VECTOR_TYPE_FLOAT32 = 0x27,
26-
PHONGO_BSON_VECTOR_TYPE_PACKED_BIT = 0x10,
27-
} phongo_bson_vector_type_t;
28-
2922
bool phongo_binary_new(zval* object, const char* data, size_t data_len, bson_subtype_t type);
3023

3124
#endif /* PHONGO_BSON_BINARY_H */

src/BSON/VectorType.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-present MongoDB, Inc.
2+
* Copyright 2025-present MongoDB, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,10 +18,42 @@
1818
#include <Zend/zend_enum.h>
1919

2020
#include "php_phongo.h"
21+
#include "VectorType.h"
2122
#include "VectorType_arginfo.h"
2223

2324
zend_class_entry* php_phongo_vectortype_ce;
2425

26+
phongo_bson_vector_type_t phongo_bson_vector_type_from_name(const char* name)
27+
{
28+
if (!strcmp(name, PHONGO_BSON_VECTOR_TYPE_FLOAT32_NAME)) {
29+
return PHONGO_BSON_VECTOR_TYPE_FLOAT32;
30+
}
31+
32+
if (!strcmp(name, PHONGO_BSON_VECTOR_TYPE_INT8_NAME)) {
33+
return PHONGO_BSON_VECTOR_TYPE_INT8;
34+
}
35+
36+
if (!strcmp(name, PHONGO_BSON_VECTOR_TYPE_PACKED_BIT_NAME)) {
37+
return PHONGO_BSON_VECTOR_TYPE_PACKED_BIT;
38+
}
39+
40+
return PHONGO_BSON_VECTOR_TYPE_UNKNOWN;
41+
}
42+
43+
const char* phongo_bson_vector_type_to_name(phongo_bson_vector_type_t type)
44+
{
45+
switch (type) {
46+
case PHONGO_BSON_VECTOR_TYPE_FLOAT32:
47+
return PHONGO_BSON_VECTOR_TYPE_FLOAT32_NAME;
48+
case PHONGO_BSON_VECTOR_TYPE_INT8:
49+
return PHONGO_BSON_VECTOR_TYPE_INT8_NAME;
50+
case PHONGO_BSON_VECTOR_TYPE_PACKED_BIT:
51+
return PHONGO_BSON_VECTOR_TYPE_PACKED_BIT_NAME;
52+
default:
53+
return NULL;
54+
}
55+
}
56+
2557
void php_phongo_vectortype_init_ce(INIT_FUNC_ARGS)
2658
{
2759
php_phongo_vectortype_ce = register_class_MongoDB_BSON_VectorType();

src/BSON/VectorType.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2025-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef PHONGO_BSON_VECTORTYPE_H
18+
#define PHONGO_BSON_VECTORTYPE_H
19+
20+
#define PHONGO_BSON_VECTOR_TYPE_FLOAT32_NAME "Float32"
21+
#define PHONGO_BSON_VECTOR_TYPE_INT8_NAME "Int8"
22+
#define PHONGO_BSON_VECTOR_TYPE_PACKED_BIT_NAME "PackedBit"
23+
24+
typedef enum {
25+
PHONGO_BSON_VECTOR_TYPE_FLOAT32 = 0x27,
26+
PHONGO_BSON_VECTOR_TYPE_INT8 = 0x03,
27+
PHONGO_BSON_VECTOR_TYPE_PACKED_BIT = 0x10,
28+
PHONGO_BSON_VECTOR_TYPE_UNKNOWN = 0,
29+
} phongo_bson_vector_type_t;
30+
31+
phongo_bson_vector_type_t phongo_bson_vector_type_from_name(const char* name);
32+
const char* phongo_bson_vector_type_to_name(phongo_bson_vector_type_t type);
33+
34+
#endif /* PHONGO_BSON_VECTORTYPE_H */

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ MongoDB\BSON\Binary::fromVector() construction of various vector types
44
<?php
55

66
var_dump(MongoDB\BSON\Binary::fromVector([1.0, -1.0, 0.5, -0.5], MongoDB\BSON\VectorType::Float32));
7-
var_dump(MongoDB\BSON\Binary::fromVector([1, 2, 3, 4], MongoDB\BSON\VectorType::Int8));
7+
var_dump(MongoDB\BSON\Binary::fromVector([-128, 0, 1, 127], MongoDB\BSON\VectorType::Int8));
88
var_dump(MongoDB\BSON\Binary::fromVector([1, 0, true, false], MongoDB\BSON\VectorType::PackedBit));
99

1010
?>
@@ -38,13 +38,13 @@ object(MongoDB\BSON\Binary)#%d (%d) {
3838
["vector"]=>
3939
array(4) {
4040
[0]=>
41-
int(1)
41+
int(-128)
4242
[1]=>
43-
int(2)
43+
int(0)
4444
[2]=>
45-
int(3)
45+
int(1)
4646
[3]=>
47-
int(4)
47+
int(127)
4848
}
4949
["vectorType"]=>
5050
int(3)

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ echo throws(function() {
1414
}, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n";
1515

1616
echo throws(function() {
17-
MongoDB\BSON\Binary::fromVector([1, 256], MongoDB\BSON\VectorType::Int8);
17+
MongoDB\BSON\Binary::fromVector([1, -129], MongoDB\BSON\VectorType::Int8);
18+
}, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n";
19+
20+
echo throws(function() {
21+
MongoDB\BSON\Binary::fromVector([1, 128], MongoDB\BSON\VectorType::Int8);
1822
}, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n";
1923

2024
?>
@@ -26,5 +30,7 @@ Expected vector to be a list
2630
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
2731
Expected vector[1] to be an integer, float given
2832
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
29-
Expected vector[1] to be a signed 8-bit integer, 256 given
33+
Expected vector[1] to be a signed 8-bit integer, -129 given
34+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
35+
Expected vector[1] to be a signed 8-bit integer, 128 given
3036
===DONE===

tests/bson/bson-binary-fromVector_error-003.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ echo throws(function() {
2424
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
2525
Expected vector to be a list
2626
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
27-
Expected vector[1] to be an integer or boolean, float given
27+
Expected vector[1] to be 0, 1, or a boolean, float given
2828
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
2929
Expected vector[1] to be 0 or 1, 2 given
3030
===DONE===

0 commit comments

Comments
 (0)