Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor read_json to accept optioal callback and update related tests and functions #1547

Merged

Conversation

Karan-Palan
Copy link
Contributor

@Karan-Palan Karan-Palan commented Feb 11, 2025

This PR refactors the read_json function to accept an optional ParseCallback parameter, enhancing the flexibility of JSON parsing. It also updates related function calls and definitions to align with this change as discussed in slack

…date related tests and functions

Signed-off-by: karan-palan <[email protected]>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The build fails because read_json has an incompatible function signature, taking an extra ParseCallback parameter while SchemaFlatFileResolver::Reader expects a function that takes only a std::filesystem::path. Hence these changes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The build fails because the function definition of SchemaFlatFileResolver::add in resolver.cc uses an rvalue reference (&&) for SchemaVisitorReference, while its declaration in jsonschema_resolver.h passes it by value, causing a mismatch, to avoid breaking changes, added this

@@ -27,21 +27,24 @@ TEST(JSONSchema_SchemaFlatFileResolver, single_schema) {
EXPECT_TRUE(
resolver("https://www.sourcemeta.com/2020-12-id.json").has_value());
EXPECT_EQ(resolver("https://www.sourcemeta.com/2020-12-id.json").value(),
sourcemeta::core::read_json(schema_path));
sourcemeta::core::read_json(schema_path, nullptr));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to explicitly pass it, as you set it to nullptr by default on the signature

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that, however it didn't work

@jviotti
Copy link
Member

jviotti commented Feb 11, 2025

I left a few comments. The current CI failures are unrelated though. Our benchmark GitHub Action seems to be failing if coming from an external fork... I'll dig into it today.

@Karan-Palan
Copy link
Contributor Author

Sure! I'll add the accidentally deleted comment after your review to save GitHub Action minutes. I've responded to all comments—the issue stemmed from function signature mismatches: read_json takes an extra ParseCallback parameter while SchemaFlatFileResolver::Reader expects a function with only a std::filesystem::path, and SchemaFlatFileResolver::add had a parameter mismatch between its definition and declaration. I had to update them accordingly. PTAL and let me know if there's a better approach!

@Karan-Palan Karan-Palan requested a review from jviotti February 11, 2025 15:49
@jviotti
Copy link
Member

jviotti commented Feb 12, 2025

@Karan-Palan Just these changes work for me. I think the rest are indeed all unnecessary:

diff --git a/src/core/json/include/sourcemeta/core/json.h b/src/core/json/include/sourcemeta/core/json.h
index c901afb0..7ab73404 100644
--- a/src/core/json/include/sourcemeta/core/json.h
+++ b/src/core/json/include/sourcemeta/core/json.h
@@ -127,7 +127,8 @@ auto parse_json(const std::basic_string<JSON::Char, JSON::CharTraits> &input,
 ///
 /// If parsing fails, sourcemeta::core::JSONParseError will be thrown.
 SOURCEMETA_CORE_JSON_EXPORT
-auto read_json(const std::filesystem::path &path) -> JSON;
+auto read_json(const std::filesystem::path &path,
+               const JSON::ParseCallback &callback = nullptr) -> JSON;
 
 // TODO: Move this function to a system integration component, as it
 // is not JSON specific
diff --git a/src/core/json/json.cc b/src/core/json/json.cc
index ed50f604..eac8322f 100644
--- a/src/core/json/json.cc
+++ b/src/core/json/json.cc
@@ -50,10 +50,11 @@ auto read_file(const std::filesystem::path &path)
   return stream;
 }
 
