Skip to content

Commit

Permalink
Render composite types using the new renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
Julius de Bruijn committed Oct 24, 2022
1 parent 035425d commit 9e7163b
Show file tree
Hide file tree
Showing 17 changed files with 662 additions and 404 deletions.
27 changes: 14 additions & 13 deletions Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,18 @@ pub(super) async fn sample(
let is_empty = data_model.is_empty();

let data_model = if ctx.render_config {
let config = render::Configuration::from_psl(ctx.configuration());
let datamodel = psl::render_datamodel_to_string(&data_model, Some(ctx.configuration()));

format!("{}\n{}", config, datamodel)
format!(
"{}\n{}\n{}",
render::Configuration::from_psl(ctx.configuration()),
render::Datamodel::from_dml(ctx.datasource(), &data_model),
psl::render_datamodel_to_string(&data_model, Some(ctx.configuration())),
)
} else {
psl::render_datamodel_to_string(&data_model, Some(ctx.configuration()))
format!(
"{}\n{}",
render::Datamodel::from_dml(ctx.datasource(), &data_model),
psl::render_datamodel_to_string(&data_model, Some(ctx.configuration())),
)
};

Ok(IntrospectionResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fn remapping_composite_fields_with_numbers() {

let expected = expect![[r#"
type OuterInner {
// This field was commented out because of an invalid name. Please provide a valid one that matches [a-zA-Z][a-zA-Z0-9_]*
/// This field was commented out because of an invalid name. Please provide a valid one that matches [a-zA-Z][a-zA-Z0-9_]*
// 1 Int @map("1")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,56 +195,12 @@ pub(crate) fn introspect(ctx: &Context, warnings: &mut Vec<Warning>) -> Result<(
"{}\n{}\n{}",
config,
psl::render_datamodel_to_string(&datamodel, Some(ctx.config)),
render_datamodel(&datamodel),
render::Datamodel::from_dml(&ctx.config.datasources[0], &datamodel),
);

Ok((version, psl::reformat(&rendered, 2).unwrap(), datamodel.is_empty()))
}

/// Render all of the data model. For now, just enums. More will be
/// added in the upcoming days.
fn render_datamodel(dml: &Datamodel) -> render::Datamodel<'_> {
let mut data_model = render::Datamodel::new();

for dml_enum in dml.enums() {
let mut r#enum = render::datamodel::Enum::new(&dml_enum.name);

if let Some(ref docs) = dml_enum.documentation {
r#enum.documentation(docs);
}

if let Some(ref schema) = dml_enum.schema {
r#enum.schema(schema);
}

if let Some(ref map) = dml_enum.database_name {
r#enum.map(map);
}

for dml_variant in dml_enum.values.iter() {
let mut variant = render::datamodel::EnumVariant::new(&dml_variant.name);

if dml_variant.commented_out {
variant = variant.into_commented_out();
}

if let Some(ref map) = dml_variant.database_name {
variant.map(map);
}

if let Some(ref docs) = dml_variant.documentation {
variant.documentation(docs);
}

r#enum.push_variant(variant);
}

data_model.push_enum(r#enum);
}

data_model
}

fn render_configuration<'a>(config: &'a Configuration, schema: &'a SqlSchema) -> render::Configuration<'a> {
let mut output = render::Configuration::default();
let prev_ds = config.datasources.first().unwrap();
Expand Down
1 change: 1 addition & 0 deletions introspection-engine/datamodel-renderer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2021"
once_cell = "1.15.0"
psl.workspace = true
regex = "1.6.0"
base64 = "0.13.1"

[dev-dependencies]
expect-test = "1.4.0"
Expand Down
34 changes: 34 additions & 0 deletions introspection-engine/datamodel-renderer/src/datamodel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ mod enumerator;
pub use composite_type::{CompositeType, CompositeTypeField};
pub use default::DefaultValue;
pub use enumerator::{Enum, EnumVariant};
use psl::dml;
use std::fmt;

/// The PSL data model declaration.
#[derive(Default, Debug)]
pub struct Datamodel<'a> {
enums: Vec<Enum<'a>>,
composite_types: Vec<CompositeType<'a>>,
}

impl<'a> Datamodel<'a> {
Expand All @@ -34,10 +36,42 @@ impl<'a> Datamodel<'a> {
pub fn push_enum(&mut self, r#enum: Enum<'a>) {
self.enums.push(r#enum);
}

/// Add a composite type block to the data model.
///
/// ```ignore
/// type Address { // <
/// street String // < this
/// } // <
/// ```
pub fn push_composite_type(&mut self, r#enum: CompositeType<'a>) {
self.composite_types.push(r#enum);
}

/// A throwaway function to help generate a rendering from the DML structures.
///
/// Delete when removing DML.
pub fn from_dml(datasource: &'a psl::Datasource, dml_data_model: &'a dml::Datamodel) -> Datamodel<'a> {
let mut data_model = Self::new();

for dml_ct in dml_data_model.composite_types() {
data_model.push_composite_type(CompositeType::from_dml(datasource, dml_ct))
}

for dml_enum in dml_data_model.enums() {
data_model.push_enum(Enum::from_dml(dml_enum));
}

data_model
}
}

impl<'a> fmt::Display for Datamodel<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for ct in self.composite_types.iter() {
writeln!(f, "{ct}")?;
}

for r#enum in self.enums.iter() {
writeln!(f, "{enum}")?;
}
Expand Down

0 comments on commit 9e7163b

Please sign in to comment.