Skip to content
Draft
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
8 changes: 6 additions & 2 deletions psl/parser-database/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,15 @@ fn resolve_enum_attributes<'db>(enum_id: crate::EnumId, ast_enum: &'db ast::Enum
}
ctx.validate_visited_arguments();
}

// @ignore
if ctx.visit_optional_single_attr("ignore") {
enum_attributes.ignored_values.insert(value_id);
ctx.validate_visited_arguments();
}
ctx.validate_visited_attributes();
}

// Now validate the enum attributes.

ctx.visit_attributes(enum_id);

// @@map
Expand Down
2 changes: 2 additions & 0 deletions psl/parser-database/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,8 @@ pub(super) struct EnumAttributes {
pub(super) mapped_name: Option<StringId>,
/// @map on enum values.
pub(super) mapped_values: HashMap<EnumValueId, StringId>,
/// @ignore on enum values.
pub(super) ignored_values: std::collections::HashSet<EnumValueId>,
/// ```ignore
/// @@schema("public")
/// ^^^^^^^^
Expand Down
7 changes: 7 additions & 0 deletions psl/parser-database/src/walkers/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,11 @@ impl<'db> EnumValueWalker<'db> {
.get(&(self.id.1))
.map(|id| &self.db[*id])
}

/// True if the enum value is ignored.
pub fn is_ignored(self) -> bool {
self.db.types.enum_attributes[&self.id.0]
.ignored_values
.contains(&self.id.1)
}
}
1 change: 1 addition & 0 deletions psl/psl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ all = ["postgresql", "sqlite", "mysql", "cockroachdb", "mssql", "mongodb"]
psl-core.workspace = true

[dev-dependencies]
psl-core = { workspace = true, features = ["postgresql", "sqlite", "mysql", "cockroachdb", "mssql", "mongodb"] }
base64.workspace = true
dissimilar.workspace = true
expect-test.workspace = true
Expand Down
18 changes: 18 additions & 0 deletions psl/psl/tests/attributes/ignore_positive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,21 @@ fn allow_ignore_on_relation_fields_on_valid_models() {
.assert_has_relation_field("rel_e")
.assert_ignored(true);
}

#[test]
fn allow_ignore_on_enum_values() {
let dml = r#"
enum SupportedCarTypes {
COUPE @ignore
SEDAN
VAN
}
"#;

let datamodel = parse_schema(dml);
let enum_walker = datamodel.assert_has_enum("SupportedCarTypes");

enum_walker.assert_has_value("COUPE").assert_ignored(true);
enum_walker.assert_has_value("SEDAN").assert_ignored(false);
enum_walker.assert_has_value("VAN").assert_ignored(false);
}
33 changes: 33 additions & 0 deletions psl/psl/tests/common/asserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use psl::{Diagnostics, StringFromEnvVar};
pub(crate) trait DatamodelAssert<'a> {
fn assert_has_model(&'a self, name: &str) -> walkers::ModelWalker<'a>;
fn assert_has_type(&'a self, name: &str) -> walkers::CompositeTypeWalker<'a>;
fn assert_has_enum(&'a self, name: &str) -> walkers::EnumWalker<'a>;
}

pub(crate) trait DatasourceAsserts {
Expand Down Expand Up @@ -154,6 +155,14 @@ impl<'a> DatamodelAssert<'a> for psl::ValidatedSchema {
.find(|m| m.name() == name)
.expect("Type {name} not found")
}

#[track_caller]
fn assert_has_enum(&'a self, name: &str) -> walkers::EnumWalker<'a> {
self.db
.walk_enums()
.find(|e| e.name() == name)
.expect("Enum {name} not found")
}
}

impl RelationFieldAssert for walkers::RelationFieldWalker<'_> {
Expand Down Expand Up @@ -755,3 +764,27 @@ impl IndexAssert for walkers::PrimaryKeyWalker<'_> {
unreachable!("Primary key cannot define the index type.");
}
}
pub(crate) trait EnumAssert<'a> {
fn assert_has_value(&'a self, name: &str) -> walkers::EnumValueWalker<'a>;
}

impl<'a> EnumAssert<'a> for walkers::EnumWalker<'a> {
#[track_caller]
fn assert_has_value(&'a self, name: &str) -> walkers::EnumValueWalker<'a> {
self.values()
.find(|v| v.name() == name)
.expect("Enum value {name} not found")
}
}

pub(crate) trait EnumValueAssert {
fn assert_ignored(&self, ignored: bool) -> &Self;
}

impl EnumValueAssert for walkers::EnumValueWalker<'_> {
#[track_caller]
fn assert_ignored(&self, ignored: bool) -> &Self {
assert_eq!(self.is_ignored(), ignored);
self
}
}
3 changes: 3 additions & 0 deletions query-engine/dmmf/src/ast_builders/datamodel_ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ fn enum_to_dmmf(en: walkers::EnumWalker<'_>) -> Enum {
};

for enum_value in en.values() {
if enum_value.is_ignored() {
continue;
}
enm.values.push(enum_value_to_dmmf(enum_value));
}

Expand Down
19 changes: 19 additions & 0 deletions schema-engine/datamodel-renderer/src/datamodel/enumerator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct EnumVariant<'a> {
comment_out: bool,
map: Option<FieldAttribute<'a>>,
documentation: Option<Documentation<'a>>,
ignore: Option<FieldAttribute<'a>>,
}

impl<'a> EnumVariant<'a> {
Expand All @@ -26,6 +27,7 @@ impl<'a> EnumVariant<'a> {
comment_out: false,
map: None,
documentation: None,
ignore: None,
}
}

Expand Down Expand Up @@ -55,6 +57,18 @@ impl<'a> EnumVariant<'a> {
self.comment_out = true;
}

/// Ignores the variant.
///
/// ```ignore
/// enum Foo {
/// Bar @ignore
/// ^^^^^ this
/// }
/// ```
pub fn ignore(&mut self) {
self.ignore = Some(FieldAttribute::new(Function::new("ignore")));
}

/// Documentation of a variant.
///
/// ```ignore
Expand Down Expand Up @@ -91,6 +105,11 @@ impl fmt::Display for EnumVariant<'_> {
map.fmt(f)?;
}

if let Some(ref ignore) = self.ignore {
f.write_str(" ")?;
ignore.fmt(f)?;
}

Ok(())
}
}
Expand Down
Loading