-auto read_json(const std::filesystem::path &path) -> JSON {
+auto read_json(const std::filesystem::path &path,
+               const JSON::ParseCallback &callback) -> JSON {
   auto stream{read_file(path)};
   try {
-    return parse_json(stream);
+    return parse_json(stream, callback);
   } catch (const JSONParseError &error) {
     // For producing better error messages
     throw JSONFileParseError(path, error);
diff --git a/src/core/jsonschema/include/sourcemeta/core/jsonschema_resolver.h b/src/core/jsonschema/include/sourcemeta/core/jsonschema_resolver.h
index 582678be..0523ec39 100644
--- a/src/core/jsonschema/include/sourcemeta/core/jsonschema_resolver.h
+++ b/src/core/jsonschema/include/sourcemeta/core/jsonschema_resolver.h
@@ -105,11 +105,13 @@ public:
 
   /// Register a schema to the flat file resolver, returning the detected
   /// identifier for the schema
-  auto add(const std::filesystem::path &path,
-           const std::optional<std::string> &default_dialect = std::nullopt,
-           const std::optional<std::string> &default_id = std::nullopt,
-           const Reader &reader = read_json,
-           SchemaVisitorReference &&reference_visitor = nullptr)
+  auto add(
+      const std::filesystem::path &path,
+      const std::optional<std::string> &default_dialect = std::nullopt,
+      const std::optional<std::string> &default_id = std::nullopt,
+      const Reader &reader =
+          [](const std::filesystem::path &path) { return read_json(path); },
+      SchemaVisitorReference &&reference_visitor = nullptr)
       -> const std::string &;
 
   // Change the identifier of a registered schema
diff --git a/test/jsonschema/jsonschema_flat_file_resolver_test.cc b/test/jsonschema/jsonschema_flat_file_resolver_test.cc
index 21ae91e2..49159ab2 100644
--- a/test/jsonschema/jsonschema_flat_file_resolver_test.cc
+++ b/test/jsonschema/jsonschema_flat_file_resolver_test.cc
@@ -357,7 +357,10 @@ TEST(JSONSchema_SchemaFlatFileResolver, custom_reference_visitor) {
   })JSON");
 
   const auto &identifier{resolver.add(
-      schema_path, std::nullopt, std::nullopt, sourcemeta::core::read_json,
+      schema_path, std::nullopt, std::nullopt,
+      [](const std::filesystem::path &path) {
+        return sourcemeta::core::read_json(path);
+      },
       [](sourcemeta::core::JSON &schema, const sourcemeta::core::URI &,
          const sourcemeta::core::JSON::String &,
          const sourcemeta::core::JSON::String &keyword,

@jviotti
Copy link
Member

jviotti commented Feb 12, 2025

Also, can you add a tiny test here exercising the new callback variant? https://github.com/sourcemeta/core/blob/db9f42ffcf7f82f7097b3d6d85e09571d702e194/test/json/json_parse_callback_test.cc

@Karan-Palan Karan-Palan force-pushed the fix/schema-flatfile-resolver-reader branch from e4b069c to c56f5b8 Compare February 12, 2025 16:43
@Karan-Palan
Copy link
Contributor Author

@Karan-Palan Just these changes work for me. I think the rest are indeed all unnecessary:

Done. I also added a test where writes out a small JSON file (in this example just "true") to a temporary location, then calls read_json with a callback that captures the parse “traces,” and finally checks both the resulting JSON and the parse events. PTAL

Signed-off-by: Juan Cruz Viotti <[email protected]>
Signed-off-by: Juan Cruz Viotti <[email protected]>
Copy link
Member

@jviotti jviotti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a commit to make the test case use an existing simple JSON stub we already had in the repo, but other than that, looks good. Thanks!

@jviotti jviotti merged commit 68c124e into sourcemeta:main Feb 12, 2025
13 checks passed
@Karan-Palan Karan-Palan deleted the fix/schema-flatfile-resolver-reader branch February 12, 2025 18:54
@Karan-Palan
Copy link
Contributor Author

I added a commit to make the test case use an existing simple JSON stub we already had in the repo, but other than that, looks good. Thanks!

Ahh I see, it's more maintainable too. Thanks, I'm getting to learn alot about c++ ecosystem and projects whilst contributing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants