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 output #34

Merged
merged 9 commits into from
Jul 26, 2024
Merged

Add json output #34

merged 9 commits into from
Jul 26, 2024

Conversation

sirbrillig
Copy link
Owner

@sirbrillig sirbrillig commented Jul 25, 2024

This adds a new option, --format which allows specifying a new output format that encodes each result as a JSON document. Note that this means that the script's output as a whole is NOT valid JSON (it's not an array of matches) but that each output MATCH is valid JSON. This is how ripgrep outputs JSON and is probably better suited for a tool which generates its output using an iterator.

Originally I wanted to support the same format as ripgrep's --json option, but the format used by ripgrep is quite complex in order to support the matching and context options of that tool, whereas grepdef will only ever report one line per match. Therefore, I opted to create a custom format which is just a JSON serialization of each SearchResult.

$ grepdef Foo . -n --type php --format json-per-match|jq
{
  "file_path": "./wp-content/mu-plugins/foobar/foo.php",
  "line_number": 75,
  "text": "class Foo {"
}

In order to make it clear that this option is unique, I've avoided using the --json option and instead require --format json-per-match. If other JSON formats are ever added in the future, this leaves the option open to give them more specific names.

Even though the format option is stored in the state of the Searcher, I also needed a way to "enforce" it at the point where each SearchResult is printed. This was tricky with the API of the search function because it just returns objects with no concern for how they are printed. To that end I created a new method, search_and_format which returns a vector of formatted Strings instead of SearchResults. This method is then used in the binary. Another advantage of this API is that the actual output of the binary's search flow can be checked in the integration tests, which is something I've wanted.

@sirbrillig sirbrillig merged commit 07227f7 into trunk Jul 26, 2024
6 checks passed
@sirbrillig sirbrillig deleted the add-json-output branch July 26, 2024 15:28
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.

1 participant