Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

Commit 2cb9464

Browse files
committed
Switched to Yaml tags to represent specific BSON objects
1 parent 2503e45 commit 2cb9464

File tree

8 files changed

+39
-40
lines changed

8 files changed

+39
-40
lines changed

generator/config/expression/dateFromString.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,5 @@ tests:
7777
dateString: '$date'
7878
timezone: '$timezone'
7979
# onNull: new Date(0)
80-
onNull: 1970-01-01T00:00:00+00:00
80+
onNull: !date 0
8181

generator/config/query/eq.yaml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,9 @@ tests:
5353
-
5454
$match:
5555
company:
56-
$regularExpression:
57-
options: ''
58-
pattern: '^MongoDB'
56+
!regex '^MongoDB'
5957
-
6058
$match:
6159
company:
6260
$eq:
63-
$regularExpression:
64-
options: ''
65-
pattern: '^MongoDB'
61+
!regex '^MongoDB'

generator/config/query/in.yaml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,5 @@ tests:
2828
$match:
2929
tags:
3030
$in:
31-
-
32-
$regularExpression:
33-
options: ''
34-
pattern: '^be'
35-
-
36-
$regularExpression:
37-
options: ''
38-
pattern: '^st'
31+
- !regex '^be'
32+
- !regex '^st'

generator/config/query/not.yaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,4 @@ tests:
2828
-
2929
$match:
3030
price:
31-
$not:
32-
$regularExpression:
33-
pattern: '^p.*'
34-
options: ''
31+
$not: !regex '^p.*'

generator/config/query/regex.yaml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ tests:
2121
$match:
2222
sku:
2323
$regex:
24-
$regularExpression:
25-
pattern: '789$'
26-
options: ''
24+
!regex '789$'
2725
-
2826
name: 'Perform Case-Insensitive Regular Expression Match'
2927
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/regex/#perform-case-insensitive-regular-expression-match'
@@ -32,6 +30,4 @@ tests:
3230
$match:
3331
sku:
3432
$regex:
35-
$regularExpression:
36-
pattern: '^ABC'
37-
options: 'i'
33+
!regex ['^ABC', 'i']

generator/src/Definition/YamlReader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function read(string $dirname): array
2121
foreach ($finder as $file) {
2222
$operator = Yaml::parseFile(
2323
$file->getPathname(),
24-
Yaml::PARSE_OBJECT | Yaml::PARSE_OBJECT_FOR_MAP | Yaml::PARSE_DATETIME,
24+
Yaml::PARSE_OBJECT | Yaml::PARSE_OBJECT_FOR_MAP | Yaml::PARSE_CUSTOM_TAGS,
2525
);
2626
$definitions[] = new OperatorDefinition(...get_object_vars($operator));
2727
}

generator/src/OperatorTestGenerator.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
namespace MongoDB\CodeGenerator;
66

77
use DateTimeInterface;
8+
use InvalidArgumentException;
9+
use MongoDB\BSON\Binary;
10+
use MongoDB\BSON\Decimal128;
811
use MongoDB\BSON\Document;
12+
use MongoDB\BSON\Int64;
13+
use MongoDB\BSON\Regex;
914
use MongoDB\BSON\UTCDateTime;
1015
use MongoDB\Builder\Pipeline;
1116
use MongoDB\CodeGenerator\Definition\GeneratorDefinition;
@@ -16,6 +21,7 @@
1621
use Nette\PhpGenerator\PhpNamespace;
1722
use Nette\PhpGenerator\Type;
1823
use RuntimeException;
24+
use Symfony\Component\Yaml\Tag\TaggedValue;
1925
use Throwable;
2026

2127
use function basename;
@@ -89,7 +95,7 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
8995
$testName = 'test' . str_replace([' ', '-'], '', ucwords(str_replace('$', '', $test->name)));
9096
$caseName = str_replace([' ', '-'], '', ucwords(str_replace('$', '', $operator->name . ' ' . $test->name)));
9197

92-
$pipeline = $this->convertTypeRecursively($test->pipeline);
98+
$pipeline = $this->convertYamlTaggedValues($test->pipeline);
9399

94100
// Wrap the pipeline array into a document
95101
$json = Document::fromPHP(['pipeline' => $pipeline])->toCanonicalExtendedJSON();
@@ -126,23 +132,33 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
126132
return $namespace;
127133
}
128134

129-
private function convertTypeRecursively(mixed $object): mixed
135+
private function convertYamlTaggedValues(mixed $object): mixed
130136
{
131-
if ($object instanceof DateTimeInterface) {
132-
return new UTCDateTime($object);
137+
if ($object instanceof TaggedValue) {
138+
$value = $object->getValue();
139+
140+
return match ($object->getTag()) {
141+
// !regex 'pattern' or !regex ['pattern', 'options']
142+
'regex' => new Regex(...(array) $value),
143+
'long' => new Int64($value),
144+
'decimal' => new Decimal128($value),
145+
'date' => new UTCDateTime($value),
146+
'binary' => new Binary($value),
147+
default => throw new InvalidArgumentException(sprintf('Yaml tag "%s" is not supported.', $object->getTag())),
148+
};
133149
}
134150

135151
if (is_array($object)) {
136152
foreach ($object as $key => $value) {
137-
$object[$key] = $this->convertTypeRecursively($value);
153+
$object[$key] = $this->convertYamlTaggedValues($value);
138154
}
139155

140156
return $object;
141157
}
142158

143159
if (is_object($object)) {
144160
foreach (get_object_vars($object) as $key => $value) {
145-
$object->{$key} = $this->convertTypeRecursively($value);
161+
$object->{$key} = $this->convertYamlTaggedValues($value);
146162
}
147163

148164
return $object;

tests/Builder/Query/Pipelines.php

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)