Skip to content

Commit 12a71e2

Browse files
silbylpil
authored andcommitted
Migrate to gleam/dynamic/decode
1 parent 3457d47 commit 12a71e2

File tree

5 files changed

+59
-40
lines changed

5 files changed

+59
-40
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ gleam add sqlight
1212
```
1313

1414
```gleam
15-
import gleam/dynamic
15+
import gleam/dynamic/decode
1616
import sqlight
1717
1818
pub fn main() {
1919
use conn <- sqlight.with_connection(":memory:")
20-
let cat_decoder = dynamic.tuple2(dynamic.string, dynamic.int)
20+
let cat_decoder = {
21+
use name <- decode.field(0, decode.string)
22+
use age <- decode.field(1, decode.int)
23+
decode.success(#(name, age))
24+
}
2125
2226
let sql = "
2327
create table cats (name text, age int);

gleam.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ allow_write = [
2626
]
2727

2828
[dependencies]
29-
gleam_stdlib = "~> 0.25"
29+
gleam_stdlib = "~> 0.51"
3030
esqlite = "~> 0.8"
3131

3232
[dev-dependencies]

manifest.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
# You typically do not need to edit this file
33

44
packages = [
5-
{ name = "esqlite", version = "0.8.8", build_tools = ["rebar3"], requirements = [], otp_app = "esqlite", source = "hex", outer_checksum = "374902457C7D94DC9409C98D3BDD1CA0D50A60DC9F3BDF1FD8EB74C0DCDF02D6" },
6-
{ name = "gleam_stdlib", version = "0.39.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "2D7DE885A6EA7F1D5015D1698920C9BAF7241102836CE0C3837A4F160128A9C4" },
5+
{ name = "esqlite", version = "0.8.9", build_tools = ["rebar3"], requirements = [], otp_app = "esqlite", source = "hex", outer_checksum = "465AE9AE28AE4192EA54C829FDC90C320447D439A9B2E10946621672FC6A6F8C" },
6+
{ name = "gleam_stdlib", version = "0.51.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "14AFA8D3DDD7045203D422715DBB822D1725992A31DF35A08D97389014B74B68" },
77
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
88
]
99

1010
[requirements]
1111
esqlite = { version = "~> 0.8" }
12-
gleam_stdlib = { version = "~> 0.25" }
12+
gleam_stdlib = { version = "~> 0.51" }
1313
gleeunit = { version = "~> 1.0" }

src/sqlight.gleam

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import gleam/dynamic.{type Decoder, type Dynamic}
1+
import gleam/dynamic/decode.{type Decoder, type Dynamic}
22
import gleam/list
33
import gleam/option.{type Option, None, Some}
44
import gleam/result
@@ -383,7 +383,7 @@ pub fn query(
383383
) -> Result(List(t), Error) {
384384
use rows <- result.then(run_query(sql, connection, arguments))
385385
use rows <- result.then(
386-
list.try_map(over: rows, with: decoder)
386+
list.try_map(over: rows, with: fn(row) { decode.run(row, decoder) })
387387
|> result.map_error(decode_error),
388388
)
389389
Ok(rows)
@@ -468,16 +468,17 @@ pub fn null() -> Value
468468
///
469469
/// Decodes 0 as `False` and any other integer as `True`.
470470
///
471-
pub fn decode_bool(value: Dynamic) -> Result(Bool, List(dynamic.DecodeError)) {
472-
case dynamic.int(value) {
473-
Ok(0) -> Ok(False)
474-
Ok(_) -> Ok(True)
475-
Error(e) -> Error(e)
471+
pub fn decode_bool() -> Decoder(Bool) {
472+
use b <- decode.then(decode.int)
473+
474+
case b {
475+
0 -> decode.success(False)
476+
_ -> decode.success(True)
476477
}
477478
}
478479

479-
fn decode_error(errors: List(dynamic.DecodeError)) -> Error {
480-
let assert [dynamic.DecodeError(expected, actual, path), ..] = errors
480+
fn decode_error(errors: List(decode.DecodeError)) -> Error {
481+
let assert [decode.DecodeError(expected, actual, path), ..] = errors
481482
let path = string.join(path, ".")
482483
let message =
483484
"Decoder failed, expected "

test/sqlight_test.gleam

+39-25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import gleam/dynamic
1+
import gleam/dynamic/decode
22
import gleam/list
33
import gleam/option
44
import gleeunit
@@ -54,18 +54,23 @@ pub fn with_connection_test() {
5454
pub fn query_1_test() {
5555
use conn <- connect()
5656
let assert Ok([#(1, 2, 3), #(4, 5, 6)]) =
57-
sqlight.query(
58-
"select 1, 2, 3 union all select 4, 5, 6",
59-
conn,
60-
[],
61-
dynamic.tuple3(dynamic.int, dynamic.int, dynamic.int),
62-
)
57+
sqlight.query("select 1, 2, 3 union all select 4, 5, 6", conn, [], {
58+
use one <- decode.field(0, decode.int)
59+
use two <- decode.field(1, decode.int)
60+
use three <- decode.field(2, decode.int)
61+
decode.success(#(one, two, three))
62+
})
6363
}
6464

6565
pub fn query_2_test() {
6666
use conn <- connect()
6767
let assert Ok([1337]) =
68-
sqlight.query("select 1337", conn, [], dynamic.element(0, dynamic.int))
68+
sqlight.query(
69+
"select 1337",
70+
conn,
71+
[],
72+
decode.field(0, decode.int, decode.success),
73+
)
6974
}
7075

7176
pub fn bind_int_test() {
@@ -75,7 +80,7 @@ pub fn bind_int_test() {
7580
"select ?",
7681
conn,
7782
[sqlight.int(12_345)],
78-
dynamic.element(0, dynamic.int),
83+
decode.field(0, decode.int, decode.success),
7984
)
8085
}
8186

@@ -86,7 +91,7 @@ pub fn bind_float_test() {
8691
"select ?",
8792
conn,
8893
[sqlight.float(12_345.6789)],
89-
dynamic.element(0, dynamic.float),
94+
decode.field(0, decode.float, decode.success),
9095
)
9196
}
9297

@@ -97,19 +102,18 @@ pub fn bind_text_test() {
97102
"select ?",
98103
conn,
99104
[sqlight.text("hello")],
100-
dynamic.element(0, dynamic.string),
105+
decode.field(0, decode.string, decode.success),
101106
)
102107
}
103108

104109
pub fn bind_blob_test() {
105110
use conn <- connect()
106111
let assert Ok([#(<<123, 0>>, "blob")]) =
107-
sqlight.query(
108-
"select ?1, typeof(?1)",
109-
conn,
110-
[sqlight.blob(<<123, 0>>)],
111-
dynamic.tuple2(dynamic.bit_array, dynamic.string),
112-
)
112+
sqlight.query("select ?1, typeof(?1)", conn, [sqlight.blob(<<123, 0>>)], {
113+
use ary <- decode.field(0, decode.bit_array)
114+
use str <- decode.field(1, decode.string)
115+
decode.success(#(ary, str))
116+
})
113117
}
114118

115119
pub fn bind_null_test() {
@@ -119,7 +123,7 @@ pub fn bind_null_test() {
119123
"select ?",
120124
conn,
121125
[sqlight.null()],
122-
dynamic.element(0, dynamic.optional(dynamic.int)),
126+
decode.field(0, decode.optional(decode.int), decode.success),
123127
)
124128
}
125129

@@ -130,7 +134,7 @@ pub fn bind_bool_test() {
130134
"select ?",
131135
conn,
132136
[sqlight.bool(True)],
133-
dynamic.element(0, sqlight.decode_bool),
137+
decode.field(0, sqlight.decode_bool(), decode.success),
134138
)
135139
}
136140

@@ -144,7 +148,7 @@ pub fn exec_test() {
144148
"select name from cats",
145149
conn,
146150
[],
147-
dynamic.element(0, dynamic.string),
151+
decode.field(0, decode.string, decode.success),
148152
)
149153
}
150154

@@ -157,7 +161,11 @@ pub fn exec_fail_test() {
157161

158162
pub fn readme_example_test() {
159163
use conn <- sqlight.with_connection(":memory:")
160-
let cat_decoder = dynamic.tuple2(dynamic.string, dynamic.int)
164+
let cat_decoder = {
165+
use name <- decode.field(0, decode.string)
166+
use age <- decode.field(1, decode.int)
167+
decode.success(#(name, age))
168+
}
161169

162170
let sql =
163171
"
@@ -242,7 +250,13 @@ pub fn decode_error_test() {
242250
sqlight.GenericError,
243251
"Decoder failed, expected String, got Int in 0",
244252
-1,
245-
)) = sqlight.query("select 1", conn, [], dynamic.element(0, dynamic.string))
253+
)) =
254+
sqlight.query(
255+
"select 1",
256+
conn,
257+
[],
258+
decode.field(0, decode.string, decode.success),
259+
)
246260
}
247261

248262
pub fn query_error_test() {
@@ -253,7 +267,7 @@ pub fn query_error_test() {
253267
"this isn't a valid query",
254268
conn,
255269
[],
256-
dynamic.element(0, dynamic.int),
270+
decode.field(0, decode.int, decode.success),
257271
)
258272
}
259273

@@ -265,14 +279,14 @@ pub fn bind_nullable_test() {
265279
"select ?",
266280
conn,
267281
[sqlight.nullable(sqlight.int, option.Some(12_345))],
268-
dynamic.element(0, dynamic.optional(dynamic.int)),
282+
decode.field(0, decode.optional(decode.int), decode.success),
269283
)
270284

271285
let assert Ok([option.None]) =
272286
sqlight.query(
273287
"select ?",
274288
conn,
275289
[sqlight.nullable(sqlight.int, option.None)],
276-
dynamic.element(0, dynamic.optional(dynamic.int)),
290+
decode.field(0, decode.optional(decode.int), decode.success),
277291
)
278292
}

0 commit comments

Comments
 (0)