@@ -107,10 +107,18 @@ def _access_member(field):
107107 return member_name
108108
109109
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+
110117def _get_bson_type_check (bson_element , ctxt_name , ast_type ):
111118 # type: (str, str, ast.Type) -> str
112119 """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 ))
114122 if len (bson_types ) == 1 :
115123 if bson_types [0 ] in ['any' , 'chain' ]:
116124 # 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):
120128 return None
121129
122130 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) )' % (
126134 ctxt_name , bson_element , bson .cpp_bindata_subtype_type_name (ast_type .bindata_subtype ))
127135 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 ])} ))' )
130139
131140
132141def _get_required_fields (struct ):
@@ -1383,7 +1392,6 @@ def _gen_array_deserializer(self, field, bson_element, ast_type, tenant):
13831392
13841393 with self ._predicate ('MONGO_likely(arrayFieldName == expectedFieldNumber)' ):
13851394 check = _get_bson_type_check ('arrayElement' , 'arrayCtxt' , ast_type )
1386- check = "MONGO_likely(%s)" % (check ) if check is not None else check
13871395
13881396 with self ._predicate (check ):
13891397 if ast_type .is_variant :
@@ -1456,11 +1464,12 @@ def _gen_variant_deserializer(self, field, bson_element, tenant):
14561464
14571465 self ._writer .write_line ('default:' )
14581466 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+ ]
14621470 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+ )
14641473 self ._writer .write_line ('break;' )
14651474 self ._writer .unindent ()
14661475 # End of inner switch.
@@ -1497,10 +1506,11 @@ def _gen_variant_deserializer(self, field, bson_element, tenant):
14971506
14981507 self ._writer .write_line ('default:' )
14991508 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 )} );' )
15041514 self ._writer .write_line ('break;' )
15051515 self ._writer .unindent ()
15061516
@@ -1615,8 +1625,6 @@ def validate_and_assign_or_uassert(field, expression):
16151625 predicate = None
16161626 if check_type :
16171627 predicate = _get_bson_type_check (bson_element , 'ctxt' , field_type )
1618- if predicate :
1619- predicate = "MONGO_likely(%s)" % (predicate )
16201628
16211629 with self ._predicate (predicate ):
16221630
0 commit comments