Skip to content

Commit

Permalink
🐛 Fix codegen to avoid needless empty lines
Browse files Browse the repository at this point in the history
Closes #29
  • Loading branch information
giacomocavalieri committed Sep 22, 2024
1 parent ceb5cd0 commit c398513
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
instead of a proper syntax error.
([Giacomo Cavalieri](https://github.com/giacomocavalieri))

- Fixed a bug where the generated code would have needless empty lines.
([Giacomo Cavalieri](https://github.com/giacomocavalieri))

## v1.7.0 - 2024-09-09

- Fixed a bug where an authentication error would result in a failure with a
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
version: 1.2.1
title: there is only one empty line between code generated for different queries
file: ./test/squirrel_test.gleam
test_name: there_is_only_one_empty_line_between_code_generated_for_different_queries_test
---
import decode
import gleam/pgo

/// A row you get from running the `one` query
/// defined in `one.sql`.
///
/// > 🐿️ This type definition was generated automatically using v-test of the
/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
///
pub type OneRow {
OneRow(res: Int)
}

/// Runs the `one` query
/// defined in `one.sql`.
///
/// > 🐿️ This function was generated automatically using v-test of
/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
///
pub fn one(db) {
let decoder =
decode.into({
use res <- decode.parameter
OneRow(res: res)
})
|> decode.field(0, decode.int)

"select 1 as res"
|> pgo.execute(db, [], decode.from(decoder, _))
}

/// A row you get from running the `two` query
/// defined in `two.sql`.
///
/// > 🐿️ This type definition was generated automatically using v-test of the
/// > [squirrel package](https://github.com/giacomocavalieri/squirrel).
///
pub type TwoRow {
TwoRow(res: Int)
}

/// Runs the `two` query
/// defined in `two.sql`.
///
/// > 🐿️ This function was generated automatically using v-test of
/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel).
///
pub fn two(db) {
let decoder =
decode.into({
use res <- decode.parameter
TwoRow(res: res)
})
|> decode.field(0, decode.int)

"select 2 as res"
|> pgo.execute(db, [], decode.from(decoder, _))
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
version: 1.2.0
version: 1.2.1
title: using uuids more than once results in a single uuid decoder helper
file: ./test/squirrel_test.gleam
test_name: using_uuids_more_than_once_results_in_a_single_uuid_decoder_helper_test
Expand Down Expand Up @@ -36,7 +36,6 @@ pub fn one(db) {
|> pgo.execute(db, [], decode.from(decoder, _))
}


/// A row you get from running the `other` query
/// defined in `other.sql`.
///
Expand Down
8 changes: 6 additions & 2 deletions src/squirrel/internal/query.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,12 @@ pub fn generate_code(queries: List(TypedQuery), version: String) -> String {
// But in case we also need some helpers we add a final section to our file
// with the hard coded helpers we need for the code to compile.
let code =
[imports_doc(imports), ..queries_docs]
|> doc.join(with: doc.lines(2))
[
imports_doc(imports),
doc.lines(2),
doc.join(queries_docs, with: doc.lines(1)),
]
|> doc.concat

case utils {
[] -> code
Expand Down
12 changes: 12 additions & 0 deletions test/squirrel_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -496,3 +496,15 @@ pub fn query_starting_with_a_semicolon_produces_syntax_error_instead_of_crashing
title: "query starting with a semicolon produces syntax error instead of crashing",
)
}

// https://github.com/giacomocavalieri/squirrel/issues/29
pub fn there_is_only_one_empty_line_between_code_generated_for_different_queries_test() {
should_codegen_queries([
#("one", "select 1 as res"),
#("two", "select 2 as res"),
])
|> birdie.snap(
title: "there is only one empty line between code generated for different queries",
)
}

0 comments on commit c398513

Please sign in to comment.