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

Prefix Yaml tags with !bson_ #43

Merged
merged 3 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ The `generator/config/*.yaml` files contains the list of operators and stages th
Each operator can contain a `tests` section with a list if pipelines. To represent specific BSON objects,
it is necessary to use Yaml tags:

| BSON Type | Example |
|-------------|----------------------------------------------|
| Regex | `!regex '^abc'` <br/> `!regex ['^abc', 'i']` |
| Int64 | `!long '123456789'` |
| Decimal128 | `!double '0.9'` |
| UTCDateTime | `!date 0` |
| Binary | `!binary 'IA=='` |
| BSON Type | Example |
|-------------|--------------------------------------------------------|
| Regex | `!bson_regex '^abc'` <br/> `!bson_regex ['^abc', 'i']` |
| Int64 | `!bson_int64 '123456789'` |
| Decimal128 | `!bson_decimal128 '0.9'` |
| UTCDateTime | `!bson_utcdatetime 0` |
| Binary | `!bson_binary 'IA=='` |

To add new test cases to operators, you can get inspiration from the official MongoDB documentation and use
the `generator/js2yaml.html` web page to manually convert a pipeline array from JS to Yaml.
2 changes: 1 addition & 1 deletion generator/config/expression/dateFromString.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,5 @@ tests:
dateString: '$date'
timezone: '$timezone'
# onNull: new Date(0)
onNull: !date 0
onNull: !bson_utcdatetime 0

2 changes: 1 addition & 1 deletion generator/config/query/bitsAllClear.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ tests:
pipeline:
- $match:
a:
$bitsAllClear: !binary 'IA=='
$bitsAllClear: !bson_binary 'IA=='
2 changes: 1 addition & 1 deletion generator/config/query/bitsAllSet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ tests:
pipeline:
- $match:
a:
$bitsAllSet: !binary 'MA=='
$bitsAllSet: !bson_binary 'MA=='
2 changes: 1 addition & 1 deletion generator/config/query/bitsAnyClear.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ tests:
pipeline:
- $match:
a:
$bitsAnyClear: !binary 'MA=='
$bitsAnyClear: !bson_binary 'MA=='
2 changes: 1 addition & 1 deletion generator/config/query/bitsAnySet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ tests:
pipeline:
- $match:
a:
$bitsAnySet: !binary 'MA=='
$bitsAnySet: !bson_binary 'MA=='
4 changes: 2 additions & 2 deletions generator/config/query/eq.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ tests:
-
$match:
company:
!regex '^MongoDB'
!bson_regex '^MongoDB'
-
$match:
company:
$eq:
!regex '^MongoDB'
!bson_regex '^MongoDB'
4 changes: 2 additions & 2 deletions generator/config/query/in.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ tests:
$match:
tags:
$in:
- !regex '^be'
- !regex '^st'
- !bson_regex '^be'
- !bson_regex '^st'
2 changes: 1 addition & 1 deletion generator/config/query/not.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ tests:
-
$match:
price:
$not: !regex '^p.*'
$not: !bson_regex '^p.*'
4 changes: 2 additions & 2 deletions generator/config/query/regex.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ tests:
$match:
sku:
$regex:
!regex '789$'
!bson_regex '789$'
-
name: 'Perform Case-Insensitive Regular Expression Match'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/regex/#perform-case-insensitive-regular-expression-match'
Expand All @@ -30,4 +30,4 @@ tests:
$match:
sku:
$regex:
!regex ['^ABC', 'i']
!bson_regex ['^ABC', 'i']
6 changes: 3 additions & 3 deletions generator/js2yaml.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ <h1>Convert JS examples into Yaml</h1>
}

function BinData(type, value) {
return new TaggedValue('binary', value);
return new TaggedValue('bson_binary', value);
}

function convert(jsString) {
Expand All @@ -91,11 +91,11 @@ <h1>Convert JS examples into Yaml</h1>
}

if (object instanceof RegExp) {
return ' !regex ' + (object.flags ? "['" + object.source + "', '" + object.flags + "']" : "'" + object.source + "'");
return ' !bson_regex ' + (object.flags ? "['" + object.source + "', '" + object.flags + "']" : "'" + object.source + "'");
}

if (object instanceof Date) {
return ' !date ' + object.getTime();
return ' !bson_utcdatetime ' + object.getTime();
}

if (object instanceof TaggedValue) {
Expand Down
10 changes: 5 additions & 5 deletions generator/src/OperatorTestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ private function convertYamlTaggedValues(mixed $object): mixed
$value = $object->getValue();

return match ($object->getTag()) {
'regex' => new Regex(...(array) $value),
'long' => new Int64($value),
'double' => new Decimal128($value),
'date' => new UTCDateTime($value),
'binary' => new Binary(base64_decode($value)),
'bson_regex' => new Regex(...(array) $value),
'bson_int128' => new Int64($value),
'bson_decimal128' => new Decimal128($value),
'bson_utcdatetime' => new UTCDateTime($value),
'bson_binary' => new Binary(base64_decode($value)),
default => throw new InvalidArgumentException(sprintf('Yaml tag "%s" is not supported.', $object->getTag())),
};
}
Expand Down