This is an automated email from the ASF dual-hosted git repository.
dkulp pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/master by this push:
new f5d2f11 Add support for uuid logical type in C++
f5d2f11 is described below
commit f5d2f11972d53eca4e6ce6f8b0e16218a7c4d6c7
Author: Andrew Onyshchuk <[email protected]>
AuthorDate: Thu Jan 2 18:06:49 2020 -0600
Add support for uuid logical type in C++
---
lang/c++/api/LogicalType.hh | 3 ++-
lang/c++/impl/Compiler.cc | 2 ++
lang/c++/impl/LogicalType.cc | 3 +++
lang/c++/impl/Node.cc | 6 ++++++
lang/c++/test/SchemaTests.cc | 15 +++++++++++++++
5 files changed, 28 insertions(+), 1 deletion(-)
diff --git a/lang/c++/api/LogicalType.hh b/lang/c++/api/LogicalType.hh
index 6940b06..3397278 100644
--- a/lang/c++/api/LogicalType.hh
+++ b/lang/c++/api/LogicalType.hh
@@ -35,7 +35,8 @@ class AVRO_DECL LogicalType {
TIME_MICROS,
TIMESTAMP_MILLIS,
TIMESTAMP_MICROS,
- DURATION
+ DURATION,
+ UUID
};
explicit LogicalType(Type type);
diff --git a/lang/c++/impl/Compiler.cc b/lang/c++/impl/Compiler.cc
index 2e1c462..6453db8 100644
--- a/lang/c++/impl/Compiler.cc
+++ b/lang/c++/impl/Compiler.cc
@@ -368,6 +368,8 @@ static LogicalType makeLogicalType(const Entity& e, const
Object& m) {
t = LogicalType::TIMESTAMP_MICROS;
else if (typeField == "duration")
t = LogicalType::DURATION;
+ else if (typeField == "uuid")
+ t = LogicalType::UUID;
return LogicalType(t);
}
diff --git a/lang/c++/impl/LogicalType.cc b/lang/c++/impl/LogicalType.cc
index f48cd33..a0d9cc3 100644
--- a/lang/c++/impl/LogicalType.cc
+++ b/lang/c++/impl/LogicalType.cc
@@ -75,6 +75,9 @@ void LogicalType::printJson(std::ostream& os) const {
case DURATION:
os << "\"logicalType\": \"duration\"";
break;
+ case UUID:
+ os << "\"logicalType\": \"uuid\"";
+ break;
}
}
diff --git a/lang/c++/impl/Node.cc b/lang/c++/impl/Node.cc
index f1c3508..bb510cc 100644
--- a/lang/c++/impl/Node.cc
+++ b/lang/c++/impl/Node.cc
@@ -147,6 +147,12 @@ void Node::setLogicalType(LogicalType logicalType) {
"FIXED type of size 12");
}
break;
+ case LogicalType::UUID:
+ if (type_ != AVRO_STRING) {
+ throw Exception("UUID logical type can only annotate "
+ "STRING type");
+ }
+ break;
}
logicalType_ = logicalType;
diff --git a/lang/c++/test/SchemaTests.cc b/lang/c++/test/SchemaTests.cc
index 27e049a..b7de980 100644
--- a/lang/c++/test/SchemaTests.cc
+++ b/lang/c++/test/SchemaTests.cc
@@ -212,6 +212,7 @@ const char* roundTripSchemas[] = {
"{\"type\":\"long\",\"logicalType\":\"timestamp-millis\"}",
"{\"type\":\"long\",\"logicalType\":\"timestamp-micros\"}",
"{\"type\":\"fixed\",\"name\":\"test\",\"size\":12,\"logicalType\":\"duration\"}",
+ "{\"type\":\"string\",\"logicalType\":\"uuid\"}",
// namespace with '$' in it.
"{\"type\":\"record\",\"namespace\":\"a.b$\",\"name\":\"Test\",\"fields\":"
@@ -227,6 +228,7 @@ const char* malformedLogicalTypes[] = {
"{\"type\":\"string\",\"logicalType\":\"timestamp-millis\"}",
"{\"type\":\"string\",\"logicalType\":\"timestamp-micros\"}",
"{\"type\":\"string\",\"logicalType\":\"duration\"}",
+ "{\"type\":\"long\",\"logicalType\":\"uuid\"}",
// Missing the required field 'precision'.
"{\"type\":\"bytes\",\"logicalType\":\"decimal\"}",
// The claimed precision is not supported by the size of the fixed type.
@@ -349,6 +351,10 @@ static void testLogicalTypes()
\"name\": \"durationType\",\n\
\"logicalType\": \"duration\"\n\
}";
+ const char* uuidType = "{\n\
+ \"type\": \"string\",\n\
+ \"logicalType\": \"uuid\"\n\
+ }";
{
BOOST_TEST_CHECKPOINT(bytesDecimalType);
ValidSchema schema1 = compileJsonSchemaFromString(bytesDecimalType);
@@ -427,6 +433,15 @@ static void testLogicalTypes()
GenericDatum datum(schema);
BOOST_CHECK(datum.logicalType().type() == LogicalType::DURATION);
}
+ {
+ BOOST_TEST_CHECKPOINT(uuidType);
+ ValidSchema schema = compileJsonSchemaFromString(uuidType);
+ BOOST_CHECK(schema.root()->type() == AVRO_STRING);
+ LogicalType logicalType = schema.root()->logicalType();
+ BOOST_CHECK(logicalType.type() == LogicalType::UUID);
+ GenericDatum datum(schema);
+ BOOST_CHECK(datum.logicalType().type() == LogicalType::UUID);
+ }
}
static void testMalformedLogicalTypes(const char* schema)