Skip to content

Commit

Permalink
Add a 'decode_timestamp' function
Browse files Browse the repository at this point in the history
To help with decoding the nested tuple format which is how erlang's
pgo handles timestamps. Named as suggested by lpil.

I'm not sure the calling convention for the functions and so whether
or not it should be set up to use parens at the call site. I've opted
not to, to follow the convention of other value types like 'int' and
'string'.
  • Loading branch information
michaeljones committed May 10, 2024
1 parent 84189bb commit 9b676d1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/gleam/pgo.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,13 @@ pub fn error_code_name(error_code: String) -> Result(String, Nil) {
_ -> Error(Nil)
}
}

/// Checks to see if the value is formatted as `#(#(Int, Int, Int), #(Int, Int, Int))`
/// to represent `#(#(year, month, day), #(hour, minute, second))`, and returns the
/// value if it is.
pub fn decode_timestamp(value: dynamic.Dynamic) {
dynamic.tuple2(
dynamic.tuple3(dynamic.int, dynamic.int, dynamic.int),
dynamic.tuple3(dynamic.int, dynamic.int, dynamic.int),
)(value)
}
10 changes: 2 additions & 8 deletions test/gleam/pgo_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ pub fn selecting_rows_test() {
dynamic.int,
dynamic.string,
dynamic.bool,
dynamic.tuple2(
dynamic.tuple3(dynamic.int, dynamic.int, dynamic.int),
dynamic.tuple3(dynamic.int, dynamic.int, dynamic.int),
),
pgo.decode_timestamp,
),
)

Expand Down Expand Up @@ -319,10 +316,7 @@ pub fn datetime_test() {
#(#(2022, 10, 10), #(11, 30, 30)),
"timestamp",
pgo.timestamp,
dynamic.tuple2(
dynamic.tuple3(dynamic.int, dynamic.int, dynamic.int),
dynamic.tuple3(dynamic.int, dynamic.int, dynamic.int),
),
pgo.decode_timestamp,
)
|> pgo.disconnect
}
Expand Down

0 comments on commit 9b676d1

Please sign in to comment.