Skip to content

Commit

Permalink
Specify nullable params in comment
Browse files Browse the repository at this point in the history
  • Loading branch information
leah-u committed Dec 3, 2024
1 parent 95fb356 commit ea6b3f3
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/squirrel/internal/query.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,39 @@ pub fn add_types(
) -> Result(TypedQuery, Error) {
let UntypedQuery(file:, name:, comment:, content:, starting_line:) = query

// Quick and dirty hack to allow nullable parameters in insert queries
// Looks for a comment like this: '-- nullable: $1, $3' in the sql file
// and makes the specified parameters nullable
let nullable_params =
comment
|> list.find_map(fn(comment) {
case comment {
"nullable:" <> params -> {
params
|> string.split(",")
|> list.try_map(fn(arg) {
case string.trim(arg) {
"$" <> n -> int.parse(n)
_ -> Error(Nil)
}
})
|> result.map(set.from_list)
}
_ -> Error(Nil)
}
})
let params = case nullable_params {
Error(_) -> params
Ok(nullable_params) ->
params
|> list.index_map(fn(type_, i) {
case nullable_params |> set.contains(i + 1) {
True -> gleam.Option(type_)
False -> type_
}
})
}

case duplicate_names(returns) {
[] ->
Ok(TypedQuery(
Expand Down

0 comments on commit ea6b3f3

Please sign in to comment.