Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Parser;
use schema2000::{render_schema, SchemaHypothesis};
use schema2000::{generate_hypothesis_from_iterator, render_schema, SchemaHypothesis};
use std::error::Error;
use std::fs::File;
use std::io::{self, Read};
Expand All @@ -12,19 +12,10 @@ fn main() -> Result<(), Box<dyn Error>> {
let deserializer = serde_json::Deserializer::from_reader(reader);
let iterator = deserializer.into_iter::<serde_json::Value>();

let mut current_hypothesis: Option<SchemaHypothesis> = None;
let hypothesis: SchemaHypothesis =
generate_hypothesis_from_iterator(iterator.map(|value| value.unwrap()));

for json_document in iterator {
let new_hypo = schema2000::generate_hypothesis(&json_document?);
if current_hypothesis.is_none() {
current_hypothesis = Some(new_hypo);
} else {
current_hypothesis =
current_hypothesis.map(|cur| schema2000::merge_hypothesis(cur, new_hypo));
}
}

let result = render_schema(&current_hypothesis.unwrap());
let result = render_schema(&hypothesis);

println!("{result}");

Expand Down
20 changes: 20 additions & 0 deletions core/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use chrono::{DateTime, NaiveDate};
use serde_json::{Map, Number, Value};
use std::collections::{BTreeMap, BTreeSet};

use crate::merge_hypothesis;

use crate::model::{
AnyNode, ArrayNode, DateNode, DateTimeNode, IntegerNode, NodeType, NumberNode, ObjectNode,
ObjectProperty, SchemaHypothesis, StringNode,
Expand Down Expand Up @@ -103,6 +105,24 @@ pub fn generate_hypothesis(dom: &Value) -> SchemaHypothesis {
}
}

pub fn generate_hypothesis_from_iterator<I>(values: I) -> SchemaHypothesis
where
I: Iterator<Item = Value>,
{
let mut current_hypothesis: Option<SchemaHypothesis> = None;

for json_document in values {
let new_hypo = generate_hypothesis(&json_document);
if current_hypothesis.is_none() {
current_hypothesis = Some(new_hypo);
} else {
current_hypothesis = current_hypothesis.map(|cur| merge_hypothesis(cur, new_hypo));
}
}

current_hypothesis.unwrap()
}

#[cfg(test)]
mod test {
use maplit::{btreemap, btreeset};
Expand Down
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(clippy::module_name_repetitions)]

pub use generate::generate_hypothesis;
pub use generate::generate_hypothesis_from_iterator;
pub use merge::merge_hypothesis;
pub use model::SchemaHypothesis;
pub use renderer::render_schema;
Expand Down