Skip to content

Commit

Permalink
Issue #44: Implement exporting table in json format
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Jan 19, 2024
1 parent 05acf42 commit 53c603f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 16 deletions.
36 changes: 24 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/gitql-ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ license = "MIT"
[dependencies]
lazy_static = "1.4.0"
chrono = "0.4.31"
serde_json = "1.0.111"
csv = "1.3.0"
24 changes: 22 additions & 2 deletions crates/gitql-ast/src/object.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::error::Error;

use csv::Writer;

use crate::value::Value;
use csv::Writer;

/// In memory representation of the list of [`Value`] in one Row
#[derive(Default)]
Expand Down Expand Up @@ -57,6 +56,27 @@ impl GitQLObject {
self.groups.len()
}

/// Export the GitQLObject as JSON String
pub fn as_json(&self) -> serde_json::Result<String> {
let mut elements: Vec<serde_json::Value> = vec![];

if let Some(group) = self.groups.get(0) {
let titles = &self.titles;
for row in &group.rows {
let mut object = serde_json::Map::new();
for (i, value) in row.values.iter().enumerate() {
object.insert(
titles[i].to_string(),
serde_json::Value::String(value.to_string()),
);
}
elements.push(serde_json::Value::Object(object));
}
}

serde_json::to_string(&serde_json::Value::Array(elements))
}

/// Export the GitQLObject as CSV String
pub fn as_csv(&self) -> Result<String, Box<dyn Error>> {
let mut writer = Writer::from_writer(vec![]);
Expand Down
2 changes: 1 addition & 1 deletion crates/gitql-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ license = "MIT"
gitql-ast = { path = "../gitql-ast", version = "0.10.0" }
gitql-parser = { path = "../gitql-parser", version = "0.11.0" }
comfy-table = "7.1.0"
termcolor = "1.2.0"
termcolor = "1.4.1"
6 changes: 5 additions & 1 deletion crates/gitql-cli/src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
pub enum OutputFormat {
/// Render the output as table
Render,
/// Print the output in json format
JSON,
/// Print the output in csv format
CSV,
}
Expand Down Expand Up @@ -132,6 +134,8 @@ pub fn parse_arguments(args: &Vec<String>) -> Command {
let output_type = &args[arg_index].to_lowercase();
if output_type == "csv" {
arguments.output_format = OutputFormat::CSV;
} else if output_type == "json" {
arguments.output_format = OutputFormat::JSON;
} else if output_type == "render" {
arguments.output_format = OutputFormat::Render;
} else {
Expand Down Expand Up @@ -179,7 +183,7 @@ pub fn print_help_list() {
println!("-q, --query <GQL Query> GitQL query to run on selected repositories");
println!("-p, --pagination Enable print result with pagination");
println!("-ps, --pagesize Set pagination page size [default: 10]");
println!("-o, --output Set output format [render, csv]");
println!("-o, --output Set output format [render, json, csv]");
println!("-a, --analysis Print Query analysis");
println!("-h, --help Print GitQL help");
println!("-v, --version Print GitQL Current Version");
Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ fn execute_gitql_query(
arguments.page_size,
);
}
OutputFormat::JSON => {
if let Ok(json) = groups.as_json() {
println!("{}", json);
}
}
OutputFormat::CSV => {
if let Ok(csv) = groups.as_csv() {
println!("{}", csv);
Expand Down

0 comments on commit 53c603f

Please sign in to comment.