-
Notifications
You must be signed in to change notification settings - Fork 7
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
Conversation
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. |
src/command_frame.cc
Outdated
for (const auto &[key, entry] : frame) { | ||
std::cout << "(LOCATION) URI: "; | ||
std::cout << key.second << "\n"; | ||
std::cout << " Schema : " << entry.root.value_or("<ANONYMOUS>") |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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...
src/command_frame.cc
Outdated
std::cout << " Pointer :"; | ||
if (!entry.pointer.empty()) { | ||
std::cout << " "; | ||
const char* enumToString(sourcemeta::jsontoolkit::ReferenceEntryType type) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool! I got it
src/command_frame.cc
Outdated
std::cout << "(REFERENCE) URI: "; | ||
sourcemeta::jsontoolkit::stringify(pointer.second, std::cout); | ||
std::cout << "\n"; | ||
bool outputJson = options.contains("json") || options.contains("j"); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
src/command_frame.cc
Outdated
std::cout << "\n"; | ||
bool outputJson = options.contains("json") || options.contains("j"); | ||
if (outputJson) { | ||
sourcemeta::jsontoolkit::JSON outputJson = sourcemeta::jsontoolkit::JSON::make_object(); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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..?
There was a problem hiding this comment.
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> | |||
|
There was a problem hiding this comment.
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?
docs/frame.markdown
Outdated
@@ -58,3 +58,9 @@ reference: | |||
```sh | |||
jsonschema frame path/to/my/schema.json | |||
``` | |||
|
|||
### Frame a JSON Schema with output in the form of JSON |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
### Frame a JSON Schema with output in the form of JSON | |
### Frame a JSON Schema and output result as a JSON document |
CI is complaining about formatting (i.e. https://github.com/Intelligence-AI/jsonschema/actions/runs/9825814598/job/27134677359?pr=116). Try running |
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? |
@jviotti status says still waiting for approval |
@Era-cell Try now. I tweaked the project settings to be more permissive with first-time contributors |
test/frame.sh
Outdated
@@ -70,4 +70,4 @@ cat << 'EOF' > "$TMP/expected.txt" | |||
- (fragment) : /$defs/string | |||
EOF | |||
|
|||
diff "$TMP/result.txt" "$TMP/expected.txt" |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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
src/command_frame.cc
Outdated
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>")}); |
There was a problem hiding this comment.
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
{ | ||
"frames": { | ||
"https://example.com": { | ||
"baseURI": "https://example.com", |
There was a problem hiding this comment.
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
?
src/command_frame.cc
Outdated
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()}); |
There was a problem hiding this comment.
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
@jviotti what is the clang formatter I should be using? I used llvm clang with vscode extention, but its still failing |
@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 |
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 |
@jviotti looks like clang formatting worked for windows but not for mac, and ubuntu |
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. |
Ok, its good.. but can you check the changes in the non-json output ... |
It's looking really good! Just one minor comment and I think we are done |
@jviotti I have updated the PR from my side.. can you check it, if I have missed any other case |
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 😅... |
@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