Skip to content

Commit

Permalink
Remove optional for argument info
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljharvey committed Oct 25, 2024
1 parent 6f48262 commit 379c281
Show file tree
Hide file tree
Showing 5 changed files with 468 additions and 242 deletions.
88 changes: 40 additions & 48 deletions crates/connectors/ndc-postgres/src/schema/mutation/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,17 @@ pub fn delete_to_procedure(
);
}

if let Some(pre_check) = &delete_by_key.pre_check {
arguments.insert(
pre_check.argument_name.clone(),
models::ArgumentInfo {
argument_type: models::Type::Nullable {
underlying_type: Box::new(models::Type::Predicate {
object_type_name: delete_by_key.collection_name.as_str().into(),
}),
},
description: Some(pre_check.description.clone()),
arguments.insert(
delete_by_key.pre_check.argument_name.clone(),
models::ArgumentInfo {
argument_type: models::Type::Nullable {
underlying_type: Box::new(models::Type::Predicate {
object_type_name: delete_by_key.collection_name.as_str().into(),
}),
},
);
}
description: Some(delete_by_key.pre_check.description.clone()),
},
);

make_procedure_type(
name.clone(),
Expand Down Expand Up @@ -79,34 +77,30 @@ pub fn update_to_procedure(
}

// pre check argument.
if let Some(pre_check) = &update_by_key.pre_check {
arguments.insert(
pre_check.argument_name.clone(),
models::ArgumentInfo {
argument_type: models::Type::Nullable {
underlying_type: Box::new(models::Type::Predicate {
object_type_name: update_by_key.collection_name.as_str().into(),
}),
},
description: Some(pre_check.description.clone()),
arguments.insert(
update_by_key.pre_check.argument_name.clone(),
models::ArgumentInfo {
argument_type: models::Type::Nullable {
underlying_type: Box::new(models::Type::Predicate {
object_type_name: update_by_key.collection_name.as_str().into(),
}),
},
);
}
description: Some(update_by_key.pre_check.description.clone()),
},
);

// post check argument.
if let Some(post_check) = &update_by_key.post_check {
arguments.insert(
post_check.argument_name.clone(),
models::ArgumentInfo {
argument_type: models::Type::Nullable {
underlying_type: Box::new(models::Type::Predicate {
object_type_name: update_by_key.collection_name.as_str().into(),
}),
},
description: Some(post_check.description.clone()),
arguments.insert(
update_by_key.post_check.argument_name.clone(),
models::ArgumentInfo {
argument_type: models::Type::Nullable {
underlying_type: Box::new(models::Type::Predicate {
object_type_name: update_by_key.collection_name.as_str().into(),
}),
},
);
}
description: Some(update_by_key.post_check.description.clone()),
},
);

// update columns argument.
// Is of the form update_columns: { <column_name>: { <operation>: <value> }, ... }.
Expand Down Expand Up @@ -212,19 +206,17 @@ pub fn insert_to_procedure(
},
);

if let Some(post_check) = &insert.post_check {
arguments.insert(
post_check.argument_name.clone(),
models::ArgumentInfo {
argument_type: models::Type::Nullable {
underlying_type: Box::new(models::Type::Predicate {
object_type_name: insert.collection_name.as_str().into(),
})
},
description: Some(post_check.description.clone()),
arguments.insert(
insert.post_check.argument_name.clone(),
models::ArgumentInfo {
argument_type: models::Type::Nullable {
underlying_type: Box::new(models::Type::Predicate {
object_type_name: insert.collection_name.as_str().into(),
}),
},
);
}
description: Some(insert.post_check.description.clone()),
},
);

make_procedure_type(
name.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct DeleteByKey {
pub table_name: sql::ast::TableName,
pub by_columns: NonEmpty<metadata::database::ColumnInfo>,
pub columns_prefix: String,
pub pre_check: Option<CheckArgument>,
pub pre_check: CheckArgument,
}

/// generate a delete for each simple unique constraint on this table
Expand Down Expand Up @@ -69,12 +69,12 @@ pub fn generate_delete_by_unique(
collection_name: collection_name.clone(),
by_columns: key_columns,
columns_prefix: "key_".to_string(),
pre_check: Some(CheckArgument {
pre_check: CheckArgument {
argument_name: "pre_check".into(),
description: format!(
"Delete permission predicate over the '{collection_name}' collection"
),
}),
},
description,
});

Expand Down Expand Up @@ -111,10 +111,12 @@ pub fn translate(
};

// Build the `UNIQUE_KEY = <value>, ...` boolean expression.
let unique_expressions = mutation.by_columns
let unique_expressions = mutation
.by_columns
.iter()
.map(|by_column| {
let argument_name = format!("{}{}", mutation.columns_prefix, by_column.name).into();
let argument_name =
format!("{}{}", mutation.columns_prefix, by_column.name).into();
let unique_key = arguments
.get(&argument_name)
.ok_or(Error::ArgumentNotFound(argument_name))?;
Expand All @@ -138,17 +140,15 @@ pub fn translate(

// Build the `pre_check` argument boolean expression.
let default_constraint = default_constraint();
let predicate_json =
mutation.pre_check
.as_ref()
.and_then(|pre_check| arguments.get(&pre_check.argument_name))
let predicate_json = arguments
.get(&mutation.pre_check.argument_name)
.unwrap_or(&default_constraint);

let predicate: models::Expression = serde_json::from_value(predicate_json.clone())
.map_err(|_| {
Error::UnexpectedStructure(format!(
"Argument '{}' should have an ndc-spec Expression structure",
mutation.pre_check.as_ref().unwrap().argument_name.clone()
mutation.pre_check.argument_name.clone()
))
})?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct InsertMutation {
pub table_name: sql::ast::TableName,
pub objects_argument_name: models::ArgumentName,
pub columns: BTreeMap<models::FieldName, metadata::database::ColumnInfo>,
pub post_check: Option<CheckArgument>,
pub post_check: CheckArgument,
}

/// generate an insert mutation.
Expand All @@ -43,12 +43,12 @@ pub fn generate(
table_name: sql::ast::TableName(table_info.table_name.clone()),
columns: table_info.columns.clone(),
objects_argument_name: "objects".into(),
post_check: Some(CheckArgument {
post_check: CheckArgument {
argument_name: "post_check".into(),
description: format!(
"Insert permission predicate over the '{collection_name}' collection"
),
}),
},
};

(name, insert_mutation)
Expand Down Expand Up @@ -225,17 +225,15 @@ pub fn translate(

// Build the `post_check` argument boolean expression.
let default_constraint = default_constraint();
let predicate_json =
mutation.post_check
.as_ref()
.and_then(|post_check| arguments.get(&post_check.argument_name))
let predicate_json = arguments
.get(&mutation.post_check.argument_name)
.unwrap_or(&default_constraint);

let predicate: models::Expression =
serde_json::from_value(predicate_json.clone()).map_err(|_| {
Error::UnexpectedStructure(format!(
"Argument '{}' should have an ndc-spec Expression structure",
mutation.post_check.as_ref().unwrap().argument_name.clone()
mutation.post_check.argument_name.clone()
))
})?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ pub struct UpdateByKey {
pub by_columns: NonEmpty<metadata::database::ColumnInfo>,
pub columns_prefix: String,
pub update_columns_argument_name: models::ArgumentName,
pub pre_check: Option<CheckArgument>,
pub post_check: Option<CheckArgument>,
pub pre_check: CheckArgument,
pub post_check: CheckArgument,
pub table_columns: BTreeMap<models::FieldName, metadata::database::ColumnInfo>,
}

Expand Down Expand Up @@ -74,18 +74,18 @@ pub fn generate_update_by_unique(
by_columns: key_columns,
columns_prefix: "key_".to_string(),
update_columns_argument_name: "update_columns".into(),
pre_check: Some(CheckArgument {
pre_check: CheckArgument {
argument_name: "pre_check".into(),
description: format!(
"Update permission pre-condition predicate over the '{collection_name}' collection"
),
}),
post_check: Some(CheckArgument {
},
post_check: CheckArgument {
argument_name: "post_check".into(),
description: format!(
"Update permission post-condition predicate over the '{collection_name}' collection"
),
}),
},
table_columns: table_info.columns.clone(),

description,
Expand Down Expand Up @@ -159,35 +159,31 @@ pub fn translate(
let default_constraint = default_constraint();

// Build the `pre_constraint` argument boolean expression.
let pre_predicate_json =
mutation.pre_check
.as_ref()
.and_then(|pre_check| arguments.get(&pre_check.argument_name))
let pre_predicate_json = arguments
.get(&mutation.pre_check.argument_name)
.unwrap_or(&default_constraint);

let pre_predicate: models::Expression =
serde_json::from_value(pre_predicate_json.clone()).map_err(|_| {
Error::UnexpectedStructure(format!(
"Argument '{}' should have an ndc-spec Expression structure",
mutation.pre_check.as_ref().unwrap().argument_name.clone()
mutation.pre_check.argument_name.clone()
))
})?;

let pre_predicate_expression =
filtering::translate(env, state, &root_and_current_tables, &pre_predicate)?;

// Build the `post_constraint` argument boolean expression.
let post_predicate_json =
mutation.post_check
.as_ref()
.and_then(|post_check| arguments.get(&post_check.argument_name))
let post_predicate_json = arguments
.get(&mutation.post_check.argument_name)
.unwrap_or(&default_constraint);

let post_predicate: models::Expression =
serde_json::from_value(post_predicate_json.clone()).map_err(|_| {
Error::UnexpectedStructure(format!(
"Argument '{}' should have an ndc-spec Expression structure",
mutation.post_check.as_ref().unwrap().argument_name.clone()
mutation.post_check.argument_name.clone()
))
})?;

Expand Down
Loading

0 comments on commit 379c281

Please sign in to comment.