This is an automated email from the ASF dual-hosted git repository.

thiru pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/main by this push:
     new d5a0ebd4d AVRO-4117: [c++] Allow non string values for non reserved 
keys in schema (#3308)
d5a0ebd4d is described below

commit d5a0ebd4d55fa930f3b44cf712ad6c5233079aa5
Author: Manik Malhotra <[email protected]>
AuthorDate: Wed Feb 12 22:04:00 2025 -0500

    AVRO-4117: [c++] Allow non string values for non reserved keys in schema 
(#3308)
    
    * AVRO-4117: Allow string values for non reserved keys in schema
    
    * AVRO-4117: [c++] Allow non string values for non reserved keys in schema
    
    * use camelCase
    
    ---------
    
    Co-authored-by: Manik Malhotra <[email protected]>
---
 lang/c++/impl/Compiler.cc     |  2 +-
 lang/c++/impl/json/JsonDom.cc | 17 +++++++++++++++++
 lang/c++/impl/json/JsonDom.hh |  1 +
 lang/c++/test/SchemaTests.cc  |  4 ++++
 4 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/lang/c++/impl/Compiler.cc b/lang/c++/impl/Compiler.cc
index 9fdd40c12..0e48e06bb 100644
--- a/lang/c++/impl/Compiler.cc
+++ b/lang/c++/impl/Compiler.cc
@@ -291,7 +291,7 @@ static void getCustomAttributes(const Object &m, 
CustomAttributes &customAttribu
     const std::unordered_set<std::string> &kKnownFields = getKnownFields();
     for (const auto &entry : m) {
         if (kKnownFields.find(entry.first) == kKnownFields.end()) {
-            customAttributes.addAttribute(entry.first, 
entry.second.stringValue());
+            customAttributes.addAttribute(entry.first, 
entry.second.toLiteralString());
         }
     }
 }
diff --git a/lang/c++/impl/json/JsonDom.cc b/lang/c++/impl/json/JsonDom.cc
index b36abad11..504c44554 100644
--- a/lang/c++/impl/json/JsonDom.cc
+++ b/lang/c++/impl/json/JsonDom.cc
@@ -177,5 +177,22 @@ std::string Entity::toString() const {
     return result;
 }
 
+std::string Entity::toLiteralString() const {
+    switch (type_) {
+        case EntityType::Null:
+            return "null";
+        case EntityType::Bool:
+            return boolValue() ? "true" : "false";
+        case EntityType::Long:
+            return std::to_string(longValue());
+        case EntityType::Double:
+            return std::to_string(doubleValue());
+        case EntityType::String:
+            return stringValue();
+        default:
+           return toString();
+    }
+}
+
 } // namespace json
 } // namespace avro
diff --git a/lang/c++/impl/json/JsonDom.hh b/lang/c++/impl/json/JsonDom.hh
index 8aa180972..75728c6ba 100644
--- a/lang/c++/impl/json/JsonDom.hh
+++ b/lang/c++/impl/json/JsonDom.hh
@@ -127,6 +127,7 @@ public:
     }
 
     std::string toString() const;
+    std::string toLiteralString() const;
 };
 
 template<typename T>
diff --git a/lang/c++/test/SchemaTests.cc b/lang/c++/test/SchemaTests.cc
index bda02ad4c..857beadf9 100644
--- a/lang/c++/test/SchemaTests.cc
+++ b/lang/c++/test/SchemaTests.cc
@@ -144,6 +144,10 @@ const char *basicSchemas[] = {
     })",
     R"({"type": "enum", "name": "Test", "symbols": ["A", "B"],"extra 
attribute": 1})",
     R"({"type": "array", "items": "long", "extra attribute": "1"})",
+    R"({"type": "array", "items": "long", "extra attribute": 1})",
+    R"({"type": "array", "items": "long", "extra attribute": true})",
+    R"({"type": "array", "items": "long", "extra attribute": 1.1})",
+    R"({"type": "array", "items": "long", "extra attribute": {"extra extra 
attribute": "1"}})",
     R"({"type": "map", "values": "long", "extra attribute": 1})",
     R"({"type": "fixed", "name": "Test", "size": 1, "extra attribute": 1})",
 

Reply via email to