felipecrv commented on code in PR #322:
URL: https://github.com/apache/arrow-nanoarrow/pull/322#discussion_r1406455666


##########
src/nanoarrow/nanoarrow_testing.hpp:
##########
@@ -613,6 +618,458 @@ class TestingJSONWriter {
   };
 };
 
+#if defined(NANOARROW_TESTING_WITH_NLOHMANN_JSON)
+
+/// \brief Reader for the Arrow integration testing JSON format
+class TestingJSONReader {
+  using json = nlohmann::json;
+
+ public:
+  ArrowErrorCode ReadSchema(const std::string& value, ArrowSchema* out,
+                            ArrowError* error = nullptr) {
+    try {
+      auto obj = json::parse(value);
+      nanoarrow::UniqueSchema schema;
+
+      NANOARROW_RETURN_NOT_OK(SetSchema(schema.get(), obj, error));
+      ArrowSchemaMove(schema.get(), out);
+      return NANOARROW_OK;
+    } catch (std::exception& e) {
+      ArrowErrorSet(error, "Exception in TestingJSONReader::ReadSchema(): %s", 
e.what());
+      return EINVAL;
+    }
+  }
+
+  ArrowErrorCode ReadField(const std::string& value, ArrowSchema* out,
+                           ArrowError* error = nullptr) {
+    try {
+      auto obj = json::parse(value);
+      nanoarrow::UniqueSchema schema;
+
+      NANOARROW_RETURN_NOT_OK(SetField(schema.get(), obj, error));
+      ArrowSchemaMove(schema.get(), out);
+      return NANOARROW_OK;
+    } catch (std::exception& e) {
+      ArrowErrorSet(error, "Exception in TestingJSONReader::ReadField(): %s", 
e.what());
+      return EINVAL;
+    }
+  }
+
+ private:
+  ArrowErrorCode SetSchema(ArrowSchema* schema, const json& value, ArrowError* 
error) {
+    NANOARROW_RETURN_NOT_OK(
+        Check(value.is_object(), error, "Expected Schema to be a JSON 
object"));
+    NANOARROW_RETURN_NOT_OK(
+        Check(value.contains("fields"), error, "Schema missing key 'fields'"));
+    NANOARROW_RETURN_NOT_OK(
+        Check(value.contains("metadata"), error, "Schema missing key 
'metadata'"));
+
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(
+        ArrowSchemaInitFromType(schema, NANOARROW_TYPE_STRUCT), error);
+
+    const auto& fields = value["fields"];
+    NANOARROW_RETURN_NOT_OK(
+        Check(fields.is_array(), error, "Schema fields must be array"));
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(ArrowSchemaAllocateChildren(schema, 
fields.size()),
+                                       error);
+    for (int64_t i = 0; i < schema->n_children; i++) {
+      NANOARROW_RETURN_NOT_OK(SetField(schema->children[i], fields[i], error));
+    }
+
+    NANOARROW_RETURN_NOT_OK(SetMetadata(schema, value["metadata"], error));
+
+    // Validate!
+    ArrowSchemaView schema_view;
+    NANOARROW_RETURN_NOT_OK(ArrowSchemaViewInit(&schema_view, schema, error));
+    return NANOARROW_OK;
+  }
+
+  ArrowErrorCode SetField(ArrowSchema* schema, const json& value, ArrowError* 
error) {
+    NANOARROW_RETURN_NOT_OK(
+        Check(value.is_object(), error, "Expected Field to be a JSON object"));
+    NANOARROW_RETURN_NOT_OK(
+        Check(value.contains("name"), error, "Field missing key 'name'"));
+    NANOARROW_RETURN_NOT_OK(
+        Check(value.contains("nullable"), error, "Field missing key 
'nullable'"));
+    NANOARROW_RETURN_NOT_OK(
+        Check(value.contains("type"), error, "Field missing key 'type'"));
+    NANOARROW_RETURN_NOT_OK(
+        Check(value.contains("children"), error, "Field missing key 
'children'"));
+    NANOARROW_RETURN_NOT_OK(
+        Check(value.contains("metadata"), error, "Field missing key 
'metadata'"));
+
+    ArrowSchemaInit(schema);
+
+    const auto& name = value["name"];
+    NANOARROW_RETURN_NOT_OK(Check(name.is_string() || name.is_null(), error,
+                                  "Field name must be string or null"));
+    if (name.is_string()) {
+      auto name_str = name.get<std::string>();
+      NANOARROW_RETURN_NOT_OK_WITH_ERROR(ArrowSchemaSetName(schema, 
name_str.c_str()),
+                                         error);
+    }
+
+    const auto& nullable = value["nullable"];
+    NANOARROW_RETURN_NOT_OK(
+        Check(nullable.is_boolean(), error, "Field nullable must be boolean"));
+    if (nullable.get<bool>()) {
+      schema->flags |= ARROW_FLAG_NULLABLE;
+    } else {
+      schema->flags &= ~ARROW_FLAG_NULLABLE;
+    }
+
+    NANOARROW_RETURN_NOT_OK(SetType(schema, value["type"], error));
+
+    const auto& children = value["children"];
+    NANOARROW_RETURN_NOT_OK(
+        Check(children.is_array(), error, "Field children must be array"));
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(
+        ArrowSchemaAllocateChildren(schema, children.size()), error);
+    for (int64_t i = 0; i < schema->n_children; i++) {
+      NANOARROW_RETURN_NOT_OK(SetField(schema->children[i], children[i], 
error));
+    }
+
+    NANOARROW_RETURN_NOT_OK(SetMetadata(schema, value["metadata"], error));
+
+    // Validate!
+    ArrowSchemaView schema_view;
+    NANOARROW_RETURN_NOT_OK(ArrowSchemaViewInit(&schema_view, schema, error));
+

Review Comment:
   ✂️ blank line



##########
src/nanoarrow/nanoarrow_testing.hpp:
##########
@@ -613,6 +618,458 @@ class TestingJSONWriter {
   };
 };
 
+#if defined(NANOARROW_TESTING_WITH_NLOHMANN_JSON)
+
+/// \brief Reader for the Arrow integration testing JSON format
+class TestingJSONReader {
+  using json = nlohmann::json;
+
+ public:
+  ArrowErrorCode ReadSchema(const std::string& value, ArrowSchema* out,
+                            ArrowError* error = nullptr) {
+    try {
+      auto obj = json::parse(value);
+      nanoarrow::UniqueSchema schema;
+
+      NANOARROW_RETURN_NOT_OK(SetSchema(schema.get(), obj, error));
+      ArrowSchemaMove(schema.get(), out);
+      return NANOARROW_OK;
+    } catch (std::exception& e) {
+      ArrowErrorSet(error, "Exception in TestingJSONReader::ReadSchema(): %s", 
e.what());
+      return EINVAL;
+    }
+  }
+
+  ArrowErrorCode ReadField(const std::string& value, ArrowSchema* out,
+                           ArrowError* error = nullptr) {
+    try {
+      auto obj = json::parse(value);
+      nanoarrow::UniqueSchema schema;
+
+      NANOARROW_RETURN_NOT_OK(SetField(schema.get(), obj, error));
+      ArrowSchemaMove(schema.get(), out);
+      return NANOARROW_OK;
+    } catch (std::exception& e) {
+      ArrowErrorSet(error, "Exception in TestingJSONReader::ReadField(): %s", 
e.what());
+      return EINVAL;
+    }
+  }
+
+ private:
+  ArrowErrorCode SetSchema(ArrowSchema* schema, const json& value, ArrowError* 
error) {
+    NANOARROW_RETURN_NOT_OK(
+        Check(value.is_object(), error, "Expected Schema to be a JSON 
object"));
+    NANOARROW_RETURN_NOT_OK(
+        Check(value.contains("fields"), error, "Schema missing key 'fields'"));
+    NANOARROW_RETURN_NOT_OK(
+        Check(value.contains("metadata"), error, "Schema missing key 
'metadata'"));
+
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(
+        ArrowSchemaInitFromType(schema, NANOARROW_TYPE_STRUCT), error);
+
+    const auto& fields = value["fields"];
+    NANOARROW_RETURN_NOT_OK(
+        Check(fields.is_array(), error, "Schema fields must be array"));
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(ArrowSchemaAllocateChildren(schema, 
fields.size()),
+                                       error);
+    for (int64_t i = 0; i < schema->n_children; i++) {
+      NANOARROW_RETURN_NOT_OK(SetField(schema->children[i], fields[i], error));
+    }
+
+    NANOARROW_RETURN_NOT_OK(SetMetadata(schema, value["metadata"], error));
+
+    // Validate!
+    ArrowSchemaView schema_view;
+    NANOARROW_RETURN_NOT_OK(ArrowSchemaViewInit(&schema_view, schema, error));
+    return NANOARROW_OK;
+  }
+
+  ArrowErrorCode SetField(ArrowSchema* schema, const json& value, ArrowError* 
error) {
+    NANOARROW_RETURN_NOT_OK(
+        Check(value.is_object(), error, "Expected Field to be a JSON object"));
+    NANOARROW_RETURN_NOT_OK(
+        Check(value.contains("name"), error, "Field missing key 'name'"));
+    NANOARROW_RETURN_NOT_OK(
+        Check(value.contains("nullable"), error, "Field missing key 
'nullable'"));
+    NANOARROW_RETURN_NOT_OK(
+        Check(value.contains("type"), error, "Field missing key 'type'"));
+    NANOARROW_RETURN_NOT_OK(
+        Check(value.contains("children"), error, "Field missing key 
'children'"));
+    NANOARROW_RETURN_NOT_OK(
+        Check(value.contains("metadata"), error, "Field missing key 
'metadata'"));
+
+    ArrowSchemaInit(schema);
+
+    const auto& name = value["name"];
+    NANOARROW_RETURN_NOT_OK(Check(name.is_string() || name.is_null(), error,
+                                  "Field name must be string or null"));
+    if (name.is_string()) {
+      auto name_str = name.get<std::string>();
+      NANOARROW_RETURN_NOT_OK_WITH_ERROR(ArrowSchemaSetName(schema, 
name_str.c_str()),
+                                         error);
+    }
+
+    const auto& nullable = value["nullable"];
+    NANOARROW_RETURN_NOT_OK(
+        Check(nullable.is_boolean(), error, "Field nullable must be boolean"));
+    if (nullable.get<bool>()) {
+      schema->flags |= ARROW_FLAG_NULLABLE;
+    } else {
+      schema->flags &= ~ARROW_FLAG_NULLABLE;
+    }
+
+    NANOARROW_RETURN_NOT_OK(SetType(schema, value["type"], error));
+
+    const auto& children = value["children"];
+    NANOARROW_RETURN_NOT_OK(
+        Check(children.is_array(), error, "Field children must be array"));
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(
+        ArrowSchemaAllocateChildren(schema, children.size()), error);
+    for (int64_t i = 0; i < schema->n_children; i++) {
+      NANOARROW_RETURN_NOT_OK(SetField(schema->children[i], children[i], 
error));
+    }
+
+    NANOARROW_RETURN_NOT_OK(SetMetadata(schema, value["metadata"], error));
+
+    // Validate!
+    ArrowSchemaView schema_view;
+    NANOARROW_RETURN_NOT_OK(ArrowSchemaViewInit(&schema_view, schema, error));
+

Review Comment:
   ✂️ blank line



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to