|
| 1 | +use super::{ |
| 2 | + Seed, |
| 3 | + content::{Parameter, at, pages}, |
| 4 | +}; |
| 5 | +use anyhow::{Context, Result}; |
| 6 | +use serde::Deserialize; |
| 7 | +use serde_json::Value; |
| 8 | +use std::{collections::BTreeMap, fs, path::Path}; |
| 9 | + |
| 10 | +#[derive(Deserialize)] |
| 11 | +#[serde(untagged)] |
| 12 | +enum ComponentParameter { |
| 13 | + Shared(String), |
| 14 | + Inline(Parameter), |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Default, Deserialize)] |
| 18 | +#[serde(deny_unknown_fields)] |
| 19 | +struct Component { |
| 20 | + icon: Option<String>, |
| 21 | + since: Option<String>, |
| 22 | + category: Option<String>, |
| 23 | + summary: Option<String>, |
| 24 | + #[serde(default)] |
| 25 | + top: Vec<ComponentParameter>, |
| 26 | + #[serde(default)] |
| 27 | + row: Vec<ComponentParameter>, |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(Default, Deserialize)] |
| 31 | +#[serde(deny_unknown_fields)] |
| 32 | +struct Example { |
| 33 | + // Mapping order is the column order in the SQL shown to readers. |
| 34 | + rows: Option<Vec<serde_json::Map<String, Value>>>, |
| 35 | +} |
| 36 | + |
| 37 | +pub(super) fn insert(seed: &mut Seed, content: &Path) -> Result<()> { |
| 38 | + let shared_path = content.join("shared-parameters.yaml"); |
| 39 | + let shared: BTreeMap<String, Parameter> = at(&shared_path, || { |
| 40 | + let entries: BTreeMap<String, Parameter> = |
| 41 | + serde_yaml_ng::from_str(&fs::read_to_string(&shared_path)?)?; |
| 42 | + for parameter in entries.values() { |
| 43 | + parameter.validate()?; |
| 44 | + } |
| 45 | + Ok(entries) |
| 46 | + })?; |
| 47 | + let dir = content.join("components"); |
| 48 | + pages(&dir, |name, c: Component, body| { |
| 49 | + seed.row( |
| 50 | + "INSERT INTO component (name, description, icon, introduced_in_version, catalog_category, catalog_summary) |
| 51 | + VALUES (?, ?, ?, ?, ?, ?)", |
| 52 | + &[&name, &body, &c.icon, &c.since, &c.category, &c.summary], |
| 53 | + )?; |
| 54 | + for (top, parameters) in [(true, c.top), (false, c.row)] { |
| 55 | + for p in ¶meters { |
| 56 | + let p = match p { |
| 57 | + ComponentParameter::Inline(p) => p, |
| 58 | + ComponentParameter::Shared(key) => shared |
| 59 | + .get(key) |
| 60 | + .with_context(|| format!("Unknown shared parameter {key:?}"))?, |
| 61 | + }; |
| 62 | + p.validate()?; |
| 63 | + seed.row( |
| 64 | + "INSERT INTO parameter (component, top_level, name, type, description_md, optional) |
| 65 | + VALUES (?, ?, ?, ?, ?, ?)", |
| 66 | + &[&name, &top, &p.name, &p.r#type, &p.description, &!p.required], |
| 67 | + )?; |
| 68 | + } |
| 69 | + } |
| 70 | + Ok(()) |
| 71 | + })?; |
| 72 | + |
| 73 | + // Some examples move before their component reference. That reference |
| 74 | + // still comes from a checked-in migration in that case. |
| 75 | + let mut dirs = fs::read_dir(&dir)?.collect::<std::io::Result<Vec<_>>>()?; |
| 76 | + dirs.sort_by_key(fs::DirEntry::file_name); |
| 77 | + for entry in dirs { |
| 78 | + let examples = entry.path().join("examples"); |
| 79 | + if entry.file_type()?.is_dir() && examples.try_exists()? { |
| 80 | + let name = entry.file_name(); |
| 81 | + let component = name.to_str().context("Invalid component directory name")?; |
| 82 | + insert_examples(seed, component, &examples)?; |
| 83 | + } |
| 84 | + } |
| 85 | + Ok(()) |
| 86 | +} |
| 87 | + |
| 88 | +fn insert_examples(seed: &mut Seed, component: &str, dir: &Path) -> Result<()> { |
| 89 | + pages(dir, |_, e: Example, description| { |
| 90 | + let properties = e |
| 91 | + .rows |
| 92 | + .map(|rows| serde_json::to_string(&rows)) |
| 93 | + .transpose()?; |
| 94 | + seed.row( |
| 95 | + "INSERT INTO example (component, description, properties) VALUES (?, ?, ?)", |
| 96 | + &[&component, &description, &properties], |
| 97 | + ) |
| 98 | + }) |
| 99 | +} |
| 100 | + |
| 101 | +#[cfg(test)] |
| 102 | +mod tests { |
| 103 | + use super::*; |
| 104 | + use rusqlite::Connection; |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn examples_keep_file_order_column_order_and_large_integers() -> Result<()> { |
| 108 | + let examples = tempfile::tempdir()?; |
| 109 | + fs::write( |
| 110 | + examples.path().join("02-result.md"), |
| 111 | + "---\nrows:\n - component: text\n contents: \"Reader's choice\"\n \"2\": 9007199254740993\n \"1\": 42\n---\nResult\n", |
| 112 | + )?; |
| 113 | + fs::write(examples.path().join("01-notes.md"), "Just a note.\n")?; |
| 114 | + let schema = "CREATE TABLE example (component, description, properties);"; |
| 115 | + let mut seed = Seed::new()?; |
| 116 | + seed.execute(schema)?; |
| 117 | + insert_examples(&mut seed, "text", examples.path())?; |
| 118 | + let db = Connection::open_in_memory()?; |
| 119 | + db.execute_batch(schema)?; |
| 120 | + db.execute_batch(&seed.finish())?; |
| 121 | + let rows = db |
| 122 | + .prepare("SELECT description, properties FROM example ORDER BY rowid")? |
| 123 | + .query_map([], |row| { |
| 124 | + Ok((row.get::<_, String>(0)?, row.get::<_, Option<String>>(1)?)) |
| 125 | + })? |
| 126 | + .collect::<rusqlite::Result<Vec<_>>>()?; |
| 127 | + assert_eq!( |
| 128 | + rows, |
| 129 | + [ |
| 130 | + ("Just a note.".into(), None), |
| 131 | + ("Result".into(), Some(r#"[{"component":"text","contents":"Reader's choice","2":9007199254740993,"1":42}]"#.into())), |
| 132 | + ] |
| 133 | + ); |
| 134 | + Ok(()) |
| 135 | + } |
| 136 | +} |
0 commit comments