Skip to content
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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog

## v4.0.0 - Unreleased
## Unreleased

- Added a `numeric_decoder` to decode numeric types coming from postgres.

## v4.0.0 - 2025-07-07

- Starting a pool no longer generates an atom, instead a name is taken as an
argument.
Expand Down
4 changes: 4 additions & 0 deletions src/pog.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -904,3 +904,7 @@ fn seconds_decoder() -> decode.Decoder(#(Int, Int)) {
}
decode.one_of(int, [float])
}

pub fn numeric_decoder() -> decode.Decoder(Float) {
decode.one_of(decode.float, [decode.int |> decode.map(int.to_float)])
}
21 changes: 21 additions & 0 deletions test/pog_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,27 @@ pub fn float_test() {
|> disconnect
}

pub fn numeric_test() {
let db =
start_default()
|> assert_roundtrip(0.0, "numeric", pog.float, pog.numeric_decoder())
|> assert_roundtrip(10.0, "numeric", pog.float, pog.numeric_decoder())
|> assert_roundtrip(1.1, "numeric", pog.float, pog.numeric_decoder())
|> assert_roundtrip(1.0, "numeric", pog.float, pog.numeric_decoder())

assert pog.query("select 1::numeric")
|> pog.returning(decode.at([0], pog.numeric_decoder()))
|> pog.execute(db.data)
== Ok(pog.Returned(count: 1, rows: [1.0]))

assert pog.query("select 0::numeric")
|> pog.returning(decode.at([0], pog.numeric_decoder()))
|> pog.execute(db.data)
== Ok(pog.Returned(count: 1, rows: [0.0]))

disconnect(db)
}

pub fn text_test() {
start_default()
|> assert_roundtrip("", "text", pog.text, decode.string)
Expand Down