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

Add --json flag in frame to provide JSON output support #116

Merged
merged 19 commits into from
Jul 23, 2024

Conversation

suprith-hub
Copy link
Contributor

@suprith-hub suprith-hub commented Jul 2, 2024

@jviotti I have used brute force way to pad spaces to indent the JSON output lines. Should that be done differently?
Please review my PR

@jviotti
Copy link
Member

jviotti commented Jul 2, 2024

It's starting to shape up, but you should output the entire thing as one big JSON document, rather than parts of it as JSON. Then you wouldn't need to worry about indentation at all.

for (const auto &[key, entry] : frame) {
std::cout << "(LOCATION) URI: ";
std::cout << key.second << "\n";
std::cout << " Schema : " << entry.root.value_or("<ANONYMOUS>")
Copy link
Member

Choose a reason for hiding this comment

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

i.e. all of these things you should be transforming to JSON. In the end you want the command to print only one big JSON document with all the information at once

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ohh, I got it. I thought it might be this way, just in sometime...

std::cout << " Pointer :";
if (!entry.pointer.empty()) {
std::cout << " ";
const char* enumToString(sourcemeta::jsontoolkit::ReferenceEntryType type) {
Copy link
Member

Choose a reason for hiding this comment

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

You should never return a const char *. Please return an std::string

Copy link
Member

@jviotti jviotti Jul 7, 2024

Choose a reason for hiding this comment

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

Also, in C++, we don't use camelCase, but snake_case in variables and function calls. Please do enum_to_string or something like that. Same applies to various other parts in this PR

Copy link
Member

Choose a reason for hiding this comment

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

Also, in modern C++, we use trailing return type style. i.e. auto enum_to_string(...) -> result

Copy link
Member

Choose a reason for hiding this comment

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

Finally, this function doesn't modify its argument, so you want to make the argument const

Copy link
Member

Choose a reason for hiding this comment

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

Also not strictly necessary in this case, but as a good practice, given this is a private function, please make it static

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool! I got it

std::cout << "(REFERENCE) URI: ";
sourcemeta::jsontoolkit::stringify(pointer.second, std::cout);
std::cout << "\n";
bool outputJson = options.contains("json") || options.contains("j");
Copy link
Member

Choose a reason for hiding this comment

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

All variables must be camel_case

Copy link
Member

Choose a reason for hiding this comment

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

Also, try to use const whenever possible. This variable is not supposed to change, so it should be a constant

std::cout << "\n";
bool outputJson = options.contains("json") || options.contains("j");
if (outputJson) {
sourcemeta::jsontoolkit::JSON outputJson = sourcemeta::jsontoolkit::JSON::make_object();
Copy link
Member

Choose a reason for hiding this comment

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

You can do auto here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Isn't specifying type always a good choice.. Why should we use auto in this case..?

Copy link
Member

Choose a reason for hiding this comment

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

You already are kinda specifying the type on the right hand side of assignment (sourcemeta::jsontoolkit::JSON from sourcemeta::jsontookit::JSON::make_object), so this is a bit repetitive. That's why I suggested auto.

@@ -1,71 +1,112 @@
#include <sourcemeta/jsontoolkit/json.h>
#include <sourcemeta/jsontoolkit/jsonschema.h>

Copy link
Member

Choose a reason for hiding this comment

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

Why was this line deleted?

@@ -58,3 +58,9 @@ reference:
```sh
jsonschema frame path/to/my/schema.json
```

### Frame a JSON Schema with output in the form of JSON
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
### Frame a JSON Schema with output in the form of JSON
### Frame a JSON Schema and output result as a JSON document

@jviotti
Copy link
Member

jviotti commented Jul 7, 2024

CI is complaining about formatting (i.e. https://github.com/Intelligence-AI/jsonschema/actions/runs/9825814598/job/27134677359?pr=116). Try running make while having ClangFormat installed to re-format the code

@jviotti
Copy link
Member

jviotti commented Jul 7, 2024

Also, can you add a variant of this test (https://github.com/Intelligence-AI/jsonschema/blob/main/test/frame.sh) to exercise the new JSON functionality?

@suprith-hub
Copy link
Contributor Author

@jviotti status says still waiting for approval

@jviotti
Copy link
Member

jviotti commented Jul 11, 2024

@Era-cell Try now. I tweaked the project settings to be more permissive with first-time contributors

src/command_frame.cc Outdated Show resolved Hide resolved
test/frame.sh Outdated
@@ -70,4 +70,4 @@ cat << 'EOF' > "$TMP/expected.txt"
- (fragment) : /$defs/string
EOF

diff "$TMP/result.txt" "$TMP/expected.txt"
Copy link
Member

Choose a reason for hiding this comment

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

This looks like an unintended change

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't have any difference between the original version, still I do not know why it says there is a diff

Copy link
Member

Choose a reason for hiding this comment

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

It's the final new line character at the end of the file. We saw it in other PRs. It's likely a misconfiguration on your editor. Google how to make sure your editor includes an end-of-file line ending

Copy link
Member

Choose a reason for hiding this comment

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

See the symbol that GitHub shows on the diff:

Screenshot 2024-07-11 at 11 54 41 PM

Copy link
Contributor Author

@suprith-hub suprith-hub Jul 12, 2024

Choose a reason for hiding this comment

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

Shet! sorry I got it, I think myself I removed the EOF line, I will correct it

test/frame_json_output.sh Outdated Show resolved Hide resolved
std::cout << " ";
for (const auto &[key, entry] : frame) {
auto frame_entry = sourcemeta::jsontoolkit::JSON::make_object();
frame_entry.assign("schema", sourcemeta::jsontoolkit::JSON{entry.root.value_or("<ANONYMOUS>")});
Copy link
Member

Choose a reason for hiding this comment

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

If the root is not defined, can you make it null rather than setting it to a string with the value of <ANONYMOUS>?

test/frame_json_output.sh Outdated Show resolved Hide resolved
{
"frames": {
"https://example.com": {
"baseURI": "https://example.com",
Copy link
Member

Choose a reason for hiding this comment

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

Here too. Maybe just call it base?

sourcemeta::jsontoolkit::JSON{pointer.first == sourcemeta::jsontoolkit::ReferenceType::Dynamic ? "Dynamic" : "Static"});
ref_entry.assign("destination", sourcemeta::jsontoolkit::JSON{entry.destination});
if (entry.base.has_value()) {
ref_entry.assign("fragmentBaseURI", sourcemeta::jsontoolkit::JSON{entry.base.value()});
Copy link
Member

Choose a reason for hiding this comment

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

Here you can make it just base. I think in general keeping the JSON properties consistent with the names in the struct will make it easier to maintain

@suprith-hub
Copy link
Contributor Author

@jviotti what is the clang formatter I should be using? I used llvm clang with vscode extention, but its still failing

@jviotti
Copy link
Member

jviotti commented Jul 15, 2024

@Era-cell I don't use VS Code, so not sure about that plugin, but you might be installing a different version? Here is how we do it for all platform on GitHub Actions: https://github.com/Intelligence-AI/jsonschema/blob/main/.github/workflows/test.yml#L40-L41

@suprith-hub
Copy link
Contributor Author

@Era-cell I don't use VS Code, so not sure about that plugin, but you might be installing a different version? Here is how we do it for all platform on GitHub Actions: https://github.com/Intelligence-AI/jsonschema/blob/main/.github/workflows/test.yml#L40-L41

@jviotti I did format using clang-format version 18.1.5 Python, it still fails, idk why

@jviotti
Copy link
Member

jviotti commented Jul 17, 2024

Do you get an error, or do you mean the formatting is not happening how you would expect it to happen? How are you running it, specifically? What you get if you run clang-format --version?

@suprith-hub
Copy link
Contributor Author

@jviotti looks like clang formatting worked for windows but not for mac, and ubuntu

@jviotti
Copy link
Member

jviotti commented Jul 18, 2024

Formatting is OK now, it's the test that's failing. We don't run tests on Windows (yet) so that's why that's green.

@suprith-hub
Copy link
Contributor Author

suprith-hub commented Jul 20, 2024

Ok, its good.. but can you check the changes in the non-json output ...

@suprith-hub suprith-hub changed the title Frame has --json flag for JSON output support Add --json flag in frame to provide JSON output support Jul 20, 2024
test/frame/pass.sh Outdated Show resolved Hide resolved
@jviotti
Copy link
Member

jviotti commented Jul 22, 2024

It's looking really good! Just one minor comment and I think we are done

src/command_frame.cc Outdated Show resolved Hide resolved
src/command_frame.cc Outdated Show resolved Hide resolved
@suprith-hub
Copy link
Contributor Author

@jviotti I have updated the PR from my side.. can you check it, if I have missed any other case

@jviotti jviotti merged commit f4963d4 into sourcemeta:main Jul 23, 2024
6 checks passed
@jviotti
Copy link
Member

jviotti commented Jul 23, 2024

Thanks a lot! Great work, and I hope you enjoyed C++ :D I'll release a new version with this change soon and announce it on LinkedIn

@suprith-hub
Copy link
Contributor Author

Thanks a lot! Great work, and I hope you enjoyed C++ :D I'll release a new version with this change soon and announce it on LinkedIn

Thank you for guiding me 😁, I took a lot of time 😅...

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