Skip to content

[Native Operations]: trim semicolon suffix #566

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

Merged
merged 4 commits into from
Aug 12, 2024
Merged
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
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

### Fixed

- Allow Native Operations that end with a semicolon when it's easy to remove them.
[#566](https://github.com/hasura/ndc-postgres/pull/566)

## [v1.0.1]

### Added
Expand Down
21 changes: 17 additions & 4 deletions crates/configuration/src/version5/metadata/native_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,13 @@ pub fn parse_native_query_from_file(
}

/// Parse a native query into parts where variables have the syntax `{{<variable>}}`.
pub fn parse_native_query(string: &str) -> NativeQueryParts {
let vec: Vec<Vec<NativeQueryPart>> = string
pub fn parse_native_query(string_untrimmed: &str) -> NativeQueryParts {
let string_trimmed = string_untrimmed.trim_end();
let vec: Vec<NativeQueryPart> = string_trimmed
.strip_suffix(';')
.unwrap_or(string_trimmed)
.split("{{")
.map(|part| match part.split_once("}}") {
.flat_map(|part| match part.split_once("}}") {
None => vec![NativeQueryPart::Text(part.to_string())],
Some((var, text)) => {
if text.is_empty() {
Expand All @@ -303,7 +306,7 @@ pub fn parse_native_query(string: &str) -> NativeQueryParts {
}
})
.collect();
NativeQueryParts(vec.concat())
NativeQueryParts(vec)
}

// tests
Expand Down Expand Up @@ -347,6 +350,16 @@ mod tests {
);
}

#[test]
fn with_trailing_semicolon() {
assert_eq!(
parse_native_query("select *, 'a ; string' from t; \n"),
NativeQueryParts(vec![NativeQueryPart::Text(
"select *, 'a ; string' from t".to_string()
)])
);
}

#[test]
fn one_parameter_and_curly_text() {
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ dev: start-dependencies
OTEL_SERVICE_NAME=ndc-postgres \
cargo watch -i "**/snapshots/*" \
-c \
-x 'test -p query-engine-metadata -p query-engine-sql -p query-engine-translation -p databases-tests --features postgres' \
-x 'test -p ndc-postgres-configuration -p query-engine-metadata -p query-engine-sql -p query-engine-translation -p databases-tests --features postgres' \
-x clippy \
-x 'run --bin ndc-postgres -- serve --otlp-endpoint http://localhost:4317 --configuration {{POSTGRES_LATEST_CHINOOK_NDC_METADATA}}'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ FROM
unnest(committee.members) AS members
) AS members_agg ON TRUE
) AS coms ON TRUE
;