@@ -107,10 +107,18 @@ def _access_member(field):
107
107
return member_name
108
108
109
109
110
+ def _std_array_expr (value_type , elems ):
111
+ # type: (str, List[str]) -> str
112
+ """Return a std::array<value_type, N>{elems} expression."""
113
+ elem_str = ', ' .join (elems )
114
+ return f'std::array<{ value_type } , { len (elems )} >{{{ elem_str } }}'
115
+
116
+
110
117
def _get_bson_type_check (bson_element , ctxt_name , ast_type ):
111
118
# type: (str, str, ast.Type) -> str
112
119
"""Get the C++ bson type check for a Type."""
113
- bson_types = ast_type .bson_serialization_type
120
+ # Deduplicate the types in the array.
121
+ bson_types = list (set (ast_type .bson_serialization_type ))
114
122
if len (bson_types ) == 1 :
115
123
if bson_types [0 ] in ['any' , 'chain' ]:
116
124
# Skip BSON validation for 'any' types since they are required to validate the
@@ -120,13 +128,14 @@ def _get_bson_type_check(bson_element, ctxt_name, ast_type):
120
128
return None
121
129
122
130
if not bson_types [0 ] == 'bindata' :
123
- return '%s.checkAndAssertType(%s, %s)' % (ctxt_name , bson_element ,
124
- bson .cpp_bson_type_name (bson_types [0 ]))
125
- return '%s.checkAndAssertBinDataType(%s, %s)' % (
131
+ return 'MONGO_likely( %s.checkAndAssertType(%s, %s)) ' % (
132
+ ctxt_name , bson_element , bson .cpp_bson_type_name (bson_types [0 ]))
133
+ return 'MONGO_likely( %s.checkAndAssertBinDataType(%s, %s) )' % (
126
134
ctxt_name , bson_element , bson .cpp_bindata_subtype_type_name (ast_type .bindata_subtype ))
127
135
else :
128
- type_list = '{%s}' % (', ' .join ([bson .cpp_bson_type_name (b ) for b in bson_types ]))
129
- return '%s.checkAndAssertTypes(%s, %s)' % (ctxt_name , bson_element , type_list )
136
+ return (
137
+ f'MONGO_likely({ ctxt_name } .checkAndAssertTypes({ bson_element } , '
138
+ f'{ _std_array_expr ("BSONType" , [bson .cpp_bson_type_name (b ) for b in bson_types ])} ))' )
130
139
131
140
132
141
def _get_required_fields (struct ):
@@ -1383,7 +1392,6 @@ def _gen_array_deserializer(self, field, bson_element, ast_type, tenant):
1383
1392
1384
1393
with self ._predicate ('MONGO_likely(arrayFieldName == expectedFieldNumber)' ):
1385
1394
check = _get_bson_type_check ('arrayElement' , 'arrayCtxt' , ast_type )
1386
- check = "MONGO_likely(%s)" % (check ) if check is not None else check
1387
1395
1388
1396
with self ._predicate (check ):
1389
1397
if ast_type .is_variant :
@@ -1456,11 +1464,12 @@ def _gen_variant_deserializer(self, field, bson_element, tenant):
1456
1464
1457
1465
self ._writer .write_line ('default:' )
1458
1466
self ._writer .indent ()
1459
- expected_types = ', ' . join (
1460
- 'BSONType::%s' % bson .cpp_bson_type_name (t .bson_serialization_type [0 ])
1461
- for t in array_types )
1467
+ expected_types = [
1468
+ bson .cpp_bson_type_name (t .bson_serialization_type [0 ]) for t in array_types
1469
+ ]
1462
1470
self ._writer .write_line (
1463
- 'ctxt.throwBadType(%s, {%s});' % (bson_element , expected_types ))
1471
+ f'ctxt.throwBadType({ bson_element } , { _std_array_expr ("BSONType" , expected_types )} );'
1472
+ )
1464
1473
self ._writer .write_line ('break;' )
1465
1474
self ._writer .unindent ()
1466
1475
# End of inner switch.
@@ -1497,10 +1506,11 @@ def _gen_variant_deserializer(self, field, bson_element, tenant):
1497
1506
1498
1507
self ._writer .write_line ('default:' )
1499
1508
self ._writer .indent ()
1500
- expected_types = ', ' .join (
1501
- 'BSONType::%s' % bson .cpp_bson_type_name (t .bson_serialization_type [0 ])
1502
- for t in scalar_types )
1503
- self ._writer .write_line ('ctxt.throwBadType(%s, {%s});' % (bson_element , expected_types ))
1509
+ expected_types = [
1510
+ bson .cpp_bson_type_name (t .bson_serialization_type [0 ]) for t in array_types
1511
+ ]
1512
+ self ._writer .write_line (f'ctxt.throwBadType({ bson_element } , '
1513
+ f'{ _std_array_expr ("BSONType" , expected_types )} );' )
1504
1514
self ._writer .write_line ('break;' )
1505
1515
self ._writer .unindent ()
1506
1516
@@ -1615,8 +1625,6 @@ def validate_and_assign_or_uassert(field, expression):
1615
1625
predicate = None
1616
1626
if check_type :
1617
1627
predicate = _get_bson_type_check (bson_element , 'ctxt' , field_type )
1618
- if predicate :
1619
- predicate = "MONGO_likely(%s)" % (predicate )
1620
1628
1621
1629
with self ._predicate (predicate ):
1622
1630
0 commit comments