Skip to content

Commit

Permalink
improve formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
wsxiaoys committed Aug 26, 2024
1 parent 538ffa4 commit 4d1d19a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion crates/tabby-index-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ clap = { workspace = true, features = ["derive"]}
tantivy.workspace = true
tabby-common = { path = "../tabby-common" }
anyhow.workspace = true
serde.workspace = true
serde.workspace = true
serde_json.workspace = true
19 changes: 17 additions & 2 deletions crates/tabby-index-cli/src/commands/head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ pub fn run_head_cli(index_path: &Path, args: &HeadArgs) -> anyhow::Result<()> {
let doc: TantivyDocument =
searcher.doc(doc_address).expect("Failed to read document");

let json = doc.to_json(&schema.schema);
let json_value = to_json_value(doc, &schema.schema);

println!("{}", json);
println!("{}", json_value);

count += 1;
if count >= args.num_docs {
Expand All @@ -55,3 +55,18 @@ pub fn run_head_cli(index_path: &Path, args: &HeadArgs) -> anyhow::Result<()> {

Ok(())
}

fn to_json_value(doc: TantivyDocument, schema: &tantivy::schema::Schema) -> serde_json::Value {
let json = doc.to_json(schema);
let mut doc: serde_json::Value = serde_json::from_str(&json).expect("Failed to parse JSON");

for (_, value) in doc.as_object_mut().expect("Expected object").iter_mut() {
if let Some(array) = value.as_array_mut() {
if array.len() == 1 {
*value = array[0].clone();
}
}
}

doc
}

0 comments on commit 4d1d19a

Please sign in to comment.