Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parses and ignores type annotations in native operaton parameters when executing #130

Merged
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
49 changes: 47 additions & 2 deletions crates/mongodb-agent-common/src/procedure/interpolated_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,18 @@ enum NativeMutationPart {
}

/// Parse a string or key in a native procedure into parts where variables have the syntax
/// `{{<variable>}}`.
/// `{{<variable>}}` or `{{ <variable> | type expression }}`.
fn parse_native_mutation(string: &str) -> Vec<NativeMutationPart> {
let vec: Vec<Vec<NativeMutationPart>> = string
.split("{{")
.filter(|part| !part.is_empty())
.map(|part| match part.split_once("}}") {
None => vec![NativeMutationPart::Text(part.to_string())],
Some((var, text)) => {
Some((placeholder_content, text)) => {
let var = match placeholder_content.split_once("|") {
Some((var_name, _type_annotation)) => var_name,
None => placeholder_content,
};
if text.is_empty() {
vec![NativeMutationPart::Parameter(var.trim().into())]
} else {
Expand Down Expand Up @@ -324,4 +328,45 @@ mod tests {
);
Ok(())
}

#[test]
fn strips_type_annotation_from_placeholder_text() -> anyhow::Result<()> {
let native_mutation = NativeMutation {
result_type: Type::Object(ObjectType {
name: Some("InsertArtist".into()),
fields: [("ok".into(), Type::Scalar(MongoScalarType::Bson(S::Bool)))].into(),
}),
command: doc! {
"insert": "Artist",
"documents": [{
"Name": "{{name | string! }}",
}],
},
selection_criteria: Default::default(),
description: Default::default(),
};

let input_arguments = [(
"name".into(),
MutationProcedureArgument::Literal {
value: json!("Regina Spektor"),
argument_type: Type::Scalar(MongoScalarType::Bson(S::String)),
},
)]
.into();

let arguments = arguments_to_mongodb_expressions(input_arguments)?;
let command = interpolated_command(&native_mutation.command, &arguments)?;

assert_eq!(
command,
bson::doc! {
"insert": "Artist",
"documents": [{
"Name": "Regina Spektor",
}],
}
);
Ok(())
}
}