mirror of
https://github.com/mongodb/mongo.git
synced 2024-12-01 09:32:32 +01:00
SERVER-29089 Add UUID to IDL basic_types.idl
This commit is contained in:
parent
638339bd4d
commit
5ab83cb6e3
@ -599,8 +599,12 @@ class _BinDataBsonCppTypeBase(BsonCppTypeBase):
|
||||
|
||||
def gen_deserializer_expression(self, indented_writer, object_instance):
|
||||
# type: (writer.IndentedTextWriter, unicode) -> unicode
|
||||
return common.template_args(
|
||||
'${object_instance}._binDataVector()', object_instance=object_instance)
|
||||
if self._field.bindata_subtype == 'uuid':
|
||||
return common.template_args(
|
||||
'${object_instance}.uuid()', object_instance=object_instance)
|
||||
else:
|
||||
return common.template_args(
|
||||
'${object_instance}._binDataVector()', object_instance=object_instance)
|
||||
|
||||
def has_serializer(self):
|
||||
# type: () -> bool
|
||||
|
@ -465,6 +465,11 @@ def parse(stream, input_file_name, resolver):
|
||||
imports += [(parsed_doc.spec.imports, resolved_file_name, import_file_name)
|
||||
for import_file_name in parsed_doc.spec.imports.imports]
|
||||
|
||||
# Merge cpp_includes as needed
|
||||
if parsed_doc.spec.globals and parsed_doc.spec.globals.cpp_includes:
|
||||
root_doc.spec.globals.cpp_includes = list(
|
||||
set(root_doc.spec.globals.cpp_includes + parsed_doc.spec.globals.cpp_includes))
|
||||
|
||||
# Merge symbol tables together
|
||||
root_doc.spec.symbols.add_imported_symbol_table(ctxt, parsed_doc.spec.symbols)
|
||||
if ctxt.errors.has_errors():
|
||||
|
@ -24,5 +24,6 @@ env.CppUnitTest(
|
||||
'$BUILD_DIR/mongo/base',
|
||||
'$BUILD_DIR/mongo/db/namespace_string',
|
||||
'$BUILD_DIR/mongo/idl/idl_parser',
|
||||
'$BUILD_DIR/mongo/util/uuid',
|
||||
],
|
||||
)
|
||||
|
@ -16,7 +16,10 @@
|
||||
# IDL Basic Types
|
||||
global:
|
||||
cpp_namespace: "mongo"
|
||||
|
||||
cpp_includes:
|
||||
- "mongo/db/namespace_string.h"
|
||||
- "mongo/util/uuid.h"
|
||||
|
||||
types:
|
||||
string:
|
||||
bson_serialization_type: string
|
||||
@ -75,6 +78,14 @@ types:
|
||||
cpp_type: "std::array<std::uint8_t, 16>"
|
||||
deserializer: "mongo::BSONElement::uuid"
|
||||
|
||||
uuid:
|
||||
bson_serialization_type: bindata
|
||||
bindata_subtype: uuid
|
||||
description: "A UUID"
|
||||
cpp_type: "mongo::UUID"
|
||||
deserializer: "UUID"
|
||||
serializer: "mongo::UUID::toCDR"
|
||||
|
||||
bindata_md5:
|
||||
bson_serialization_type: bindata
|
||||
bindata_subtype: md5
|
||||
@ -103,4 +114,11 @@ types:
|
||||
bson_serialization_type: timestamp
|
||||
description: "A BSON TimeStamp"
|
||||
cpp_type: "mongo::Timestamp"
|
||||
deserializer: "mongo::BSONElement::timestamp"
|
||||
deserializer: "mongo::BSONElement::timestamp"
|
||||
|
||||
namespacestring:
|
||||
bson_serialization_type: string
|
||||
description: "A MongoDB NamespaceString"
|
||||
cpp_type: "mongo::NamespaceString"
|
||||
serializer: mongo::NamespaceString::toString
|
||||
deserializer: mongo::NamespaceString
|
||||
|
@ -989,6 +989,7 @@ TEST(IDLBinData, TestParse) {
|
||||
TestBinDataParser<One_function, Function>();
|
||||
TestBinDataParser<One_uuid, newUUID>();
|
||||
TestBinDataParser<One_md5, MD5Type>();
|
||||
TestBinDataParser<One_UUID, newUUID>();
|
||||
}
|
||||
|
||||
// Mixed: test a type that accepts a custom bindata type
|
||||
@ -1027,6 +1028,41 @@ TEST(IDLBinData, TestCustomType) {
|
||||
}
|
||||
}
|
||||
|
||||
// Positive: test a type that accepts a custom UUID type
|
||||
TEST(IDLBinData, TestUUIDclass) {
|
||||
IDLParserErrorContext ctxt("root");
|
||||
|
||||
auto uuid = UUID::gen();
|
||||
auto testDoc = BSON("value" << uuid);
|
||||
|
||||
auto element = testDoc.firstElement();
|
||||
ASSERT_EQUALS(element.type(), BinData);
|
||||
ASSERT_EQUALS(element.binDataType(), newUUID);
|
||||
|
||||
auto testStruct = One_UUID::parse(ctxt, testDoc);
|
||||
ASSERT_TRUE(testStruct.getValue() == uuid);
|
||||
|
||||
// Positive: Test we can roundtrip from the just parsed document
|
||||
{
|
||||
BSONObjBuilder builder;
|
||||
testStruct.serialize(&builder);
|
||||
auto loopbackDoc = builder.obj();
|
||||
|
||||
ASSERT_BSONOBJ_EQ(testDoc, loopbackDoc);
|
||||
}
|
||||
|
||||
// Positive: Test we can serialize from nothing the same document
|
||||
{
|
||||
BSONObjBuilder builder;
|
||||
One_UUID one_new;
|
||||
one_new.setValue(uuid);
|
||||
one_new.serialize(&builder);
|
||||
|
||||
auto serializedDoc = builder.obj();
|
||||
ASSERT_BSONOBJ_EQ(testDoc, serializedDoc);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A simple class that derives from an IDL generated class
|
||||
*/
|
||||
|
@ -18,7 +18,6 @@ global:
|
||||
# Use a nested namespace simply to exercise nested namespace support for the code generator.
|
||||
cpp_namespace: "mongo::idl::test"
|
||||
cpp_includes:
|
||||
- "mongo/db/namespace_string.h"
|
||||
- "mongo/idl/idl_test_types.h"
|
||||
|
||||
imports:
|
||||
@ -26,18 +25,6 @@ imports:
|
||||
- "mongo/idl/unittest_import.idl"
|
||||
|
||||
types:
|
||||
##################################################################################################
|
||||
#
|
||||
# Test a custom non-BSONElement deserialization and serialization methods for a string type
|
||||
#
|
||||
##################################################################################################
|
||||
|
||||
namespacestring:
|
||||
bson_serialization_type: string
|
||||
description: "A MongoDB NamespaceString"
|
||||
cpp_type: "mongo::NamespaceString"
|
||||
serializer: mongo::NamespaceString::toString
|
||||
deserializer: mongo::NamespaceString
|
||||
|
||||
##################################################################################################
|
||||
#
|
||||
|
@ -91,4 +91,9 @@ structs:
|
||||
one_timestamp:
|
||||
description: UnitTest for a single timestamp
|
||||
fields:
|
||||
value: timestamp
|
||||
value: timestamp
|
||||
|
||||
one_UUID:
|
||||
description: UnitTest for a single UUID
|
||||
fields:
|
||||
value: uuid
|
||||
|
@ -33,6 +33,7 @@
|
||||
|
||||
#include <third_party/murmurhash3/MurmurHash3.h>
|
||||
|
||||
#include "mongo/base/data_range.h"
|
||||
#include "mongo/base/status_with.h"
|
||||
#include "mongo/bson/bsonelement.h"
|
||||
#include "mongo/bson/bsonmisc.h"
|
||||
@ -47,12 +48,11 @@ namespace mongo {
|
||||
class UUID {
|
||||
using UUIDStorage = std::array<unsigned char, 16>;
|
||||
|
||||
public:
|
||||
/**
|
||||
* This constructor exists for IDL only.
|
||||
*/
|
||||
UUID() = default;
|
||||
// Make the IDL generated parser a friend
|
||||
friend class One_UUID;
|
||||
friend class Logical_session_id;
|
||||
|
||||
public:
|
||||
/**
|
||||
* The number of bytes contained in a UUID.
|
||||
*/
|
||||
@ -87,6 +87,13 @@ public:
|
||||
*/
|
||||
static bool isUUIDString(const std::string& s);
|
||||
|
||||
/**
|
||||
* Returns a ConstDataRange view of the UUID.
|
||||
*/
|
||||
ConstDataRange toCDR() const {
|
||||
return ConstDataRange(reinterpret_cast<const char*>(_uuid.data()), _uuid.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Append to builder as BinData(4, "...") element with the given name.
|
||||
*/
|
||||
@ -127,6 +134,8 @@ public:
|
||||
};
|
||||
|
||||
private:
|
||||
UUID() = default;
|
||||
|
||||
UUID(const UUIDStorage& uuid) : _uuid(uuid) {}
|
||||
|
||||
UUIDStorage _uuid; // UUID in network byte order
|
||||
|
Loading…
Reference in New Issue
Block a user