Skip to content

Commit

Permalink
add --pretty flag to schema commands
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseditson committed Dec 3, 2024
1 parent 6c26eb3 commit 50c6829
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/binary/command/schemas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ impl BinaryCommand for Command {
.arg(
arg!(-i --inline "If provided, this will print the schema to stdout instead of generating schema files.").required(false),
)
.arg(
arg!(-p --pretty "If provided, will prettify json before printing or dumping.").required(false),
)
}
fn handler(
&self,
Expand All @@ -39,6 +42,7 @@ impl BinaryCommand for Command {
let mut fs = file_system_stdlib::NativeFileSystem::new(build_dir);
let object = args.get_one::<String>("object");
let inline = *args.get_one::<bool>("inline").unwrap();
let pretty = *args.get_one::<bool>("pretty").unwrap();
let site = Site::load(&fs)?;
if inline {
let schema = if let Some(object) = object {
Expand All @@ -47,13 +51,14 @@ impl BinaryCommand for Command {
.get(object)
.ok_or_else(|| SchemaError::NoObject(object.clone()))?;
let site_url = site.site_url();
generate_json_schema(&format!("{}/{}.schema.json", site_url, object), def)
generate_json_schema(&format!("{}/{}.schema.json", site_url, object), def, pretty)
} else {
generate_root_json_schema(
&format!("{}/root.schema.json", site.site_url()),
site.manifest.site_name.as_deref(),
&format!("Object definitions for {}", site.site_url()),
&site.object_definitions,
pretty,
)
};
println!("{}", schema);
Expand Down
16 changes: 13 additions & 3 deletions src/json_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub fn generate_root_json_schema(
title: Option<&str>,
description: &str,
objects: &HashMap<String, ObjectDefinition>,
pretty: bool,
) -> String {
let mut schema = serde_json::Map::new();
schema.insert(
Expand All @@ -33,14 +34,19 @@ pub fn generate_root_json_schema(
);
}
schema.insert("properties".into(), properties.into());
serde_json::to_string_pretty(&schema).unwrap()
if pretty {
serde_json::to_string_pretty(&schema).unwrap()
} else {
serde_json::to_string(&schema).unwrap()
}
}

pub fn generate_json_schema(
id: &str,
// title: &str,
// description: &str,
definition: &ObjectDefinition,
pretty: bool,
) -> String {
let mut schema = serde_json::Map::new();
schema.insert(
Expand All @@ -55,7 +61,11 @@ pub fn generate_json_schema(
"properties".into(),
definition.to_json_schema_properties().into(),
);
serde_json::to_string_pretty(&schema).unwrap()
if pretty {
serde_json::to_string_pretty(&schema).unwrap()
} else {
serde_json::to_string(&schema).unwrap()
}
}

#[cfg(test)]
Expand Down Expand Up @@ -91,7 +101,7 @@ pub mod tests {
let table: Table = toml::from_str(artist_and_example_definition_str())?;
let defs = ObjectDefinition::from_table(&table, &HashMap::new())?;

let schema_str = generate_json_schema("artist", defs.get("artist").unwrap());
let schema_str = generate_json_schema("artist", defs.get("artist").unwrap(), false);
println!("SCHEMA: {}", schema_str);
let instance = json!({
"tour_dates": [{
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,13 @@ impl<F: FileSystemAPI> Archival<F> {
self.fs_mutex.with_fs(|fs| self.site.dump_schemas(fs))
}
#[cfg(feature = "json-schema")]
pub fn generate_root_json_schema(&self) -> String {
pub fn generate_root_json_schema(&self, pretty: bool) -> String {
json_schema::generate_root_json_schema(
&format!("{}/root.schema.json", self.site.site_url()),
self.site.manifest.site_name.as_deref(),
&format!("Object definitions for {}", self.site.site_url()),
&self.site.object_definitions,
pretty,
)
}
pub fn dist_file(&self, path: &Path) -> Option<Vec<u8>> {
Expand Down
9 changes: 6 additions & 3 deletions src/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ impl Site {
let _ = fs.remove_dir_all(&self.manifest.schemas_dir);
fs.create_dir_all(&self.manifest.schemas_dir)?;
for (name, def) in &self.object_definitions {
println!("HI");
let schema = json_schema::generate_json_schema(
&format!("{}/{}.schema.json", site_url, name),
def,
true,
);
fs.write_str(
&self
Expand All @@ -166,8 +166,11 @@ impl Site {
.get(object)
.ok_or_else(|| InvalidFileError::UnknownObject(object.clone()))?;
let site_url = self.site_url();
let schema =
json_schema::generate_json_schema(&format!("{}/{}.schema.json", site_url, object), def);
let schema = json_schema::generate_json_schema(
&format!("{}/{}.schema.json", site_url, object),
def,
true,
);
fs.write_str(
&self
.manifest
Expand Down

0 comments on commit 50c6829

Please sign in to comment.